Rust嵌入式初探(一):小灯闪烁实验

作者 @飞洲人飞舟魂转载请注明出处.

实验芯片:

stm32f103zet6

实验开发板:

正点原子精英板(ALIENTEK ELITE)

参考资料:

《The Embedded Rust Book》《 The Rust Programming Language》

实验源代码:
#![no_std]
#![no_main]

// pick a panicking behavior
use panic_halt as _; //定义panic为halt
use cortex_m_rt::entry;//cortex-m3内核的程序入口
use stm32f1::stm32f103;//stm32f103系列芯片的总线读写库(Peripheral Access Crate,PAC)

#[entry]
fn main() -> ! {
    let peripherals = stm32f103::Peripherals::take().unwrap();
    let rcc = peripherals.RCC;
    rcc.apb2enr.modify(|_, w| w.iopben().bit(true));//使能PORTB时钟
    let gpiob = peripherals.GPIOB;
    gpiob.crl.modify(|_, w| w.mode5().bits(0b11));//将GPIOB第5引脚 设为推挽输出
    loop {
        gpiob.odr.modify(|_, w| w.odr5().bit(false)); //灯亮
        my_delay();
        gpiob.odr.modify(|_, w| w.odr5().bit(true)); //灯灭
        my_delay();
    }
}
fn my_delay() {//简陋的延迟函数
    let mut count = 2;
    while count > 0 {
        count = count - 1;
        let mut count2 = 120000;
        while count2 > 0 {
            count2 = count2 - 1;
        }
    }
}

实验效果:实现了GPIOB口上的小灯闪烁

====================================

源码分析

接着让我们来分析一下跑马灯程序,

#![no_std]
#![no_main]

// pick a panicking behavior
use panic_halt as _; //定义panic为halt
use cortex_m_rt::entry;//cortex-m3内核的程序入口
use stm32f1::stm32f103;//stm32f103系列芯片的总线读写库(Peripheral Access Crate,PAC)

#[entry]
fn main() -> ! {
    let peripherals = stm32f103::Peripherals::take().unwrap();
    let rcc = peripherals.RCC;
    rcc.apb2enr.modify(|_, w| w.iopben().bit(true));//使能PORTB时钟
    let gpiob = peripherals.GPIOB;
    gpiob.crl.modify(|_, w| w.mode5().bits(0b11));//将GPIOB第5引脚 设为推挽输出
    loop {
        gpiob.odr.modify(|_, w| w.odr5().bit(false)); //灯亮
        my_delay();
        gpiob.odr.modify(|_, w| w.odr5().bit(true)); //灯灭
        my_delay();
    }
}
fn my_delay() {//简陋的延迟函数
    let mut count = 2;
    while count > 0 {
        count = count - 1;
        let mut count2 = 120000;
        while count2 > 0 {
            count2 = count2 - 1;
        }
    }
}

让我们从开头开始,先看#[entry]宏,这个宏是整个程序的入口.要知道在嵌入式裸机开发中,是不存在操作系统的,所以,标准库是没法在这里使用的,而标准的main函数也不能使用,故而最开始也要用#![no_std],#[no_main],宏来设置不使用标准库.程序是必须要一个入口的,我们前面禁用了标准的入口,于是就要用一个新的入口来代替,当然你可以自己写一个,但是我们这里还是用一个叫做cortex-m-rt的Crates来获取代替的程序入口,就像下面这样:

use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
    loop {
        /* .. */
    }
}

如果一定要做类比的话,想想用keli做stm32开发的时候的启动文件比如startup_stm32f10x_hd.s,startup_stm32f10x_ld.s等汇编文件,在这些文件中做了初始化堆栈,初始化向量表,初始化时钟,调用主函数等工作,#[entry]宏基本上也做了相同的事情,不过有一点区别那就是这个宏好像并没有做初始化时钟的工作.我们知道在startup_stm32f10x_hd.s等文件中,通过调用SystemInit()函数实现了将系统时钟从复位时的HSI变换为HSE.而我做了下面的实验,代码如下:

#![no_std]
#![no_main]

// pick a panicking behavior
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
                     // use panic_abort as _; // requires nightly
                     // use panic_itm as _; // logs messages over ITM; requires ITM support
                     // use panic_semihosting as _; // logs messages to the host stderr; requires a debugger

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use stm32f1::stm32f103;

#[entry]
fn main() -> ! {
    // your code goes here
    let peripherals = stm32f103::Peripherals::take().unwrap();
    let rcc = peripherals.RCC;
    let data = rcc.cfgr.read().bits();//得到cfgr寄存器的sws字段的数据
    match (data>>2) & 0x00000003 {
        //看用的是何种时钟
        0x00000000 => hprintln!("HSI as clock!").unwrap(),
        0x00000001 => hprintln!("HSE as clock!").unwrap(),
        0x00000002 => hprintln!("PLL as clock!").unwrap(),
        _ => hprintln!("unexcept error!").unwrap(),
    };
    loop {}
}

实验结果如下:

可以看出仍然是使用内部高速时钟HSI的,也就是并没有初始化时钟.

此外还有个小细节是,由于标准库被禁用,因此标准的panic也没了,同样的,我们也有一些现成的Crates库,本例我们用的是panic_halt,其实还有panic-abort,panic-semihosting等.像panic-semihosting就会在程序崩溃时向串口打印相应的崩溃信息.

posted @ 2021-03-08 21:15  飞洲人飞舟魂  阅读(892)  评论(0编辑  收藏  举报
.c_ad_block { display: none !important; }