Skip to content

Commit 13c60b1

Browse files
committed
feat : Add async-io feature and embedded-io-async dependency
Added the async-io feature to Cargo.toml, enabling async support for UART and other modules. Added embedded-io-async as an optional dependency, activated by the async-io feature. Updated code to use feature gating for async implementations.
1 parent a826acd commit 13c60b1

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test-rsa = []
2121
test-ecdsa = []
2222
test-hmac = []
2323
test-hash = []
24+
async-io = ["embedded-io-async"]
2425

2526
[dependencies]
2627
ast1060-pac = { git = "https://github.com/rusty1968/ast1060-pac.git", features = ["rt"] }
@@ -35,3 +36,7 @@ cortex-m = { version = "0.7.5" }
3536
cortex-m-rt = { version = "0.6.5", features = ["device"] }
3637
panic-halt = "1.0.0"
3738

39+
[dependencies.embedded-io-async]
40+
version = "0.6"
41+
optional = true
42+

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ pub mod rsa;
99
pub mod syscon;
1010
pub mod tests;
1111
pub mod uart;
12+
#[cfg(feature = "async-io")]
13+
pub mod uart_async;
14+
1215
pub mod watchdog;

src/uart.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,15 @@ impl UartController<'_> {
137137
// Additional configurations can be added here
138138
}
139139

140+
/// Returns true if the Transmitter Holding Register (THR) is empty.
141+
pub fn is_thr_empty(&self) -> bool {
142+
self.uart.uartlsr().read().thre().bit_is_set()
143+
}
144+
140145
/// Sends a byte using the FIFO.
141146
pub fn send_byte_fifo(&mut self, data: u8) {
142147
// Wait until the Transmitter Holding Register (THR) is empty
143-
while self.uart.uartlsr().read().thre().bit_is_clear() {}
148+
while !self.is_thr_empty() {}
144149

145150
// Write the byte to the Transmit Holding Register (THR)
146151
self.uart
@@ -166,6 +171,21 @@ impl UartController<'_> {
166171
}
167172

168173
impl<'a> UartController<'a> {
174+
/// Reads a byte from the UART receiver buffer register (UARTRBR).
175+
pub fn read_rbr(&self) -> u8 {
176+
self.uart.uartrbr().read().uartrbr().bits()
177+
}
178+
179+
/// Returns true if data is available in the UART receiver buffer (DR bit is set).
180+
pub fn is_data_ready(&self) -> bool {
181+
self.uart.uartlsr().read().dr().bit_is_set()
182+
}
183+
184+
/// Writes a byte to the UART transmit holding register (THR).
185+
pub fn write_thr(&mut self, byte: u8) {
186+
self.uart.uartthr().write(|w| unsafe { w.bits(u32::from(byte)) });
187+
}
188+
169189
// Wait until the Transmitter Holding Register (THR) is empty
170190
pub fn new(uart: Uart, delay: &'a mut dyn DelayNs) -> Self {
171191
Self { uart, delay }
@@ -230,3 +250,4 @@ impl embedded_io::Write for UartController<'_> {
230250
Ok(())
231251
}
232252
}
253+

src/uart_async.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use ast1060_pac::Uart;
2+
3+
use crate::uart::UartController;
4+
5+
use embedded_io_async::{Read, Write};
6+
use core::future::poll_fn;
7+
use core::task::{Context, Poll};
8+
9+
/// Async implementation of embedded_io_async::Read for UartController.
10+
///
11+
/// Reads bytes asynchronously from the UART receiver buffer.
12+
/// Waits until data is available before reading each byte.
13+
impl embedded_io_async::Read for UartController<'_> {
14+
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
15+
let mut count = 0;
16+
for byte in buf.iter_mut() {
17+
poll_fn(|cx| {
18+
if self.is_data_ready() {
19+
*byte = self.read_rbr();
20+
Poll::Ready(Ok(()))
21+
} else {
22+
cx.waker().wake_by_ref();
23+
Poll::Pending
24+
}
25+
})
26+
.await?;
27+
count += 1;
28+
}
29+
Ok(count)
30+
}
31+
}
32+
33+
/// Async implementation of embedded_io_async::Write for UartController.
34+
///
35+
/// Writes bytes asynchronously to the UART transmit holding register.
36+
/// Waits until the transmitter is ready before sending each byte.
37+
impl embedded_io_async::Write for UartController<'_> {
38+
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
39+
for &byte in buf {
40+
poll_fn(|cx| {
41+
if self.is_thr_empty() {
42+
self.write_thr(byte);
43+
Poll::Ready(Ok(()))
44+
} else {
45+
cx.waker().wake_by_ref();
46+
Poll::Pending
47+
}
48+
})
49+
.await?;
50+
}
51+
Ok(buf.len())
52+
}
53+
}

0 commit comments

Comments
 (0)