Skip to content

Commit 75f80ca

Browse files
authored
Merge pull request #13 from decaday/feat/usart
USART Support
2 parents e80f867 + 87e71e1 commit 75f80ca

File tree

8 files changed

+2600
-2
lines changed

8 files changed

+2600
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
2929
embedded-hal-async = { version = "1.0" }
3030
embedded-hal-nb = { version = "1.0" }
3131
embedded-can = "0.4"
32+
embedded-io = { version = "0.6.0" }
33+
embedded-io-async = { version = "0.6.1" }
34+
nb = "1.0.0"
3235

3336
defmt = { version = "0.3", optional = true }
3437
defmt-rtt = { version = "0.4", optional = true }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ For a full list of chip capabilities and peripherals, check the [py32-data](http
4747
| INTERRUPT | || | |
4848
| DMA | N/A | | | |
4949
| EXTI | | ✅+ | | |
50-
| USART | | | | |
50+
| USART | | | | |
5151
| I2C | || | |
5252
| SPI | | | | |
5353
| ADC | | ✅+ | | |

examples/py32f030/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
1515

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

20+
embedded-io = { version = "0.6.0" }
21+
embedded-io-async = { version = "0.6.1" }
2022

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

examples/py32f030/src/bin/usart.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(impl_trait_in_assoc_type)]
4+
5+
use embassy_executor::Spawner;
6+
use defmt::*;
7+
use py32_hal::usart::{Config, Uart};
8+
use py32_hal::{bind_interrupts, peripherals, usart};
9+
use {defmt_rtt as _, panic_probe as _};
10+
11+
#[embassy_executor::main]
12+
async fn main(_spawner: Spawner) {
13+
info!("Hello World!");
14+
15+
let p = py32_hal::init(Default::default());
16+
17+
let config = Config::default();
18+
let mut usart = Uart::new_blocking(p.USART1, p.PA3, p.PA2, config).unwrap();
19+
20+
unwrap!(usart.blocking_write(b"Hello Embassy World!"));
21+
info!("wrote Hello, starting echo");
22+
23+
let mut buf = [0u8; 1];
24+
loop {
25+
unwrap!(usart.blocking_read(&mut buf));
26+
unwrap!(usart.blocking_write(&buf));
27+
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(impl_trait_in_assoc_type)]
4+
5+
use defmt::*;
6+
use embassy_executor::Spawner;
7+
use py32_hal::usart::{BufferedUart, Config};
8+
use py32_hal::{bind_interrupts, peripherals, usart};
9+
use py32_hal::time::Hertz;
10+
use py32_hal::rcc::{Pll, PllSource, Sysclk};
11+
use embedded_io_async::Read;
12+
use embedded_io_async::Write;
13+
use {defmt_rtt as _, panic_probe as _};
14+
15+
bind_interrupts!(struct Irqs {
16+
USART1 => usart::BufferedInterruptHandler<peripherals::USART1>;
17+
});
18+
19+
#[embassy_executor::main]
20+
async fn main(_spawner: Spawner) {
21+
let mut cfg: py32_hal::Config = Default::default();
22+
cfg.rcc.hsi = Some(Hertz::mhz(24));
23+
let p = py32_hal::init(cfg);
24+
info!("Hello World!");
25+
26+
let config = Config::default();
27+
let mut tx_buf = [0u8; 256];
28+
let mut rx_buf = [0u8; 256];
29+
let mut usart = BufferedUart::new(p.USART1, Irqs, p.PA3, p.PA2, &mut tx_buf, &mut rx_buf, config).unwrap();
30+
31+
usart.write_all(b"Hello Embassy World!\r\n").await.unwrap();
32+
info!("wrote Hello, starting echo");
33+
34+
let mut buf = [0; 5];
35+
loop {
36+
// When using defmt, be cautious with the info! and other logging macros!
37+
// If you're using a single channel (as is usually the case), defmt requires global_logger to acquire interrupts to be disabled.
38+
// For example, defmt-rtt uses critical_section, which temporarily disables global interrupts.
39+
//This can lead to USART Overrun error(SR.ORE), causing some data to be lost.
40+
usart.read_exact(&mut buf[..]).await.unwrap();
41+
// info!("Received:{} {}", buf, buf.len());
42+
usart.write_all(&buf[..]).await.unwrap();
43+
44+
// use embedded_io_async::BufRead;
45+
// let buf = usart.fill_buf().await.unwrap();
46+
// let n = buf.len();
47+
// usart.consume(n);
48+
}
49+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod rcc;
3939
pub mod i2c;
4040
pub mod adc;
4141
pub mod dma;
42+
pub mod usart;
4243
pub mod timer;
4344
#[cfg(feature = "_time-driver")]
4445
pub mod time_driver;

0 commit comments

Comments
 (0)