11//! It doesn't depend on DMA or interrupts, relying instead on continuous polling.
22
33use super :: * ;
4- use crate :: common:: os;
4+ use crate :: common:: os:: * ;
55use embedded_hal_nb as e_nb;
66use embedded_io as e_io;
77
88// TX -------------------------------------------------------------------------
99
10- pub struct UartPollTx < U > {
10+ pub struct UartPollTx < U , T > {
1111 uart : U ,
12- retry_times : u32 ,
12+ timeout : T ,
1313 flush_retry_times : u32 ,
1414}
1515
16- impl < U : UartPeriph > UartPollTx < U > {
17- pub fn new ( uart : U , retry_times : u32 , flush_retry_times : u32 ) -> Self {
16+ impl < U : UartPeriph , T : Timeout > UartPollTx < U , T > {
17+ pub fn new ( uart : U , timeout : T , flush_retry_times : u32 ) -> Self {
1818 Self {
1919 uart,
20- retry_times ,
20+ timeout ,
2121 flush_retry_times,
2222 }
2323 }
2424}
2525
26- impl < U : UartPeriph > e_nb:: serial:: ErrorType for UartPollTx < U > {
26+ impl < U : UartPeriph , T : Timeout > e_nb:: serial:: ErrorType for UartPollTx < U , T > {
2727 type Error = Error ;
2828}
29- impl < U : UartPeriph > e_io:: ErrorType for UartPollTx < U > {
29+ impl < U : UartPeriph , T : Timeout > e_io:: ErrorType for UartPollTx < U , T > {
3030 type Error = Error ;
3131}
3232
3333// NB Write ----
3434
35- impl < U : UartPeriph > e_nb:: serial:: Write < u16 > for UartPollTx < U > {
35+ impl < U : UartPeriph , T : Timeout > e_nb:: serial:: Write < u16 > for UartPollTx < U , T > {
3636 #[ inline]
3737 fn write ( & mut self , word : u16 ) -> nb:: Result < ( ) , Self :: Error > {
3838 self . uart . write ( word)
@@ -49,18 +49,19 @@ impl<U: UartPeriph> e_nb::serial::Write<u16> for UartPollTx<U> {
4949
5050// IO Write ----
5151
52- impl < U : UartPeriph > e_io:: Write for UartPollTx < U > {
52+ impl < U : UartPeriph , T : Timeout > e_io:: Write for UartPollTx < U , T > {
5353 fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
5454 if buf. len ( ) == 0 {
5555 return Err ( Error :: Other ) ;
5656 }
5757
5858 // try first data
5959 let mut rst = Err ( nb:: Error :: WouldBlock ) ;
60- for _ in 0 ..=self . retry_times {
60+ let mut t = self . timeout . start ( ) ;
61+ while !t. timeout ( ) {
6162 rst = self . uart . write ( buf[ 0 ] as u16 ) ;
6263 if let Err ( nb:: Error :: WouldBlock ) = rst {
63- os :: yield_cpu ( ) ;
64+ t . interval ( ) ;
6465 } else {
6566 break ;
6667 }
@@ -86,40 +87,40 @@ impl<U: UartPeriph> e_io::Write for UartPollTx<U> {
8687 if self . uart . is_tx_empty ( ) && self . uart . is_tx_complete ( ) {
8788 return Ok ( ( ) ) ;
8889 }
89- os :: yield_cpu ( ) ;
90+ yield_cpu ( ) ;
9091 }
9192 Err ( Error :: Other )
9293 }
9394}
9495
9596// RX -------------------------------------------------------------------------
9697
97- pub struct UartPollRx < U > {
98+ pub struct UartPollRx < U , T > {
9899 uart : U ,
99- retry_times : u32 ,
100+ timeout : T ,
100101 continue_retry_times : u32 ,
101102}
102103
103- impl < U : UartPeriph > UartPollRx < U > {
104- pub fn new ( uart : U , retry_times : u32 , continue_retry_times : u32 ) -> Self {
104+ impl < U : UartPeriph , T : Timeout > UartPollRx < U , T > {
105+ pub fn new ( uart : U , timeout : T , continue_retry_times : u32 ) -> Self {
105106 Self {
106107 uart,
107- retry_times ,
108+ timeout ,
108109 continue_retry_times,
109110 }
110111 }
111112}
112113
113- impl < U : UartPeriph > e_nb:: serial:: ErrorType for UartPollRx < U > {
114+ impl < U : UartPeriph , T : Timeout > e_nb:: serial:: ErrorType for UartPollRx < U , T > {
114115 type Error = Error ;
115116}
116- impl < U : UartPeriph > e_io:: ErrorType for UartPollRx < U > {
117+ impl < U : UartPeriph , T : Timeout > e_io:: ErrorType for UartPollRx < U , T > {
117118 type Error = Error ;
118119}
119120
120121// NB Read ----
121122
122- impl < U : UartPeriph > e_nb:: serial:: Read < u16 > for UartPollRx < U > {
123+ impl < U : UartPeriph , T : Timeout > e_nb:: serial:: Read < u16 > for UartPollRx < U , T > {
123124 #[ inline]
124125 fn read ( & mut self ) -> nb:: Result < u16 , Self :: Error > {
125126 self . uart . read ( )
@@ -128,18 +129,19 @@ impl<U: UartPeriph> e_nb::serial::Read<u16> for UartPollRx<U> {
128129
129130// IO Read ----
130131
131- impl < U : UartPeriph > e_io:: Read for UartPollRx < U > {
132+ impl < U : UartPeriph , T : Timeout > e_io:: Read for UartPollRx < U , T > {
132133 fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
133134 if buf. len ( ) == 0 {
134135 return Err ( Error :: Other ) ;
135136 }
136137
137138 // try first data
138139 let mut rst = Err ( nb:: Error :: WouldBlock ) ;
139- for _ in 0 ..=self . retry_times {
140+ let mut t = self . timeout . start ( ) ;
141+ while !t. timeout ( ) {
140142 rst = self . uart . read ( ) ;
141143 if let Err ( nb:: Error :: WouldBlock ) = rst {
142- os :: yield_cpu ( ) ;
144+ t . interval ( ) ;
143145 } else {
144146 break ;
145147 }
@@ -165,7 +167,7 @@ impl<U: UartPeriph> e_io::Read for UartPollRx<U> {
165167 return Ok ( n) ;
166168 }
167169 retry += 1 ;
168- os :: yield_cpu ( ) ;
170+ yield_cpu ( ) ;
169171 }
170172 }
171173 }
0 commit comments