diff --git a/.cargo/config.toml b/.cargo/config.toml index c0dda4d..a0aa49b 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -7,6 +7,9 @@ oe = "objcopy --example" blinky = "be blinky" blinky-probe = "ee blinky" blinky-bin = "oe blinky --release -- -O binary target/thumbv7em-none-eabihf/release/examples/blinky.bin" +serial = "be serial" +serial-probe = "ee serial" +serial-bin = "oe serial --release -- -O binary target/thumbv7em-none-eabihf/release/examples/serial.bin" [build] target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) diff --git a/examples/blinky.rs b/examples/blinky.rs index dba3971..efb859d 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -10,7 +10,7 @@ use portenta_h7_async::led; #[main] async fn main(spawner: Spawner) { - info!("Starting"); + info!("Init"); let portenta_h7_async::Board { led_red, led_blue, diff --git a/examples/serial.rs b/examples/serial.rs new file mode 100644 index 0000000..2bbe250 --- /dev/null +++ b/examples/serial.rs @@ -0,0 +1,44 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::info; +use defmt_rtt as _; +use embassy_executor::{main, task, Spawner}; +use embassy_time::Timer; +use panic_probe as _; +use portenta_h7_async::{Uart0, Uart1}; + +const DATA: &[u8] = b"Hello world!"; + +#[main] +async fn main(spawner: Spawner) { + info!("Init"); + let portenta_h7_async::Board { uart0, uart1, .. } = portenta_h7_async::Board::take(); + + spawner.spawn(uart_write(uart0)).unwrap(); + spawner.spawn(uart_read(uart1)).unwrap(); + + loop { + Timer::after_millis(100).await; + } +} + +#[task] +async fn uart_write(mut uart: Uart0) { + loop { + uart.blocking_write(DATA).unwrap(); // TODO Use async version, it panics, fix it + Timer::after_millis(1_000).await; + } +} + +#[task] +async fn uart_read(mut uart: Uart1) { + let rx_buf: &mut [u8] = &mut [0; DATA.len()]; + loop { + Timer::after_millis(1_000).await; + uart.blocking_read(rx_buf).unwrap(); // TODO Use async version, it panics, fix it + + info!("{}", core::str::from_utf8(&rx_buf).unwrap()); + } +} diff --git a/src/lib.rs b/src/lib.rs index 45f8d32..c698c4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,32 @@ #![no_std] pub mod led; -pub mod panic; mod sys; +use core::sync::atomic::{AtomicBool, Ordering}; + use embassy_stm32::{ + bind_interrupts, gpio::{Level, Output, Speed}, peripherals, + usart::{self, Uart}, + Config, }; -use core::sync::atomic::{AtomicBool, Ordering}; +bind_interrupts!(struct Irqs { + USART1 => usart::InterruptHandler; + UART4 => usart::InterruptHandler; +}); + +// Naming according to breakout board +pub type Uart0 = Uart<'static, peripherals::UART4, peripherals::DMA1_CH0, peripherals::DMA1_CH1>; +pub type Uart1 = Uart<'static, peripherals::USART1, peripherals::DMA1_CH2, peripherals::DMA1_CH3>; pub struct Board { pub led_red: Output<'static, peripherals::PK5>, pub led_green: Output<'static, peripherals::PK6>, pub led_blue: Output<'static, peripherals::PK7>, + pub uart0: Uart0, + pub uart1: Uart1, } impl Board { @@ -25,15 +38,49 @@ impl Board { pub fn setup() -> Self { sys::Clk::new().reset().enable_ext_clock(); - let p = embassy_stm32::init(Default::default()); + // TODO Configure 480 MHz (sys) and 240 MHz (per) + let config = Config::default(); + let p = embassy_stm32::init(config); + + // User leds let led_red = Output::new(p.PK5, Level::High, Speed::Low); let led_green = Output::new(p.PK6, Level::High, Speed::Low); let led_blue = Output::new(p.PK7, Level::High, Speed::Low); + // Uart0 of breakout board + let uart0 = Uart::new_with_rtscts( + p.UART4, + p.PI9, + p.PA0, + Irqs, + p.PA15, + p.PB0, + p.DMA1_CH0, + p.DMA1_CH1, + Default::default(), + ) + .unwrap(); + + // Uart1 of breakout board + let uart1 = Uart::new_with_rtscts( + p.USART1, + p.PA10, + p.PA9, + Irqs, + p.PA12, + p.PA11, + p.DMA1_CH2, + p.DMA1_CH3, + Default::default(), + ) + .unwrap(); + Self { led_red, led_green, led_blue, + uart0, + uart1, } } } diff --git a/src/panic.rs b/src/panic.rs deleted file mode 100644 index 4fa3db2..0000000 --- a/src/panic.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! panic - -use core::panic::PanicInfo; - -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop {} -}