Browse Source

Simplified examples a bit and fixed/added commentry

Signed-off-by: Daniel Egger <daniel@eggers-club.de>
trying.tmp
Daniel Egger 4 years ago
parent
commit
2b60666a31
  1. 7
      examples/adc_values.rs
  2. 14
      examples/blinky.rs
  3. 18
      examples/blinky_adc.rs
  4. 13
      examples/blinky_delay.rs
  5. 17
      examples/blinky_multiple.rs
  6. 19
      examples/blinky_timer.rs
  7. 50
      examples/flash_systick.rs
  8. 19
      examples/led_hal_button_irq.rs
  9. 13
      examples/serial_echo.rs
  10. 12
      examples/serial_spi_bridge.rs
  11. 10
      examples/spi_hal_apa102c.rs
  12. 14
      examples/watchdog.rs

7
examples/adc_values.rs

@ -6,15 +6,12 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::{prelude::*, stm32};
use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core};
use cortex_m_rt::{entry, exception};
use core::fmt::Write;
use core::cell::RefCell;
use core::{cell::RefCell, fmt::Write};
struct Shared {
adc: hal::adc::Adc,

14
examples/blinky.rs

@ -6,29 +6,27 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::{prelude::*, stm32};
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let Some(p) = stm32::Peripherals::take() {
if let Some(mut p) = stm32::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 rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
/* (Re-)configure PA1 as output */
// (Re-)configure PA1 as output
let mut led = gpioa.pa1.into_push_pull_output(cs);
loop {
/* Turn PA1 on a million times in a row */
// Turn PA1 on a million times in a row
for _ in 0..1_000_000 {
led.set_high();
}
/* Then turn PA1 off a million times in a row */
// Then turn PA1 off a million times in a row
for _ in 0..1_000_000 {
led.set_low();
}

18
examples/blinky_adc.rs

@ -6,33 +6,29 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::delay::Delay;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::adc::Adc;
use crate::hal::{adc::Adc, delay::Delay, prelude::*, stm32};
use cortex_m::peripheral::Peripherals;
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
if let (Some(mut p), Some(cp)) = (stm32::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 rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
/* (Re-)configure PA1 as output */
// (Re-)configure PA1 as output
let mut led = gpioa.pa1.into_push_pull_output(cs);
/* (Re-)configure PA0 as analog in */
// (Re-)configure PA0 as analog input
let mut an_in = gpioa.pa0.into_analog(cs);
/* Get delay provider */
// 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);
loop {

13
examples/blinky_delay.rs

@ -6,26 +6,23 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::delay::Delay;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::{delay::Delay, prelude::*, stm32};
use cortex_m::peripheral::Peripherals;
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
if let (Some(mut p), Some(cp)) = (stm32::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 rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
/* (Re-)configure PA1 as output */
// (Re-)configure PA1 as output
let mut led = gpioa.pa1.into_push_pull_output(cs);
/* Get delay provider */
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
loop {

17
examples/blinky_multiple.rs

@ -6,33 +6,30 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::delay::Delay;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::{delay::Delay, prelude::*, stm32};
use cortex_m::peripheral::Peripherals;
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
if let (Some(mut p), Some(cp)) = (stm32::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 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 */
// (Re-)configure PA1 as output
let led1 = gpioa.pa1.into_push_pull_output(cs);
/* (Re-)configure PB1 as output */
// (Re-)configure PB1 as output
let led2 = gpiob.pb1.into_push_pull_output(cs);
/* Get delay provider */
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
/* Store them together */
// Store them together after erasing the type
let mut leds = [led1.downgrade(), led2.downgrade()];
loop {
for l in &mut leds {

19
examples/blinky_timer.rs

@ -6,30 +6,29 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::time::*;
use crate::hal::timers::*;
use crate::hal::{prelude::*, stm32, time::Hertz, timers::*};
use cortex_m_rt::entry;
use nb::block;
#[entry]
fn main() -> ! {
if let Some(p) = stm32::Peripherals::take() {
if let Some(mut p) = stm32::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 rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
/* (Re-)configure PA1 as output */
// (Re-)configure PA1 as output
let mut led = 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);
loop {
led.toggle();
block!(timer.wait()).ok();
// Wait for the timer to expire
nb::block!(timer.wait()).ok();
}
});
}

50
examples/flash_systick.rs

@ -6,49 +6,46 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::gpio::*;
use crate::hal::prelude::*;
use crate::hal::stm32;
use crate::hal::{gpio::*, prelude::*, stm32};
use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::syst::SystClkSource::Core;
use cortex_m::peripheral::Peripherals;
use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals};
use cortex_m_rt::{entry, exception};
use core::cell::RefCell;
use core::ops::DerefMut;
// Mutex protected structure for our shared GPIO pin
static GPIO: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), 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 rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);
let mut syst = cp.SYST;
/* (Re-)configure PA1 as output */
// (Re-)configure PA1 as output
let led = gpioa.pa1.into_push_pull_output(cs);
// Transfer GPIO into a shared structure
*GPIO.borrow(cs).borrow_mut() = Some(led);
/* Initialise SysTick counter with a defined value */
let mut syst = cp.SYST;
// Initialise SysTick counter with a defined value
unsafe { syst.cvr.write(1) };
/* Set source for SysTick counter, here full operating frequency (== 8MHz) */
// Set source for SysTick counter, here full operating frequency (== 48MHz)
syst.set_clock_source(Core);
/* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */
// Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms
syst.set_reload(4_000_000 - 1);
/* Start counter */
// Start counting
syst.enable_counter();
/* Start interrupt generation */
// Enable interrupt generation
syst.enable_interrupt();
});
}
@ -58,28 +55,29 @@ fn main() -> ! {
}
}
/* Define an exception, i.e. function to call when exception occurs. Here if our SysTick timer
* trips the flash function will be called and the specified stated passed in via argument */
//, flash, state: u8 = 1);
// Define an exception handler, i.e. function to call when exception occurs. Here, if our SysTick
// timer generates an exception the following handler will be called
#[exception]
fn SysTick() -> ! {
// Exception handler state variable
static mut state: u8 = 1;
/* Enter critical section */
// Enter critical section
cortex_m::interrupt::free(|cs| {
// Borrow access to our GPIO pin from the shared structure
if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() {
/* Check state variable, keep LED off most of the time and turn it on every 10th tick */
// Check state variable, keep LED off most of the time and turn it on every 10th tick
if *state < 10 {
/* If set turn off the LED */
// Turn off the LED
led.set_low();
/* And now increment state variable */
// And now increment state variable
*state += 1;
} else {
/* If not set, turn on the LED */
// Turn on the LED
led.set_high();
/* And set new state variable back to 0 */
// And set new state variable back to 0
*state = 0;
}
}

19
examples/led_hal_button_irq.rs

@ -6,18 +6,17 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::delay::Delay;
use crate::hal::gpio::*;
use crate::hal::prelude::*;
use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
use crate::hal::{
delay::Delay,
gpio::*,
prelude::*,
stm32::{interrupt, Interrupt, Peripherals, EXTI},
};
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
use cortex_m_rt::entry;
use crate::hal::stm32::{interrupt, Interrupt, Peripherals, EXTI};
use core::cell::RefCell;
use core::ops::DerefMut;
use core::{cell::RefCell, ops::DerefMut};
// Make our LED globally available
static LED: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));

13
examples/serial_echo.rs

@ -6,11 +6,7 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::serial::Serial;
use crate::hal::stm32;
use nb::block;
use crate::hal::{prelude::*, serial::Serial, stm32};
use cortex_m_rt::entry;
@ -29,8 +25,11 @@ fn main() -> ! {
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
loop {
let received = block!(serial.read()).unwrap();
block!(serial.write(received)).ok();
// 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();
}
});
}

12
examples/serial_spi_bridge.rs

@ -6,11 +6,13 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::serial::Serial;
use crate::hal::spi::Spi;
use crate::hal::spi::{Mode, Phase, Polarity};
use crate::hal::stm32;
use crate::hal::{
prelude::*,
serial::Serial,
spi::Spi,
spi::{Mode, Phase, Polarity},
stm32,
};
use nb::block;

10
examples/spi_hal_apa102c.rs

@ -6,10 +6,12 @@ use panic_halt;
use stm32f0xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::spi::Spi;
use crate::hal::spi::{Mode, Phase, Polarity};
use crate::hal::stm32;
use crate::hal::{
prelude::*,
spi::Spi,
spi::{Mode, Phase, Polarity},
stm32,
};
use cortex_m_rt::entry;

14
examples/watchdog.rs

@ -4,19 +4,17 @@
#[allow(unused)]
use panic_halt;
use core::fmt::Write;
use stm32f0xx_hal as hal;
use crate::hal::delay::Delay;
use crate::hal::prelude::*;
use crate::hal::serial::Serial;
use crate::hal::stm32;
use crate::hal::time::Hertz;
use crate::hal::watchdog::Watchdog;
use crate::hal::{
delay::Delay, prelude::*, serial::Serial, stm32, time::Hertz, watchdog::Watchdog,
};
use cortex_m::peripheral::Peripherals;
use cortex_m_rt::entry;
use core::fmt::Write;
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
@ -35,8 +33,10 @@ fn main() -> ! {
// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
// Configure serial TX pin
let tx = 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);
serial.write_str("RESET \r\n").ok();

Loading…
Cancel
Save