Skip to content

Commit b3d9289

Browse files
committed
Interrupt driven UART
1 parent 964dc85 commit b3d9289

File tree

3 files changed

+48
-177
lines changed

3 files changed

+48
-177
lines changed

TODO

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ cores/arduino/syscalls.c
2525
_write
2626
UNUSED() s. syscalls_sam3.c
2727

28-
cores/arduino/UARTClass.cpp
29-
Remove rts_pin=UART0_RTS_PIN;
30-
tx_pin=UART0_TX_PIN;
31-
cts_pin=UART0_CTS_PIN;
32-
rx_pin=UART0_RX_PIN;
33-
Enable interrupt driven communication
28+
cores/arduino/WInterrupts.c
29+
Pin Wake functionality
30+
31+
- Rename RFduino-Functions
32+
- PIN_COUNT... in variant.h/c

cores/arduino/UARTClass.cpp

Lines changed: 41 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ UARTClass::UARTClass( RingBuffer* pRx_buffer, RingBuffer* pTx_buffer )
9292

9393
void UARTClass::begin( const uint32_t dwBaudRate )
9494
{
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 );
9696
}
9797

9898
void UARTClass::begin( const uint32_t dwBaudRate, uint8_t rx_pin, uint8_t tx_pin )
9999
{
100-
transmitting = false;
101100
this->begin( dwBaudRate, rx_pin, tx_pin, 255, 255 );
102101
}
103102

@@ -182,43 +181,22 @@ void UARTClass::begin( const uint32_t dwBaudRate, uint8_t rx_pin, uint8_t tx_pin
182181

183182
UART0_State = UART0_State_BeforeFirstTX;
184183

185-
/*
184+
186185
NRF_UART0->INTENSET |= (UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos )
187186
| (UART_INTENSET_TXDRDY_Enabled << UART_INTENSET_TXDRDY_Pos );
188187

189-
attachInterrupt(UART0_IRQn, UART0_Interrupt);
190-
*/
188+
attachInterrupt(UART0_IRQn, UART0_Interrupt );
189+
191190

192191
NRF_UART0->TASKS_STARTTX = 1;
193192
NRF_UART0->TASKS_STARTRX = 1;
194193
NRF_UART0->EVENTS_RXDRDY = 0;
195194
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-
217195
}
218196

219197
void UARTClass::end( void )
220198
{
221-
if (this->UART0_State == UART0_State_NotStarted)
199+
if (UART0_State == UART0_State_NotStarted)
222200
return;
223201

224202
// Wait for any outstanding data to be sent
@@ -240,7 +218,7 @@ void UARTClass::end( void )
240218
| (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
241219
| (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
242220

243-
this->UART0_State = UART0_State_NotStarted;
221+
UART0_State = UART0_State_NotStarted;
244222

245223
// clear any received data
246224
_rx_buffer->_iHead = _rx_buffer->_iTail ;
@@ -249,11 +227,7 @@ void UARTClass::end( void )
249227

250228
int UARTClass::available( void )
251229
{
252-
return (int)NRF_UART0->EVENTS_RXDRDY;
253-
/* FIXME
254-
*******
255230
return (uint32_t)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE ;
256-
*/
257231
}
258232

259233
int UARTClass::peek( void )
@@ -266,63 +240,47 @@ int UARTClass::peek( void )
266240

267241
int UARTClass::read( void )
268242
{
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-
/*
277243
// if the head isn't ahead of the tail, we don't have any characters
278244
if ( _rx_buffer->_iHead == _rx_buffer->_iTail )
279245
return -1 ;
280246

281247
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail] ;
282248
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
283249
return uc ;
284-
*/
285250
}
286251

287252
void UARTClass::flush( void )
288253
{
289-
// Wait for any outstanding data to be sent
290254
while (transmitting)
291255
;
292256
}
293257

294258
void UARTClass::tx( void )
295259
{
296-
/*
260+
// if something in buffer?
297261
if ( _tx_buffer->_iHead == _tx_buffer->_iTail )
298262
{
263+
// set transmitting state top stop
299264
transmitting = false;
300265
return;
301266
}
302267

268+
// set transmitting state to start
303269
transmitting = true;
304270

305271
uint8_t uc = _tx_buffer->_aucBuffer[_tx_buffer->_iTail] ;
306272
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE ;
307273

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;
310280
}
311281

312282
size_t UARTClass::write( const uint8_t uc_data )
313283
{
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-
/*
326284
// Wait if tx_buffer space is not available
327285
while ((_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE == _tx_buffer->_iTail)
328286
;
@@ -331,35 +289,52 @@ size_t UARTClass::write( const uint8_t uc_data )
331289

332290
if (! transmitting)
333291
tx();
334-
*/
292+
335293
return 1;
336294
}
337295

296+
// IRQ handler
297+
void UART0_Interrupt()
298+
{
299+
Serial.IrqHandler();
300+
}
301+
338302
void UARTClass::IrqHandler( void )
339303
{
340-
/*
341-
if (UART0_TXReady())
304+
if (NRF_UART0->EVENTS_TXDRDY)
342305
{
343-
UART0_TXReset();
306+
// TXReset
307+
NRF_UART0->EVENTS_TXDRDY = 0;
344308
tx();
345309
}
346310

347311
// did we receive data
348-
if (UART0_RXReady())
312+
if (NRF_UART0->EVENTS_RXDRDY)
349313
{
350-
UART0_RXReset();
314+
// RXReset
315+
NRF_UART0->EVENTS_RXDRDY = 0;
351316

352-
uint8_t ch = UART0_RXData();
317+
// Read Data
318+
uint8_t ch = NRF_UART0->RXD;
353319

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);
355324
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+
}
356331

332+
// Store in buffer
357333
_rx_buffer->store_char(ch);
358334

359335
if (serialEvent)
360336
serialEvent();
361337
}
362-
*/
363338
}
364339

365340
int UARTClass::UART0_BaudRate()
@@ -387,108 +362,3 @@ int UARTClass::UART0_BaudRate()
387362
return 0;
388363
}
389364

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-
*/

cores/arduino/UARTClass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ typedef enum
9191

9292
typedef void Uart;
9393

94+
void UART0_Interrupt();
95+
9496
class UARTClass : public HardwareSerial
9597
{
9698
protected:

0 commit comments

Comments
 (0)