Skip to content

Commit 6aa0777

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 6aa0777

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,16 @@ impl UartController<'_> {
137137
// Additional configurations can be added here
138138
}
139139

140+
/// Returns true if the Transmitter Holding Register (THR) is empty.
141+
#[must_use]
142+
pub fn is_thr_empty(&self) -> bool {
143+
self.uart.uartlsr().read().thre().bit_is_set()
144+
}
145+
140146
/// Sends a byte using the FIFO.
141147
pub fn send_byte_fifo(&mut self, data: u8) {
142148
// Wait until the Transmitter Holding Register (THR) is empty
143-
while self.uart.uartlsr().read().thre().bit_is_clear() {}
149+
while !self.is_thr_empty() {}
144150

145151
// Write the byte to the Transmit Holding Register (THR)
146152
self.uart
@@ -166,6 +172,25 @@ impl UartController<'_> {
166172
}
167173

168174
impl<'a> UartController<'a> {
175+
/// Reads a byte from the UART receiver buffer register (UARTRBR).
176+
#[must_use]
177+
pub fn read_rbr(&self) -> u8 {
178+
self.uart.uartrbr().read().uartrbr().bits()
179+
}
180+
181+
/// Returns true if data is available in the UART receiver buffer (DR bit is set).
182+
#[must_use]
183+
pub fn is_data_ready(&self) -> bool {
184+
self.uart.uartlsr().read().dr().bit_is_set()
185+
}
186+
187+
/// Writes a byte to the UART transmit holding register (THR).
188+
pub fn write_thr(&mut self, byte: u8) {
189+
self.uart
190+
.uartthr()
191+
.write(|w| unsafe { w.bits(u32::from(byte)) });
192+
}
193+
169194
// Wait until the Transmitter Holding Register (THR) is empty
170195
pub fn new(uart: Uart, delay: &'a mut dyn DelayNs) -> Self {
171196
Self { uart, delay }

src/uart_async.rs

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

0 commit comments

Comments
 (0)