aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/uart/buffered.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-rp/src/uart/buffered.rs')
-rw-r--r--embassy-rp/src/uart/buffered.rs304
1 files changed, 158 insertions, 146 deletions
diff --git a/embassy-rp/src/uart/buffered.rs b/embassy-rp/src/uart/buffered.rs
index da18138b5..02649ad81 100644
--- a/embassy-rp/src/uart/buffered.rs
+++ b/embassy-rp/src/uart/buffered.rs
@@ -34,28 +34,29 @@ impl State {
34} 34}
35 35
36/// Buffered UART driver. 36/// Buffered UART driver.
37pub struct BufferedUart<'d, T: Instance> { 37pub struct BufferedUart {
38 pub(crate) rx: BufferedUartRx<'d, T>, 38 pub(super) rx: BufferedUartRx,
39 pub(crate) tx: BufferedUartTx<'d, T>, 39 pub(super) tx: BufferedUartTx,
40} 40}
41 41
42/// Buffered UART RX handle. 42/// Buffered UART RX handle.
43pub struct BufferedUartRx<'d, T: Instance> { 43pub struct BufferedUartRx {
44 pub(crate) phantom: PhantomData<&'d mut T>, 44 pub(super) info: &'static Info,
45 pub(super) state: &'static State,
45} 46}
46 47
47/// Buffered UART TX handle. 48/// Buffered UART TX handle.
48pub struct BufferedUartTx<'d, T: Instance> { 49pub struct BufferedUartTx {
49 pub(crate) phantom: PhantomData<&'d mut T>, 50 pub(super) info: &'static Info,
51 pub(super) state: &'static State,
50} 52}
51 53
52pub(crate) fn init_buffers<'d, T: Instance + 'd>( 54pub(super) fn init_buffers<'d>(
53 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 55 info: &Info,
56 state: &State,
54 tx_buffer: Option<&'d mut [u8]>, 57 tx_buffer: Option<&'d mut [u8]>,
55 rx_buffer: Option<&'d mut [u8]>, 58 rx_buffer: Option<&'d mut [u8]>,
56) { 59) {
57 let state = T::buffered_state();
58
59 if let Some(tx_buffer) = tx_buffer { 60 if let Some(tx_buffer) = tx_buffer {
60 let len = tx_buffer.len(); 61 let len = tx_buffer.len();
61 unsafe { state.tx_buf.init(tx_buffer.as_mut_ptr(), len) }; 62 unsafe { state.tx_buf.init(tx_buffer.as_mut_ptr(), len) };
@@ -76,61 +77,73 @@ pub(crate) fn init_buffers<'d, T: Instance + 'd>(
76 // This means we can leave the interrupt enabled the whole time as long as 77 // This means we can leave the interrupt enabled the whole time as long as
77 // we clear it after it happens. The downside is that the we manually have 78 // we clear it after it happens. The downside is that the we manually have
78 // to pend the ISR when we want data transmission to start. 79 // to pend the ISR when we want data transmission to start.
79 let regs = T::regs(); 80 info.regs.uartimsc().write(|w| {
80 regs.uartimsc().write(|w| {
81 w.set_rxim(true); 81 w.set_rxim(true);
82 w.set_rtim(true); 82 w.set_rtim(true);
83 w.set_txim(true); 83 w.set_txim(true);
84 }); 84 });
85 85
86 T::Interrupt::unpend(); 86 info.interrupt.unpend();
87 unsafe { T::Interrupt::enable() }; 87 unsafe { info.interrupt.enable() };
88} 88}
89 89
90impl<'d, T: Instance> BufferedUart<'d, T> { 90impl BufferedUart {
91 /// Create a buffered UART instance. 91 /// Create a buffered UART instance.
92 pub fn new( 92 pub fn new<'d, T: Instance>(
93 _uart: Peri<'d, T>, 93 _uart: Peri<'d, T>,
94 tx: Peri<'d, impl TxPin<T>>, 94 tx: Peri<'d, impl TxPin<T>>,
95 rx: Peri<'d, impl RxPin<T>>, 95 rx: Peri<'d, impl RxPin<T>>,
96 irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 96 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>,
97 tx_buffer: &'d mut [u8], 97 tx_buffer: &'d mut [u8],
98 rx_buffer: &'d mut [u8], 98 rx_buffer: &'d mut [u8],
99 config: Config, 99 config: Config,
100 ) -> Self { 100 ) -> Self {
101 super::Uart::<'d, T, Async>::init(Some(tx.into()), Some(rx.into()), None, None, config); 101 super::Uart::<'d, Async>::init(T::info(), Some(tx.into()), Some(rx.into()), None, None, config);
102 init_buffers::<T>(irq, Some(tx_buffer), Some(rx_buffer)); 102 init_buffers(T::info(), T::buffered_state(), Some(tx_buffer), Some(rx_buffer));
103 103
104 Self { 104 Self {
105 rx: BufferedUartRx { phantom: PhantomData }, 105 rx: BufferedUartRx {
106 tx: BufferedUartTx { phantom: PhantomData }, 106 info: T::info(),
107 state: T::buffered_state(),
108 },
109 tx: BufferedUartTx {
110 info: T::info(),
111 state: T::buffered_state(),
112 },
107 } 113 }
108 } 114 }
109 115
110 /// Create a buffered UART instance with flow control. 116 /// Create a buffered UART instance with flow control.
111 pub fn new_with_rtscts( 117 pub fn new_with_rtscts<'d, T: Instance>(
112 _uart: Peri<'d, T>, 118 _uart: Peri<'d, T>,
113 tx: Peri<'d, impl TxPin<T>>, 119 tx: Peri<'d, impl TxPin<T>>,
114 rx: Peri<'d, impl RxPin<T>>, 120 rx: Peri<'d, impl RxPin<T>>,
115 rts: Peri<'d, impl RtsPin<T>>, 121 rts: Peri<'d, impl RtsPin<T>>,
116 cts: Peri<'d, impl CtsPin<T>>, 122 cts: Peri<'d, impl CtsPin<T>>,
117 irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 123 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>,
118 tx_buffer: &'d mut [u8], 124 tx_buffer: &'d mut [u8],
119 rx_buffer: &'d mut [u8], 125 rx_buffer: &'d mut [u8],
120 config: Config, 126 config: Config,
121 ) -> Self { 127 ) -> Self {
122 super::Uart::<'d, T, Async>::init( 128 super::Uart::<'d, Async>::init(
129 T::info(),
123 Some(tx.into()), 130 Some(tx.into()),
124 Some(rx.into()), 131 Some(rx.into()),
125 Some(rts.into()), 132 Some(rts.into()),
126 Some(cts.into()), 133 Some(cts.into()),
127 config, 134 config,
128 ); 135 );
129 init_buffers::<T>(irq, Some(tx_buffer), Some(rx_buffer)); 136 init_buffers(T::info(), T::buffered_state(), Some(tx_buffer), Some(rx_buffer));
130 137
131 Self { 138 Self {
132 rx: BufferedUartRx { phantom: PhantomData }, 139 rx: BufferedUartRx {
133 tx: BufferedUartTx { phantom: PhantomData }, 140 info: T::info(),
141 state: T::buffered_state(),
142 },
143 tx: BufferedUartTx {
144 info: T::info(),
145 state: T::buffered_state(),
146 },
134 } 147 }
135 } 148 }
136 149
@@ -160,68 +173,75 @@ impl<'d, T: Instance> BufferedUart<'d, T> {
160 } 173 }
161 174
162 /// sets baudrate on runtime 175 /// sets baudrate on runtime
163 pub fn set_baudrate(&mut self, baudrate: u32) { 176 pub fn set_baudrate<'d>(&mut self, baudrate: u32) {
164 super::Uart::<'d, T, Async>::set_baudrate_inner(baudrate); 177 super::Uart::<'d, Async>::set_baudrate_inner(self.rx.info, baudrate);
165 } 178 }
166 179
167 /// Split into separate RX and TX handles. 180 /// Split into separate RX and TX handles.
168 pub fn split(self) -> (BufferedUartTx<'d, T>, BufferedUartRx<'d, T>) { 181 pub fn split(self) -> (BufferedUartTx, BufferedUartRx) {
169 (self.tx, self.rx) 182 (self.tx, self.rx)
170 } 183 }
171 184
172 /// Split the Uart into a transmitter and receiver by mutable reference, 185 /// Split the Uart into a transmitter and receiver by mutable reference,
173 /// which is particularly useful when having two tasks correlating to 186 /// which is particularly useful when having two tasks correlating to
174 /// transmitting and receiving. 187 /// transmitting and receiving.
175 pub fn split_ref(&mut self) -> (&mut BufferedUartTx<'d, T>, &mut BufferedUartRx<'d, T>) { 188 pub fn split_ref(&mut self) -> (&mut BufferedUartTx, &mut BufferedUartRx) {
176 (&mut self.tx, &mut self.rx) 189 (&mut self.tx, &mut self.rx)
177 } 190 }
178} 191}
179 192
180impl<'d, T: Instance> BufferedUartRx<'d, T> { 193impl BufferedUartRx {
181 /// Create a new buffered UART RX. 194 /// Create a new buffered UART RX.
182 pub fn new( 195 pub fn new<'d, T: Instance>(
183 _uart: Peri<'d, T>, 196 _uart: Peri<'d, T>,
184 irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 197 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>,
185 rx: Peri<'d, impl RxPin<T>>, 198 rx: Peri<'d, impl RxPin<T>>,
186 rx_buffer: &'d mut [u8], 199 rx_buffer: &'d mut [u8],
187 config: Config, 200 config: Config,
188 ) -> Self { 201 ) -> Self {
189 super::Uart::<'d, T, Async>::init(None, Some(rx.into()), None, None, config); 202 super::Uart::<'d, Async>::init(T::info(), None, Some(rx.into()), None, None, config);
190 init_buffers::<T>(irq, None, Some(rx_buffer)); 203 init_buffers(T::info(), T::buffered_state(), None, Some(rx_buffer));
191 204
192 Self { phantom: PhantomData } 205 Self {
206 info: T::info(),
207 state: T::buffered_state(),
208 }
193 } 209 }
194 210
195 /// Create a new buffered UART RX with flow control. 211 /// Create a new buffered UART RX with flow control.
196 pub fn new_with_rts( 212 pub fn new_with_rts<'d, T: Instance>(
197 _uart: Peri<'d, T>, 213 _uart: Peri<'d, T>,
198 irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 214 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>,
199 rx: Peri<'d, impl RxPin<T>>, 215 rx: Peri<'d, impl RxPin<T>>,
200 rts: Peri<'d, impl RtsPin<T>>, 216 rts: Peri<'d, impl RtsPin<T>>,
201 rx_buffer: &'d mut [u8], 217 rx_buffer: &'d mut [u8],
202 config: Config, 218 config: Config,
203 ) -> Self { 219 ) -> Self {
204 super::Uart::<'d, T, Async>::init(None, Some(rx.into()), Some(rts.into()), None, config); 220 super::Uart::<'d, Async>::init(T::info(), None, Some(rx.into()), Some(rts.into()), None, config);
205 init_buffers::<T>(irq, None, Some(rx_buffer)); 221 init_buffers(T::info(), T::buffered_state(), None, Some(rx_buffer));
206 222
207 Self { phantom: PhantomData } 223 Self {
224 info: T::info(),
225 state: T::buffered_state(),
226 }
208 } 227 }
209 228
210 fn read<'a>(buf: &'a mut [u8]) -> impl Future<Output = Result<usize, Error>> + 'a 229 fn read<'a>(
211 where 230 info: &'static Info,
212 T: 'd, 231 state: &'static State,
213 { 232 buf: &'a mut [u8],
233 ) -> impl Future<Output = Result<usize, Error>> + 'a {
214 poll_fn(move |cx| { 234 poll_fn(move |cx| {
215 if let Poll::Ready(r) = Self::try_read(buf) { 235 if let Poll::Ready(r) = Self::try_read(info, state, buf) {
216 return Poll::Ready(r); 236 return Poll::Ready(r);
217 } 237 }
218 T::buffered_state().rx_waker.register(cx.waker()); 238 state.rx_waker.register(cx.waker());
219 Poll::Pending 239 Poll::Pending
220 }) 240 })
221 } 241 }
222 242
223 fn get_rx_error() -> Option<Error> { 243 fn get_rx_error(state: &State) -> Option<Error> {
224 let errs = T::buffered_state().rx_error.swap(0, Ordering::Relaxed); 244 let errs = state.rx_error.swap(0, Ordering::Relaxed);
225 if errs & RXE_OVERRUN != 0 { 245 if errs & RXE_OVERRUN != 0 {
226 Some(Error::Overrun) 246 Some(Error::Overrun)
227 } else if errs & RXE_BREAK != 0 { 247 } else if errs & RXE_BREAK != 0 {
@@ -235,15 +255,11 @@ impl<'d, T: Instance> BufferedUartRx<'d, T> {
235 } 255 }
236 } 256 }
237 257
238 fn try_read(buf: &mut [u8]) -> Poll<Result<usize, Error>> 258 fn try_read(info: &Info, state: &State, buf: &mut [u8]) -> Poll<Result<usize, Error>> {
239 where
240 T: 'd,
241 {
242 if buf.is_empty() { 259 if buf.is_empty() {
243 return Poll::Ready(Ok(0)); 260 return Poll::Ready(Ok(0));
244 } 261 }
245 262
246 let state = T::buffered_state();
247 let mut rx_reader = unsafe { state.rx_buf.reader() }; 263 let mut rx_reader = unsafe { state.rx_buf.reader() };
248 let n = rx_reader.pop(|data| { 264 let n = rx_reader.pop(|data| {
249 let n = data.len().min(buf.len()); 265 let n = data.len().min(buf.len());
@@ -252,7 +268,7 @@ impl<'d, T: Instance> BufferedUartRx<'d, T> {
252 }); 268 });
253 269
254 let result = if n == 0 { 270 let result = if n == 0 {
255 match Self::get_rx_error() { 271 match Self::get_rx_error(state) {
256 None => return Poll::Pending, 272 None => return Poll::Pending,
257 Some(e) => Err(e), 273 Some(e) => Err(e),
258 } 274 }
@@ -262,8 +278,7 @@ impl<'d, T: Instance> BufferedUartRx<'d, T> {
262 278
263 // (Re-)Enable the interrupt to receive more data in case it was 279 // (Re-)Enable the interrupt to receive more data in case it was
264 // disabled because the buffer was full or errors were detected. 280 // disabled because the buffer was full or errors were detected.
265 let regs = T::regs(); 281 info.regs.uartimsc().write_set(|w| {
266 regs.uartimsc().write_set(|w| {
267 w.set_rxim(true); 282 w.set_rxim(true);
268 w.set_rtim(true); 283 w.set_rtim(true);
269 }); 284 });
@@ -274,23 +289,19 @@ impl<'d, T: Instance> BufferedUartRx<'d, T> {
274 /// Read from UART RX buffer blocking execution until done. 289 /// Read from UART RX buffer blocking execution until done.
275 pub fn blocking_read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { 290 pub fn blocking_read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
276 loop { 291 loop {
277 match Self::try_read(buf) { 292 match Self::try_read(self.info, self.state, buf) {
278 Poll::Ready(res) => return res, 293 Poll::Ready(res) => return res,
279 Poll::Pending => continue, 294 Poll::Pending => continue,
280 } 295 }
281 } 296 }
282 } 297 }
283 298
284 fn fill_buf<'a>() -> impl Future<Output = Result<&'a [u8], Error>> 299 fn fill_buf<'a>(state: &'static State) -> impl Future<Output = Result<&'a [u8], Error>> {
285 where
286 T: 'd,
287 {
288 poll_fn(move |cx| { 300 poll_fn(move |cx| {
289 let state = T::buffered_state();
290 let mut rx_reader = unsafe { state.rx_buf.reader() }; 301 let mut rx_reader = unsafe { state.rx_buf.reader() };
291 let (p, n) = rx_reader.pop_buf(); 302 let (p, n) = rx_reader.pop_buf();
292 let result = if n == 0 { 303 let result = if n == 0 {
293 match Self::get_rx_error() { 304 match Self::get_rx_error(state) {
294 None => { 305 None => {
295 state.rx_waker.register(cx.waker()); 306 state.rx_waker.register(cx.waker());
296 return Poll::Pending; 307 return Poll::Pending;
@@ -306,64 +317,70 @@ impl<'d, T: Instance> BufferedUartRx<'d, T> {
306 }) 317 })
307 } 318 }
308 319
309 fn consume(amt: usize) { 320 fn consume(info: &Info, state: &State, amt: usize) {
310 let state = T::buffered_state();
311 let mut rx_reader = unsafe { state.rx_buf.reader() }; 321 let mut rx_reader = unsafe { state.rx_buf.reader() };
312 rx_reader.pop_done(amt); 322 rx_reader.pop_done(amt);
313 323
314 // (Re-)Enable the interrupt to receive more data in case it was 324 // (Re-)Enable the interrupt to receive more data in case it was
315 // disabled because the buffer was full or errors were detected. 325 // disabled because the buffer was full or errors were detected.
316 let regs = T::regs(); 326 info.regs.uartimsc().write_set(|w| {
317 regs.uartimsc().write_set(|w| {
318 w.set_rxim(true); 327 w.set_rxim(true);
319 w.set_rtim(true); 328 w.set_rtim(true);
320 }); 329 });
321 } 330 }
322 331
323 /// we are ready to read if there is data in the buffer 332 /// we are ready to read if there is data in the buffer
324 fn read_ready() -> Result<bool, Error> { 333 fn read_ready(state: &State) -> Result<bool, Error> {
325 let state = T::buffered_state();
326 Ok(!state.rx_buf.is_empty()) 334 Ok(!state.rx_buf.is_empty())
327 } 335 }
328} 336}
329 337
330impl<'d, T: Instance> BufferedUartTx<'d, T> { 338impl BufferedUartTx {
331 /// Create a new buffered UART TX. 339 /// Create a new buffered UART TX.
332 pub fn new( 340 pub fn new<'d, T: Instance>(
333 _uart: Peri<'d, T>, 341 _uart: Peri<'d, T>,
334 irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 342 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>,
335 tx: Peri<'d, impl TxPin<T>>, 343 tx: Peri<'d, impl TxPin<T>>,
336 tx_buffer: &'d mut [u8], 344 tx_buffer: &'d mut [u8],
337 config: Config, 345 config: Config,
338 ) -> Self { 346 ) -> Self {
339 super::Uart::<'d, T, Async>::init(Some(tx.into()), None, None, None, config); 347 super::Uart::<'d, Async>::init(T::info(), Some(tx.into()), None, None, None, config);
340 init_buffers::<T>(irq, Some(tx_buffer), None); 348 init_buffers(T::info(), T::buffered_state(), Some(tx_buffer), None);
341 349
342 Self { phantom: PhantomData } 350 Self {
351 info: T::info(),
352 state: T::buffered_state(),
353 }
343 } 354 }
344 355
345 /// Create a new buffered UART TX with flow control. 356 /// Create a new buffered UART TX with flow control.
346 pub fn new_with_cts( 357 pub fn new_with_cts<'d, T: Instance>(
347 _uart: Peri<'d, T>, 358 _uart: Peri<'d, T>,
348 irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>, 359 _irq: impl Binding<T::Interrupt, BufferedInterruptHandler<T>>,
349 tx: Peri<'d, impl TxPin<T>>, 360 tx: Peri<'d, impl TxPin<T>>,
350 cts: Peri<'d, impl CtsPin<T>>, 361 cts: Peri<'d, impl CtsPin<T>>,
351 tx_buffer: &'d mut [u8], 362 tx_buffer: &'d mut [u8],
352 config: Config, 363 config: Config,
353 ) -> Self { 364 ) -> Self {
354 super::Uart::<'d, T, Async>::init(Some(tx.into()), None, None, Some(cts.into()), config); 365 super::Uart::<'d, Async>::init(T::info(), Some(tx.into()), None, None, Some(cts.into()), config);
355 init_buffers::<T>(irq, Some(tx_buffer), None); 366 init_buffers(T::info(), T::buffered_state(), Some(tx_buffer), None);
356 367
357 Self { phantom: PhantomData } 368 Self {
369 info: T::info(),
370 state: T::buffered_state(),
371 }
358 } 372 }
359 373
360 fn write(buf: &[u8]) -> impl Future<Output = Result<usize, Error>> + '_ { 374 fn write<'d>(
375 info: &'static Info,
376 state: &'static State,
377 buf: &'d [u8],
378 ) -> impl Future<Output = Result<usize, Error>> + 'd {
361 poll_fn(move |cx| { 379 poll_fn(move |cx| {
362 if buf.is_empty() { 380 if buf.is_empty() {
363 return Poll::Ready(Ok(0)); 381 return Poll::Ready(Ok(0));
364 } 382 }
365 383
366 let state = T::buffered_state();
367 let mut tx_writer = unsafe { state.tx_buf.writer() }; 384 let mut tx_writer = unsafe { state.tx_buf.writer() };
368 let n = tx_writer.push(|data| { 385 let n = tx_writer.push(|data| {
369 let n = data.len().min(buf.len()); 386 let n = data.len().min(buf.len());
@@ -379,14 +396,13 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
379 // FIFO and the number of bytes drops below a threshold. When the 396 // FIFO and the number of bytes drops below a threshold. When the
380 // FIFO was empty we have to manually pend the interrupt to shovel 397 // FIFO was empty we have to manually pend the interrupt to shovel
381 // TX data from the buffer into the FIFO. 398 // TX data from the buffer into the FIFO.
382 T::Interrupt::pend(); 399 info.interrupt.pend();
383 Poll::Ready(Ok(n)) 400 Poll::Ready(Ok(n))
384 }) 401 })
385 } 402 }
386 403
387 fn flush() -> impl Future<Output = Result<(), Error>> { 404 fn flush(state: &'static State) -> impl Future<Output = Result<(), Error>> {
388 poll_fn(move |cx| { 405 poll_fn(move |cx| {
389 let state = T::buffered_state();
390 if !state.tx_buf.is_empty() { 406 if !state.tx_buf.is_empty() {
391 state.tx_waker.register(cx.waker()); 407 state.tx_waker.register(cx.waker());
392 return Poll::Pending; 408 return Poll::Pending;
@@ -403,8 +419,7 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
403 } 419 }
404 420
405 loop { 421 loop {
406 let state = T::buffered_state(); 422 let mut tx_writer = unsafe { self.state.tx_buf.writer() };
407 let mut tx_writer = unsafe { state.tx_buf.writer() };
408 let n = tx_writer.push(|data| { 423 let n = tx_writer.push(|data| {
409 let n = data.len().min(buf.len()); 424 let n = data.len().min(buf.len());
410 data[..n].copy_from_slice(&buf[..n]); 425 data[..n].copy_from_slice(&buf[..n]);
@@ -416,7 +431,7 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
416 // FIFO and the number of bytes drops below a threshold. When the 431 // FIFO and the number of bytes drops below a threshold. When the
417 // FIFO was empty we have to manually pend the interrupt to shovel 432 // FIFO was empty we have to manually pend the interrupt to shovel
418 // TX data from the buffer into the FIFO. 433 // TX data from the buffer into the FIFO.
419 T::Interrupt::pend(); 434 self.info.interrupt.pend();
420 return Ok(n); 435 return Ok(n);
421 } 436 }
422 } 437 }
@@ -425,8 +440,7 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
425 /// Flush UART TX blocking execution until done. 440 /// Flush UART TX blocking execution until done.
426 pub fn blocking_flush(&mut self) -> Result<(), Error> { 441 pub fn blocking_flush(&mut self) -> Result<(), Error> {
427 loop { 442 loop {
428 let state = T::buffered_state(); 443 if self.state.tx_buf.is_empty() {
429 if state.tx_buf.is_empty() {
430 return Ok(()); 444 return Ok(());
431 } 445 }
432 } 446 }
@@ -434,7 +448,7 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
434 448
435 /// Check if UART is busy. 449 /// Check if UART is busy.
436 pub fn busy(&self) -> bool { 450 pub fn busy(&self) -> bool {
437 T::regs().uartfr().read().busy() 451 self.info.regs.uartfr().read().busy()
438 } 452 }
439 453
440 /// Assert a break condition after waiting for the transmit buffers to empty, 454 /// Assert a break condition after waiting for the transmit buffers to empty,
@@ -445,7 +459,7 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
445 /// This method may block for a long amount of time since it has to wait 459 /// This method may block for a long amount of time since it has to wait
446 /// for the transmit fifo to empty, which may take a while on slow links. 460 /// for the transmit fifo to empty, which may take a while on slow links.
447 pub async fn send_break(&mut self, bits: u32) { 461 pub async fn send_break(&mut self, bits: u32) {
448 let regs = T::regs(); 462 let regs = self.info.regs;
449 let bits = bits.max({ 463 let bits = bits.max({
450 let lcr = regs.uartlcr_h().read(); 464 let lcr = regs.uartlcr_h().read();
451 let width = lcr.wlen() as u32 + 5; 465 let width = lcr.wlen() as u32 + 5;
@@ -458,7 +472,7 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
458 let div_clk = clk_peri_freq() as u64 * 64; 472 let div_clk = clk_peri_freq() as u64 * 64;
459 let wait_usecs = (1_000_000 * bits as u64 * divx64 * 16 + div_clk - 1) / div_clk; 473 let wait_usecs = (1_000_000 * bits as u64 * divx64 * 16 + div_clk - 1) / div_clk;
460 474
461 Self::flush().await.unwrap(); 475 Self::flush(self.state).await.unwrap();
462 while self.busy() {} 476 while self.busy() {}
463 regs.uartlcr_h().write_set(|w| w.set_brk(true)); 477 regs.uartlcr_h().write_set(|w| w.set_brk(true));
464 Timer::after_micros(wait_usecs).await; 478 Timer::after_micros(wait_usecs).await;
@@ -466,28 +480,26 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> {
466 } 480 }
467} 481}
468 482
469impl<'d, T: Instance> Drop for BufferedUartRx<'d, T> { 483impl Drop for BufferedUartRx {
470 fn drop(&mut self) { 484 fn drop(&mut self) {
471 let state = T::buffered_state(); 485 unsafe { self.state.rx_buf.deinit() }
472 unsafe { state.rx_buf.deinit() }
473 486
474 // TX is inactive if the buffer is not available. 487 // TX is inactive if the buffer is not available.
475 // We can now unregister the interrupt handler 488 // We can now unregister the interrupt handler
476 if !state.tx_buf.is_available() { 489 if !self.state.tx_buf.is_available() {
477 T::Interrupt::disable(); 490 self.info.interrupt.disable();
478 } 491 }
479 } 492 }
480} 493}
481 494
482impl<'d, T: Instance> Drop for BufferedUartTx<'d, T> { 495impl Drop for BufferedUartTx {
483 fn drop(&mut self) { 496 fn drop(&mut self) {
484 let state = T::buffered_state(); 497 unsafe { self.state.tx_buf.deinit() }
485 unsafe { state.tx_buf.deinit() }
486 498
487 // RX is inactive if the buffer is not available. 499 // RX is inactive if the buffer is not available.
488 // We can now unregister the interrupt handler 500 // We can now unregister the interrupt handler
489 if !state.rx_buf.is_available() { 501 if !self.state.rx_buf.is_available() {
490 T::Interrupt::disable(); 502 self.info.interrupt.disable();
491 } 503 }
492 } 504 }
493} 505}
@@ -499,7 +511,7 @@ pub struct BufferedInterruptHandler<T: Instance> {
499 511
500impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for BufferedInterruptHandler<T> { 512impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for BufferedInterruptHandler<T> {
501 unsafe fn on_interrupt() { 513 unsafe fn on_interrupt() {
502 let r = T::regs(); 514 let r = T::info().regs;
503 if r.uartdmacr().read().rxdmae() { 515 if r.uartdmacr().read().rxdmae() {
504 return; 516 return;
505 } 517 }
@@ -603,95 +615,95 @@ impl embedded_io::Error for Error {
603 } 615 }
604} 616}
605 617
606impl<'d, T: Instance> embedded_io_async::ErrorType for BufferedUart<'d, T> { 618impl embedded_io_async::ErrorType for BufferedUart {
607 type Error = Error; 619 type Error = Error;
608} 620}
609 621
610impl<'d, T: Instance> embedded_io_async::ErrorType for BufferedUartRx<'d, T> { 622impl embedded_io_async::ErrorType for BufferedUartRx {
611 type Error = Error; 623 type Error = Error;
612} 624}
613 625
614impl<'d, T: Instance> embedded_io_async::ErrorType for BufferedUartTx<'d, T> { 626impl embedded_io_async::ErrorType for BufferedUartTx {
615 type Error = Error; 627 type Error = Error;
616} 628}
617 629
618impl<'d, T: Instance + 'd> embedded_io_async::Read for BufferedUart<'d, T> { 630impl embedded_io_async::Read for BufferedUart {
619 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { 631 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
620 BufferedUartRx::<'d, T>::read(buf).await 632 BufferedUartRx::read(self.rx.info, self.rx.state, buf).await
621 } 633 }
622} 634}
623 635
624impl<'d, T: Instance + 'd> embedded_io_async::Read for BufferedUartRx<'d, T> { 636impl embedded_io_async::Read for BufferedUartRx {
625 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { 637 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
626 Self::read(buf).await 638 Self::read(self.info, self.state, buf).await
627 } 639 }
628} 640}
629 641
630impl<'d, T: Instance + 'd> embedded_io_async::ReadReady for BufferedUart<'d, T> { 642impl embedded_io_async::ReadReady for BufferedUart {
631 fn read_ready(&mut self) -> Result<bool, Self::Error> { 643 fn read_ready(&mut self) -> Result<bool, Self::Error> {
632 BufferedUartRx::<'d, T>::read_ready() 644 BufferedUartRx::read_ready(self.rx.state)
633 } 645 }
634} 646}
635 647
636impl<'d, T: Instance + 'd> embedded_io_async::ReadReady for BufferedUartRx<'d, T> { 648impl embedded_io_async::ReadReady for BufferedUartRx {
637 fn read_ready(&mut self) -> Result<bool, Self::Error> { 649 fn read_ready(&mut self) -> Result<bool, Self::Error> {
638 Self::read_ready() 650 Self::read_ready(self.state)
639 } 651 }
640} 652}
641 653
642impl<'d, T: Instance + 'd> embedded_io_async::BufRead for BufferedUart<'d, T> { 654impl embedded_io_async::BufRead for BufferedUart {
643 async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { 655 async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
644 BufferedUartRx::<'d, T>::fill_buf().await 656 BufferedUartRx::fill_buf(self.rx.state).await
645 } 657 }
646 658
647 fn consume(&mut self, amt: usize) { 659 fn consume(&mut self, amt: usize) {
648 BufferedUartRx::<'d, T>::consume(amt) 660 BufferedUartRx::consume(self.rx.info, self.rx.state, amt)
649 } 661 }
650} 662}
651 663
652impl<'d, T: Instance + 'd> embedded_io_async::BufRead for BufferedUartRx<'d, T> { 664impl embedded_io_async::BufRead for BufferedUartRx {
653 async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { 665 async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
654 Self::fill_buf().await 666 Self::fill_buf(self.state).await
655 } 667 }
656 668
657 fn consume(&mut self, amt: usize) { 669 fn consume(&mut self, amt: usize) {
658 Self::consume(amt) 670 Self::consume(self.info, self.state, amt)
659 } 671 }
660} 672}
661 673
662impl<'d, T: Instance + 'd> embedded_io_async::Write for BufferedUart<'d, T> { 674impl embedded_io_async::Write for BufferedUart {
663 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { 675 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
664 BufferedUartTx::<'d, T>::write(buf).await 676 BufferedUartTx::write(self.tx.info, self.tx.state, buf).await
665 } 677 }
666 678
667 async fn flush(&mut self) -> Result<(), Self::Error> { 679 async fn flush(&mut self) -> Result<(), Self::Error> {
668 BufferedUartTx::<'d, T>::flush().await 680 BufferedUartTx::flush(self.tx.state).await
669 } 681 }
670} 682}
671 683
672impl<'d, T: Instance + 'd> embedded_io_async::Write for BufferedUartTx<'d, T> { 684impl embedded_io_async::Write for BufferedUartTx {
673 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { 685 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
674 Self::write(buf).await 686 Self::write(self.info, self.state, buf).await
675 } 687 }
676 688
677 async fn flush(&mut self) -> Result<(), Self::Error> { 689 async fn flush(&mut self) -> Result<(), Self::Error> {
678 Self::flush().await 690 Self::flush(self.state).await
679 } 691 }
680} 692}
681 693
682impl<'d, T: Instance + 'd> embedded_io::Read for BufferedUart<'d, T> { 694impl embedded_io::Read for BufferedUart {
683 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { 695 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
684 self.rx.blocking_read(buf) 696 self.rx.blocking_read(buf)
685 } 697 }
686} 698}
687 699
688impl<'d, T: Instance + 'd> embedded_io::Read for BufferedUartRx<'d, T> { 700impl embedded_io::Read for BufferedUartRx {
689 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { 701 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
690 self.blocking_read(buf) 702 self.blocking_read(buf)
691 } 703 }
692} 704}
693 705
694impl<'d, T: Instance + 'd> embedded_io::Write for BufferedUart<'d, T> { 706impl embedded_io::Write for BufferedUart {
695 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { 707 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
696 self.tx.blocking_write(buf) 708 self.tx.blocking_write(buf)
697 } 709 }
@@ -701,7 +713,7 @@ impl<'d, T: Instance + 'd> embedded_io::Write for BufferedUart<'d, T> {
701 } 713 }
702} 714}
703 715
704impl<'d, T: Instance + 'd> embedded_io::Write for BufferedUartTx<'d, T> { 716impl embedded_io::Write for BufferedUartTx {
705 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { 717 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
706 self.blocking_write(buf) 718 self.blocking_write(buf)
707 } 719 }
@@ -711,11 +723,11 @@ impl<'d, T: Instance + 'd> embedded_io::Write for BufferedUartTx<'d, T> {
711 } 723 }
712} 724}
713 725
714impl<'d, T: Instance> embedded_hal_02::serial::Read<u8> for BufferedUartRx<'d, T> { 726impl embedded_hal_02::serial::Read<u8> for BufferedUartRx {
715 type Error = Error; 727 type Error = Error;
716 728
717 fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { 729 fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
718 let r = T::regs(); 730 let r = self.info.regs;
719 if r.uartfr().read().rxfe() { 731 if r.uartfr().read().rxfe() {
720 return Err(nb::Error::WouldBlock); 732 return Err(nb::Error::WouldBlock);
721 } 733 }
@@ -736,7 +748,7 @@ impl<'d, T: Instance> embedded_hal_02::serial::Read<u8> for BufferedUartRx<'d, T
736 } 748 }
737} 749}
738 750
739impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUartTx<'d, T> { 751impl embedded_hal_02::blocking::serial::Write<u8> for BufferedUartTx {
740 type Error = Error; 752 type Error = Error;
741 753
742 fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> { 754 fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> {
@@ -755,7 +767,7 @@ impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for BufferedU
755 } 767 }
756} 768}
757 769
758impl<'d, T: Instance> embedded_hal_02::serial::Read<u8> for BufferedUart<'d, T> { 770impl embedded_hal_02::serial::Read<u8> for BufferedUart {
759 type Error = Error; 771 type Error = Error;
760 772
761 fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { 773 fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
@@ -763,7 +775,7 @@ impl<'d, T: Instance> embedded_hal_02::serial::Read<u8> for BufferedUart<'d, T>
763 } 775 }
764} 776}
765 777
766impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUart<'d, T> { 778impl embedded_hal_02::blocking::serial::Write<u8> for BufferedUart {
767 type Error = Error; 779 type Error = Error;
768 780
769 fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> { 781 fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> {
@@ -782,25 +794,25 @@ impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for BufferedU
782 } 794 }
783} 795}
784 796
785impl<'d, T: Instance> embedded_hal_nb::serial::ErrorType for BufferedUartRx<'d, T> { 797impl embedded_hal_nb::serial::ErrorType for BufferedUartRx {
786 type Error = Error; 798 type Error = Error;
787} 799}
788 800
789impl<'d, T: Instance> embedded_hal_nb::serial::ErrorType for BufferedUartTx<'d, T> { 801impl embedded_hal_nb::serial::ErrorType for BufferedUartTx {
790 type Error = Error; 802 type Error = Error;
791} 803}
792 804
793impl<'d, T: Instance> embedded_hal_nb::serial::ErrorType for BufferedUart<'d, T> { 805impl embedded_hal_nb::serial::ErrorType for BufferedUart {
794 type Error = Error; 806 type Error = Error;
795} 807}
796 808
797impl<'d, T: Instance> embedded_hal_nb::serial::Read for BufferedUartRx<'d, T> { 809impl embedded_hal_nb::serial::Read for BufferedUartRx {
798 fn read(&mut self) -> nb::Result<u8, Self::Error> { 810 fn read(&mut self) -> nb::Result<u8, Self::Error> {
799 embedded_hal_02::serial::Read::read(self) 811 embedded_hal_02::serial::Read::read(self)
800 } 812 }
801} 813}
802 814
803impl<'d, T: Instance> embedded_hal_nb::serial::Write for BufferedUartTx<'d, T> { 815impl embedded_hal_nb::serial::Write for BufferedUartTx {
804 fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> { 816 fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
805 self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other) 817 self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other)
806 } 818 }
@@ -810,13 +822,13 @@ impl<'d, T: Instance> embedded_hal_nb::serial::Write for BufferedUartTx<'d, T> {
810 } 822 }
811} 823}
812 824
813impl<'d, T: Instance> embedded_hal_nb::serial::Read for BufferedUart<'d, T> { 825impl embedded_hal_nb::serial::Read for BufferedUart {
814 fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { 826 fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
815 embedded_hal_02::serial::Read::read(&mut self.rx) 827 embedded_hal_02::serial::Read::read(&mut self.rx)
816 } 828 }
817} 829}
818 830
819impl<'d, T: Instance> embedded_hal_nb::serial::Write for BufferedUart<'d, T> { 831impl embedded_hal_nb::serial::Write for BufferedUart {
820 fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> { 832 fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
821 self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other) 833 self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other)
822 } 834 }