RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/split/
uart_full_duplex.rs

1use embassy_nrf::buffered_uarte::BufferedUarte;
2use embassy_nrf::timer::Instance as TimerInstance;
3use embassy_nrf::uarte::Instance as UarteInstance;
4use embedded_io_async::Write as _;
5use rktk::drivers::interface::split::SplitDriver;
6
7#[derive(Debug)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9pub enum UartFullDuplexSplitDriverError {
10    GeneralError(&'static str),
11}
12
13impl rktk::drivers::interface::Error for UartFullDuplexSplitDriverError {}
14
15pub struct UartFullDuplexSplitDriver<I: UarteInstance + 'static, T: TimerInstance + 'static> {
16    uarte: BufferedUarte<'static, I, T>,
17}
18
19impl<I: UarteInstance + 'static, T: TimerInstance + 'static> UartFullDuplexSplitDriver<I, T> {
20    pub fn new(uarte: BufferedUarte<'static, I, T>) -> Self {
21        Self { uarte }
22    }
23}
24
25impl<I: UarteInstance + 'static, T: TimerInstance + 'static> SplitDriver
26    for UartFullDuplexSplitDriver<I, T>
27{
28    type Error = UartFullDuplexSplitDriverError;
29
30    async fn recv(&mut self, buf: &mut [u8], _is_master: bool) -> Result<usize, Self::Error> {
31        let size = self
32            .uarte
33            .read(buf)
34            .await
35            .map_err(|_| UartFullDuplexSplitDriverError::GeneralError("Read error"))?;
36        Ok(size)
37    }
38
39    async fn send_all(&mut self, buf: &[u8], _is_master: bool) -> Result<(), Self::Error> {
40        self.uarte
41            .write_all(buf)
42            .await
43            .map_err(|_| UartFullDuplexSplitDriverError::GeneralError("Write error"))
44    }
45}