Skip to content

Commit

Permalink
Merge pull request #13 from decaday/feat/usart
Browse files Browse the repository at this point in the history
USART Support
  • Loading branch information
decaday authored Nov 16, 2024
2 parents e80f867 + 87e71e1 commit 75f80ca
Show file tree
Hide file tree
Showing 8 changed files with 2,600 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
embedded-hal-async = { version = "1.0" }
embedded-hal-nb = { version = "1.0" }
embedded-can = "0.4"
embedded-io = { version = "0.6.0" }
embedded-io-async = { version = "0.6.1" }
nb = "1.0.0"

defmt = { version = "0.3", optional = true }
defmt-rtt = { version = "0.4", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ For a full list of chip capabilities and peripherals, check the [py32-data](http
| INTERRUPT | || | |
| DMA | N/A | | | |
| EXTI | | ✅+ | | |
| USART | | | | |
| USART | | | | |
| I2C | || | |
| SPI | | | | |
| ADC | | ✅+ | | |
Expand Down
4 changes: 3 additions & 1 deletion examples/py32f030/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }

embassy-sync = { version = "0.6.0", features = ["defmt"] }
embassy-executor = { version = "0.6.1", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-time = { version = "0.3.2", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-1_000"] }
embassy-time = { version = "0.3.2", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }

embedded-io = { version = "0.6.0" }
embedded-io-async = { version = "0.6.1" }

py32-hal = { path = "../../", features = [ "time-driver-tim3", "py32f030f16"]}

Expand Down
28 changes: 28 additions & 0 deletions examples/py32f030/src/bin/usart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_std]
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use embassy_executor::Spawner;
use defmt::*;
use py32_hal::usart::{Config, Uart};
use py32_hal::{bind_interrupts, peripherals, usart};
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
info!("Hello World!");

let p = py32_hal::init(Default::default());

let config = Config::default();
let mut usart = Uart::new_blocking(p.USART1, p.PA3, p.PA2, config).unwrap();

unwrap!(usart.blocking_write(b"Hello Embassy World!"));
info!("wrote Hello, starting echo");

let mut buf = [0u8; 1];
loop {
unwrap!(usart.blocking_read(&mut buf));
unwrap!(usart.blocking_write(&buf));
}
}
49 changes: 49 additions & 0 deletions examples/py32f030/src/bin/usart_buffered.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_std]
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use defmt::*;
use embassy_executor::Spawner;
use py32_hal::usart::{BufferedUart, Config};
use py32_hal::{bind_interrupts, peripherals, usart};
use py32_hal::time::Hertz;
use py32_hal::rcc::{Pll, PllSource, Sysclk};
use embedded_io_async::Read;
use embedded_io_async::Write;
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
USART1 => usart::BufferedInterruptHandler<peripherals::USART1>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut cfg: py32_hal::Config = Default::default();
cfg.rcc.hsi = Some(Hertz::mhz(24));
let p = py32_hal::init(cfg);
info!("Hello World!");

let config = Config::default();
let mut tx_buf = [0u8; 256];
let mut rx_buf = [0u8; 256];
let mut usart = BufferedUart::new(p.USART1, Irqs, p.PA3, p.PA2, &mut tx_buf, &mut rx_buf, config).unwrap();

usart.write_all(b"Hello Embassy World!\r\n").await.unwrap();
info!("wrote Hello, starting echo");

let mut buf = [0; 5];
loop {
// When using defmt, be cautious with the info! and other logging macros!
// If you're using a single channel (as is usually the case), defmt requires global_logger to acquire interrupts to be disabled.
// For example, defmt-rtt uses critical_section, which temporarily disables global interrupts.
//This can lead to USART Overrun error(SR.ORE), causing some data to be lost.
usart.read_exact(&mut buf[..]).await.unwrap();
// info!("Received:{} {}", buf, buf.len());
usart.write_all(&buf[..]).await.unwrap();

// use embedded_io_async::BufRead;
// let buf = usart.fill_buf().await.unwrap();
// let n = buf.len();
// usart.consume(n);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod rcc;
pub mod i2c;
pub mod adc;
pub mod dma;
pub mod usart;
pub mod timer;
#[cfg(feature = "_time-driver")]
pub mod time_driver;
Expand Down
Loading

0 comments on commit 75f80ca

Please sign in to comment.