@@ -76,7 +76,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
76
76
obj -> uart -> TXD = 0 ;
77
77
78
78
obj -> index = 0 ;
79
-
79
+
80
80
obj -> uart -> PSELRTS = RTS_PIN_NUMBER ;
81
81
obj -> uart -> PSELTXD = tx ; //TX_PIN_NUMBER;
82
82
obj -> uart -> PSELCTS = CTS_PIN_NUMBER ;
@@ -162,14 +162,20 @@ extern "C" {
162
162
#endif
163
163
void UART0_IRQHandler ()
164
164
{
165
- uint32_t irtype = 0 ;
166
-
167
- if ((NRF_UART0 -> INTENSET & 0x80 ) && NRF_UART0 -> EVENTS_TXDRDY ) {
168
- irtype = 1 ;
169
- } else if ((NRF_UART0 -> INTENSET & 0x04 ) && NRF_UART0 -> EVENTS_RXDRDY ) {
170
- irtype = 2 ;
165
+ if ((NRF_UART0 -> INTENSET & UART_INTENSET_TXDRDY_Msk ) && NRF_UART0 -> EVENTS_TXDRDY )
166
+ {
167
+ uart_irq (1 , 0 );
168
+
169
+ /* Explicitly clear TX flag to prevent interrupt from firing
170
+ immediately after returning from ISR. This ensures that the
171
+ last interrupt in a transmission sequence is correcly handled.
172
+ */
173
+ NRF_UART0 -> EVENTS_TXDRDY = 0 ;
174
+ }
175
+ else if ((NRF_UART0 -> INTENSET & UART_INTENSET_RXDRDY_Msk ) && NRF_UART0 -> EVENTS_RXDRDY )
176
+ {
177
+ uart_irq (2 , 0 );
171
178
}
172
- uart_irq (irtype , 0 );
173
179
}
174
180
175
181
#ifdef __cplusplus
@@ -239,11 +245,24 @@ int serial_getc(serial_t *obj)
239
245
240
246
void serial_putc (serial_t * obj , int c )
241
247
{
242
- while (!serial_writable (obj )) {
248
+ /* In interrupt mode, send character immediately. Otherwise, block until
249
+ UART is ready to receive next character before sending.
250
+
251
+ The TXDRDY flag is cleared in interrupt handler to ensure that it is
252
+ cleared even if there are no more characters to send.
253
+ */
254
+ if (NRF_UART0 -> INTENSET & UART_INTENSET_TXDRDY_Msk )
255
+ {
256
+ obj -> uart -> TXD = (uint8_t )c ;
243
257
}
258
+ else
259
+ {
260
+ while (!serial_writable (obj )) {
261
+ }
244
262
245
- obj -> uart -> EVENTS_TXDRDY = 0 ;
246
- obj -> uart -> TXD = (uint8_t )c ;
263
+ obj -> uart -> EVENTS_TXDRDY = 0 ;
264
+ obj -> uart -> TXD = (uint8_t )c ;
265
+ }
247
266
}
248
267
249
268
int serial_readable (serial_t * obj )
0 commit comments