1 /*
2 * Copyright (c) 2016-2019 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @brief Driver for Nordic Semiconductor nRF5X UART
9 */
10
11 #include <zephyr/drivers/pinctrl.h>
12 #include <zephyr/drivers/uart.h>
13 #include <zephyr/pm/device.h>
14 #include <zephyr/irq.h>
15 #include <soc.h>
16 #include <hal/nrf_uart.h>
17
18 /*
19 * Extract information from devicetree.
20 *
21 * This driver only supports one instance of this IP block, so the
22 * instance number is always 0.
23 */
24 #define DT_DRV_COMPAT nordic_nrf_uart
25
26 #define PROP(prop) DT_INST_PROP(0, prop)
27 #define HAS_PROP(prop) DT_INST_NODE_HAS_PROP(0, prop)
28
29 #define BAUDRATE PROP(current_speed)
30
31 #define DISABLE_RX PROP(disable_rx)
32 #define HW_FLOW_CONTROL_AVAILABLE PROP(hw_flow_control)
33
34 #define IRQN DT_INST_IRQN(0)
35 #define IRQ_PRIO DT_INST_IRQ(0, priority)
36
37 static NRF_UART_Type *const uart0_addr = (NRF_UART_Type *)DT_INST_REG_ADDR(0);
38
39 struct uart_nrfx_config {
40 const struct pinctrl_dev_config *pcfg;
41 };
42
43 /* Device data structure */
44 struct uart_nrfx_data {
45 struct uart_config uart_config;
46 };
47
48 #ifdef CONFIG_UART_0_ASYNC
49 static struct {
50 uart_callback_t callback;
51 void *user_data;
52
53 uint8_t *rx_buffer;
54 uint8_t *rx_secondary_buffer;
55 size_t rx_buffer_length;
56 size_t rx_secondary_buffer_length;
57 volatile size_t rx_counter;
58 volatile size_t rx_offset;
59 int32_t rx_timeout;
60 struct k_timer rx_timeout_timer;
61 bool rx_enabled;
62
63 bool tx_abort;
64 const uint8_t *volatile tx_buffer;
65 /* note: this is aliased with atomic_t in uart_nrfx_poll_out() */
66 unsigned long tx_buffer_length;
67 volatile size_t tx_counter;
68 #if HW_FLOW_CONTROL_AVAILABLE
69 int32_t tx_timeout;
70 struct k_timer tx_timeout_timer;
71 #endif
72 } uart0_cb;
73 #endif /* CONFIG_UART_0_ASYNC */
74
75 #ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
76
77 static uart_irq_callback_user_data_t irq_callback; /**< Callback function pointer */
78 static void *irq_cb_data; /**< Callback function arg */
79
80 /* Variable used to override the state of the TXDRDY event in the initial state
81 * of the driver. This event is not set by the hardware until a first byte is
82 * sent, and we want to use it as an indication if the transmitter is ready
83 * to accept a new byte.
84 */
85 static volatile uint8_t uart_sw_event_txdrdy;
86 static volatile bool disable_tx_irq;
87
88 #endif /* CONFIG_UART_0_INTERRUPT_DRIVEN */
89
event_txdrdy_check(void)90 static bool event_txdrdy_check(void)
91 {
92 return (nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_TXDRDY)
93 #ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
94 || uart_sw_event_txdrdy
95 #endif
96 );
97 }
98
event_txdrdy_clear(void)99 static void event_txdrdy_clear(void)
100 {
101 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_TXDRDY);
102 #ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
103 uart_sw_event_txdrdy = 0U;
104 #endif
105 }
106
107
108 /**
109 * @brief Set the baud rate
110 *
111 * This routine set the given baud rate for the UART.
112 *
113 * @param dev UART device struct
114 * @param baudrate Baud rate
115 *
116 * @retval 0 on success.
117 * @retval -EINVAL for invalid baudrate.
118 */
119
baudrate_set(const struct device * dev,uint32_t baudrate)120 static int baudrate_set(const struct device *dev, uint32_t baudrate)
121 {
122 nrf_uart_baudrate_t nrf_baudrate; /* calculated baudrate divisor */
123
124 switch (baudrate) {
125 case 300:
126 /* value not supported by Nordic HAL */
127 nrf_baudrate = 0x00014000;
128 break;
129 case 600:
130 /* value not supported by Nordic HAL */
131 nrf_baudrate = 0x00027000;
132 break;
133 case 1200:
134 nrf_baudrate = NRF_UART_BAUDRATE_1200;
135 break;
136 case 2400:
137 nrf_baudrate = NRF_UART_BAUDRATE_2400;
138 break;
139 case 4800:
140 nrf_baudrate = NRF_UART_BAUDRATE_4800;
141 break;
142 case 9600:
143 nrf_baudrate = NRF_UART_BAUDRATE_9600;
144 break;
145 case 14400:
146 nrf_baudrate = NRF_UART_BAUDRATE_14400;
147 break;
148 case 19200:
149 nrf_baudrate = NRF_UART_BAUDRATE_19200;
150 break;
151 case 28800:
152 nrf_baudrate = NRF_UART_BAUDRATE_28800;
153 break;
154 #if defined(UART_BAUDRATE_BAUDRATE_Baud31250)
155 case 31250:
156 nrf_baudrate = NRF_UART_BAUDRATE_31250;
157 break;
158 #endif
159 case 38400:
160 nrf_baudrate = NRF_UART_BAUDRATE_38400;
161 break;
162 #if defined(UART_BAUDRATE_BAUDRATE_Baud56000)
163 case 56000:
164 nrf_baudrate = NRF_UART_BAUDRATE_56000;
165 break;
166 #endif
167 case 57600:
168 nrf_baudrate = NRF_UART_BAUDRATE_57600;
169 break;
170 case 76800:
171 nrf_baudrate = NRF_UART_BAUDRATE_76800;
172 break;
173 case 115200:
174 nrf_baudrate = NRF_UART_BAUDRATE_115200;
175 break;
176 case 230400:
177 nrf_baudrate = NRF_UART_BAUDRATE_230400;
178 break;
179 case 250000:
180 nrf_baudrate = NRF_UART_BAUDRATE_250000;
181 break;
182 case 460800:
183 nrf_baudrate = NRF_UART_BAUDRATE_460800;
184 break;
185 case 921600:
186 nrf_baudrate = NRF_UART_BAUDRATE_921600;
187 break;
188 case 1000000:
189 nrf_baudrate = NRF_UART_BAUDRATE_1000000;
190 break;
191 default:
192 return -EINVAL;
193 }
194
195 nrf_uart_baudrate_set(uart0_addr, nrf_baudrate);
196
197 return 0;
198 }
199
200 /**
201 * @brief Poll the device for input.
202 *
203 * @param dev UART device struct
204 * @param c Pointer to character
205 *
206 * @return 0 if a character arrived, -1 if the input buffer if empty.
207 */
208
uart_nrfx_poll_in(const struct device * dev,unsigned char * c)209 static int uart_nrfx_poll_in(const struct device *dev, unsigned char *c)
210 {
211 if (!nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_RXDRDY)) {
212 return -1;
213 }
214
215 /* Clear the interrupt */
216 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXDRDY);
217
218 /* got a character */
219 *c = nrf_uart_rxd_get(uart0_addr);
220
221 return 0;
222 }
223
224 #ifdef CONFIG_UART_0_ASYNC
225 static void uart_nrfx_isr(const struct device *dev);
226 #endif
227
228 /**
229 * @brief Output a character in polled mode.
230 *
231 * @param dev UART device struct
232 * @param c Character to send
233 */
uart_nrfx_poll_out(const struct device * dev,unsigned char c)234 static void uart_nrfx_poll_out(const struct device *dev, unsigned char c)
235 {
236 atomic_t *lock;
237 #ifdef CONFIG_UART_0_ASYNC
238 while (uart0_cb.tx_buffer) {
239 /* If there is ongoing asynchronous transmission, and we are in
240 * ISR, then call uart interrupt routine, otherwise
241 * busy wait until transmission is finished.
242 */
243 if (k_is_in_isr()) {
244 uart_nrfx_isr(dev);
245 }
246 }
247 /* Use tx_buffer_length as lock, this way uart_nrfx_tx will
248 * return -EBUSY during poll_out.
249 */
250 lock = &uart0_cb.tx_buffer_length;
251 #else
252 static atomic_val_t poll_out_lock;
253
254 lock = &poll_out_lock;
255 #endif
256
257 if (!k_is_in_isr()) {
258 uint8_t safety_cnt = 100;
259
260 while (atomic_cas((atomic_t *) lock,
261 (atomic_val_t) 0,
262 (atomic_val_t) 1) == false) {
263 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
264 /* k_sleep allows other threads to execute and finish
265 * their transactions.
266 */
267 k_msleep(1);
268 } else {
269 k_busy_wait(1000);
270 }
271 if (--safety_cnt == 0) {
272 break;
273 }
274 }
275 } else {
276 *lock = 1;
277 }
278 /* Reset the transmitter ready state. */
279 event_txdrdy_clear();
280
281 /* Activate the transmitter. */
282 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STARTTX);
283
284 /* Send the provided character. */
285 nrf_uart_txd_set(uart0_addr, (uint8_t)c);
286
287 /* Wait until the transmitter is ready, i.e. the character is sent. */
288 int res;
289
290 NRFX_WAIT_FOR(event_txdrdy_check(), 1000, 1, res);
291
292 /* Deactivate the transmitter so that it does not needlessly
293 * consume power.
294 */
295 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPTX);
296
297 /* Release the lock. */
298 *lock = 0;
299 }
300
301 /** Console I/O function */
uart_nrfx_err_check(const struct device * dev)302 static int uart_nrfx_err_check(const struct device *dev)
303 {
304 /* register bitfields maps to the defines in uart.h */
305 return nrf_uart_errorsrc_get_and_clear(uart0_addr);
306 }
307
uart_nrfx_configure(const struct device * dev,const struct uart_config * cfg)308 static int uart_nrfx_configure(const struct device *dev,
309 const struct uart_config *cfg)
310 {
311 struct uart_nrfx_data *data = dev->data;
312 nrf_uart_config_t uart_cfg;
313
314 #if defined(UART_CONFIG_STOP_Msk)
315 switch (cfg->stop_bits) {
316 case UART_CFG_STOP_BITS_1:
317 uart_cfg.stop = NRF_UART_STOP_ONE;
318 break;
319 case UART_CFG_STOP_BITS_2:
320 uart_cfg.stop = NRF_UART_STOP_TWO;
321 break;
322 default:
323 return -ENOTSUP;
324 }
325 #else
326 if (cfg->stop_bits != UART_CFG_STOP_BITS_1) {
327 return -ENOTSUP;
328 }
329 #endif
330
331 if (cfg->data_bits != UART_CFG_DATA_BITS_8) {
332 return -ENOTSUP;
333 }
334
335 switch (cfg->flow_ctrl) {
336 case UART_CFG_FLOW_CTRL_NONE:
337 uart_cfg.hwfc = NRF_UART_HWFC_DISABLED;
338 break;
339 case UART_CFG_FLOW_CTRL_RTS_CTS:
340 if (HW_FLOW_CONTROL_AVAILABLE) {
341 uart_cfg.hwfc = NRF_UART_HWFC_ENABLED;
342 } else {
343 return -ENOTSUP;
344 }
345 break;
346 default:
347 return -ENOTSUP;
348 }
349
350 #if defined(UART_CONFIG_PARITYTYPE_Msk)
351 uart_cfg.paritytype = NRF_UART_PARITYTYPE_EVEN;
352 #endif
353 switch (cfg->parity) {
354 case UART_CFG_PARITY_NONE:
355 uart_cfg.parity = NRF_UART_PARITY_EXCLUDED;
356 break;
357 case UART_CFG_PARITY_EVEN:
358 uart_cfg.parity = NRF_UART_PARITY_INCLUDED;
359 break;
360 #if defined(UART_CONFIG_PARITYTYPE_Msk)
361 case UART_CFG_PARITY_ODD:
362 uart_cfg.parity = NRF_UART_PARITY_INCLUDED;
363 uart_cfg.paritytype = NRF_UART_PARITYTYPE_ODD;
364 break;
365 #endif
366 default:
367 return -ENOTSUP;
368 }
369
370 if (baudrate_set(dev, cfg->baudrate) != 0) {
371 return -ENOTSUP;
372 }
373
374 nrf_uart_configure(uart0_addr, &uart_cfg);
375
376 data->uart_config = *cfg;
377
378 return 0;
379 }
380
381 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
uart_nrfx_config_get(const struct device * dev,struct uart_config * cfg)382 static int uart_nrfx_config_get(const struct device *dev,
383 struct uart_config *cfg)
384 {
385 struct uart_nrfx_data *data = dev->data;
386
387 *cfg = data->uart_config;
388 return 0;
389 }
390 #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
391
392 #ifdef CONFIG_UART_0_ASYNC
393
user_callback(const struct device * dev,struct uart_event * event)394 static void user_callback(const struct device *dev, struct uart_event *event)
395 {
396 if (uart0_cb.callback) {
397 uart0_cb.callback(dev, event, uart0_cb.user_data);
398 }
399 }
400
uart_nrfx_callback_set(const struct device * dev,uart_callback_t callback,void * user_data)401 static int uart_nrfx_callback_set(const struct device *dev,
402 uart_callback_t callback,
403 void *user_data)
404 {
405 uart0_cb.callback = callback;
406 uart0_cb.user_data = user_data;
407
408 return 0;
409 }
410
uart_nrfx_tx(const struct device * dev,const uint8_t * buf,size_t len,int32_t timeout)411 static int uart_nrfx_tx(const struct device *dev, const uint8_t *buf,
412 size_t len,
413 int32_t timeout)
414 {
415 if (atomic_cas((atomic_t *) &uart0_cb.tx_buffer_length,
416 (atomic_val_t) 0,
417 (atomic_val_t) len) == false) {
418 return -EBUSY;
419 }
420
421 uart0_cb.tx_buffer = buf;
422 #if HW_FLOW_CONTROL_AVAILABLE
423 uart0_cb.tx_timeout = timeout;
424 #endif
425 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_TXDRDY);
426 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STARTTX);
427 nrf_uart_int_enable(uart0_addr, NRF_UART_INT_MASK_TXDRDY);
428
429 uint8_t txd = uart0_cb.tx_buffer[uart0_cb.tx_counter];
430
431 nrf_uart_txd_set(uart0_addr, txd);
432
433 return 0;
434 }
435
uart_nrfx_tx_abort(const struct device * dev)436 static int uart_nrfx_tx_abort(const struct device *dev)
437 {
438 if (uart0_cb.tx_buffer_length == 0) {
439 return -EINVAL;
440 }
441 #if HW_FLOW_CONTROL_AVAILABLE
442 if (uart0_cb.tx_timeout != SYS_FOREVER_US) {
443 k_timer_stop(&uart0_cb.tx_timeout_timer);
444 }
445 #endif
446 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPTX);
447
448 struct uart_event evt = {
449 .type = UART_TX_ABORTED,
450 .data.tx.buf = uart0_cb.tx_buffer,
451 .data.tx.len = uart0_cb.tx_counter
452 };
453
454 uart0_cb.tx_buffer_length = 0;
455 uart0_cb.tx_counter = 0;
456
457 user_callback(dev, &evt);
458
459 return 0;
460 }
461
uart_nrfx_rx_enable(const struct device * dev,uint8_t * buf,size_t len,int32_t timeout)462 static int uart_nrfx_rx_enable(const struct device *dev, uint8_t *buf,
463 size_t len,
464 int32_t timeout)
465 {
466 if (DISABLE_RX) {
467 __ASSERT(false, "TX only UART instance");
468 return -ENOTSUP;
469 }
470
471 if (uart0_cb.rx_buffer_length != 0) {
472 return -EBUSY;
473 }
474
475 uart0_cb.rx_enabled = 1;
476 uart0_cb.rx_buffer = buf;
477 uart0_cb.rx_buffer_length = len;
478 uart0_cb.rx_counter = 0;
479 uart0_cb.rx_secondary_buffer_length = 0;
480 uart0_cb.rx_timeout = timeout;
481
482 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_ERROR);
483 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXDRDY);
484 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXTO);
485 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STARTRX);
486 nrf_uart_int_enable(uart0_addr, NRF_UART_INT_MASK_RXDRDY |
487 NRF_UART_INT_MASK_ERROR |
488 NRF_UART_INT_MASK_RXTO);
489
490 return 0;
491 }
492
uart_nrfx_rx_buf_rsp(const struct device * dev,uint8_t * buf,size_t len)493 static int uart_nrfx_rx_buf_rsp(const struct device *dev, uint8_t *buf,
494 size_t len)
495 {
496 int err;
497 unsigned int key = irq_lock();
498
499 if (!uart0_cb.rx_enabled) {
500 err = -EACCES;
501 } else if (uart0_cb.rx_secondary_buffer_length != 0) {
502 err = -EBUSY;
503 } else {
504 uart0_cb.rx_secondary_buffer = buf;
505 uart0_cb.rx_secondary_buffer_length = len;
506 err = 0;
507 }
508
509 irq_unlock(key);
510
511 return err;
512 }
513
uart_nrfx_rx_disable(const struct device * dev)514 static int uart_nrfx_rx_disable(const struct device *dev)
515 {
516 if (uart0_cb.rx_buffer_length == 0) {
517 return -EFAULT;
518 }
519
520 uart0_cb.rx_enabled = 0;
521 if (uart0_cb.rx_timeout != SYS_FOREVER_US) {
522 k_timer_stop(&uart0_cb.rx_timeout_timer);
523 }
524 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPRX);
525
526 return 0;
527 }
528
rx_rdy_evt(const struct device * dev)529 static void rx_rdy_evt(const struct device *dev)
530 {
531 struct uart_event event;
532 size_t rx_cnt = uart0_cb.rx_counter;
533
534 event.type = UART_RX_RDY;
535 event.data.rx.buf = uart0_cb.rx_buffer;
536 event.data.rx.len = rx_cnt - uart0_cb.rx_offset;
537 event.data.rx.offset = uart0_cb.rx_offset;
538
539 uart0_cb.rx_offset = rx_cnt;
540
541 user_callback(dev, &event);
542 }
543
buf_released_evt(const struct device * dev)544 static void buf_released_evt(const struct device *dev)
545 {
546 struct uart_event event = {
547 .type = UART_RX_BUF_RELEASED,
548 .data.rx_buf.buf = uart0_cb.rx_buffer
549 };
550 user_callback(dev, &event);
551 }
552
rx_disabled_evt(const struct device * dev)553 static void rx_disabled_evt(const struct device *dev)
554 {
555 struct uart_event event = {
556 .type = UART_RX_DISABLED
557 };
558 user_callback(dev, &event);
559 }
560
rx_reset_state(void)561 static void rx_reset_state(void)
562 {
563 nrf_uart_int_disable(uart0_addr,
564 NRF_UART_INT_MASK_RXDRDY |
565 NRF_UART_INT_MASK_ERROR |
566 NRF_UART_INT_MASK_RXTO);
567 uart0_cb.rx_buffer_length = 0;
568 uart0_cb.rx_enabled = 0;
569 uart0_cb.rx_counter = 0;
570 uart0_cb.rx_offset = 0;
571 uart0_cb.rx_secondary_buffer_length = 0;
572 }
573
rx_isr(const struct device * dev)574 static void rx_isr(const struct device *dev)
575 {
576 struct uart_event event;
577
578 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXDRDY);
579
580 if (!uart0_cb.rx_buffer_length || !uart0_cb.rx_enabled) {
581 /* Byte received when receiving is disabled - data lost. */
582 nrf_uart_rxd_get(uart0_addr);
583 } else {
584 if (uart0_cb.rx_counter == 0 &&
585 uart0_cb.rx_secondary_buffer_length == 0) {
586 event.type = UART_RX_BUF_REQUEST;
587 user_callback(dev, &event);
588 }
589 uart0_cb.rx_buffer[uart0_cb.rx_counter] =
590 nrf_uart_rxd_get(uart0_addr);
591 uart0_cb.rx_counter++;
592 if (uart0_cb.rx_timeout == 0) {
593 rx_rdy_evt(dev);
594 } else if (uart0_cb.rx_timeout != SYS_FOREVER_US) {
595 k_timer_start(&uart0_cb.rx_timeout_timer,
596 K_USEC(uart0_cb.rx_timeout),
597 K_NO_WAIT);
598 }
599 }
600
601 if (uart0_cb.rx_buffer_length == uart0_cb.rx_counter) {
602 if (uart0_cb.rx_timeout != SYS_FOREVER_US) {
603 k_timer_stop(&uart0_cb.rx_timeout_timer);
604 }
605 rx_rdy_evt(dev);
606
607 unsigned int key = irq_lock();
608
609 if (uart0_cb.rx_secondary_buffer_length == 0) {
610 uart0_cb.rx_enabled = 0;
611 }
612 irq_unlock(key);
613
614 if (uart0_cb.rx_secondary_buffer_length) {
615 buf_released_evt(dev);
616 /* Switch to secondary buffer. */
617 uart0_cb.rx_buffer_length =
618 uart0_cb.rx_secondary_buffer_length;
619 uart0_cb.rx_buffer = uart0_cb.rx_secondary_buffer;
620 uart0_cb.rx_secondary_buffer_length = 0;
621 uart0_cb.rx_counter = 0;
622 uart0_cb.rx_offset = 0;
623
624 event.type = UART_RX_BUF_REQUEST;
625 user_callback(dev, &event);
626 } else {
627 uart_nrfx_rx_disable(dev);
628 }
629 }
630 }
631
tx_isr(const struct device * dev)632 static void tx_isr(const struct device *dev)
633 {
634 uart0_cb.tx_counter++;
635 if (uart0_cb.tx_counter < uart0_cb.tx_buffer_length &&
636 !uart0_cb.tx_abort) {
637 #if HW_FLOW_CONTROL_AVAILABLE
638 if (uart0_cb.tx_timeout != SYS_FOREVER_US) {
639 k_timer_start(&uart0_cb.tx_timeout_timer,
640 K_USEC(uart0_cb.tx_timeout),
641 K_NO_WAIT);
642 }
643 #endif
644 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_TXDRDY);
645
646 uint8_t txd = uart0_cb.tx_buffer[uart0_cb.tx_counter];
647
648 nrf_uart_txd_set(uart0_addr, txd);
649 } else {
650 #if HW_FLOW_CONTROL_AVAILABLE
651
652 if (uart0_cb.tx_timeout != SYS_FOREVER_US) {
653 k_timer_stop(&uart0_cb.tx_timeout_timer);
654 }
655 #endif
656 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPTX);
657 struct uart_event event = {
658 .type = UART_TX_DONE,
659 .data.tx.buf = uart0_cb.tx_buffer,
660 .data.tx.len = uart0_cb.tx_counter
661 };
662 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_TXDRDY);
663 uart0_cb.tx_buffer_length = 0;
664 uart0_cb.tx_counter = 0;
665 uart0_cb.tx_buffer = NULL;
666
667 nrf_uart_int_disable(uart0_addr, NRF_UART_INT_MASK_TXDRDY);
668 user_callback(dev, &event);
669 }
670 }
671
672 #define UART_ERROR_FROM_MASK(mask) \
673 (mask & NRF_UART_ERROR_OVERRUN_MASK ? UART_ERROR_OVERRUN \
674 : mask & NRF_UART_ERROR_PARITY_MASK ? UART_ERROR_PARITY \
675 : mask & NRF_UART_ERROR_FRAMING_MASK ? UART_ERROR_FRAMING \
676 : mask & NRF_UART_ERROR_BREAK_MASK ? UART_BREAK \
677 : 0)
678
error_isr(const struct device * dev)679 static void error_isr(const struct device *dev)
680 {
681 if (uart0_cb.rx_timeout != SYS_FOREVER_US) {
682 k_timer_stop(&uart0_cb.rx_timeout_timer);
683 }
684 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_ERROR);
685
686 if (!uart0_cb.rx_enabled) {
687 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPRX);
688 }
689 struct uart_event event = {
690 .type = UART_RX_STOPPED,
691 .data.rx_stop.reason =
692 UART_ERROR_FROM_MASK(
693 nrf_uart_errorsrc_get_and_clear(uart0_addr)),
694 .data.rx_stop.data.len = uart0_cb.rx_counter
695 - uart0_cb.rx_offset,
696 .data.rx_stop.data.offset = uart0_cb.rx_offset,
697 .data.rx_stop.data.buf = uart0_cb.rx_buffer
698 };
699
700 user_callback(dev, &event);
701 /* Abort transfer. */
702 uart_nrfx_rx_disable(dev);
703 }
704
705 /*
706 * In nRF hardware RX timeout can occur only after stopping the peripheral,
707 * it is used as a sign that peripheral has finished its operation and is
708 * disabled.
709 */
rxto_isr(const struct device * dev)710 static void rxto_isr(const struct device *dev)
711 {
712 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXTO);
713
714 /* Send rxrdy if there is any data pending. */
715 if (uart0_cb.rx_counter - uart0_cb.rx_offset) {
716 rx_rdy_evt(dev);
717 }
718
719 buf_released_evt(dev);
720 if (uart0_cb.rx_secondary_buffer_length) {
721 uart0_cb.rx_buffer = uart0_cb.rx_secondary_buffer;
722 buf_released_evt(dev);
723 }
724
725 rx_reset_state();
726 rx_disabled_evt(dev);
727 }
728
uart_nrfx_isr(const struct device * uart)729 void uart_nrfx_isr(const struct device *uart)
730 {
731 if (nrf_uart_int_enable_check(uart0_addr, NRF_UART_INT_MASK_ERROR) &&
732 nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_ERROR)) {
733 error_isr(uart);
734 } else if (nrf_uart_int_enable_check(uart0_addr,
735 NRF_UART_INT_MASK_RXDRDY) &&
736 nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_RXDRDY)) {
737 rx_isr(uart);
738 }
739
740 if (nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_TXDRDY)
741 && nrf_uart_int_enable_check(uart0_addr,
742 NRF_UART_INT_MASK_TXDRDY)) {
743 tx_isr(uart);
744 }
745
746 if (nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_RXTO)) {
747 rxto_isr(uart);
748 }
749 }
750
rx_timeout(struct k_timer * timer)751 static void rx_timeout(struct k_timer *timer)
752 {
753 rx_rdy_evt(DEVICE_DT_INST_GET(0));
754 }
755
756 #if HW_FLOW_CONTROL_AVAILABLE
tx_timeout(struct k_timer * timer)757 static void tx_timeout(struct k_timer *timer)
758 {
759 struct uart_event evt;
760
761 if (uart0_cb.tx_timeout != SYS_FOREVER_US) {
762 k_timer_stop(&uart0_cb.tx_timeout_timer);
763 }
764 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPTX);
765 evt.type = UART_TX_ABORTED;
766 evt.data.tx.buf = uart0_cb.tx_buffer;
767 evt.data.tx.len = uart0_cb.tx_buffer_length;
768 uart0_cb.tx_buffer_length = 0;
769 uart0_cb.tx_counter = 0;
770 user_callback(DEVICE_DT_INST_GET(0), &evt);
771 }
772 #endif
773
774 #endif /* CONFIG_UART_0_ASYNC */
775
776
777 #ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
778
779 /** Interrupt driven FIFO fill function */
uart_nrfx_fifo_fill(const struct device * dev,const uint8_t * tx_data,int len)780 static int uart_nrfx_fifo_fill(const struct device *dev,
781 const uint8_t *tx_data,
782 int len)
783 {
784 uint8_t num_tx = 0U;
785
786 while ((len - num_tx > 0) &&
787 event_txdrdy_check()) {
788
789 /* Clear the interrupt */
790 event_txdrdy_clear();
791
792 /* Send a character */
793 nrf_uart_txd_set(uart0_addr, (uint8_t)tx_data[num_tx++]);
794 }
795
796 return (int)num_tx;
797 }
798
799 /** Interrupt driven FIFO read function */
uart_nrfx_fifo_read(const struct device * dev,uint8_t * rx_data,const int size)800 static int uart_nrfx_fifo_read(const struct device *dev,
801 uint8_t *rx_data,
802 const int size)
803 {
804 uint8_t num_rx = 0U;
805
806 while ((size - num_rx > 0) &&
807 nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_RXDRDY)) {
808 /* Clear the interrupt */
809 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXDRDY);
810
811 /* Receive a character */
812 rx_data[num_rx++] = (uint8_t)nrf_uart_rxd_get(uart0_addr);
813 }
814
815 return num_rx;
816 }
817
818 /** Interrupt driven transfer enabling function */
uart_nrfx_irq_tx_enable(const struct device * dev)819 static void uart_nrfx_irq_tx_enable(const struct device *dev)
820 {
821 uint32_t key;
822
823 disable_tx_irq = false;
824
825 /* Indicate that this device started a transaction that should not be
826 * interrupted by putting the SoC into the deep sleep mode.
827 */
828 pm_device_busy_set(dev);
829
830 /* Activate the transmitter. */
831 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STARTTX);
832
833 nrf_uart_int_enable(uart0_addr, NRF_UART_INT_MASK_TXDRDY);
834
835 /* Critical section is used to avoid any UART related interrupt which
836 * can occur after the if statement and before call of the function
837 * forcing an interrupt.
838 */
839 key = irq_lock();
840 if (uart_sw_event_txdrdy) {
841 /* Due to HW limitation first TXDRDY interrupt shall be
842 * triggered by the software.
843 */
844 NVIC_SetPendingIRQ(IRQN);
845 }
846 irq_unlock(key);
847 }
848
849 /** Interrupt driven transfer disabling function */
uart_nrfx_irq_tx_disable(const struct device * dev)850 static void uart_nrfx_irq_tx_disable(const struct device *dev)
851 {
852 /* Disable TX interrupt in uart_nrfx_isr() when transmission is done. */
853 disable_tx_irq = true;
854 }
855
856 /** Interrupt driven receiver enabling function */
uart_nrfx_irq_rx_enable(const struct device * dev)857 static void uart_nrfx_irq_rx_enable(const struct device *dev)
858 {
859 nrf_uart_int_enable(uart0_addr, NRF_UART_INT_MASK_RXDRDY);
860 }
861
862 /** Interrupt driven receiver disabling function */
uart_nrfx_irq_rx_disable(const struct device * dev)863 static void uart_nrfx_irq_rx_disable(const struct device *dev)
864 {
865 nrf_uart_int_disable(uart0_addr, NRF_UART_INT_MASK_RXDRDY);
866 }
867
868 /** Interrupt driven transfer empty function */
uart_nrfx_irq_tx_ready_complete(const struct device * dev)869 static int uart_nrfx_irq_tx_ready_complete(const struct device *dev)
870 {
871 /* Signal TX readiness only when the TX interrupt is enabled and there
872 * is no pending request to disable it. Note that this function may get
873 * called after the TX interrupt is requested to be disabled but before
874 * the disabling is actually performed (in the IRQ handler).
875 */
876 return nrf_uart_int_enable_check(uart0_addr,
877 NRF_UART_INT_MASK_TXDRDY) &&
878 !disable_tx_irq &&
879 event_txdrdy_check();
880 }
881
882 /** Interrupt driven receiver ready function */
uart_nrfx_irq_rx_ready(const struct device * dev)883 static int uart_nrfx_irq_rx_ready(const struct device *dev)
884 {
885 return nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_RXDRDY);
886 }
887
888 /** Interrupt driven error enabling function */
uart_nrfx_irq_err_enable(const struct device * dev)889 static void uart_nrfx_irq_err_enable(const struct device *dev)
890 {
891 nrf_uart_int_enable(uart0_addr, NRF_UART_INT_MASK_ERROR);
892 }
893
894 /** Interrupt driven error disabling function */
uart_nrfx_irq_err_disable(const struct device * dev)895 static void uart_nrfx_irq_err_disable(const struct device *dev)
896 {
897 nrf_uart_int_disable(uart0_addr, NRF_UART_INT_MASK_ERROR);
898 }
899
900 /** Interrupt driven pending status function */
uart_nrfx_irq_is_pending(const struct device * dev)901 static int uart_nrfx_irq_is_pending(const struct device *dev)
902 {
903 return ((nrf_uart_int_enable_check(uart0_addr,
904 NRF_UART_INT_MASK_TXDRDY) &&
905 uart_nrfx_irq_tx_ready_complete(dev))
906 ||
907 (nrf_uart_int_enable_check(uart0_addr,
908 NRF_UART_INT_MASK_RXDRDY) &&
909 uart_nrfx_irq_rx_ready(dev)));
910 }
911
912 /** Interrupt driven interrupt update function */
uart_nrfx_irq_update(const struct device * dev)913 static int uart_nrfx_irq_update(const struct device *dev)
914 {
915 return 1;
916 }
917
918 /** Set the callback function */
uart_nrfx_irq_callback_set(const struct device * dev,uart_irq_callback_user_data_t cb,void * cb_data)919 static void uart_nrfx_irq_callback_set(const struct device *dev,
920 uart_irq_callback_user_data_t cb,
921 void *cb_data)
922 {
923 (void)dev;
924 irq_callback = cb;
925 irq_cb_data = cb_data;
926 }
927
928 /**
929 * @brief Interrupt service routine.
930 *
931 * This simply calls the callback function, if one exists.
932 *
933 * @param arg Argument to ISR.
934 */
uart_nrfx_isr(const struct device * dev)935 static void uart_nrfx_isr(const struct device *dev)
936 {
937 if (disable_tx_irq &&
938 nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_TXDRDY)) {
939 nrf_uart_int_disable(uart0_addr, NRF_UART_INT_MASK_TXDRDY);
940
941 /* Deactivate the transmitter so that it does not needlessly
942 * consume power.
943 */
944 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STOPTX);
945
946 /* The transaction is over. It is okay to enter the deep sleep
947 * mode if needed.
948 */
949 pm_device_busy_clear(dev);
950
951 disable_tx_irq = false;
952
953 return;
954 }
955
956 if (nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_ERROR)) {
957 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_ERROR);
958 }
959
960 if (irq_callback) {
961 irq_callback(dev, irq_cb_data);
962 }
963 }
964 #endif /* CONFIG_UART_0_INTERRUPT_DRIVEN */
965
966 /**
967 * @brief Initialize UART channel
968 *
969 * This routine is called to reset the chip in a quiescent state.
970 * It is assumed that this function is called only once per UART.
971 *
972 * @param dev UART device struct
973 *
974 * @return 0 on success
975 */
uart_nrfx_init(const struct device * dev)976 static int uart_nrfx_init(const struct device *dev)
977 {
978 const struct uart_nrfx_config *config = dev->config;
979 struct uart_nrfx_data *data = dev->data;
980 int err;
981
982 nrf_uart_disable(uart0_addr);
983
984 err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
985 if (err < 0) {
986 return err;
987 }
988
989 /* Set initial configuration */
990 err = uart_nrfx_configure(dev, &data->uart_config);
991 if (err) {
992 return err;
993 }
994
995 /* Enable the UART and activate its receiver. With the current API
996 * the receiver needs to be active all the time. The transmitter
997 * will be activated when there is something to send.
998 */
999 nrf_uart_enable(uart0_addr);
1000
1001 if (!DISABLE_RX) {
1002 nrf_uart_event_clear(uart0_addr, NRF_UART_EVENT_RXDRDY);
1003
1004 nrf_uart_task_trigger(uart0_addr, NRF_UART_TASK_STARTRX);
1005 }
1006
1007 #ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
1008 /* Simulate that the TXDRDY event is set, so that the transmitter status
1009 * is indicated correctly.
1010 */
1011 uart_sw_event_txdrdy = 1U;
1012 #endif
1013
1014 #if defined(CONFIG_UART_0_ASYNC) || defined(CONFIG_UART_0_INTERRUPT_DRIVEN)
1015
1016 IRQ_CONNECT(IRQN,
1017 IRQ_PRIO,
1018 uart_nrfx_isr,
1019 DEVICE_DT_INST_GET(0),
1020 0);
1021 irq_enable(IRQN);
1022 #endif
1023
1024 #ifdef CONFIG_UART_0_ASYNC
1025 k_timer_init(&uart0_cb.rx_timeout_timer, rx_timeout, NULL);
1026 #if HW_FLOW_CONTROL_AVAILABLE
1027 k_timer_init(&uart0_cb.tx_timeout_timer, tx_timeout, NULL);
1028 #endif
1029 #endif
1030 return 0;
1031 }
1032
1033 /* Common function: uart_nrfx_irq_tx_ready_complete is used for two API entries
1034 * because Nordic hardware does not distinguish between them.
1035 */
1036 static const struct uart_driver_api uart_nrfx_uart_driver_api = {
1037 #ifdef CONFIG_UART_0_ASYNC
1038 .callback_set = uart_nrfx_callback_set,
1039 .tx = uart_nrfx_tx,
1040 .tx_abort = uart_nrfx_tx_abort,
1041 .rx_enable = uart_nrfx_rx_enable,
1042 .rx_buf_rsp = uart_nrfx_rx_buf_rsp,
1043 .rx_disable = uart_nrfx_rx_disable,
1044 #endif /* CONFIG_UART_0_ASYNC */
1045 .poll_in = uart_nrfx_poll_in,
1046 .poll_out = uart_nrfx_poll_out,
1047 .err_check = uart_nrfx_err_check,
1048 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
1049 .configure = uart_nrfx_configure,
1050 .config_get = uart_nrfx_config_get,
1051 #endif
1052 #ifdef CONFIG_UART_0_INTERRUPT_DRIVEN
1053 .fifo_fill = uart_nrfx_fifo_fill,
1054 .fifo_read = uart_nrfx_fifo_read,
1055 .irq_tx_enable = uart_nrfx_irq_tx_enable,
1056 .irq_tx_disable = uart_nrfx_irq_tx_disable,
1057 .irq_tx_ready = uart_nrfx_irq_tx_ready_complete,
1058 .irq_rx_enable = uart_nrfx_irq_rx_enable,
1059 .irq_rx_disable = uart_nrfx_irq_rx_disable,
1060 .irq_tx_complete = uart_nrfx_irq_tx_ready_complete,
1061 .irq_rx_ready = uart_nrfx_irq_rx_ready,
1062 .irq_err_enable = uart_nrfx_irq_err_enable,
1063 .irq_err_disable = uart_nrfx_irq_err_disable,
1064 .irq_is_pending = uart_nrfx_irq_is_pending,
1065 .irq_update = uart_nrfx_irq_update,
1066 .irq_callback_set = uart_nrfx_irq_callback_set,
1067 #endif /* CONFIG_UART_0_INTERRUPT_DRIVEN */
1068 };
1069
1070 #ifdef CONFIG_PM_DEVICE
uart_nrfx_pm_action(const struct device * dev,enum pm_device_action action)1071 static int uart_nrfx_pm_action(const struct device *dev,
1072 enum pm_device_action action)
1073 {
1074 const struct uart_nrfx_config *config = dev->config;
1075 int ret;
1076
1077 switch (action) {
1078 case PM_DEVICE_ACTION_RESUME:
1079 if (IS_ENABLED(CONFIG_UART_0_GPIO_MANAGEMENT)) {
1080 ret = pinctrl_apply_state(config->pcfg,
1081 PINCTRL_STATE_DEFAULT);
1082 if (ret < 0) {
1083 return ret;
1084 }
1085 }
1086
1087 nrf_uart_enable(uart0_addr);
1088 if (!DISABLE_RX) {
1089 nrf_uart_task_trigger(uart0_addr,
1090 NRF_UART_TASK_STARTRX);
1091 }
1092 break;
1093 case PM_DEVICE_ACTION_SUSPEND:
1094 nrf_uart_disable(uart0_addr);
1095
1096 if (IS_ENABLED(CONFIG_UART_0_GPIO_MANAGEMENT)) {
1097 ret = pinctrl_apply_state(config->pcfg,
1098 PINCTRL_STATE_SLEEP);
1099 if (ret < 0) {
1100 return ret;
1101 }
1102 }
1103 break;
1104 default:
1105 return -ENOTSUP;
1106 }
1107
1108 return 0;
1109 }
1110 #endif /* CONFIG_PM_DEVICE */
1111
1112 PINCTRL_DT_INST_DEFINE(0);
1113
1114 NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(0));
1115
1116 static const struct uart_nrfx_config uart_nrfx_uart0_config = {
1117 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),
1118 };
1119
1120 static struct uart_nrfx_data uart_nrfx_uart0_data = {
1121 .uart_config = {
1122 .stop_bits = UART_CFG_STOP_BITS_1,
1123 .data_bits = UART_CFG_DATA_BITS_8,
1124 .baudrate = BAUDRATE,
1125 #ifdef CONFIG_UART_0_NRF_PARITY_BIT
1126 .parity = UART_CFG_PARITY_EVEN,
1127 #else
1128 .parity = UART_CFG_PARITY_NONE,
1129 #endif /* CONFIG_UART_0_NRF_PARITY_BIT */
1130 .flow_ctrl = PROP(hw_flow_control) ?
1131 UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE,
1132 }
1133 };
1134
1135 PM_DEVICE_DT_INST_DEFINE(0, uart_nrfx_pm_action);
1136
1137 DEVICE_DT_INST_DEFINE(0,
1138 uart_nrfx_init,
1139 PM_DEVICE_DT_INST_GET(0),
1140 &uart_nrfx_uart0_data,
1141 &uart_nrfx_uart0_config,
1142 /* Initialize UART device before UART console. */
1143 PRE_KERNEL_1,
1144 CONFIG_SERIAL_INIT_PRIORITY,
1145 &uart_nrfx_uart_driver_api);
1146