@@ -27,7 +27,7 @@ void serialEventRun(void)
27
27
if (serialEvent && Serial.available () ) serialEvent ();
28
28
}
29
29
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)
31
31
{
32
32
nrfUart = _nrfUart;
33
33
IRQn = _IRQn;
@@ -39,7 +39,7 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
39
39
_begun = false ;
40
40
}
41
41
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)
43
43
{
44
44
nrfUart = _nrfUart;
45
45
IRQn = _IRQn;
@@ -55,8 +55,8 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
55
55
56
56
void Uart::setPins (uint8_t pin_rx, uint8_t pin_tx)
57
57
{
58
- uc_pinRX = pin_rx;
59
- uc_pinTX = pin_tx;
58
+ uc_pinRX = g_ADigitalPinMap[ pin_rx] ;
59
+ uc_pinTX = g_ADigitalPinMap[ pin_tx] ;
60
60
}
61
61
62
62
void Uart::begin (unsigned long baudrate)
@@ -69,15 +69,15 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/)
69
69
// skip if already begun
70
70
if ( _begun ) return ;
71
71
72
- nrfUart->PSELTXD = uc_pinTX;
73
- nrfUart->PSELRXD = uc_pinRX;
72
+ nrfUart->PSEL . TXD = uc_pinTX;
73
+ nrfUart->PSEL . RXD = uc_pinRX;
74
74
75
75
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 ;
79
79
} 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 ;
81
81
}
82
82
83
83
uint32_t nrfBaudRate;
@@ -118,15 +118,16 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/)
118
118
119
119
nrfUart->BAUDRATE = nrfBaudRate;
120
120
121
- nrfUart->ENABLE = UART_ENABLE_ENABLE_Enabled ;
121
+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Enabled ;
122
122
123
- nrfUart->EVENTS_RXDRDY = 0x0UL ;
124
- nrfUart->EVENTS_TXDRDY = 0x0UL ;
123
+ nrfUart->TXD . PTR = ( uint32_t )txBuffer ;
124
+ nrfUart->EVENTS_ENDTX = 0x0UL ;
125
125
126
+ nrfUart->RXD .PTR = (uint32_t )&rxRcv;
127
+ nrfUart->RXD .MAXCNT = 1 ;
126
128
nrfUart->TASKS_STARTRX = 0x1UL ;
127
- nrfUart->TASKS_STARTTX = 0x1UL ;
128
129
129
- nrfUart->INTENSET = UART_INTENSET_RXDRDY_Msk ;
130
+ nrfUart->INTENSET = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
130
131
131
132
NVIC_ClearPendingIRQ (IRQn);
132
133
NVIC_SetPriority (IRQn, 3 );
@@ -140,18 +141,18 @@ void Uart::end()
140
141
{
141
142
NVIC_DisableIRQ (IRQn);
142
143
143
- nrfUart->INTENCLR = UART_INTENCLR_RXDRDY_Msk ;
144
+ nrfUart->INTENCLR = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
144
145
145
146
nrfUart->TASKS_STOPRX = 0x1UL ;
146
147
nrfUart->TASKS_STOPTX = 0x1UL ;
147
148
148
- nrfUart->ENABLE = UART_ENABLE_ENABLE_Disabled ;
149
+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Disabled ;
149
150
150
- nrfUart->PSELTXD = 0xFFFFFFFF ;
151
- nrfUart->PSELRXD = 0xFFFFFFFF ;
151
+ nrfUart->PSEL . TXD = 0xFFFFFFFF ;
152
+ nrfUart->PSEL . RXD = 0xFFFFFFFF ;
152
153
153
- nrfUart->PSELRTS = 0xFFFFFFFF ;
154
- nrfUart->PSELCTS = 0xFFFFFFFF ;
154
+ nrfUart->PSEL . RTS = 0xFFFFFFFF ;
155
+ nrfUart->PSEL . CTS = 0xFFFFFFFF ;
155
156
156
157
rxBuffer.clear ();
157
158
@@ -162,15 +163,28 @@ void Uart::end()
162
163
163
164
void Uart::flush ()
164
165
{
165
- rxBuffer.clear ();
166
+ if ( _begun ) {
167
+ xSemaphoreTake (_mutex, portMAX_DELAY);
168
+ xSemaphoreGive (_mutex);
169
+ }
166
170
}
167
171
168
172
void Uart::IrqHandler ()
169
173
{
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 )
171
185
{
172
- nrfUart->EVENTS_RXDRDY = 0x0UL ;
173
- rxBuffer. store_char (nrfUart-> RXD );
186
+ nrfUart->EVENTS_ENDTX = 0x0UL ;
187
+ xSemaphoreGiveFromISR (_mutex, NULL );
174
188
}
175
189
}
176
190
@@ -189,25 +203,38 @@ int Uart::read()
189
203
return rxBuffer.read_char ();
190
204
}
191
205
192
- size_t Uart::write (const uint8_t data)
206
+ size_t Uart::write (uint8_t data)
193
207
{
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 ;
195
214
196
- nrfUart-> TXD = data ;
215
+ size_t sent = 0 ;
197
216
198
- while (!nrfUart->EVENTS_TXDRDY );
217
+ do
218
+ {
219
+ size_t remaining = size - sent;
220
+ size_t txSize = min (remaining, SERIAL_BUFFER_SIZE);
199
221
200
- nrfUart-> EVENTS_TXDRDY = 0x0UL ;
222
+ xSemaphoreTake (_mutex, portMAX_DELAY) ;
201
223
202
- xSemaphoreGive (_mutex );
224
+ memcpy (txBuffer, buffer + sent, txSize );
203
225
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;
205
233
}
206
234
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 );
208
236
209
237
#ifdef HAVE_HWSERIAL2
210
- // TODO UART1 is UARTE only, need update class Uart to work
211
238
Uart Serial2 ( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
212
239
#endif
213
240
@@ -217,4 +244,11 @@ extern "C"
217
244
{
218
245
SERIAL_PORT_HARDWARE.IrqHandler ();
219
246
}
247
+
248
+ #ifdef HAVE_HWSERIAL2
249
+ void UARTE1_IRQHandler ()
250
+ {
251
+ Serial2.IrqHandler ();
252
+ }
253
+ #endif
220
254
}
0 commit comments