@@ -27,7 +27,7 @@ void serialEventRun(void)
2727 if (serialEvent && Serial.available () ) serialEvent ();
2828}
2929
30- Uart::Uart (NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX)
30+ Uart::Uart (NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX)
3131{
3232 nrfUart = _nrfUart;
3333 IRQn = _IRQn;
@@ -39,7 +39,7 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
3939 _begun = false ;
4040}
4141
42- Uart::Uart (NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
42+ Uart::Uart (NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
4343{
4444 nrfUart = _nrfUart;
4545 IRQn = _IRQn;
@@ -55,8 +55,8 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
5555
5656void Uart::setPins (uint8_t pin_rx, uint8_t pin_tx)
5757{
58- uc_pinRX = pin_rx;
59- uc_pinTX = pin_tx;
58+ uc_pinRX = g_ADigitalPinMap[ pin_rx] ;
59+ uc_pinTX = g_ADigitalPinMap[ pin_tx] ;
6060}
6161
6262void Uart::begin (unsigned long baudrate)
@@ -69,23 +69,15 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
6969 // skip if already begun
7070 if ( _begun ) return ;
7171
72- nrfUart->PSELTXD = uc_pinTX;
73- nrfUart->PSELRXD = uc_pinRX;
72+ nrfUart->PSEL . TXD = uc_pinTX;
73+ nrfUart->PSEL . RXD = uc_pinRX;
7474
7575 if (uc_hwFlow == 1 ) {
76- nrfUart->PSELCTS = uc_pinCTS;
77- nrfUart->PSELRTS = uc_pinRTS;
78- if (config==SERIAL_8E1) {
79- nrfUart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
80- } else {
81- nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
82- }
76+ nrfUart->PSEL .CTS = uc_pinCTS;
77+ nrfUart->PSEL .RTS = uc_pinRTS;
78+ nrfUart->CONFIG = config | (UARTE_CONFIG_HWFC_Enabled << UARTE_CONFIG_HWFC_Pos);
8379 } else {
84- if (config==SERIAL_8E1) {
85- nrfUart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
86- } else {
87- nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
88- }
80+ nrfUart->CONFIG = config | (UARTE_CONFIG_HWFC_Disabled << UARTE_CONFIG_HWFC_Pos);
8981 }
9082
9183 uint32_t nrfBaudRate;
@@ -126,15 +118,16 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
126118
127119 nrfUart->BAUDRATE = nrfBaudRate;
128120
129- nrfUart->ENABLE = UART_ENABLE_ENABLE_Enabled ;
121+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Enabled ;
130122
131- nrfUart->EVENTS_RXDRDY = 0x0UL ;
132- nrfUart->EVENTS_TXDRDY = 0x0UL ;
123+ nrfUart->TXD . PTR = ( uint32_t )txBuffer ;
124+ nrfUart->EVENTS_ENDTX = 0x0UL ;
133125
126+ nrfUart->RXD .PTR = (uint32_t )&rxRcv;
127+ nrfUart->RXD .MAXCNT = 1 ;
134128 nrfUart->TASKS_STARTRX = 0x1UL ;
135- nrfUart->TASKS_STARTTX = 0x1UL ;
136129
137- nrfUart->INTENSET = UART_INTENSET_RXDRDY_Msk ;
130+ nrfUart->INTENSET = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
138131
139132 NVIC_ClearPendingIRQ (IRQn);
140133 NVIC_SetPriority (IRQn, 3 );
@@ -148,18 +141,18 @@ void Uart::end()
148141{
149142 NVIC_DisableIRQ (IRQn);
150143
151- nrfUart->INTENCLR = UART_INTENCLR_RXDRDY_Msk ;
144+ nrfUart->INTENCLR = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
152145
153146 nrfUart->TASKS_STOPRX = 0x1UL ;
154147 nrfUart->TASKS_STOPTX = 0x1UL ;
155148
156- nrfUart->ENABLE = UART_ENABLE_ENABLE_Disabled ;
149+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Disabled ;
157150
158- nrfUart->PSELTXD = 0xFFFFFFFF ;
159- nrfUart->PSELRXD = 0xFFFFFFFF ;
151+ nrfUart->PSEL . TXD = 0xFFFFFFFF ;
152+ nrfUart->PSEL . RXD = 0xFFFFFFFF ;
160153
161- nrfUart->PSELRTS = 0xFFFFFFFF ;
162- nrfUart->PSELCTS = 0xFFFFFFFF ;
154+ nrfUart->PSEL . RTS = 0xFFFFFFFF ;
155+ nrfUart->PSEL . CTS = 0xFFFFFFFF ;
163156
164157 rxBuffer.clear ();
165158
@@ -170,15 +163,28 @@ void Uart::end()
170163
171164void Uart::flush ()
172165{
173- rxBuffer.clear ();
166+ if ( _begun ) {
167+ xSemaphoreTake (_mutex, portMAX_DELAY);
168+ xSemaphoreGive (_mutex);
169+ }
174170}
175171
176172void Uart::IrqHandler ()
177173{
178- if (nrfUart->EVENTS_RXDRDY )
174+ if (nrfUart->EVENTS_ENDRX )
175+ {
176+ nrfUart->EVENTS_ENDRX = 0x0UL ;
177+ if (nrfUart->RXD .AMOUNT )
178+ {
179+ rxBuffer.store_char (rxRcv);
180+ }
181+ nrfUart->TASKS_STARTRX = 0x1UL ;
182+ }
183+
184+ if (nrfUart->EVENTS_ENDTX )
179185 {
180- nrfUart->EVENTS_RXDRDY = 0x0UL ;
181- rxBuffer. store_char (nrfUart-> RXD );
186+ nrfUart->EVENTS_ENDTX = 0x0UL ;
187+ xSemaphoreGiveFromISR (_mutex, NULL );
182188 }
183189}
184190
@@ -197,26 +203,39 @@ int Uart::read()
197203 return rxBuffer.read_char ();
198204}
199205
200- size_t Uart::write (const uint8_t data)
206+ size_t Uart::write (uint8_t data)
207+ {
208+ return write (&data, 1 );
209+ }
210+
211+ size_t Uart::write (const uint8_t *buffer, size_t size)
201212{
202- xSemaphoreTake (_mutex, portMAX_DELAY) ;
213+ if (size == 0 ) return 0 ;
203214
204- nrfUart-> TXD = data ;
215+ size_t sent = 0 ;
205216
206- while (!nrfUart->EVENTS_TXDRDY );
217+ do
218+ {
219+ size_t remaining = size - sent;
220+ size_t txSize = min (remaining, SERIAL_BUFFER_SIZE);
221+
222+ xSemaphoreTake (_mutex, portMAX_DELAY);
207223
208- nrfUart-> EVENTS_TXDRDY = 0x0UL ;
224+ memcpy (txBuffer, buffer + sent, txSize) ;
209225
210- xSemaphoreGive (_mutex);
226+ nrfUart->TXD .MAXCNT = txSize;
227+ nrfUart->TASKS_STARTTX = 0x1UL ;
228+ sent += txSize;
211229
212- return 1 ;
230+ } while (sent < size);
231+
232+ return sent;
213233}
214234
215- Uart SERIAL_PORT_HARDWARE ( NRF_UART0 , UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
235+ Uart SERIAL_PORT_HARDWARE ( NRF_UARTE0 , UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
216236
217- #ifdef HAVE_HWSERIAL2
218- // TODO UART1 is UARTE only, need update class Uart to work
219- Uart Serial2 ( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
237+ #ifdef SERIAL_PORT_HARDWARE1
238+ Uart SERIAL_PORT_HARDWARE1 ( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
220239#endif
221240
222241extern " C"
@@ -225,4 +244,11 @@ extern "C"
225244 {
226245 SERIAL_PORT_HARDWARE.IrqHandler ();
227246 }
247+
248+ #ifdef SERIAL_PORT_HARDWARE1
249+ void UARTE1_IRQHandler ()
250+ {
251+ SERIAL_PORT_HARDWARE1.IrqHandler ();
252+ }
253+ #endif
228254}
0 commit comments