Skip to content

Commit

Permalink
Add serial example
Browse files Browse the repository at this point in the history
  • Loading branch information
gdobato committed Nov 6, 2023
1 parent d84dd75 commit b3a2937
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ 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::led;

#[main]
async fn main(spawner: Spawner) {
info!("Starting");
info!("Init");
let portenta_h7_async::Board {
led_red,
led_blue,
Expand Down
44 changes: 44 additions & 0 deletions examples/serial.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
53 changes: 50 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<peripherals::USART1>;
UART4 => usart::InterruptHandler<peripherals::UART4>;
});

// 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 {
Expand All @@ -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,
}
}
}
8 changes: 0 additions & 8 deletions src/panic.rs

This file was deleted.

0 comments on commit b3a2937

Please sign in to comment.