@@ -36,7 +36,10 @@ pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
36
36
/// are disabled before using `Uarte`. See product specification:
37
37
/// - nrf52832: Section 15.2
38
38
/// - nrf52840: Section 6.1.2
39
- pub struct Uarte < T > ( T ) ;
39
+ pub struct Uarte < T > {
40
+ uarte : T ,
41
+ pins : Pins ,
42
+ }
40
43
41
44
impl < T > Uarte < T >
42
45
where
93
96
// Configure frequency.
94
97
uarte. baudrate . write ( |w| w. baudrate ( ) . variant ( baudrate) ) ;
95
98
96
- Uarte ( uarte)
99
+ Uarte { uarte, pins }
97
100
}
98
101
99
102
/// Write via UARTE.
@@ -116,19 +119,19 @@ where
116
119
compiler_fence ( SeqCst ) ;
117
120
118
121
// Reset the events.
119
- self . 0 . events_endtx . reset ( ) ;
120
- self . 0 . events_txstopped . reset ( ) ;
122
+ self . uarte . events_endtx . reset ( ) ;
123
+ self . uarte . events_txstopped . reset ( ) ;
121
124
122
125
// Set up the DMA write.
123
- self . 0 . txd . ptr . write ( |w|
126
+ self . uarte . txd . ptr . write ( |w|
124
127
// We're giving the register a pointer to the stack. Since we're
125
128
// waiting for the UARTE transaction to end before this stack pointer
126
129
// becomes invalid, there's nothing wrong here.
127
130
//
128
131
// The PTR field is a full 32 bits wide and accepts the full range
129
132
// of values.
130
133
unsafe { w. ptr ( ) . bits ( tx_buffer. as_ptr ( ) as u32 ) } ) ;
131
- self . 0 . txd . maxcnt . write ( |w|
134
+ self . uarte . txd . maxcnt . write ( |w|
132
135
// We're giving it the length of the buffer, so no danger of
133
136
// accessing invalid memory. We have verified that the length of the
134
137
// buffer fits in an `u8`, so the cast to `u8` is also fine.
@@ -138,16 +141,16 @@ where
138
141
unsafe { w. maxcnt ( ) . bits ( tx_buffer. len ( ) as _ ) } ) ;
139
142
140
143
// Start UARTE Transmit transaction.
141
- self . 0 . tasks_starttx . write ( |w|
144
+ self . uarte . tasks_starttx . write ( |w|
142
145
// `1` is a valid value to write to task registers.
143
146
unsafe { w. bits ( 1 ) } ) ;
144
147
145
148
// Wait for transmission to end.
146
149
let mut endtx;
147
150
let mut txstopped;
148
151
loop {
149
- endtx = self . 0 . events_endtx . read ( ) . bits ( ) != 0 ;
150
- txstopped = self . 0 . events_txstopped . read ( ) . bits ( ) != 0 ;
152
+ endtx = self . uarte . events_endtx . read ( ) . bits ( ) != 0 ;
153
+ txstopped = self . uarte . events_txstopped . read ( ) . bits ( ) != 0 ;
151
154
if endtx || txstopped {
152
155
break ;
153
156
}
@@ -164,7 +167,7 @@ where
164
167
165
168
// Lower power consumption by disabling the transmitter once we're
166
169
// finished.
167
- self . 0 . tasks_stoptx . write ( |w|
170
+ self . uarte . tasks_stoptx . write ( |w|
168
171
// `1` is a valid value to write to task registers.
169
172
unsafe { w. bits ( 1 ) } ) ;
170
173
@@ -181,11 +184,11 @@ where
181
184
self . start_read ( rx_buffer) ?;
182
185
183
186
// Wait for transmission to end.
184
- while self . 0 . events_endrx . read ( ) . bits ( ) == 0 { }
187
+ while self . uarte . events_endrx . read ( ) . bits ( ) == 0 { }
185
188
186
189
self . finalize_read ( ) ;
187
190
188
- if self . 0 . rxd . amount . read ( ) . bits ( ) != rx_buffer. len ( ) as u32 {
191
+ if self . uarte . rxd . amount . read ( ) . bits ( ) != rx_buffer. len ( ) as u32 {
189
192
return Err ( Error :: Receive ) ;
190
193
}
191
194
@@ -226,7 +229,7 @@ where
226
229
let mut timeout_occured = false ;
227
230
228
231
loop {
229
- event_complete |= self . 0 . events_endrx . read ( ) . bits ( ) != 0 ;
232
+ event_complete |= self . uarte . events_endrx . read ( ) . bits ( ) != 0 ;
230
233
timeout_occured |= timer. wait ( ) . is_ok ( ) ;
231
234
if event_complete || timeout_occured {
232
235
break ;
@@ -241,7 +244,7 @@ where
241
244
// Cleanup, even in the error case.
242
245
self . finalize_read ( ) ;
243
246
244
- let bytes_read = self . 0 . rxd . amount . read ( ) . bits ( ) as usize ;
247
+ let bytes_read = self . uarte . rxd . amount . read ( ) . bits ( ) as usize ;
245
248
246
249
if timeout_occured && !event_complete {
247
250
return Err ( Error :: Timeout ( bytes_read) ) ;
@@ -272,15 +275,15 @@ where
272
275
compiler_fence ( SeqCst ) ;
273
276
274
277
// Set up the DMA read
275
- self . 0 . rxd . ptr . write ( |w|
278
+ self . uarte . rxd . ptr . write ( |w|
276
279
// We're giving the register a pointer to the stack. Since we're
277
280
// waiting for the UARTE transaction to end before this stack pointer
278
281
// becomes invalid, there's nothing wrong here.
279
282
//
280
283
// The PTR field is a full 32 bits wide and accepts the full range
281
284
// of values.
282
285
unsafe { w. ptr ( ) . bits ( rx_buffer. as_ptr ( ) as u32 ) } ) ;
283
- self . 0 . rxd . maxcnt . write ( |w|
286
+ self . uarte . rxd . maxcnt . write ( |w|
284
287
// We're giving it the length of the buffer, so no danger of
285
288
// accessing invalid memory. We have verified that the length of the
286
289
// buffer fits in an `u8`, so the cast to `u8` is also fine.
@@ -290,7 +293,7 @@ where
290
293
unsafe { w. maxcnt ( ) . bits ( rx_buffer. len ( ) as _ ) } ) ;
291
294
292
295
// Start UARTE Receive transaction.
293
- self . 0 . tasks_startrx . write ( |w|
296
+ self . uarte . tasks_startrx . write ( |w|
294
297
// `1` is a valid value to write to task registers.
295
298
unsafe { w. bits ( 1 ) } ) ;
296
299
@@ -300,7 +303,7 @@ where
300
303
/// Finalize a UARTE read transaction by clearing the event.
301
304
fn finalize_read ( & mut self ) {
302
305
// Reset the event, otherwise it will always read `1` from now on.
303
- self . 0 . events_endrx . write ( |w| w) ;
306
+ self . uarte . events_endrx . write ( |w| w) ;
304
307
305
308
// Conservative compiler fence to prevent optimizations that do not
306
309
// take in to account actions by DMA. The fence has been placed here,
@@ -311,26 +314,26 @@ where
311
314
/// Stop an unfinished UART read transaction and flush FIFO to DMA buffer.
312
315
fn cancel_read ( & mut self ) {
313
316
// Stop reception.
314
- self . 0 . tasks_stoprx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
317
+ self . uarte . tasks_stoprx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
315
318
316
319
// Wait for the reception to have stopped.
317
- while self . 0 . events_rxto . read ( ) . bits ( ) == 0 { }
320
+ while self . uarte . events_rxto . read ( ) . bits ( ) == 0 { }
318
321
319
322
// Reset the event flag.
320
- self . 0 . events_rxto . write ( |w| w) ;
323
+ self . uarte . events_rxto . write ( |w| w) ;
321
324
322
325
// Ask UART to flush FIFO to DMA buffer.
323
- self . 0 . tasks_flushrx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
326
+ self . uarte . tasks_flushrx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
324
327
325
328
// Wait for the flush to complete.
326
- while self . 0 . events_endrx . read ( ) . bits ( ) == 0 { }
329
+ while self . uarte . events_endrx . read ( ) . bits ( ) == 0 { }
327
330
328
331
// The event flag itself is later reset by `finalize_read`.
329
332
}
330
333
331
334
/// Return the raw interface to the underlying UARTE peripheral.
332
- pub fn free ( self ) -> T {
333
- self . 0
335
+ pub fn free ( self ) -> ( T , Pins ) {
336
+ ( self . uarte , self . pins )
334
337
}
335
338
}
336
339
0 commit comments