@@ -19,7 +19,7 @@ use crate::pac::{uarte0_ns as uarte0, UARTE0_NS as UARTE0, UARTE1_NS as UARTE1};
19
19
#[ cfg( not( feature = "9160" ) ) ]
20
20
use crate :: pac:: { uarte0, UARTE0 } ;
21
21
22
- use crate :: gpio:: { Floating , Input , Output , Pin , PushPull } ;
22
+ use crate :: gpio:: { Floating , Input , Output , Pin , Port , PushPull } ;
23
23
use crate :: prelude:: * ;
24
24
use crate :: slice_in_ram_or;
25
25
use crate :: target_constants:: EASY_DMA_SIZE ;
@@ -36,10 +36,7 @@ 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 > {
40
- uarte : T ,
41
- pins : Pins ,
42
- }
39
+ pub struct Uarte < T > ( T ) ;
43
40
44
41
impl < T > Uarte < T >
45
42
where
96
93
// Configure frequency.
97
94
uarte. baudrate . write ( |w| w. baudrate ( ) . variant ( baudrate) ) ;
98
95
99
- Uarte { uarte, pins }
96
+ Uarte ( uarte)
100
97
}
101
98
102
99
/// Write via UARTE.
@@ -119,19 +116,19 @@ where
119
116
compiler_fence ( SeqCst ) ;
120
117
121
118
// Reset the events.
122
- self . uarte . events_endtx . reset ( ) ;
123
- self . uarte . events_txstopped . reset ( ) ;
119
+ self . 0 . events_endtx . reset ( ) ;
120
+ self . 0 . events_txstopped . reset ( ) ;
124
121
125
122
// Set up the DMA write.
126
- self . uarte . txd . ptr . write ( |w|
123
+ self . 0 . txd . ptr . write ( |w|
127
124
// We're giving the register a pointer to the stack. Since we're
128
125
// waiting for the UARTE transaction to end before this stack pointer
129
126
// becomes invalid, there's nothing wrong here.
130
127
//
131
128
// The PTR field is a full 32 bits wide and accepts the full range
132
129
// of values.
133
130
unsafe { w. ptr ( ) . bits ( tx_buffer. as_ptr ( ) as u32 ) } ) ;
134
- self . uarte . txd . maxcnt . write ( |w|
131
+ self . 0 . txd . maxcnt . write ( |w|
135
132
// We're giving it the length of the buffer, so no danger of
136
133
// accessing invalid memory. We have verified that the length of the
137
134
// buffer fits in an `u8`, so the cast to `u8` is also fine.
@@ -141,16 +138,16 @@ where
141
138
unsafe { w. maxcnt ( ) . bits ( tx_buffer. len ( ) as _ ) } ) ;
142
139
143
140
// Start UARTE Transmit transaction.
144
- self . uarte . tasks_starttx . write ( |w|
141
+ self . 0 . tasks_starttx . write ( |w|
145
142
// `1` is a valid value to write to task registers.
146
143
unsafe { w. bits ( 1 ) } ) ;
147
144
148
145
// Wait for transmission to end.
149
146
let mut endtx;
150
147
let mut txstopped;
151
148
loop {
152
- endtx = self . uarte . events_endtx . read ( ) . bits ( ) != 0 ;
153
- txstopped = self . uarte . events_txstopped . read ( ) . bits ( ) != 0 ;
149
+ endtx = self . 0 . events_endtx . read ( ) . bits ( ) != 0 ;
150
+ txstopped = self . 0 . events_txstopped . read ( ) . bits ( ) != 0 ;
154
151
if endtx || txstopped {
155
152
break ;
156
153
}
@@ -167,7 +164,7 @@ where
167
164
168
165
// Lower power consumption by disabling the transmitter once we're
169
166
// finished.
170
- self . uarte . tasks_stoptx . write ( |w|
167
+ self . 0 . tasks_stoptx . write ( |w|
171
168
// `1` is a valid value to write to task registers.
172
169
unsafe { w. bits ( 1 ) } ) ;
173
170
@@ -184,11 +181,11 @@ where
184
181
self . start_read ( rx_buffer) ?;
185
182
186
183
// Wait for transmission to end.
187
- while self . uarte . events_endrx . read ( ) . bits ( ) == 0 { }
184
+ while self . 0 . events_endrx . read ( ) . bits ( ) == 0 { }
188
185
189
186
self . finalize_read ( ) ;
190
187
191
- if self . uarte . rxd . amount . read ( ) . bits ( ) != rx_buffer. len ( ) as u32 {
188
+ if self . 0 . rxd . amount . read ( ) . bits ( ) != rx_buffer. len ( ) as u32 {
192
189
return Err ( Error :: Receive ) ;
193
190
}
194
191
@@ -229,7 +226,7 @@ where
229
226
let mut timeout_occured = false ;
230
227
231
228
loop {
232
- event_complete |= self . uarte . events_endrx . read ( ) . bits ( ) != 0 ;
229
+ event_complete |= self . 0 . events_endrx . read ( ) . bits ( ) != 0 ;
233
230
timeout_occured |= timer. wait ( ) . is_ok ( ) ;
234
231
if event_complete || timeout_occured {
235
232
break ;
@@ -244,7 +241,7 @@ where
244
241
// Cleanup, even in the error case.
245
242
self . finalize_read ( ) ;
246
243
247
- let bytes_read = self . uarte . rxd . amount . read ( ) . bits ( ) as usize ;
244
+ let bytes_read = self . 0 . rxd . amount . read ( ) . bits ( ) as usize ;
248
245
249
246
if timeout_occured && !event_complete {
250
247
return Err ( Error :: Timeout ( bytes_read) ) ;
@@ -275,15 +272,15 @@ where
275
272
compiler_fence ( SeqCst ) ;
276
273
277
274
// Set up the DMA read
278
- self . uarte . rxd . ptr . write ( |w|
275
+ self . 0 . rxd . ptr . write ( |w|
279
276
// We're giving the register a pointer to the stack. Since we're
280
277
// waiting for the UARTE transaction to end before this stack pointer
281
278
// becomes invalid, there's nothing wrong here.
282
279
//
283
280
// The PTR field is a full 32 bits wide and accepts the full range
284
281
// of values.
285
282
unsafe { w. ptr ( ) . bits ( rx_buffer. as_ptr ( ) as u32 ) } ) ;
286
- self . uarte . rxd . maxcnt . write ( |w|
283
+ self . 0 . rxd . maxcnt . write ( |w|
287
284
// We're giving it the length of the buffer, so no danger of
288
285
// accessing invalid memory. We have verified that the length of the
289
286
// buffer fits in an `u8`, so the cast to `u8` is also fine.
@@ -293,7 +290,7 @@ where
293
290
unsafe { w. maxcnt ( ) . bits ( rx_buffer. len ( ) as _ ) } ) ;
294
291
295
292
// Start UARTE Receive transaction.
296
- self . uarte . tasks_startrx . write ( |w|
293
+ self . 0 . tasks_startrx . write ( |w|
297
294
// `1` is a valid value to write to task registers.
298
295
unsafe { w. bits ( 1 ) } ) ;
299
296
@@ -303,7 +300,7 @@ where
303
300
/// Finalize a UARTE read transaction by clearing the event.
304
301
fn finalize_read ( & mut self ) {
305
302
// Reset the event, otherwise it will always read `1` from now on.
306
- self . uarte . events_endrx . write ( |w| w) ;
303
+ self . 0 . events_endrx . write ( |w| w) ;
307
304
308
305
// Conservative compiler fence to prevent optimizations that do not
309
306
// take in to account actions by DMA. The fence has been placed here,
@@ -314,26 +311,66 @@ where
314
311
/// Stop an unfinished UART read transaction and flush FIFO to DMA buffer.
315
312
fn cancel_read ( & mut self ) {
316
313
// Stop reception.
317
- self . uarte . tasks_stoprx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
314
+ self . 0 . tasks_stoprx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
318
315
319
316
// Wait for the reception to have stopped.
320
- while self . uarte . events_rxto . read ( ) . bits ( ) == 0 { }
317
+ while self . 0 . events_rxto . read ( ) . bits ( ) == 0 { }
321
318
322
319
// Reset the event flag.
323
- self . uarte . events_rxto . write ( |w| w) ;
320
+ self . 0 . events_rxto . write ( |w| w) ;
324
321
325
322
// Ask UART to flush FIFO to DMA buffer.
326
- self . uarte . tasks_flushrx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
323
+ self . 0 . tasks_flushrx . write ( |w| unsafe { w. bits ( 1 ) } ) ;
327
324
328
325
// Wait for the flush to complete.
329
- while self . uarte . events_endrx . read ( ) . bits ( ) == 0 { }
326
+ while self . 0 . events_endrx . read ( ) . bits ( ) == 0 { }
330
327
331
328
// The event flag itself is later reset by `finalize_read`.
332
329
}
333
330
334
331
/// Return the raw interface to the underlying UARTE peripheral.
335
332
pub fn free ( self ) -> ( T , Pins ) {
336
- ( self . uarte , self . pins )
333
+ let rxd = self . 0 . psel . rxd . read ( ) ;
334
+ let txd = self . 0 . psel . txd . read ( ) ;
335
+ let cts = self . 0 . psel . cts . read ( ) ;
336
+ let rts = self . 0 . psel . rts . read ( ) ;
337
+ (
338
+ self . 0 ,
339
+ Pins {
340
+ #[ cfg( any( feature = "52833" , feature = "52840" ) ) ]
341
+ rxd : Pin :: new ( Port :: from_bit ( rxd. port ( ) . bit ( ) ) , rxd. pin ( ) . bits ( ) ) ,
342
+ #[ cfg( not( any( feature = "52833" , feature = "52840" ) ) ) ]
343
+ rxd : Pin :: new ( Port :: Port0 , rxd. pin ( ) . bits ( ) ) ,
344
+ #[ cfg( any( feature = "52833" , feature = "52840" ) ) ]
345
+ txd : Pin :: new ( Port :: from_bit ( txd. port ( ) . bit ( ) ) , txd. pin ( ) . bits ( ) ) ,
346
+ #[ cfg( not( any( feature = "52833" , feature = "52840" ) ) ) ]
347
+ txd : Pin :: new ( Port :: Port0 , txd. pin ( ) . bits ( ) ) ,
348
+ cts : if cts. connect ( ) . bit_is_set ( ) {
349
+ #[ cfg( any( feature = "52833" , feature = "52840" ) ) ]
350
+ {
351
+ Some ( Pin :: new ( Port :: from_bit ( cts. port ( ) . bit ( ) ) , cts. pin ( ) . bits ( ) ) )
352
+ }
353
+ #[ cfg( not( any( feature = "52833" , feature = "52840" ) ) ) ]
354
+ {
355
+ Some ( Pin :: new ( Port :: Port0 , cts. pin ( ) . bits ( ) ) )
356
+ }
357
+ } else {
358
+ None
359
+ } ,
360
+ rts : if rts. connect ( ) . bit_is_set ( ) {
361
+ #[ cfg( any( feature = "52833" , feature = "52840" ) ) ]
362
+ {
363
+ Some ( Pin :: new ( Port :: from_bit ( rts. port ( ) . bit ( ) ) , rts. pin ( ) . bits ( ) ) )
364
+ }
365
+ #[ cfg( not( any( feature = "52833" , feature = "52840" ) ) ) ]
366
+ {
367
+ Some ( Pin :: new ( Port :: Port0 , rts. pin ( ) . bits ( ) ) )
368
+ }
369
+ } else {
370
+ None
371
+ } ,
372
+ } ,
373
+ )
337
374
}
338
375
}
339
376
0 commit comments