@@ -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,15 +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- nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos ) | UART_CONFIG_HWFC_Enabled ;
76+ nrfUart->PSEL . CTS = uc_pinCTS;
77+ nrfUart->PSEL . RTS = uc_pinRTS;
78+ nrfUart->CONFIG = (UARTE_CONFIG_PARITY_Excluded << UARTE_CONFIG_PARITY_Pos ) | UARTE_CONFIG_HWFC_Enabled ;
7979 } else {
80- nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos ) | UART_CONFIG_HWFC_Disabled ;
80+ nrfUart->CONFIG = (UARTE_CONFIG_PARITY_Excluded << UARTE_CONFIG_PARITY_Pos ) | UARTE_CONFIG_HWFC_Disabled ;
8181 }
8282
8383 uint32_t nrfBaudRate;
@@ -118,15 +118,16 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/)
118118
119119 nrfUart->BAUDRATE = nrfBaudRate;
120120
121- nrfUart->ENABLE = UART_ENABLE_ENABLE_Enabled ;
121+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Enabled ;
122122
123- nrfUart->EVENTS_RXDRDY = 0x0UL ;
124- nrfUart->EVENTS_TXDRDY = 0x0UL ;
123+ nrfUart->TXD . PTR = ( uint32_t )txBuffer ;
124+ nrfUart->EVENTS_ENDTX = 0x0UL ;
125125
126+ nrfUart->RXD .PTR = (uint32_t )&rxRcv;
127+ nrfUart->RXD .MAXCNT = 1 ;
126128 nrfUart->TASKS_STARTRX = 0x1UL ;
127- nrfUart->TASKS_STARTTX = 0x1UL ;
128129
129- nrfUart->INTENSET = UART_INTENSET_RXDRDY_Msk ;
130+ nrfUart->INTENSET = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
130131
131132 NVIC_ClearPendingIRQ (IRQn);
132133 NVIC_SetPriority (IRQn, 3 );
@@ -140,18 +141,18 @@ void Uart::end()
140141{
141142 NVIC_DisableIRQ (IRQn);
142143
143- nrfUart->INTENCLR = UART_INTENCLR_RXDRDY_Msk ;
144+ nrfUart->INTENCLR = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
144145
145146 nrfUart->TASKS_STOPRX = 0x1UL ;
146147 nrfUart->TASKS_STOPTX = 0x1UL ;
147148
148- nrfUart->ENABLE = UART_ENABLE_ENABLE_Disabled ;
149+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Disabled ;
149150
150- nrfUart->PSELTXD = 0xFFFFFFFF ;
151- nrfUart->PSELRXD = 0xFFFFFFFF ;
151+ nrfUart->PSEL . TXD = 0xFFFFFFFF ;
152+ nrfUart->PSEL . RXD = 0xFFFFFFFF ;
152153
153- nrfUart->PSELRTS = 0xFFFFFFFF ;
154- nrfUart->PSELCTS = 0xFFFFFFFF ;
154+ nrfUart->PSEL . RTS = 0xFFFFFFFF ;
155+ nrfUart->PSEL . CTS = 0xFFFFFFFF ;
155156
156157 rxBuffer.clear ();
157158
@@ -162,15 +163,28 @@ void Uart::end()
162163
163164void Uart::flush ()
164165{
165- rxBuffer.clear ();
166+ if ( _begun ) {
167+ xSemaphoreTake (_mutex, portMAX_DELAY);
168+ xSemaphoreGive (_mutex);
169+ }
166170}
167171
168172void Uart::IrqHandler ()
169173{
170- 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 )
171185 {
172- nrfUart->EVENTS_RXDRDY = 0x0UL ;
173- rxBuffer. store_char (nrfUart-> RXD );
186+ nrfUart->EVENTS_ENDTX = 0x0UL ;
187+ xSemaphoreGiveFromISR (_mutex, NULL );
174188 }
175189}
176190
@@ -189,25 +203,38 @@ int Uart::read()
189203 return rxBuffer.read_char ();
190204}
191205
192- size_t Uart::write (const uint8_t data)
206+ size_t Uart::write (uint8_t data)
193207{
194- xSemaphoreTake (_mutex, portMAX_DELAY);
208+ return write (&data, 1 );
209+ }
210+
211+ size_t Uart::write (const uint8_t *buffer, size_t size)
212+ {
213+ if (size == 0 ) return 0 ;
195214
196- nrfUart-> TXD = data ;
215+ size_t sent = 0 ;
197216
198- while (!nrfUart->EVENTS_TXDRDY );
217+ do
218+ {
219+ size_t remaining = size - sent;
220+ size_t txSize = min (remaining, SERIAL_BUFFER_SIZE);
199221
200- nrfUart-> EVENTS_TXDRDY = 0x0UL ;
222+ xSemaphoreTake (_mutex, portMAX_DELAY) ;
201223
202- xSemaphoreGive (_mutex );
224+ memcpy (txBuffer, buffer + sent, txSize );
203225
204- return 1 ;
226+ nrfUart->TXD .MAXCNT = txSize;
227+ nrfUart->TASKS_STARTTX = 0x1UL ;
228+ sent += txSize;
229+
230+ } while (sent < size);
231+
232+ return sent;
205233}
206234
207- 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 );
208236
209237#ifdef HAVE_HWSERIAL2
210- // TODO UART1 is UARTE only, need update class Uart to work
211238Uart Serial2 ( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
212239#endif
213240
@@ -217,4 +244,11 @@ extern "C"
217244 {
218245 SERIAL_PORT_HARDWARE.IrqHandler ();
219246 }
247+
248+ #ifdef HAVE_HWSERIAL2
249+ void UARTE1_IRQHandler ()
250+ {
251+ Serial2.IrqHandler ();
252+ }
253+ #endif
220254}
0 commit comments