Browse Source

Reduce the scope of some critical sections in the examples

crude-e-h-1.0.0-alpha.1
Torkel Danielsson 2 years ago committed by Daniel Egger
parent
commit
db7428d398
  1. 49
      examples/blinky_adc.rs
  2. 24
      examples/blinky_delay.rs
  3. 51
      examples/blinky_multiple.rs
  4. 24
      examples/blinky_timer.rs
  5. 44
      examples/dac.rs
  6. 30
      examples/serial_echo.rs
  7. 46
      examples/serial_spi_bridge.rs
  8. 64
      examples/spi_hal_apa102c.rs
  9. 52
      examples/watchdog.rs

49
examples/blinky_adc.rs

@ -13,37 +13,38 @@ use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
let gpioa = p.GPIOA.split(&mut rcc);
// (Re-)configure PA1 as output
let mut led = gpioa.pa1.into_push_pull_output(cs);
// (Re-)configure PA0 as analog input
let mut an_in = gpioa.pa0.into_analog(cs);
let (mut led, mut an_in) = cortex_m::interrupt::free(move |cs| {
(
// (Re-)configure PA1 as output
gpioa.pa1.into_push_pull_output(cs),
// (Re-)configure PA0 as analog input
gpioa.pa0.into_analog(cs),
)
});
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Get access to the ADC
let mut adc = Adc::new(p.ADC, &mut rcc);
// Get access to the ADC
let mut adc = Adc::new(p.ADC, &mut rcc);
loop {
led.toggle().ok();
loop {
led.toggle().ok();
let time: u16 = if let Ok(val) = adc.read(&mut an_in) as Result<u16, _> {
/* shift the value right by 3, same as divide by 8, reduces
the 0-4095 range into something approximating 1-512 */
(val >> 3) + 1
} else {
1000
};
let time: u16 = if let Ok(val) = adc.read(&mut an_in) as Result<u16, _> {
/* shift the value right by 3, same as divide by 8, reduces
the 0-4095 range into something approximating 1-512 */
(val >> 3) + 1
} else {
1000
};
delay.delay_ms(time);
}
});
delay.delay_ms(time);
}
}
loop {

24
examples/blinky_delay.rs

@ -13,22 +13,22 @@ use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
let gpioa = p.GPIOA.split(&mut rcc);
// (Re-)configure PA1 as output
let mut led = gpioa.pa1.into_push_pull_output(cs);
// (Re-)configure PA1 as output
let mut led = cortex_m::interrupt::free(move |cs| {
gpioa.pa1.into_push_pull_output(cs);
});
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
loop {
led.toggle().ok();
delay.delay_ms(1_000_u16);
}
});
loop {
led.toggle().ok();
delay.delay_ms(1_000_u16);
}
}
loop {

51
examples/blinky_multiple.rs

@ -13,35 +13,36 @@ use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
let gpiob = p.GPIOB.split(&mut rcc);
// (Re-)configure PA1 as output
let led1 = gpioa.pa1.into_push_pull_output(cs);
// (Re-)configure PB1 as output
let led2 = gpiob.pb1.into_push_pull_output(cs);
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
let gpiob = p.GPIOB.split(&mut rcc);
let (led1, led2) = cortex_m::interrupt::free(move |cs| {
(
// (Re-)configure PA1 as output
gpioa.pa1.into_push_pull_output(cs),
// (Re-)configure PB1 as output
gpiob.pb1.into_push_pull_output(cs),
)
});
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Store them together after erasing the type
let mut leds = [led1.downgrade(), led2.downgrade()];
loop {
for l in &mut leds {
l.set_high().ok();
}
delay.delay_ms(1_000_u16);
// Store them together after erasing the type
let mut leds = [led1.downgrade(), led2.downgrade()];
loop {
for l in &mut leds {
l.set_high().ok();
}
delay.delay_ms(1_000_u16);
for l in &mut leds {
l.set_low().ok();
}
delay.delay_ms(1_000_u16);
for l in &mut leds {
l.set_low().ok();
}
});
delay.delay_ms(1_000_u16);
}
}
loop {

24
examples/blinky_timer.rs

@ -12,24 +12,22 @@ use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let Some(mut p) = pac::Peripherals::take() {
cortex_m::interrupt::free(move |cs| {
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
let gpioa = p.GPIOA.split(&mut rcc);
// (Re-)configure PA1 as output
let mut led = gpioa.pa1.into_push_pull_output(cs);
// (Re-)configure PA1 as output
let mut led = cortex_m::interrupt::free(move |cs| gpioa.pa1.into_push_pull_output(cs));
// Set up a timer expiring after 1s
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
// Set up a timer expiring after 1s
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
loop {
led.toggle().ok();
loop {
led.toggle().ok();
// Wait for the timer to expire
nb::block!(timer.wait()).ok();
}
});
// Wait for the timer to expire
nb::block!(timer.wait()).ok();
}
}
loop {

44
examples/dac.rs

@ -22,37 +22,37 @@ enum Direction {
#[entry]
fn main() -> ! {
if let (Some(mut dp), Some(_cp)) = (pac::Peripherals::take(), cortex_m::Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
let mut dac = cortex_m::interrupt::free(move |cs| {
let gpioa = dp.GPIOA.split(&mut rcc);
let mut dac = dac(dp.DAC, gpioa.pa4.into_analog(cs), &mut rcc);
dac(dp.DAC, gpioa.pa4.into_analog(cs), &mut rcc)
});
dac.enable();
dac.enable();
let mut dir = Direction::Upcounting;
let mut val = 0;
let mut dir = Direction::Upcounting;
let mut val = 0;
dac.set_value(2058);
cortex_m::asm::bkpt();
dac.set_value(2058);
cortex_m::asm::bkpt();
dac.set_value(4095);
cortex_m::asm::bkpt();
dac.set_value(4095);
cortex_m::asm::bkpt();
loop {
dac.set_value(val);
match val {
0 => dir = Direction::Upcounting,
4095 => dir = Direction::Downcounting,
_ => (),
};
loop {
dac.set_value(val);
match val {
0 => dir = Direction::Upcounting,
4095 => dir = Direction::Downcounting,
_ => (),
};
match dir {
Direction::Upcounting => val += 1,
Direction::Downcounting => val -= 1,
}
match dir {
Direction::Upcounting => val += 1,
Direction::Downcounting => val -= 1,
}
});
}
}
loop {

30
examples/serial_echo.rs

@ -12,25 +12,27 @@ use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let Some(p) = pac::Peripherals::take() {
cortex_m::interrupt::free(move |cs| {
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);
let gpioa = p.GPIOA.split(&mut rcc);
let gpioa = p.GPIOA.split(&mut rcc);
let tx = gpioa.pa9.into_alternate_af1(cs);
let rx = gpioa.pa10.into_alternate_af1(cs);
let (tx, rx) = cortex_m::interrupt::free(move |cs| {
(
gpioa.pa9.into_alternate_af1(cs),
gpioa.pa10.into_alternate_af1(cs),
)
});
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
loop {
// Wait for reception of a single byte
let received = nb::block!(serial.read()).unwrap();
loop {
// Wait for reception of a single byte
let received = nb::block!(serial.read()).unwrap();
// Send back previously received byte and wait for completion
nb::block!(serial.write(received)).ok();
}
});
// Send back previously received byte and wait for completion
nb::block!(serial.write(received)).ok();
}
}
loop {

46
examples/serial_spi_bridge.rs

@ -31,35 +31,37 @@ fn main() -> ! {
};
if let Some(p) = pac::Peripherals::take() {
cortex_m::interrupt::free(move |cs| {
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().freeze(&mut flash);
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().freeze(&mut flash);
let (sck, miso, mosi, tx, rx) = cortex_m::interrupt::free(move |cs| {
let gpioa = p.GPIOA.split(&mut rcc);
// Configure pins for SPI
let sck = gpioa.pa5.into_alternate_af0(cs);
let miso = gpioa.pa6.into_alternate_af0(cs);
let mosi = gpioa.pa7.into_alternate_af0(cs);
// Configure SPI with 1MHz rate
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 1.mhz(), &mut rcc);
(
// SPI pins
gpioa.pa5.into_alternate_af0(cs),
gpioa.pa6.into_alternate_af0(cs),
gpioa.pa7.into_alternate_af0(cs),
// USART pins
gpioa.pa9.into_alternate_af1(cs),
gpioa.pa10.into_alternate_af1(cs),
)
});
let tx = gpioa.pa9.into_alternate_af1(cs);
let rx = gpioa.pa10.into_alternate_af1(cs);
// Configure SPI with 1MHz rate
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 1.mhz(), &mut rcc);
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
let (mut tx, mut rx) = serial.split();
let (mut tx, mut rx) = serial.split();
let mut data = [0];
loop {
let serial_received = block!(rx.read()).unwrap();
spi.write(&[serial_received]).ok();
let spi_received = spi.transfer(&mut data).unwrap();
block!(tx.write(spi_received[0])).ok();
}
});
let mut data = [0];
loop {
let serial_received = block!(rx.read()).unwrap();
spi.write(&[serial_received]).ok();
let spi_received = spi.transfer(&mut data).unwrap();
block!(tx.write(spi_received[0])).ok();
}
}
loop {

64
examples/spi_hal_apa102c.rs

@ -22,45 +22,47 @@ fn main() -> ! {
};
if let Some(p) = pac::Peripherals::take() {
cortex_m::interrupt::free(move |cs| {
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().freeze(&mut flash);
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().freeze(&mut flash);
let gpioa = p.GPIOA.split(&mut rcc);
let gpioa = p.GPIOA.split(&mut rcc);
// Configure pins for SPI
let sck = gpioa.pa5.into_alternate_af0(cs);
let miso = gpioa.pa6.into_alternate_af0(cs);
let mosi = gpioa.pa7.into_alternate_af0(cs);
// Configure pins for SPI
let (sck, miso, mosi) = cortex_m::interrupt::free(move |cs| {
(
gpioa.pa5.into_alternate_af0(cs),
gpioa.pa6.into_alternate_af0(cs),
gpioa.pa7.into_alternate_af0(cs),
)
});
// Configure SPI with 100kHz rate
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 100_000.hz(), &mut rcc);
// Configure SPI with 100kHz rate
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 100_000.hz(), &mut rcc);
// Cycle through colors on 16 chained APA102C LEDs
loop {
for r in 0..255 {
let _ = spi.write(&[0, 0, 0, 0]);
for _i in 0..16 {
let _ = spi.write(&[0b1110_0001, 0, 0, r]);
}
let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]);
// Cycle through colors on 16 chained APA102C LEDs
loop {
for r in 0..255 {
let _ = spi.write(&[0, 0, 0, 0]);
for _i in 0..16 {
let _ = spi.write(&[0b1110_0001, 0, 0, r]);
}
for b in 0..255 {
let _ = spi.write(&[0, 0, 0, 0]);
for _i in 0..16 {
let _ = spi.write(&[0b1110_0001, b, 0, 0]);
}
let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]);
let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]);
}
for b in 0..255 {
let _ = spi.write(&[0, 0, 0, 0]);
for _i in 0..16 {
let _ = spi.write(&[0b1110_0001, b, 0, 0]);
}
for g in 0..255 {
let _ = spi.write(&[0, 0, 0, 0]);
for _i in 0..16 {
let _ = spi.write(&[0b1110_0001, 0, g, 0]);
}
let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]);
let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]);
}
for g in 0..255 {
let _ = spi.write(&[0, 0, 0, 0]);
for _i in 0..16 {
let _ = spi.write(&[0b1110_0001, 0, g, 0]);
}
let _ = spi.write(&[0xFF, 0xFF, 0xFF, 0xFF]);
}
});
}
}
loop {

52
examples/watchdog.rs

@ -15,42 +15,40 @@ use core::fmt::Write;
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
let gpioa = p.GPIOA.split(&mut rcc);
let dbgmcu = p.DBGMCU;
let gpioa = p.GPIOA.split(&mut rcc);
let dbgmcu = p.DBGMCU;
// Disable the watchdog when the cpu is stopped under debug
dbgmcu.apb1_fz.modify(|_, w| w.dbg_iwdg_stop().set_bit());
// Disable the watchdog when the cpu is stopped under debug
dbgmcu.apb1_fz.modify(|_, w| w.dbg_iwdg_stop().set_bit());
let mut watchdog = Watchdog::new(p.IWDG);
let mut watchdog = Watchdog::new(p.IWDG);
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Configure serial TX pin
let tx = gpioa.pa9.into_alternate_af1(cs);
// Configure serial TX pin
let tx = cortex_m::interrupt::free(move |cs| gpioa.pa9.into_alternate_af1(cs));
// Obtain a serial peripheral with for unidirectional communication
let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc);
// Obtain a serial peripheral with for unidirectional communication
let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc);
serial.write_str("RESET \r\n").ok();
serial.write_str("RESET \r\n").ok();
watchdog.start(Hertz(1));
delay.delay_ms(500_u16);
watchdog.feed();
delay.delay_ms(500_u16);
watchdog.feed();
delay.delay_ms(500_u16);
serial.write_str("This will get printed \r\n").ok();
watchdog.feed();
watchdog.start(Hertz(1));
delay.delay_ms(500_u16);
watchdog.feed();
delay.delay_ms(500_u16);
watchdog.feed();
delay.delay_ms(500_u16);
serial.write_str("This will get printed \r\n").ok();
watchdog.feed();
// Now a reset happens while delaying
delay.delay_ms(1500_u16);
serial.write_str("This won't\r\n").ok();
});
// Now a reset happens while delaying
delay.delay_ms(1500_u16);
serial.write_str("This won't\r\n").ok();
}
loop {

Loading…
Cancel
Save