@@ -92,12 +92,11 @@ UARTClass::UARTClass( RingBuffer* pRx_buffer, RingBuffer* pTx_buffer )
92
92
93
93
void UARTClass::begin ( const uint32_t dwBaudRate )
94
94
{
95
- this ->begin ( dwBaudRate, UART0_RX_PIN, UART0_TX_PIN, UART0_RTS_PIN, UART0_CTS_PIN );
95
+ this ->begin ( dwBaudRate, UART0_RX_PIN, UART0_TX_PIN, UART0_RTS_PIN, UART0_CTS_PIN );
96
96
}
97
97
98
98
void UARTClass::begin ( const uint32_t dwBaudRate, uint8_t rx_pin, uint8_t tx_pin )
99
99
{
100
- transmitting = false ;
101
100
this ->begin ( dwBaudRate, rx_pin, tx_pin, 255 , 255 );
102
101
}
103
102
@@ -182,43 +181,22 @@ void UARTClass::begin( const uint32_t dwBaudRate, uint8_t rx_pin, uint8_t tx_pin
182
181
183
182
UART0_State = UART0_State_BeforeFirstTX;
184
183
185
- /*
184
+
186
185
NRF_UART0->INTENSET |= (UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos )
187
186
| (UART_INTENSET_TXDRDY_Enabled << UART_INTENSET_TXDRDY_Pos );
188
187
189
- attachInterrupt(UART0_IRQn, UART0_Interrupt);
190
- */
188
+ attachInterrupt (UART0_IRQn, UART0_Interrupt );
189
+
191
190
192
191
NRF_UART0->TASKS_STARTTX = 1 ;
193
192
NRF_UART0->TASKS_STARTRX = 1 ;
194
193
NRF_UART0->EVENTS_RXDRDY = 0 ;
195
194
NRF_UART0->EVENTS_TXDRDY = 0 ;
196
-
197
- /*
198
- if (! override_uart_limit)
199
- {
200
- if (RFduinoBLE_enabled && dwBaudRate > 9600)
201
- {
202
- const char *error = "BLE + UART > 9600 baud not recommended due to critical BLE timing requirements.\r\n"
203
- "To override, add: override_uart_limit = true; to the top of setup() in your sketch.";
204
-
205
- // attempt to notify user of error condition
206
- const char *p = error;
207
- while (*p)
208
- UART0_TX(*p++);
209
-
210
- // don't continue
211
- while (1)
212
- ;
213
- }
214
- }
215
- */
216
-
217
195
}
218
196
219
197
void UARTClass::end ( void )
220
198
{
221
- if (this -> UART0_State == UART0_State_NotStarted)
199
+ if (UART0_State == UART0_State_NotStarted)
222
200
return ;
223
201
224
202
// Wait for any outstanding data to be sent
@@ -240,7 +218,7 @@ void UARTClass::end( void )
240
218
| (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
241
219
| (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
242
220
243
- this -> UART0_State = UART0_State_NotStarted;
221
+ UART0_State = UART0_State_NotStarted;
244
222
245
223
// clear any received data
246
224
_rx_buffer->_iHead = _rx_buffer->_iTail ;
@@ -249,11 +227,7 @@ void UARTClass::end( void )
249
227
250
228
int UARTClass::available ( void )
251
229
{
252
- return (int )NRF_UART0->EVENTS_RXDRDY ;
253
- /* FIXME
254
- *******
255
230
return (uint32_t )(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail ) % SERIAL_BUFFER_SIZE ;
256
- */
257
231
}
258
232
259
233
int UARTClass::peek ( void )
@@ -266,63 +240,47 @@ int UARTClass::peek( void )
266
240
267
241
int UARTClass::read ( void )
268
242
{
269
- if (NRF_UART0->EVENTS_RXDRDY != 1 )
270
- {
271
- return -1 ;
272
- }
273
- NRF_UART0->EVENTS_RXDRDY = 0 ;
274
- return (unsigned int )NRF_UART0->RXD ;
275
-
276
- /*
277
243
// if the head isn't ahead of the tail, we don't have any characters
278
244
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
279
245
return -1 ;
280
246
281
247
uint8_t uc = _rx_buffer->_aucBuffer [_rx_buffer->_iTail ] ;
282
248
_rx_buffer->_iTail = (unsigned int )(_rx_buffer->_iTail + 1 ) % SERIAL_BUFFER_SIZE ;
283
249
return uc ;
284
- */
285
250
}
286
251
287
252
void UARTClass::flush ( void )
288
253
{
289
- // Wait for any outstanding data to be sent
290
254
while (transmitting)
291
255
;
292
256
}
293
257
294
258
void UARTClass::tx ( void )
295
259
{
296
- /*
260
+ // if something in buffer?
297
261
if ( _tx_buffer->_iHead == _tx_buffer->_iTail )
298
262
{
263
+ // set transmitting state top stop
299
264
transmitting = false ;
300
265
return ;
301
266
}
302
267
268
+ // set transmitting state to start
303
269
transmitting = true ;
304
270
305
271
uint8_t uc = _tx_buffer->_aucBuffer [_tx_buffer->_iTail ] ;
306
272
_tx_buffer->_iTail = (unsigned int )(_tx_buffer->_iTail + 1 ) % SERIAL_BUFFER_SIZE ;
307
273
308
- // UART0_TXD(uc);
309
- */
274
+ // tx byte
275
+ NRF_UART0->TXD = uc;
276
+
277
+ // don't change start if not started
278
+ if (UART0_State == UART0_State_BeforeFirstTX)
279
+ UART0_State = UART0_State_AfterFirstTX;
310
280
}
311
281
312
282
size_t UARTClass::write ( const uint8_t uc_data )
313
283
{
314
- // silently discard data if Serial.begin() hasn't been called
315
- if (UART0_State == UART0_State_NotStarted)
316
- return 1 ;
317
-
318
- nrf_gpio_pin_clear (23u );
319
- NRF_UART0->TXD = (uint8_t )uc_data;
320
- while (NRF_UART0->EVENTS_TXDRDY != 1 )
321
- ;
322
- NRF_UART0->EVENTS_TXDRDY = 0 ;
323
- nrf_gpio_pin_set (23u );
324
-
325
- /*
326
284
// Wait if tx_buffer space is not available
327
285
while ((_tx_buffer->_iHead + 1 ) % SERIAL_BUFFER_SIZE == _tx_buffer->_iTail )
328
286
;
@@ -331,35 +289,52 @@ size_t UARTClass::write( const uint8_t uc_data )
331
289
332
290
if (! transmitting)
333
291
tx ();
334
- */
292
+
335
293
return 1 ;
336
294
}
337
295
296
+ // IRQ handler
297
+ void UART0_Interrupt ()
298
+ {
299
+ Serial.IrqHandler ();
300
+ }
301
+
338
302
void UARTClass::IrqHandler ( void )
339
303
{
340
- /*
341
- if (UART0_TXReady())
304
+ if (NRF_UART0->EVENTS_TXDRDY )
342
305
{
343
- UART0_TXReset();
306
+ // TXReset
307
+ NRF_UART0->EVENTS_TXDRDY = 0 ;
344
308
tx ();
345
309
}
346
310
347
311
// did we receive data
348
- if (UART0_RXReady() )
312
+ if (NRF_UART0-> EVENTS_RXDRDY )
349
313
{
350
- UART0_RXReset();
314
+ // RXReset
315
+ NRF_UART0->EVENTS_RXDRDY = 0 ;
351
316
352
- uint8_t ch = UART0_RXData();
317
+ // Read Data
318
+ uint8_t ch = NRF_UART0->RXD ;
353
319
354
- if (UART0_RXErrorReset())
320
+ // Check errors (UART0_RXErrorReset)
321
+ if (NRF_UART0->ERRORSRC & UART_ERRORSRC_OVERRUN_Msk)
322
+ {
323
+ NRF_UART0->ERRORSRC = (UART_ERRORSRC_OVERRUN_Clear << UART_ERRORSRC_OVERRUN_Pos);
355
324
return ;
325
+ }
326
+ else if (NRF_UART0->ERRORSRC & UART_ERRORSRC_FRAMING_Msk)
327
+ {
328
+ NRF_UART0->ERRORSRC = (UART_ERRORSRC_FRAMING_Clear << UART_ERRORSRC_FRAMING_Pos);
329
+ return ;
330
+ }
356
331
332
+ // Store in buffer
357
333
_rx_buffer->store_char (ch);
358
334
359
335
if (serialEvent)
360
336
serialEvent ();
361
337
}
362
- */
363
338
}
364
339
365
340
int UARTClass::UART0_BaudRate ()
@@ -387,108 +362,3 @@ int UARTClass::UART0_BaudRate()
387
362
return 0 ;
388
363
}
389
364
390
-
391
- /*
392
- *
393
- * UART0 core
394
- *
395
-
396
- // UART0_TX is called by syscalls _write (note: printf() must end with '\n')
397
- // we don't want to lockup waiting for TX until after UART0_Start()
398
-
399
- // NRF_UART0->EVENTS_TXDRDY is initialized to 0 at power up
400
- // we cannot test this event until after one byte has been transmitted
401
-
402
-
403
- void UART0_Start( int dwBaudRate, uint8_t rx_pin, uint8_t tx_pin, uint8_t rts_pin, uint8_t cts_pin )
404
- {
405
- }
406
-
407
- void UART0_Stop()
408
- {
409
- }
410
-
411
- void UART0_FlushTX()
412
- {
413
- Serial.flush();
414
- }
415
-
416
- // delegate to serial for syscalls/write and error messages
417
- void UART0_TX( const uint8_t uc_data )
418
- {
419
- Serial.write( uc_data );
420
- }
421
-
422
- void UART0_TXD( const uint8_t uc_data )
423
- {
424
- // tx byte
425
- NRF_UART0->TXD = uc_data;
426
-
427
- // don't change start if not started
428
- if (UART0_State == UART0_State_BeforeFirstTX)
429
- UART0_State = UART0_State_AfterFirstTX;
430
- }
431
-
432
- // UART0_RXReady declared inline in variant.h
433
-
434
- // UART0_RXData declared inline in variant.h
435
-
436
- void UART0_RXReset()
437
- {
438
- NRF_UART0->EVENTS_RXDRDY = 0;
439
- }
440
-
441
- void UART0_TXReset()
442
- {
443
- NRF_UART0->EVENTS_TXDRDY = 0;
444
- }
445
-
446
- int UART0_RXErrorReset()
447
- {
448
- if (NRF_UART0->ERRORSRC & UART_ERRORSRC_OVERRUN_Msk)
449
- {
450
- NRF_UART0->ERRORSRC = (UART_ERRORSRC_OVERRUN_Clear << UART_ERRORSRC_OVERRUN_Pos);
451
- return true;
452
- }
453
- *
454
- else if (NRF_UART0->ERRORSRC & UART_ERRORSRC_PARITY_Msk)
455
- {
456
- NRF_UART0->ERRORSRC = (UART_ERRORSRC_PARITY_Clear << UART_ERRORSRC_PARITY_Pos);
457
- }
458
- *
459
- else if (NRF_UART0->ERRORSRC & UART_ERRORSRC_FRAMING_Msk)
460
- {
461
- NRF_UART0->ERRORSRC = (UART_ERRORSRC_FRAMING_Clear << UART_ERRORSRC_FRAMING_Pos);
462
- return true;
463
- }
464
- *
465
- else if (NRF_UART0->ERRORSRC & UART_ERRORSRC_BREAK_Msk)
466
- {
467
- NRF_UART0->ERRORSRC = (UART_ERRORSRC_BREAK_Clear << UART_ERRORSRC_BREAK_Pos);
468
- }
469
- *
470
-
471
- return false;
472
- }
473
-
474
- uint8_t UART0_RX()
475
- {
476
- uint8_t uc_data;
477
-
478
- // if you call UART0_RX(), you expect UART0 to be Started (no need to check UART0_State)
479
-
480
- do
481
- {
482
- // byte available
483
- while (! UART0_RXReady())
484
- ;
485
-
486
- UART0_RXReset();
487
-
488
- uc_data = UART0_RXData();
489
-
490
- } while (UART0_RXErrorReset());
491
-
492
- return uc_data;
493
- }
494
- */
0 commit comments