Skip to content

Commit

Permalink
new interrupt driven serial echo
Browse files Browse the repository at this point in the history
  • Loading branch information
AdinAck committed Oct 1, 2023
1 parent eed06fb commit 5d6bf43
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- PWM complementary output capability for TIM1 with new example to demonstrate
- New `serial_echo_irq` example to showcase an interrupt driven serial echo
### Changed

- Updated the `cast` dependency from 0.2 to 0.3
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ required-features = ["stm32f042", "rt"]
[[example]]
name = "usb_serial"
required-features = ["rt", "stm32f042", "stm32-usbd"]

[[example]]
name = "serial_echo_irq"
required-features = ["stm32f031"]
102 changes: 102 additions & 0 deletions examples/serial_echo_irq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//! Interrupt Driven Serial Echo Example
//! For NUCLEO-F031K6

#![no_main]
#![no_std]
#![deny(unsafe_code)]
#![allow(non_camel_case_types)]

use core::cell::RefCell;
use nb::block;
use panic_halt as _;

use cortex_m::interrupt::Mutex;
use cortex_m_rt::entry;

use hal::{
delay::Delay,
gpio::{
gpioa::{PA15, PA2},
Alternate, AF1,
},
pac::{self, interrupt, Interrupt, USART1},
prelude::*,
serial::Serial,
};
use stm32f0xx_hal as hal;

type SERIAL = Serial<USART1, PA2<Alternate<AF1>>, PA15<Alternate<AF1>>>;

/*
Create our global variables:
We use a Mutex because Mutexes require a CriticalSection
context in order to be borrowed. Since CriticalSection
contexts cannot overlap (by definition) we can rest assured
that the resource inside the Mutex will not violate
the RefMut's runtime borrowing rules (Given that we do not
try to borrow the RefMut more than once per CriticalSection).
*/
static SER_PORT: Mutex<RefCell<Option<SERIAL>>> = Mutex::new(RefCell::new(None));

#[entry]
fn main() -> ! {
let (mut delay, mut led) = cortex_m::interrupt::free(|cs| {
let dp = pac::Peripherals::take().unwrap(); // might as well panic if this doesn't work
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
let mut flash = dp.FLASH;
let mut rcc = dp.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);

let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);

let delay = Delay::new(cp.SYST, &rcc);

// setup UART
let (tx, rx) = (
gpioa.pa2.into_alternate_af1(cs),
gpioa.pa15.into_alternate_af1(cs),
);

// initialize global serial
*SER_PORT.borrow(cs).borrow_mut() =
Some(Serial::usart1(dp.USART1, (tx, rx), 9_600.bps(), &mut rcc));

if let Some(ser) = SER_PORT.borrow(cs).borrow_mut().as_mut() {
ser.listen(hal::serial::Event::Rxne); // trigger the USART1 interrupt when bytes are available (receive buffer not empty)
}

let led = gpiob.pb3.into_push_pull_output(cs);

(delay, led)
});

#[allow(unsafe_code)] // just this once ;)
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::USART1);
}

loop {
led.toggle().ok();

delay.delay_ms(1_000u16);
}
}

#[interrupt]
fn USART1() {
cortex_m::interrupt::free(|cs| {
if let Some(ser) = SER_PORT.borrow(cs).borrow_mut().as_mut() {
if let Ok(data) = block!(ser.read()) {
block!(ser.write(data)).ok();
} else {
/*
Failed to read a byte:
There could be some kind of alignment error or the UART
was disconnected or something.
*/
}
}
});
}

0 comments on commit 5d6bf43

Please sign in to comment.