Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018-2021 Nordic Semiconductor ASA
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * @brief Driver for Nordic Semiconductor nRF UARTE
9 : : */
10 : :
11 : : #include <drivers/uart.h>
12 : : #include <pm/device.h>
13 : : #include <hal/nrf_uarte.h>
14 : : #include <nrfx_timer.h>
15 : : #include <sys/util.h>
16 : : #include <kernel.h>
17 : : #include <soc.h>
18 : : #include <helpers/nrfx_gppi.h>
19 : :
20 : : #include <logging/log.h>
21 : : LOG_MODULE_REGISTER(uart_nrfx_uarte, CONFIG_UART_LOG_LEVEL);
22 : :
23 : : #ifdef CONFIG_PINCTRL
24 : : #include <drivers/pinctrl.h>
25 : : #else
26 : : #include <hal/nrf_gpio.h>
27 : : #endif /* CONFIG_PINCTRL */
28 : :
29 : : /* Generalize PPI or DPPI channel management */
30 : : #if defined(CONFIG_HAS_HW_NRF_PPI)
31 : : #include <nrfx_ppi.h>
32 : : #define gppi_channel_t nrf_ppi_channel_t
33 : : #define gppi_channel_alloc nrfx_ppi_channel_alloc
34 : : #define gppi_channel_enable nrfx_ppi_channel_enable
35 : : #elif defined(CONFIG_HAS_HW_NRF_DPPIC)
36 : : #include <nrfx_dppi.h>
37 : : #define gppi_channel_t uint8_t
38 : : #define gppi_channel_alloc nrfx_dppi_channel_alloc
39 : : #define gppi_channel_enable nrfx_dppi_channel_enable
40 : : #else
41 : : #error "No PPI or DPPI"
42 : : #endif
43 : :
44 : :
45 : : #if (defined(CONFIG_UART_0_NRF_UARTE) && \
46 : : defined(CONFIG_UART_0_INTERRUPT_DRIVEN)) || \
47 : : (defined(CONFIG_UART_1_NRF_UARTE) && \
48 : : defined(CONFIG_UART_1_INTERRUPT_DRIVEN)) || \
49 : : (defined(CONFIG_UART_2_NRF_UARTE) && \
50 : : defined(CONFIG_UART_2_INTERRUPT_DRIVEN)) || \
51 : : (defined(CONFIG_UART_3_NRF_UARTE) && \
52 : : defined(CONFIG_UART_3_INTERRUPT_DRIVEN))
53 : : #define UARTE_INTERRUPT_DRIVEN 1
54 : : #endif
55 : :
56 : : #if (defined(CONFIG_UART_0_NRF_UARTE) && !defined(CONFIG_UART_0_ASYNC)) || \
57 : : (defined(CONFIG_UART_1_NRF_UARTE) && !defined(CONFIG_UART_1_ASYNC)) || \
58 : : (defined(CONFIG_UART_2_NRF_UARTE) && !defined(CONFIG_UART_2_ASYNC)) || \
59 : : (defined(CONFIG_UART_3_NRF_UARTE) && !defined(CONFIG_UART_3_ASYNC))
60 : : #define UARTE_ANY_NONE_ASYNC 1
61 : : #endif
62 : :
63 : : #if (defined(CONFIG_UART_0_NRF_UARTE) && defined(CONFIG_UART_0_ASYNC)) || \
64 : : (defined(CONFIG_UART_1_NRF_UARTE) && defined(CONFIG_UART_1_ASYNC)) || \
65 : : (defined(CONFIG_UART_2_NRF_UARTE) && defined(CONFIG_UART_2_ASYNC)) || \
66 : : (defined(CONFIG_UART_3_NRF_UARTE) && defined(CONFIG_UART_3_ASYNC))
67 : : #define UARTE_ANY_ASYNC 1
68 : : #endif
69 : :
70 : : /*
71 : : * RX timeout is divided into time slabs, this define tells how many divisions
72 : : * should be made. More divisions - higher timeout accuracy and processor usage.
73 : : */
74 : : #define RX_TIMEOUT_DIV 5
75 : :
76 : : /* Size of hardware fifo in RX path. */
77 : : #define UARTE_HW_RX_FIFO_SIZE 5
78 : :
79 : : #ifdef UARTE_ANY_ASYNC
80 : : struct uarte_async_cb {
81 : : uart_callback_t user_callback;
82 : : void *user_data;
83 : :
84 : : const uint8_t *tx_buf;
85 : : volatile size_t tx_size;
86 : : const uint8_t *xfer_buf;
87 : : size_t xfer_len;
88 : :
89 : : uint8_t tx_cache[CONFIG_UART_ASYNC_TX_CACHE_SIZE];
90 : : size_t tx_cache_offset;
91 : :
92 : : struct k_timer tx_timeout_timer;
93 : :
94 : : uint8_t *rx_buf;
95 : : size_t rx_buf_len;
96 : : size_t rx_offset;
97 : : uint8_t *rx_next_buf;
98 : : size_t rx_next_buf_len;
99 : : uint32_t rx_total_byte_cnt; /* Total number of bytes received */
100 : : uint32_t rx_total_user_byte_cnt; /* Total number of bytes passed to user */
101 : : int32_t rx_timeout; /* Timeout set by user */
102 : : int32_t rx_timeout_slab; /* rx_timeout divided by RX_TIMEOUT_DIV */
103 : : int32_t rx_timeout_left; /* Current time left until user callback */
104 : : struct k_timer rx_timeout_timer;
105 : : union {
106 : : gppi_channel_t ppi;
107 : : uint32_t cnt;
108 : : } rx_cnt;
109 : : volatile int tx_amount;
110 : :
111 : : atomic_t low_power_mask;
112 : : uint8_t rx_flush_buffer[UARTE_HW_RX_FIFO_SIZE];
113 : : uint8_t rx_flush_cnt;
114 : : bool rx_enabled;
115 : : bool hw_rx_counting;
116 : : bool pending_tx;
117 : : /* Flag to ensure that RX timeout won't be executed during ENDRX ISR */
118 : : volatile bool is_in_irq;
119 : : };
120 : : #endif /* UARTE_ANY_ASYNC */
121 : :
122 : : #ifdef UARTE_INTERRUPT_DRIVEN
123 : : struct uarte_nrfx_int_driven {
124 : : uart_irq_callback_user_data_t cb; /**< Callback function pointer */
125 : : void *cb_data; /**< Callback function arg */
126 : : uint8_t *tx_buffer;
127 : : uint16_t tx_buff_size;
128 : : volatile bool disable_tx_irq;
129 : : #ifdef CONFIG_PM_DEVICE
130 : : bool rx_irq_enabled;
131 : : #endif
132 : : atomic_t fifo_fill_lock;
133 : : };
134 : : #endif
135 : :
136 : : /* Device data structure */
137 : : struct uarte_nrfx_data {
138 : : const struct device *dev;
139 : : struct uart_config uart_config;
140 : : #ifdef UARTE_INTERRUPT_DRIVEN
141 : : struct uarte_nrfx_int_driven *int_driven;
142 : : #endif
143 : : #ifdef UARTE_ANY_ASYNC
144 : : struct uarte_async_cb *async;
145 : : #endif
146 : : atomic_val_t poll_out_lock;
147 : : uint8_t char_out;
148 : : uint8_t rx_data;
149 : : gppi_channel_t ppi_ch_endtx;
150 : : };
151 : :
152 : : #define UARTE_LOW_POWER_TX BIT(0)
153 : : #define UARTE_LOW_POWER_RX BIT(1)
154 : :
155 : : /* If enabled, pins are managed when going to low power mode. */
156 : : #define UARTE_CFG_FLAG_GPIO_MGMT BIT(0)
157 : :
158 : : /* If enabled then ENDTX is PPI'ed to TXSTOP */
159 : : #define UARTE_CFG_FLAG_PPI_ENDTX BIT(1)
160 : :
161 : : /* If enabled then UARTE peripheral is disabled when not used. This allows
162 : : * to achieve lowest power consumption in idle.
163 : : */
164 : : #define UARTE_CFG_FLAG_LOW_POWER BIT(4)
165 : :
166 : : /**
167 : : * @brief Structure for UARTE configuration.
168 : : */
169 : : struct uarte_nrfx_config {
170 : : NRF_UARTE_Type *uarte_regs; /* Instance address */
171 : : uint32_t flags;
172 : : bool disable_rx;
173 : : #ifdef CONFIG_PINCTRL
174 : : const struct pinctrl_dev_config *pcfg;
175 : : #else
176 : : uint32_t tx_pin;
177 : : uint32_t rx_pin;
178 : : uint32_t rts_pin;
179 : : uint32_t cts_pin;
180 : : bool rx_pull_up;
181 : : bool cts_pull_up;
182 : : #endif /* CONFIG_PINCTRL */
183 : : #ifdef UARTE_ANY_ASYNC
184 : : nrfx_timer_t timer;
185 : : #endif
186 : : };
187 : :
188 : 1713630 : static inline NRF_UARTE_Type *get_uarte_instance(const struct device *dev)
189 : : {
190 : 1713630 : const struct uarte_nrfx_config *config = dev->config;
191 : :
192 : 1713630 : return config->uarte_regs;
193 : : }
194 : :
195 : : #ifndef CONFIG_PINCTRL
196 : : static void uarte_nrfx_pins_configure(const struct device *dev, bool sleep)
197 : : {
198 : : const struct uarte_nrfx_config *cfg = dev->config;
199 : :
200 : : if (!sleep) {
201 : : if (cfg->tx_pin != NRF_UARTE_PSEL_DISCONNECTED) {
202 : : nrf_gpio_pin_write(cfg->tx_pin, 1);
203 : : nrf_gpio_cfg_output(cfg->tx_pin);
204 : : }
205 : :
206 : : if (cfg->rx_pin != NRF_UARTE_PSEL_DISCONNECTED) {
207 : : nrf_gpio_cfg_input(cfg->rx_pin,
208 : : (cfg->rx_pull_up ?
209 : : NRF_GPIO_PIN_PULLUP :
210 : : NRF_GPIO_PIN_NOPULL));
211 : : }
212 : :
213 : : if (cfg->rts_pin != NRF_UARTE_PSEL_DISCONNECTED) {
214 : : nrf_gpio_pin_write(cfg->rts_pin, 1);
215 : : nrf_gpio_cfg_output(cfg->rts_pin);
216 : : }
217 : :
218 : : if (cfg->cts_pin != NRF_UARTE_PSEL_DISCONNECTED) {
219 : : nrf_gpio_cfg_input(cfg->cts_pin,
220 : : (cfg->cts_pull_up ?
221 : : NRF_GPIO_PIN_PULLUP :
222 : : NRF_GPIO_PIN_NOPULL));
223 : : }
224 : : } else {
225 : : if (cfg->tx_pin != NRF_UARTE_PSEL_DISCONNECTED) {
226 : : nrf_gpio_cfg_default(cfg->tx_pin);
227 : : }
228 : :
229 : : if (cfg->rx_pin != NRF_UARTE_PSEL_DISCONNECTED) {
230 : : nrf_gpio_cfg_default(cfg->rx_pin);
231 : : }
232 : :
233 : : if (cfg->rts_pin != NRF_UARTE_PSEL_DISCONNECTED) {
234 : : nrf_gpio_cfg_default(cfg->rts_pin);
235 : : }
236 : :
237 : : if (cfg->cts_pin != NRF_UARTE_PSEL_DISCONNECTED) {
238 : : nrf_gpio_cfg_default(cfg->cts_pin);
239 : : }
240 : : }
241 : :
242 : : nrf_uarte_txrx_pins_set(cfg->uarte_regs, cfg->tx_pin, cfg->rx_pin);
243 : : nrf_uarte_hwfc_pins_set(cfg->uarte_regs, cfg->rts_pin, cfg->cts_pin);
244 : : }
245 : : #endif /* !CONFIG_PINCTRL */
246 : :
247 : 0 : static void endtx_isr(const struct device *dev)
248 : : {
249 : 0 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
250 : :
251 : 0 : int key = irq_lock();
252 : :
253 [ # # ]: 0 : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDTX)) {
254 : 0 : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDTX);
255 : 0 : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPTX);
256 : : }
257 : :
258 : 0 : irq_unlock(key);
259 : :
260 : 0 : }
261 : :
262 : : #ifdef UARTE_ANY_NONE_ASYNC
263 : : /**
264 : : * @brief Interrupt service routine.
265 : : *
266 : : * This simply calls the callback function, if one exists.
267 : : *
268 : : * @param arg Argument to ISR.
269 : : */
270 : 0 : static void uarte_nrfx_isr_int(void *arg)
271 : : {
272 : 0 : const struct device *dev = arg;
273 : 0 : const struct uarte_nrfx_config *config = dev->config;
274 : 0 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
275 : :
276 : : /* If interrupt driven and asynchronous APIs are disabled then UART
277 : : * interrupt is still called to stop TX. Unless it is done using PPI.
278 : : */
279 [ # # # # ]: 0 : if (nrf_uarte_int_enable_check(uarte, NRF_UARTE_INT_ENDTX_MASK) &&
280 : 0 : nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDTX)) {
281 : 0 : endtx_isr(dev);
282 : : }
283 : :
284 [ # # ]: 0 : if (config->flags & UARTE_CFG_FLAG_LOW_POWER) {
285 : 0 : int key = irq_lock();
286 : :
287 [ # # ]: 0 : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED)) {
288 : 0 : nrf_uarte_disable(uarte);
289 : : }
290 : :
291 : : #ifdef UARTE_INTERRUPT_DRIVEN
292 : : struct uarte_nrfx_data *data = dev->data;
293 : :
294 : : if (!data->int_driven || data->int_driven->fifo_fill_lock == 0)
295 : : #endif
296 : : {
297 : 0 : nrf_uarte_int_disable(uarte,
298 : : NRF_UARTE_INT_TXSTOPPED_MASK);
299 : : }
300 : :
301 : 0 : irq_unlock(key);
302 : : }
303 : :
304 : : #ifdef UARTE_INTERRUPT_DRIVEN
305 : : struct uarte_nrfx_data *data = dev->data;
306 : :
307 : : if (!data->int_driven) {
308 : : return;
309 : : }
310 : :
311 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED)) {
312 : : data->int_driven->fifo_fill_lock = 0;
313 : : if (data->int_driven->disable_tx_irq) {
314 : : nrf_uarte_int_disable(uarte,
315 : : NRF_UARTE_INT_TXSTOPPED_MASK);
316 : : data->int_driven->disable_tx_irq = false;
317 : : return;
318 : : }
319 : :
320 : : }
321 : :
322 : :
323 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ERROR)) {
324 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ERROR);
325 : : }
326 : :
327 : : if (data->int_driven->cb) {
328 : : data->int_driven->cb(dev, data->int_driven->cb_data);
329 : : }
330 : : #endif /* UARTE_INTERRUPT_DRIVEN */
331 : 0 : }
332 : : #endif /* UARTE_ANY_NONE_ASYNC */
333 : :
334 : : /**
335 : : * @brief Set the baud rate
336 : : *
337 : : * This routine set the given baud rate for the UARTE.
338 : : *
339 : : * @param dev UARTE device struct
340 : : * @param baudrate Baud rate
341 : : *
342 : : * @return 0 on success or error code
343 : : */
344 : 1 : static int baudrate_set(const struct device *dev, uint32_t baudrate)
345 : : {
346 : : nrf_uarte_baudrate_t nrf_baudrate; /* calculated baudrate divisor */
347 : 1 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
348 : :
349 [ - - - - : 1 : switch (baudrate) {
- - - - -
- - - - -
+ - - - -
- - ]
350 : 0 : case 300:
351 : : /* value not supported by Nordic HAL */
352 : 0 : nrf_baudrate = 0x00014000;
353 : 0 : break;
354 : 0 : case 600:
355 : : /* value not supported by Nordic HAL */
356 : 0 : nrf_baudrate = 0x00027000;
357 : 0 : break;
358 : 0 : case 1200:
359 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_1200;
360 : 0 : break;
361 : 0 : case 2400:
362 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_2400;
363 : 0 : break;
364 : 0 : case 4800:
365 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_4800;
366 : 0 : break;
367 : 0 : case 9600:
368 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_9600;
369 : 0 : break;
370 : 0 : case 14400:
371 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_14400;
372 : 0 : break;
373 : 0 : case 19200:
374 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_19200;
375 : 0 : break;
376 : 0 : case 28800:
377 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_28800;
378 : 0 : break;
379 : 0 : case 31250:
380 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_31250;
381 : 0 : break;
382 : 0 : case 38400:
383 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_38400;
384 : 0 : break;
385 : 0 : case 56000:
386 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_56000;
387 : 0 : break;
388 : 0 : case 57600:
389 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_57600;
390 : 0 : break;
391 : 0 : case 76800:
392 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_76800;
393 : 0 : break;
394 : 1 : case 115200:
395 : 1 : nrf_baudrate = NRF_UARTE_BAUDRATE_115200;
396 : 1 : break;
397 : 0 : case 230400:
398 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_230400;
399 : 0 : break;
400 : 0 : case 250000:
401 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_250000;
402 : 0 : break;
403 : 0 : case 460800:
404 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_460800;
405 : 0 : break;
406 : 0 : case 921600:
407 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_921600;
408 : 0 : break;
409 : 0 : case 1000000:
410 : 0 : nrf_baudrate = NRF_UARTE_BAUDRATE_1000000;
411 : 0 : break;
412 : 0 : default:
413 : 0 : return -EINVAL;
414 : : }
415 : :
416 : 1 : nrf_uarte_baudrate_set(uarte, nrf_baudrate);
417 : :
418 : 1 : return 0;
419 : : }
420 : :
421 : 1 : static int uarte_nrfx_configure(const struct device *dev,
422 : : const struct uart_config *cfg)
423 : : {
424 : 1 : struct uarte_nrfx_data *data = dev->data;
425 : : nrf_uarte_config_t uarte_cfg;
426 : :
427 : : #if defined(UARTE_CONFIG_STOP_Msk)
428 [ + - - ]: 1 : switch (cfg->stop_bits) {
429 : 1 : case UART_CFG_STOP_BITS_1:
430 : 1 : uarte_cfg.stop = NRF_UARTE_STOP_ONE;
431 : 1 : break;
432 : 0 : case UART_CFG_STOP_BITS_2:
433 : 0 : uarte_cfg.stop = NRF_UARTE_STOP_TWO;
434 : 0 : break;
435 : 0 : default:
436 : 0 : return -ENOTSUP;
437 : : }
438 : : #else
439 : : if (cfg->stop_bits != UART_CFG_STOP_BITS_1) {
440 : : return -ENOTSUP;
441 : : }
442 : : #endif
443 : :
444 [ - + ]: 1 : if (cfg->data_bits != UART_CFG_DATA_BITS_8) {
445 : 0 : return -ENOTSUP;
446 : : }
447 : :
448 [ + - - ]: 1 : switch (cfg->flow_ctrl) {
449 : 1 : case UART_CFG_FLOW_CTRL_NONE:
450 : 1 : uarte_cfg.hwfc = NRF_UARTE_HWFC_DISABLED;
451 : 1 : break;
452 : 0 : case UART_CFG_FLOW_CTRL_RTS_CTS:
453 : 0 : uarte_cfg.hwfc = NRF_UARTE_HWFC_ENABLED;
454 : 0 : break;
455 : 0 : default:
456 : 0 : return -ENOTSUP;
457 : : }
458 : :
459 : : #if defined(UARTE_CONFIG_PARITYTYPE_Msk)
460 : 1 : uarte_cfg.paritytype = NRF_UARTE_PARITYTYPE_EVEN;
461 : : #endif
462 [ + - - - ]: 1 : switch (cfg->parity) {
463 : 1 : case UART_CFG_PARITY_NONE:
464 : 1 : uarte_cfg.parity = NRF_UARTE_PARITY_EXCLUDED;
465 : 1 : break;
466 : 0 : case UART_CFG_PARITY_EVEN:
467 : 0 : uarte_cfg.parity = NRF_UARTE_PARITY_INCLUDED;
468 : 0 : break;
469 : : #if defined(UARTE_CONFIG_PARITYTYPE_Msk)
470 : 0 : case UART_CFG_PARITY_ODD:
471 : 0 : uarte_cfg.parity = NRF_UARTE_PARITY_INCLUDED;
472 : 0 : uarte_cfg.paritytype = NRF_UARTE_PARITYTYPE_ODD;
473 : 0 : break;
474 : : #endif
475 : 0 : default:
476 : 0 : return -ENOTSUP;
477 : : }
478 : :
479 [ - + ]: 1 : if (baudrate_set(dev, cfg->baudrate) != 0) {
480 : 0 : return -ENOTSUP;
481 : : }
482 : :
483 : 1 : nrf_uarte_configure(get_uarte_instance(dev), &uarte_cfg);
484 : :
485 : 1 : data->uart_config = *cfg;
486 : :
487 : 1 : return 0;
488 : : }
489 : :
490 : : #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
491 : 0 : static int uarte_nrfx_config_get(const struct device *dev,
492 : : struct uart_config *cfg)
493 : : {
494 : 0 : struct uarte_nrfx_data *data = dev->data;
495 : :
496 : 0 : *cfg = data->uart_config;
497 : 0 : return 0;
498 : : }
499 : : #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
500 : :
501 : :
502 : 0 : static int uarte_nrfx_err_check(const struct device *dev)
503 : : {
504 : 0 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
505 : : /* register bitfields maps to the defines in uart.h */
506 : 0 : return nrf_uarte_errorsrc_get_and_clear(uarte);
507 : : }
508 : :
509 : : /* Function returns true if new transfer can be started. Since TXSTOPPED
510 : : * (and ENDTX) is cleared before triggering new transfer, TX is ready for new
511 : : * transfer if any event is set.
512 : : */
513 : 1522480 : static bool is_tx_ready(const struct device *dev)
514 : : {
515 : 1522480 : const struct uarte_nrfx_config *config = dev->config;
516 : 1522480 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
517 : 1522480 : bool ppi_endtx = config->flags & UARTE_CFG_FLAG_PPI_ENDTX;
518 : :
519 [ + + - - ]: 2662640 : return nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED) ||
520 [ - + ]: 1140160 : (!ppi_endtx ?
521 : 0 : nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDTX) : 0);
522 : : }
523 : :
524 : : /* Wait until the transmitter is in the idle state. When this function returns,
525 : : * IRQ's are locked with the returned key.
526 : : */
527 : 191156 : static int wait_tx_ready(const struct device *dev)
528 : : {
529 : : int key;
530 : :
531 : 0 : do {
532 : : /* wait arbitrary time before back off. */
533 : : bool res;
534 : :
535 [ + + + - ]: 1331320 : NRFX_WAIT_FOR(is_tx_ready(dev), 100, 1, res);
536 : :
537 [ + - ]: 191156 : if (res) {
538 : 191156 : key = irq_lock();
539 [ + - ]: 191156 : if (is_tx_ready(dev)) {
540 : 191156 : break;
541 : : }
542 : :
543 : 0 : irq_unlock(key);
544 : : }
545 : : if (IS_ENABLED(CONFIG_MULTITHREADING)) {
546 : 0 : k_msleep(1);
547 : : }
548 : : } while (1);
549 : :
550 : 191156 : return key;
551 : : }
552 : :
553 : : #ifdef UARTE_ANY_ASYNC
554 : :
555 : : /* Using Macro instead of static inline function to handle NO_OPTIMIZATIONS case
556 : : * where static inline fails on linking.
557 : : */
558 : : #define HW_RX_COUNTING_ENABLED(data) \
559 : : (IS_ENABLED(CONFIG_UARTE_NRF_HW_ASYNC) ? data->async->hw_rx_counting : false)
560 : :
561 : : #endif /* UARTE_ANY_ASYNC */
562 : :
563 : 0 : static void uarte_enable(const struct device *dev, uint32_t mask)
564 : : {
565 : : #ifdef UARTE_ANY_ASYNC
566 : : const struct uarte_nrfx_config *config = dev->config;
567 : : struct uarte_nrfx_data *data = dev->data;
568 : :
569 : : if (data->async) {
570 : : bool disabled = data->async->low_power_mask == 0;
571 : :
572 : : data->async->low_power_mask |= mask;
573 : : if (HW_RX_COUNTING_ENABLED(data) && disabled) {
574 : : const nrfx_timer_t *timer = &config->timer;
575 : :
576 : : nrfx_timer_enable(timer);
577 : :
578 : : for (int i = 0; i < data->async->rx_flush_cnt; i++) {
579 : : nrfx_timer_increment(timer);
580 : : }
581 : : }
582 : : }
583 : : #endif
584 : 0 : nrf_uarte_enable(get_uarte_instance(dev));
585 : 0 : }
586 : :
587 : : /* At this point we should have irq locked and any previous transfer completed.
588 : : * Transfer can be started, no need to wait for completion.
589 : : */
590 : 191156 : static void tx_start(const struct device *dev, const uint8_t *buf, size_t len)
591 : : {
592 : 191156 : const struct uarte_nrfx_config *config = dev->config;
593 : 191156 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
594 : :
595 : : #if CONFIG_PM_DEVICE
596 : : enum pm_device_state state;
597 : :
598 : : (void)pm_device_state_get(dev, &state);
599 : : if (state != PM_DEVICE_STATE_ACTIVE) {
600 : : return;
601 : : }
602 : : #endif
603 : 191156 : nrf_uarte_tx_buffer_set(uarte, buf, len);
604 : 191156 : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDTX);
605 : 191156 : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_TXSTOPPED);
606 : :
607 [ - + ]: 191156 : if (config->flags & UARTE_CFG_FLAG_LOW_POWER) {
608 : 0 : uarte_enable(dev, UARTE_LOW_POWER_TX);
609 : 0 : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
610 : : }
611 : :
612 : 191156 : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTTX);
613 : 191156 : }
614 : :
615 : : #if defined(UARTE_ANY_ASYNC) || defined(CONFIG_PM_DEVICE)
616 : : static void uart_disable(const struct device *dev)
617 : : {
618 : : #ifdef UARTE_ANY_ASYNC
619 : : const struct uarte_nrfx_config *config = dev->config;
620 : : struct uarte_nrfx_data *data = dev->data;
621 : :
622 : : if (data->async && HW_RX_COUNTING_ENABLED(data)) {
623 : : nrfx_timer_disable(&config->timer);
624 : : /* Timer/counter value is reset when disabled. */
625 : : data->async->rx_total_byte_cnt = 0;
626 : : data->async->rx_total_user_byte_cnt = 0;
627 : : }
628 : : #endif
629 : :
630 : : nrf_uarte_disable(get_uarte_instance(dev));
631 : : }
632 : : #endif
633 : :
634 : : #ifdef UARTE_ANY_ASYNC
635 : :
636 : : static void timer_handler(nrf_timer_event_t event_type, void *p_context) { }
637 : : static void rx_timeout(struct k_timer *timer);
638 : : static void tx_timeout(struct k_timer *timer);
639 : :
640 : : static int uarte_nrfx_rx_counting_init(const struct device *dev)
641 : : {
642 : : struct uarte_nrfx_data *data = dev->data;
643 : : const struct uarte_nrfx_config *cfg = dev->config;
644 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
645 : : int ret;
646 : :
647 : : if (HW_RX_COUNTING_ENABLED(data)) {
648 : : nrfx_timer_config_t tmr_config = NRFX_TIMER_DEFAULT_CONFIG;
649 : :
650 : : tmr_config.mode = NRF_TIMER_MODE_COUNTER;
651 : : tmr_config.bit_width = NRF_TIMER_BIT_WIDTH_32;
652 : : ret = nrfx_timer_init(&cfg->timer,
653 : : &tmr_config,
654 : : timer_handler);
655 : : if (ret != NRFX_SUCCESS) {
656 : : LOG_ERR("Timer already initialized, "
657 : : "switching to software byte counting.");
658 : : data->async->hw_rx_counting = false;
659 : : } else {
660 : : nrfx_timer_enable(&cfg->timer);
661 : : nrfx_timer_clear(&cfg->timer);
662 : : }
663 : : }
664 : :
665 : : if (HW_RX_COUNTING_ENABLED(data)) {
666 : : ret = gppi_channel_alloc(&data->async->rx_cnt.ppi);
667 : : if (ret != NRFX_SUCCESS) {
668 : : LOG_ERR("Failed to allocate PPI Channel, "
669 : : "switching to software byte counting.");
670 : : data->async->hw_rx_counting = false;
671 : : nrfx_timer_uninit(&cfg->timer);
672 : : }
673 : : }
674 : :
675 : : if (HW_RX_COUNTING_ENABLED(data)) {
676 : : #if CONFIG_HAS_HW_NRF_PPI
677 : : ret = nrfx_ppi_channel_assign(
678 : : data->async->rx_cnt.ppi,
679 : : nrf_uarte_event_address_get(uarte,
680 : : NRF_UARTE_EVENT_RXDRDY),
681 : : nrfx_timer_task_address_get(&cfg->timer,
682 : : NRF_TIMER_TASK_COUNT));
683 : :
684 : : if (ret != NRFX_SUCCESS) {
685 : : return -EIO;
686 : : }
687 : : #else
688 : : nrf_uarte_publish_set(uarte,
689 : : NRF_UARTE_EVENT_RXDRDY,
690 : : data->async->rx_cnt.ppi);
691 : : nrf_timer_subscribe_set(cfg->timer.p_reg,
692 : : NRF_TIMER_TASK_COUNT,
693 : : data->async->rx_cnt.ppi);
694 : :
695 : : #endif
696 : : ret = gppi_channel_enable(data->async->rx_cnt.ppi);
697 : : if (ret != NRFX_SUCCESS) {
698 : : return -EIO;
699 : : }
700 : : } else {
701 : : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_RXDRDY_MASK);
702 : : }
703 : :
704 : : return 0;
705 : : }
706 : :
707 : : static int uarte_nrfx_init(const struct device *dev)
708 : : {
709 : : struct uarte_nrfx_data *data = dev->data;
710 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
711 : :
712 : : int ret = uarte_nrfx_rx_counting_init(dev);
713 : :
714 : : if (ret != 0) {
715 : : return ret;
716 : : }
717 : :
718 : : data->async->low_power_mask = UARTE_LOW_POWER_TX;
719 : : nrf_uarte_int_enable(uarte,
720 : : NRF_UARTE_INT_ENDRX_MASK |
721 : : NRF_UARTE_INT_RXSTARTED_MASK |
722 : : NRF_UARTE_INT_ERROR_MASK |
723 : : NRF_UARTE_INT_RXTO_MASK);
724 : : nrf_uarte_enable(uarte);
725 : :
726 : : /**
727 : : * Stop any currently running RX operations. This can occur when a
728 : : * bootloader sets up the UART hardware and does not clean it up
729 : : * before jumping to the next application.
730 : : */
731 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXSTARTED)) {
732 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPRX);
733 : : while (!nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXTO) &&
734 : : !nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ERROR)) {
735 : : /* Busy wait for event to register */
736 : : }
737 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXSTARTED);
738 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
739 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXTO);
740 : : }
741 : :
742 : : k_timer_init(&data->async->rx_timeout_timer, rx_timeout, NULL);
743 : : k_timer_user_data_set(&data->async->rx_timeout_timer, data);
744 : : k_timer_init(&data->async->tx_timeout_timer, tx_timeout, NULL);
745 : : k_timer_user_data_set(&data->async->tx_timeout_timer, data);
746 : :
747 : : return 0;
748 : : }
749 : :
750 : : /* Attempt to start TX (asynchronous transfer). If hardware is not ready, then pending
751 : : * flag is set. When current poll_out is completed, pending transfer is started.
752 : : * Function must be called with interrupts locked.
753 : : */
754 : : static void start_tx_locked(const struct device *dev, struct uarte_nrfx_data *data)
755 : : {
756 : : if (!is_tx_ready(dev)) {
757 : : /* Active poll out, postpone until it is completed. */
758 : : data->async->pending_tx = true;
759 : : } else {
760 : : data->async->pending_tx = false;
761 : : data->async->tx_amount = -1;
762 : : tx_start(dev, data->async->xfer_buf, data->async->xfer_len);
763 : : }
764 : : }
765 : :
766 : : /* Setup cache buffer (used for sending data outside of RAM memory).
767 : : * During setup data is copied to cache buffer and transfer length is set.
768 : : *
769 : : * @return True if cache was set, false if no more data to put in cache.
770 : : */
771 : : static bool setup_tx_cache(struct uarte_nrfx_data *data)
772 : : {
773 : : size_t remaining = data->async->tx_size - data->async->tx_cache_offset;
774 : :
775 : : if (!remaining) {
776 : : return false;
777 : : }
778 : :
779 : : size_t len = MIN(remaining, sizeof(data->async->tx_cache));
780 : :
781 : : data->async->xfer_len = len;
782 : : data->async->xfer_buf = data->async->tx_cache;
783 : : memcpy(data->async->tx_cache, &data->async->tx_buf[data->async->tx_cache_offset], len);
784 : :
785 : : return true;
786 : : }
787 : :
788 : : static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
789 : : size_t len,
790 : : int32_t timeout)
791 : : {
792 : : struct uarte_nrfx_data *data = dev->data;
793 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
794 : :
795 : : int key = irq_lock();
796 : :
797 : : if (data->async->tx_size) {
798 : : irq_unlock(key);
799 : : return -EBUSY;
800 : : }
801 : :
802 : : data->async->tx_size = len;
803 : : data->async->tx_buf = buf;
804 : : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
805 : :
806 : : if (nrfx_is_in_ram(buf)) {
807 : : data->async->xfer_buf = buf;
808 : : data->async->xfer_len = len;
809 : : } else {
810 : : data->async->tx_cache_offset = 0;
811 : : (void)setup_tx_cache(data);
812 : : }
813 : :
814 : : start_tx_locked(dev, data);
815 : :
816 : : irq_unlock(key);
817 : :
818 : : if (data->uart_config.flow_ctrl == UART_CFG_FLOW_CTRL_RTS_CTS
819 : : && timeout != SYS_FOREVER_US) {
820 : : k_timer_start(&data->async->tx_timeout_timer, K_USEC(timeout),
821 : : K_NO_WAIT);
822 : : }
823 : : return 0;
824 : : }
825 : :
826 : : static int uarte_nrfx_tx_abort(const struct device *dev)
827 : : {
828 : : struct uarte_nrfx_data *data = dev->data;
829 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
830 : :
831 : : if (data->async->tx_buf == NULL) {
832 : : return -EFAULT;
833 : : }
834 : :
835 : : data->async->pending_tx = false;
836 : : k_timer_stop(&data->async->tx_timeout_timer);
837 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPTX);
838 : :
839 : : return 0;
840 : : }
841 : :
842 : : static void user_callback(const struct device *dev, struct uart_event *evt)
843 : : {
844 : : struct uarte_nrfx_data *data = dev->data;
845 : :
846 : : if (data->async->user_callback) {
847 : : data->async->user_callback(dev, evt, data->async->user_data);
848 : : }
849 : : }
850 : :
851 : : static void notify_uart_rx_rdy(const struct device *dev, size_t len)
852 : : {
853 : : struct uarte_nrfx_data *data = dev->data;
854 : : struct uart_event evt = {
855 : : .type = UART_RX_RDY,
856 : : .data.rx.buf = data->async->rx_buf,
857 : : .data.rx.len = len,
858 : : .data.rx.offset = data->async->rx_offset
859 : : };
860 : :
861 : : user_callback(dev, &evt);
862 : : }
863 : :
864 : : static void rx_buf_release(const struct device *dev, uint8_t **buf)
865 : : {
866 : : if (*buf) {
867 : : struct uart_event evt = {
868 : : .type = UART_RX_BUF_RELEASED,
869 : : .data.rx_buf.buf = *buf,
870 : : };
871 : :
872 : : user_callback(dev, &evt);
873 : : *buf = NULL;
874 : : }
875 : : }
876 : :
877 : : static void notify_rx_disable(const struct device *dev)
878 : : {
879 : : struct uart_event evt = {
880 : : .type = UART_RX_DISABLED,
881 : : };
882 : :
883 : : user_callback(dev, (struct uart_event *)&evt);
884 : : }
885 : :
886 : : static int uarte_nrfx_rx_enable(const struct device *dev, uint8_t *buf,
887 : : size_t len,
888 : : int32_t timeout)
889 : : {
890 : : struct uarte_nrfx_data *data = dev->data;
891 : : const struct uarte_nrfx_config *cfg = dev->config;
892 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
893 : :
894 : : if (cfg->disable_rx) {
895 : : __ASSERT(false, "TX only UARTE instance");
896 : : return -ENOTSUP;
897 : : }
898 : :
899 : : if (data->async->rx_enabled) {
900 : : return -EBUSY;
901 : : }
902 : :
903 : : data->async->rx_timeout = timeout;
904 : : /* Set minimum interval to 3 RTC ticks. 3 is used due to RTC limitation
905 : : * which cannot set timeout for next tick. Assuming delay in processing
906 : : * 3 instead of 2 is used. Note that lower value would work in a similar
907 : : * way but timeouts would always occur later than expected, most likely
908 : : * after ~3 ticks.
909 : : */
910 : : data->async->rx_timeout_slab =
911 : : MAX(timeout / RX_TIMEOUT_DIV,
912 : : NRFX_CEIL_DIV(3 * 1000000, CONFIG_SYS_CLOCK_TICKS_PER_SEC));
913 : :
914 : : data->async->rx_buf = buf;
915 : : data->async->rx_buf_len = len;
916 : : data->async->rx_offset = 0;
917 : : data->async->rx_next_buf = NULL;
918 : : data->async->rx_next_buf_len = 0;
919 : :
920 : : if (cfg->flags & UARTE_CFG_FLAG_LOW_POWER) {
921 : : if (data->async->rx_flush_cnt) {
922 : : int cpy_len = MIN(len, data->async->rx_flush_cnt);
923 : :
924 : : memcpy(buf, data->async->rx_flush_buffer, cpy_len);
925 : : buf += cpy_len;
926 : : len -= cpy_len;
927 : :
928 : : /* If flush content filled whole new buffer complete the
929 : : * request and indicate rx being disabled.
930 : : */
931 : : if (!len) {
932 : : data->async->rx_flush_cnt -= cpy_len;
933 : : notify_uart_rx_rdy(dev, cpy_len);
934 : : rx_buf_release(dev, &data->async->rx_buf);
935 : : notify_rx_disable(dev);
936 : : return 0;
937 : : }
938 : : }
939 : : }
940 : :
941 : : nrf_uarte_rx_buffer_set(uarte, buf, len);
942 : :
943 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
944 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXSTARTED);
945 : :
946 : : data->async->rx_enabled = true;
947 : : if (cfg->flags & UARTE_CFG_FLAG_LOW_POWER) {
948 : : int key = irq_lock();
949 : :
950 : : uarte_enable(dev, UARTE_LOW_POWER_RX);
951 : : irq_unlock(key);
952 : : }
953 : :
954 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
955 : :
956 : : return 0;
957 : : }
958 : :
959 : : static int uarte_nrfx_rx_buf_rsp(const struct device *dev, uint8_t *buf,
960 : : size_t len)
961 : : {
962 : : struct uarte_nrfx_data *data = dev->data;
963 : : int err;
964 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
965 : : int key = irq_lock();
966 : :
967 : : if (data->async->rx_buf == NULL) {
968 : : err = -EACCES;
969 : : } else if (data->async->rx_next_buf == NULL) {
970 : : data->async->rx_next_buf = buf;
971 : : data->async->rx_next_buf_len = len;
972 : : nrf_uarte_rx_buffer_set(uarte, buf, len);
973 : : nrf_uarte_shorts_enable(uarte, NRF_UARTE_SHORT_ENDRX_STARTRX);
974 : : err = 0;
975 : : } else {
976 : : err = -EBUSY;
977 : : }
978 : :
979 : : irq_unlock(key);
980 : :
981 : : return err;
982 : : }
983 : :
984 : : static int uarte_nrfx_callback_set(const struct device *dev,
985 : : uart_callback_t callback,
986 : : void *user_data)
987 : : {
988 : : struct uarte_nrfx_data *data = dev->data;
989 : :
990 : : if (!data->async) {
991 : : return -ENOTSUP;
992 : : }
993 : :
994 : : data->async->user_callback = callback;
995 : : data->async->user_data = user_data;
996 : :
997 : : return 0;
998 : : }
999 : :
1000 : : static int uarte_nrfx_rx_disable(const struct device *dev)
1001 : : {
1002 : : struct uarte_nrfx_data *data = dev->data;
1003 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1004 : :
1005 : : if (data->async->rx_buf == NULL) {
1006 : : return -EFAULT;
1007 : : }
1008 : : if (data->async->rx_next_buf != NULL) {
1009 : : nrf_uarte_shorts_disable(uarte, NRF_UARTE_SHORT_ENDRX_STARTRX);
1010 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXSTARTED);
1011 : : }
1012 : :
1013 : : k_timer_stop(&data->async->rx_timeout_timer);
1014 : : data->async->rx_enabled = false;
1015 : :
1016 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPRX);
1017 : :
1018 : : return 0;
1019 : : }
1020 : :
1021 : : static void tx_timeout(struct k_timer *timer)
1022 : : {
1023 : : struct uarte_nrfx_data *data = k_timer_user_data_get(timer);
1024 : : (void) uarte_nrfx_tx_abort(data->dev);
1025 : : }
1026 : :
1027 : : /**
1028 : : * Whole timeout is divided by RX_TIMEOUT_DIV into smaller units, rx_timeout
1029 : : * is executed periodically every rx_timeout_slab us. If between executions
1030 : : * data was received, then we start counting down time from start, if not, then
1031 : : * we subtract rx_timeout_slab from rx_timeout_left.
1032 : : * If rx_timeout_left is less than rx_timeout_slab it means that receiving has
1033 : : * timed out and we should tell user about that.
1034 : : */
1035 : : static void rx_timeout(struct k_timer *timer)
1036 : : {
1037 : : struct uarte_nrfx_data *data = k_timer_user_data_get(timer);
1038 : : const struct device *dev = data->dev;
1039 : : const struct uarte_nrfx_config *cfg = dev->config;
1040 : : uint32_t read;
1041 : :
1042 : : if (data->async->is_in_irq) {
1043 : : return;
1044 : : }
1045 : :
1046 : : /* Disable ENDRX ISR, in case ENDRX event is generated, it will be
1047 : : * handled after rx_timeout routine is complete.
1048 : : */
1049 : : nrf_uarte_int_disable(get_uarte_instance(dev),
1050 : : NRF_UARTE_INT_ENDRX_MASK);
1051 : :
1052 : : if (HW_RX_COUNTING_ENABLED(data)) {
1053 : : read = nrfx_timer_capture(&cfg->timer, 0);
1054 : : } else {
1055 : : read = data->async->rx_cnt.cnt;
1056 : : }
1057 : :
1058 : : /* Check if data was received since last function call */
1059 : : if (read != data->async->rx_total_byte_cnt) {
1060 : : data->async->rx_total_byte_cnt = read;
1061 : : data->async->rx_timeout_left = data->async->rx_timeout;
1062 : : }
1063 : :
1064 : : /* Check if there is data that was not sent to user yet
1065 : : * Note though that 'len' is a count of data bytes received, but not
1066 : : * necessarily the amount available in the current buffer
1067 : : */
1068 : : int32_t len = data->async->rx_total_byte_cnt
1069 : : - data->async->rx_total_user_byte_cnt;
1070 : :
1071 : : if (!HW_RX_COUNTING_ENABLED(data) &&
1072 : : (len < 0)) {
1073 : : /* Prevent too low value of rx_cnt.cnt which may occur due to
1074 : : * latencies in handling of the RXRDY interrupt.
1075 : : * At this point, the number of received bytes is at least
1076 : : * equal to what was reported to the user.
1077 : : */
1078 : : data->async->rx_cnt.cnt = data->async->rx_total_user_byte_cnt;
1079 : : len = 0;
1080 : : }
1081 : :
1082 : : /* Check for current buffer being full.
1083 : : * if the UART receives characters before the ENDRX is handled
1084 : : * and the 'next' buffer is set up, then the SHORT between ENDRX and
1085 : : * STARTRX will mean that data will be going into to the 'next' buffer
1086 : : * until the ENDRX event gets a chance to be handled.
1087 : : */
1088 : : bool clipped = false;
1089 : :
1090 : : if (len + data->async->rx_offset > data->async->rx_buf_len) {
1091 : : len = data->async->rx_buf_len - data->async->rx_offset;
1092 : : clipped = true;
1093 : : }
1094 : :
1095 : : if (len > 0) {
1096 : : if (clipped ||
1097 : : (data->async->rx_timeout_left
1098 : : < data->async->rx_timeout_slab)) {
1099 : : /* rx_timeout us elapsed since last receiving */
1100 : : notify_uart_rx_rdy(dev, len);
1101 : : data->async->rx_offset += len;
1102 : : data->async->rx_total_user_byte_cnt += len;
1103 : : } else {
1104 : : data->async->rx_timeout_left -=
1105 : : data->async->rx_timeout_slab;
1106 : : }
1107 : :
1108 : : /* If there's nothing left to report until the buffers are
1109 : : * switched then the timer can be stopped
1110 : : */
1111 : : if (clipped) {
1112 : : k_timer_stop(&data->async->rx_timeout_timer);
1113 : : }
1114 : : }
1115 : :
1116 : : nrf_uarte_int_enable(get_uarte_instance(dev),
1117 : : NRF_UARTE_INT_ENDRX_MASK);
1118 : :
1119 : : }
1120 : :
1121 : : #define UARTE_ERROR_FROM_MASK(mask) \
1122 : : ((mask) & NRF_UARTE_ERROR_OVERRUN_MASK ? UART_ERROR_OVERRUN \
1123 : : : (mask) & NRF_UARTE_ERROR_PARITY_MASK ? UART_ERROR_PARITY \
1124 : : : (mask) & NRF_UARTE_ERROR_FRAMING_MASK ? UART_ERROR_FRAMING \
1125 : : : (mask) & NRF_UARTE_ERROR_BREAK_MASK ? UART_BREAK \
1126 : : : 0)
1127 : :
1128 : : static void error_isr(const struct device *dev)
1129 : : {
1130 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1131 : : uint32_t err = nrf_uarte_errorsrc_get_and_clear(uarte);
1132 : : struct uart_event evt = {
1133 : : .type = UART_RX_STOPPED,
1134 : : .data.rx_stop.reason = UARTE_ERROR_FROM_MASK(err),
1135 : : };
1136 : : user_callback(dev, &evt);
1137 : : (void) uarte_nrfx_rx_disable(dev);
1138 : : }
1139 : :
1140 : : static void rxstarted_isr(const struct device *dev)
1141 : : {
1142 : : struct uarte_nrfx_data *data = dev->data;
1143 : : struct uart_event evt = {
1144 : : .type = UART_RX_BUF_REQUEST,
1145 : : };
1146 : : user_callback(dev, &evt);
1147 : : if (data->async->rx_timeout != SYS_FOREVER_US) {
1148 : : data->async->rx_timeout_left = data->async->rx_timeout;
1149 : : k_timer_start(&data->async->rx_timeout_timer,
1150 : : K_USEC(data->async->rx_timeout_slab),
1151 : : K_USEC(data->async->rx_timeout_slab));
1152 : : }
1153 : : }
1154 : :
1155 : : static void endrx_isr(const struct device *dev)
1156 : : {
1157 : : struct uarte_nrfx_data *data = dev->data;
1158 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1159 : :
1160 : : data->async->is_in_irq = true;
1161 : :
1162 : : /* ensure rx timer is stopped - it will be restarted in RXSTARTED
1163 : : * handler if needed
1164 : : */
1165 : : k_timer_stop(&data->async->rx_timeout_timer);
1166 : :
1167 : : /* this is the amount that the EasyDMA controller has copied into the
1168 : : * buffer
1169 : : */
1170 : : const int rx_amount = nrf_uarte_rx_amount_get(uarte) +
1171 : : data->async->rx_flush_cnt;
1172 : :
1173 : : data->async->rx_flush_cnt = 0;
1174 : :
1175 : : /* The 'rx_offset' can be bigger than 'rx_amount', so it the length
1176 : : * of data we report back the the user may need to be clipped.
1177 : : * This can happen because the 'rx_offset' count derives from RXRDY
1178 : : * events, which can occur already for the next buffer before we are
1179 : : * here to handle this buffer. (The next buffer is now already active
1180 : : * because of the ENDRX_STARTRX shortcut)
1181 : : */
1182 : : int rx_len = rx_amount - data->async->rx_offset;
1183 : :
1184 : : if (rx_len < 0) {
1185 : : rx_len = 0;
1186 : : }
1187 : :
1188 : : data->async->rx_total_user_byte_cnt += rx_len;
1189 : :
1190 : : /* Only send the RX_RDY event if there is something to send */
1191 : : if (rx_len > 0) {
1192 : : notify_uart_rx_rdy(dev, rx_len);
1193 : : }
1194 : :
1195 : : if (!data->async->rx_enabled) {
1196 : : data->async->is_in_irq = false;
1197 : : return;
1198 : : }
1199 : :
1200 : : rx_buf_release(dev, &data->async->rx_buf);
1201 : :
1202 : : /* If there is a next buffer, then STARTRX will have already been
1203 : : * invoked by the short (the next buffer will be filling up already)
1204 : : * and here we just do the swap of which buffer the driver is following,
1205 : : * the next rx_timeout() will update the rx_offset.
1206 : : */
1207 : : int key = irq_lock();
1208 : :
1209 : : if (data->async->rx_next_buf) {
1210 : : data->async->rx_buf = data->async->rx_next_buf;
1211 : : data->async->rx_buf_len = data->async->rx_next_buf_len;
1212 : : data->async->rx_next_buf = NULL;
1213 : : data->async->rx_next_buf_len = 0;
1214 : :
1215 : : data->async->rx_offset = 0;
1216 : : /* Check is based on assumption that ISR handler handles
1217 : : * ENDRX before RXSTARTED so if short was set on time, RXSTARTED
1218 : : * event will be set.
1219 : : */
1220 : : if (!nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXSTARTED)) {
1221 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
1222 : : }
1223 : : /* Remove the short until the subsequent next buffer is setup */
1224 : : nrf_uarte_shorts_disable(uarte, NRF_UARTE_SHORT_ENDRX_STARTRX);
1225 : : } else {
1226 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPRX);
1227 : : }
1228 : :
1229 : : irq_unlock(key);
1230 : :
1231 : : data->async->is_in_irq = false;
1232 : : }
1233 : :
1234 : : /* Function for flushing internal RX fifo. Function can be called in case
1235 : : * flushed data is discarded or when data is valid and needs to be retrieved.
1236 : : *
1237 : : * However, UARTE does not update RXAMOUNT register if fifo is empty. Old value
1238 : : * remains. In certain cases it makes it impossible to distinguish between
1239 : : * case when fifo was empty and not. Function is trying to minimize chances of
1240 : : * error with following measures:
1241 : : * - RXAMOUNT is read before flushing and compared against value after flushing
1242 : : * if they differ it indicates that data was flushed
1243 : : * - user buffer is dirtied and if RXAMOUNT did not changed it is checked if
1244 : : * it is still dirty. If not then it indicates that data was flushed
1245 : : *
1246 : : * In other cases function indicates that fifo was empty. It means that if
1247 : : * number of bytes in the fifo equal last rx transfer length and data is equal
1248 : : * to dirty marker it will be discarded.
1249 : : *
1250 : : * @param dev Device.
1251 : : * @param buf Buffer for flushed data, null indicates that flushed data can be
1252 : : * dropped.
1253 : : * @param len Buffer size, not used if @p buf is null.
1254 : : *
1255 : : * @return number of bytes flushed from the fifo.
1256 : : */
1257 : : static uint8_t rx_flush(const struct device *dev, uint8_t *buf, uint32_t len)
1258 : : {
1259 : : /* Flushing RX fifo requires buffer bigger than 4 bytes to empty fifo*/
1260 : : static const uint8_t dirty;
1261 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1262 : : uint32_t prev_rx_amount = nrf_uarte_rx_amount_get(uarte);
1263 : : uint8_t tmp_buf[UARTE_HW_RX_FIFO_SIZE];
1264 : : uint8_t *flush_buf = buf ? buf : tmp_buf;
1265 : : size_t flush_len = buf ? len : sizeof(tmp_buf);
1266 : :
1267 : : if (buf) {
1268 : : memset(buf, dirty, len);
1269 : : flush_buf = buf;
1270 : : flush_len = len;
1271 : : } else {
1272 : : flush_buf = tmp_buf;
1273 : : flush_len = sizeof(tmp_buf);
1274 : : }
1275 : :
1276 : : nrf_uarte_rx_buffer_set(uarte, flush_buf, flush_len);
1277 : : /* Final part of handling RXTO event is in ENDRX interrupt
1278 : : * handler. ENDRX is generated as a result of FLUSHRX task.
1279 : : */
1280 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1281 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_FLUSHRX);
1282 : : while (!nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX)) {
1283 : : /* empty */
1284 : : }
1285 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1286 : :
1287 : : if (!buf) {
1288 : : return nrf_uarte_rx_amount_get(uarte);
1289 : : }
1290 : :
1291 : : uint32_t rx_amount = nrf_uarte_rx_amount_get(uarte);
1292 : :
1293 : : if (rx_amount != prev_rx_amount) {
1294 : : return rx_amount;
1295 : : }
1296 : :
1297 : : for (int i = 0; i < flush_len; i++) {
1298 : : if (buf[i] != dirty) {
1299 : : return rx_amount;
1300 : : }
1301 : : }
1302 : :
1303 : : return 0;
1304 : : }
1305 : :
1306 : : static void async_uart_release(const struct device *dev, uint32_t dir_mask)
1307 : : {
1308 : : struct uarte_nrfx_data *data = dev->data;
1309 : : int key = irq_lock();
1310 : :
1311 : : data->async->low_power_mask &= ~dir_mask;
1312 : : if (!data->async->low_power_mask) {
1313 : : if (dir_mask == UARTE_LOW_POWER_RX) {
1314 : : data->async->rx_flush_cnt =
1315 : : rx_flush(dev, data->async->rx_flush_buffer,
1316 : : sizeof(data->async->rx_flush_buffer));
1317 : : }
1318 : :
1319 : : uart_disable(dev);
1320 : : }
1321 : :
1322 : : irq_unlock(key);
1323 : : }
1324 : :
1325 : : /* This handler is called when the receiver is stopped. If rx was aborted
1326 : : * data from fifo is flushed.
1327 : : */
1328 : : static void rxto_isr(const struct device *dev)
1329 : : {
1330 : : const struct uarte_nrfx_config *config = dev->config;
1331 : : struct uarte_nrfx_data *data = dev->data;
1332 : :
1333 : : rx_buf_release(dev, &data->async->rx_buf);
1334 : : rx_buf_release(dev, &data->async->rx_next_buf);
1335 : :
1336 : : /* If the rx_enabled flag is still set at this point, it means that
1337 : : * RX is being disabled because all provided RX buffers have been
1338 : : * filled up. Clear the flag then, so that RX can be enabled again.
1339 : : *
1340 : : * If the flag is already cleared, it means that RX was aborted by
1341 : : * a call to uart_rx_disable() and data from FIFO should be discarded.
1342 : : */
1343 : : if (data->async->rx_enabled) {
1344 : : data->async->rx_enabled = false;
1345 : : } else {
1346 : : (void)rx_flush(dev, NULL, 0);
1347 : : }
1348 : :
1349 : : if (config->flags & UARTE_CFG_FLAG_LOW_POWER) {
1350 : : async_uart_release(dev, UARTE_LOW_POWER_RX);
1351 : : }
1352 : :
1353 : : notify_rx_disable(dev);
1354 : : }
1355 : :
1356 : : static void txstopped_isr(const struct device *dev)
1357 : : {
1358 : : const struct uarte_nrfx_config *config = dev->config;
1359 : : struct uarte_nrfx_data *data = dev->data;
1360 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1361 : : int key;
1362 : :
1363 : : if (config->flags & UARTE_CFG_FLAG_LOW_POWER) {
1364 : : nrf_uarte_int_disable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
1365 : : async_uart_release(dev, UARTE_LOW_POWER_TX);
1366 : :
1367 : : if (!data->async->tx_size) {
1368 : : return;
1369 : : }
1370 : : }
1371 : :
1372 : : if (!data->async->tx_buf) {
1373 : : return;
1374 : : }
1375 : :
1376 : : key = irq_lock();
1377 : : size_t amount = (data->async->tx_amount >= 0) ?
1378 : : data->async->tx_amount : nrf_uarte_tx_amount_get(uarte);
1379 : :
1380 : : irq_unlock(key);
1381 : :
1382 : : /* If there is a pending tx request, it means that uart_tx()
1383 : : * was called when there was ongoing uart_poll_out. Handling
1384 : : * TXSTOPPED interrupt means that uart_poll_out has completed.
1385 : : */
1386 : : if (data->async->pending_tx) {
1387 : : key = irq_lock();
1388 : : start_tx_locked(dev, data);
1389 : : irq_unlock(key);
1390 : : return;
1391 : : }
1392 : :
1393 : : /* Cache buffer is used because tx_buf wasn't in RAM. */
1394 : : if (data->async->tx_buf != data->async->xfer_buf) {
1395 : : /* In that case setup next chunk. If that was the last chunk
1396 : : * fall back to reporting TX_DONE.
1397 : : */
1398 : : if (amount == data->async->xfer_len) {
1399 : : data->async->tx_cache_offset += amount;
1400 : : if (setup_tx_cache(data)) {
1401 : : key = irq_lock();
1402 : : start_tx_locked(dev, data);
1403 : : irq_unlock(key);
1404 : : return;
1405 : : }
1406 : :
1407 : : /* Amount is already included in tx_cache_offset. */
1408 : : amount = data->async->tx_cache_offset;
1409 : : } else {
1410 : : /* TX was aborted, include tx_cache_offset in amount. */
1411 : : amount += data->async->tx_cache_offset;
1412 : : }
1413 : : }
1414 : :
1415 : : k_timer_stop(&data->async->tx_timeout_timer);
1416 : :
1417 : : struct uart_event evt = {
1418 : : .data.tx.buf = data->async->tx_buf,
1419 : : .data.tx.len = amount,
1420 : : };
1421 : : if (amount == data->async->tx_size) {
1422 : : evt.type = UART_TX_DONE;
1423 : : } else {
1424 : : evt.type = UART_TX_ABORTED;
1425 : : }
1426 : :
1427 : : nrf_uarte_int_disable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
1428 : : data->async->tx_buf = NULL;
1429 : : data->async->tx_size = 0;
1430 : :
1431 : : user_callback(dev, &evt);
1432 : : }
1433 : :
1434 : : static void uarte_nrfx_isr_async(const struct device *dev)
1435 : : {
1436 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1437 : : struct uarte_nrfx_data *data = dev->data;
1438 : :
1439 : : if (!HW_RX_COUNTING_ENABLED(data)
1440 : : && nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXDRDY)) {
1441 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXDRDY);
1442 : : data->async->rx_cnt.cnt++;
1443 : : return;
1444 : : }
1445 : :
1446 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ERROR)) {
1447 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ERROR);
1448 : : error_isr(dev);
1449 : : }
1450 : :
1451 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX)
1452 : : && nrf_uarte_int_enable_check(uarte, NRF_UARTE_INT_ENDRX_MASK)) {
1453 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1454 : : endrx_isr(dev);
1455 : : }
1456 : :
1457 : : /* RXSTARTED must be handled after ENDRX because it starts the RX timeout
1458 : : * and if order is swapped then ENDRX will stop this timeout.
1459 : : * Skip if ENDRX is set when RXSTARTED is set. It means that
1460 : : * ENDRX occurred after check for ENDRX in isr which may happen when
1461 : : * UARTE interrupt got preempted. Events are not cleared
1462 : : * and isr will be called again. ENDRX will be handled first.
1463 : : */
1464 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXSTARTED) &&
1465 : : !nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX)) {
1466 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXSTARTED);
1467 : : rxstarted_isr(dev);
1468 : : }
1469 : :
1470 : : /* RXTO must be handled after ENDRX which should notify the buffer.
1471 : : * Skip if ENDRX is set when RXTO is set. It means that
1472 : : * ENDRX occurred after check for ENDRX in isr which may happen when
1473 : : * UARTE interrupt got preempted. Events are not cleared
1474 : : * and isr will be called again. ENDRX will be handled first.
1475 : : */
1476 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXTO) &&
1477 : : !nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX)) {
1478 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXTO);
1479 : : rxto_isr(dev);
1480 : : }
1481 : :
1482 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDTX)
1483 : : && nrf_uarte_int_enable_check(uarte, NRF_UARTE_INT_ENDTX_MASK)) {
1484 : : endtx_isr(dev);
1485 : : }
1486 : :
1487 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED)
1488 : : && nrf_uarte_int_enable_check(uarte,
1489 : : NRF_UARTE_INT_TXSTOPPED_MASK)) {
1490 : : txstopped_isr(dev);
1491 : : }
1492 : : }
1493 : :
1494 : : #endif /* UARTE_ANY_ASYNC */
1495 : :
1496 : : /**
1497 : : * @brief Poll the device for input.
1498 : : *
1499 : : * @param dev UARTE device struct
1500 : : * @param c Pointer to character
1501 : : *
1502 : : * @return 0 if a character arrived, -1 if the input buffer is empty.
1503 : : */
1504 : 0 : static int uarte_nrfx_poll_in(const struct device *dev, unsigned char *c)
1505 : : {
1506 : :
1507 : 0 : const struct uarte_nrfx_data *data = dev->data;
1508 : 0 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1509 : :
1510 : : #ifdef UARTE_ANY_ASYNC
1511 : : if (data->async) {
1512 : : return -ENOTSUP;
1513 : : }
1514 : : #endif
1515 : :
1516 [ # # ]: 0 : if (!nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX)) {
1517 : 0 : return -1;
1518 : : }
1519 : :
1520 : 0 : *c = data->rx_data;
1521 : :
1522 : : /* clear the interrupt */
1523 : 0 : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1524 : 0 : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
1525 : :
1526 : 0 : return 0;
1527 : : }
1528 : :
1529 : : /**
1530 : : * @brief Output a character in polled mode.
1531 : : *
1532 : : * @param dev UARTE device struct
1533 : : * @param c Character to send
1534 : : */
1535 : 191156 : static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
1536 : : {
1537 : 191156 : struct uarte_nrfx_data *data = dev->data;
1538 [ + - - + ]: 191156 : bool isr_mode = k_is_in_isr() || k_is_pre_kernel();
1539 : : int key;
1540 : :
1541 [ - + ]: 191156 : if (isr_mode) {
1542 : : while (1) {
1543 : 0 : key = irq_lock();
1544 [ # # ]: 0 : if (is_tx_ready(dev)) {
1545 : : #if UARTE_ANY_ASYNC
1546 : : if (data->async && data->async->tx_size &&
1547 : : data->async->tx_amount < 0) {
1548 : : data->async->tx_amount =
1549 : : nrf_uarte_tx_amount_get(
1550 : : get_uarte_instance(dev));
1551 : : }
1552 : : #endif
1553 : 0 : break;
1554 : : }
1555 : :
1556 : 0 : irq_unlock(key);
1557 : : }
1558 : : } else {
1559 : 191156 : key = wait_tx_ready(dev);
1560 : : }
1561 : :
1562 : 191156 : data->char_out = c;
1563 : 191156 : tx_start(dev, &data->char_out, 1);
1564 : :
1565 : 191156 : irq_unlock(key);
1566 : 191156 : }
1567 : :
1568 : :
1569 : : #ifdef UARTE_INTERRUPT_DRIVEN
1570 : : /** Interrupt driven FIFO fill function */
1571 : : static int uarte_nrfx_fifo_fill(const struct device *dev,
1572 : : const uint8_t *tx_data,
1573 : : int len)
1574 : : {
1575 : : struct uarte_nrfx_data *data = dev->data;
1576 : :
1577 : : len = MIN(len, data->int_driven->tx_buff_size);
1578 : : if (!atomic_cas(&data->int_driven->fifo_fill_lock, 0, 1)) {
1579 : : return 0;
1580 : : }
1581 : :
1582 : : /* Copy data to RAM buffer for EasyDMA transfer */
1583 : : for (int i = 0; i < len; i++) {
1584 : : data->int_driven->tx_buffer[i] = tx_data[i];
1585 : : }
1586 : :
1587 : : int key = irq_lock();
1588 : :
1589 : : if (!is_tx_ready(dev)) {
1590 : : data->int_driven->fifo_fill_lock = 0;
1591 : : len = 0;
1592 : : } else {
1593 : : tx_start(dev, data->int_driven->tx_buffer, len);
1594 : : }
1595 : :
1596 : : irq_unlock(key);
1597 : :
1598 : : return len;
1599 : : }
1600 : :
1601 : : /** Interrupt driven FIFO read function */
1602 : : static int uarte_nrfx_fifo_read(const struct device *dev,
1603 : : uint8_t *rx_data,
1604 : : const int size)
1605 : : {
1606 : : int num_rx = 0;
1607 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1608 : : const struct uarte_nrfx_data *data = dev->data;
1609 : :
1610 : : if (size > 0 && nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX)) {
1611 : : /* Clear the interrupt */
1612 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1613 : :
1614 : : /* Receive a character */
1615 : : rx_data[num_rx++] = (uint8_t)data->rx_data;
1616 : :
1617 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
1618 : : }
1619 : :
1620 : : return num_rx;
1621 : : }
1622 : :
1623 : : /** Interrupt driven transfer enabling function */
1624 : : static void uarte_nrfx_irq_tx_enable(const struct device *dev)
1625 : : {
1626 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1627 : : struct uarte_nrfx_data *data = dev->data;
1628 : : int key = irq_lock();
1629 : :
1630 : : data->int_driven->disable_tx_irq = false;
1631 : : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
1632 : :
1633 : : irq_unlock(key);
1634 : : }
1635 : :
1636 : : /** Interrupt driven transfer disabling function */
1637 : : static void uarte_nrfx_irq_tx_disable(const struct device *dev)
1638 : : {
1639 : : struct uarte_nrfx_data *data = dev->data;
1640 : : /* TX IRQ will be disabled after current transmission is finished */
1641 : : data->int_driven->disable_tx_irq = true;
1642 : : }
1643 : :
1644 : : /** Interrupt driven transfer ready function */
1645 : : static int uarte_nrfx_irq_tx_ready_complete(const struct device *dev)
1646 : : {
1647 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1648 : : struct uarte_nrfx_data *data = dev->data;
1649 : :
1650 : : /* ENDTX flag is always on so that ISR is called when we enable TX IRQ.
1651 : : * Because of that we have to explicitly check if ENDTX interrupt is
1652 : : * enabled, otherwise this function would always return true no matter
1653 : : * what would be the source of interrupt.
1654 : : */
1655 : : bool ready = !data->int_driven->disable_tx_irq &&
1656 : : nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED) &&
1657 : : nrf_uarte_int_enable_check(uarte,
1658 : : NRF_UARTE_INT_TXSTOPPED_MASK);
1659 : :
1660 : : if (ready) {
1661 : : data->int_driven->fifo_fill_lock = 0;
1662 : : }
1663 : :
1664 : : return ready;
1665 : : }
1666 : :
1667 : : static int uarte_nrfx_irq_rx_ready(const struct device *dev)
1668 : : {
1669 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1670 : :
1671 : : return nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDRX);
1672 : : }
1673 : :
1674 : : /** Interrupt driven receiver enabling function */
1675 : : static void uarte_nrfx_irq_rx_enable(const struct device *dev)
1676 : : {
1677 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1678 : :
1679 : : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_ENDRX_MASK);
1680 : : }
1681 : :
1682 : : /** Interrupt driven receiver disabling function */
1683 : : static void uarte_nrfx_irq_rx_disable(const struct device *dev)
1684 : : {
1685 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1686 : :
1687 : : nrf_uarte_int_disable(uarte, NRF_UARTE_INT_ENDRX_MASK);
1688 : : }
1689 : :
1690 : : /** Interrupt driven error enabling function */
1691 : : static void uarte_nrfx_irq_err_enable(const struct device *dev)
1692 : : {
1693 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1694 : :
1695 : : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_ERROR_MASK);
1696 : : }
1697 : :
1698 : : /** Interrupt driven error disabling function */
1699 : : static void uarte_nrfx_irq_err_disable(const struct device *dev)
1700 : : {
1701 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1702 : :
1703 : : nrf_uarte_int_disable(uarte, NRF_UARTE_INT_ERROR_MASK);
1704 : : }
1705 : :
1706 : : /** Interrupt driven pending status function */
1707 : : static int uarte_nrfx_irq_is_pending(const struct device *dev)
1708 : : {
1709 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1710 : :
1711 : : return ((nrf_uarte_int_enable_check(uarte,
1712 : : NRF_UARTE_INT_TXSTOPPED_MASK) &&
1713 : : uarte_nrfx_irq_tx_ready_complete(dev))
1714 : : ||
1715 : : (nrf_uarte_int_enable_check(uarte,
1716 : : NRF_UARTE_INT_ENDRX_MASK) &&
1717 : : uarte_nrfx_irq_rx_ready(dev)));
1718 : : }
1719 : :
1720 : : /** Interrupt driven interrupt update function */
1721 : : static int uarte_nrfx_irq_update(const struct device *dev)
1722 : : {
1723 : : return 1;
1724 : : }
1725 : :
1726 : : /** Set the callback function */
1727 : : static void uarte_nrfx_irq_callback_set(const struct device *dev,
1728 : : uart_irq_callback_user_data_t cb,
1729 : : void *cb_data)
1730 : : {
1731 : : struct uarte_nrfx_data *data = dev->data;
1732 : :
1733 : : data->int_driven->cb = cb;
1734 : : data->int_driven->cb_data = cb_data;
1735 : : }
1736 : : #endif /* UARTE_INTERRUPT_DRIVEN */
1737 : :
1738 : : static const struct uart_driver_api uart_nrfx_uarte_driver_api = {
1739 : : .poll_in = uarte_nrfx_poll_in,
1740 : : .poll_out = uarte_nrfx_poll_out,
1741 : : .err_check = uarte_nrfx_err_check,
1742 : : #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
1743 : : .configure = uarte_nrfx_configure,
1744 : : .config_get = uarte_nrfx_config_get,
1745 : : #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
1746 : : #ifdef UARTE_ANY_ASYNC
1747 : : .callback_set = uarte_nrfx_callback_set,
1748 : : .tx = uarte_nrfx_tx,
1749 : : .tx_abort = uarte_nrfx_tx_abort,
1750 : : .rx_enable = uarte_nrfx_rx_enable,
1751 : : .rx_buf_rsp = uarte_nrfx_rx_buf_rsp,
1752 : : .rx_disable = uarte_nrfx_rx_disable,
1753 : : #endif /* UARTE_ANY_ASYNC */
1754 : : #ifdef UARTE_INTERRUPT_DRIVEN
1755 : : .fifo_fill = uarte_nrfx_fifo_fill,
1756 : : .fifo_read = uarte_nrfx_fifo_read,
1757 : : .irq_tx_enable = uarte_nrfx_irq_tx_enable,
1758 : : .irq_tx_disable = uarte_nrfx_irq_tx_disable,
1759 : : .irq_tx_ready = uarte_nrfx_irq_tx_ready_complete,
1760 : : .irq_rx_enable = uarte_nrfx_irq_rx_enable,
1761 : : .irq_rx_disable = uarte_nrfx_irq_rx_disable,
1762 : : .irq_tx_complete = uarte_nrfx_irq_tx_ready_complete,
1763 : : .irq_rx_ready = uarte_nrfx_irq_rx_ready,
1764 : : .irq_err_enable = uarte_nrfx_irq_err_enable,
1765 : : .irq_err_disable = uarte_nrfx_irq_err_disable,
1766 : : .irq_is_pending = uarte_nrfx_irq_is_pending,
1767 : : .irq_update = uarte_nrfx_irq_update,
1768 : : .irq_callback_set = uarte_nrfx_irq_callback_set,
1769 : : #endif /* UARTE_INTERRUPT_DRIVEN */
1770 : : };
1771 : :
1772 : 1 : static int endtx_stoptx_ppi_init(NRF_UARTE_Type *uarte,
1773 : : struct uarte_nrfx_data *data)
1774 : : {
1775 : : nrfx_err_t ret;
1776 : :
1777 : 1 : ret = gppi_channel_alloc(&data->ppi_ch_endtx);
1778 [ - + ]: 1 : if (ret != NRFX_SUCCESS) {
1779 [ # # ]: 0 : LOG_ERR("Failed to allocate PPI Channel");
1780 : 0 : return -EIO;
1781 : : }
1782 : :
1783 : 1 : nrfx_gppi_channel_endpoints_setup(data->ppi_ch_endtx,
1784 : : nrf_uarte_event_address_get(uarte, NRF_UARTE_EVENT_ENDTX),
1785 : : nrf_uarte_task_address_get(uarte, NRF_UARTE_TASK_STOPTX));
1786 : 1 : nrfx_gppi_channels_enable(BIT(data->ppi_ch_endtx));
1787 : :
1788 : 1 : return 0;
1789 : : }
1790 : :
1791 : 1 : static int uarte_instance_init(const struct device *dev,
1792 : : uint8_t interrupts_active)
1793 : : {
1794 : : int err;
1795 : 1 : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1796 : 1 : struct uarte_nrfx_data *data = dev->data;
1797 : 1 : const struct uarte_nrfx_config *cfg = dev->config;
1798 : :
1799 : 1 : nrf_uarte_disable(uarte);
1800 : :
1801 : 1 : data->dev = dev;
1802 : :
1803 : : #ifdef CONFIG_PINCTRL
1804 : 1 : err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
1805 [ - + ]: 1 : if (err < 0) {
1806 : 0 : return err;
1807 : : }
1808 : : #else
1809 : : uarte_nrfx_pins_configure(dev, false);
1810 : : #endif /* CONFIG_PINCTRL */
1811 : :
1812 : 1 : err = uarte_nrfx_configure(dev, &data->uart_config);
1813 [ - + ]: 1 : if (err) {
1814 : 0 : return err;
1815 : : }
1816 : :
1817 : 1 : if (IS_ENABLED(CONFIG_UART_ENHANCED_POLL_OUT) &&
1818 [ + - ]: 1 : cfg->flags & UARTE_CFG_FLAG_PPI_ENDTX) {
1819 : 1 : err = endtx_stoptx_ppi_init(uarte, data);
1820 [ - + ]: 1 : if (err < 0) {
1821 : 0 : return err;
1822 : : }
1823 : : }
1824 : :
1825 : :
1826 : : #ifdef UARTE_ANY_ASYNC
1827 : : if (data->async) {
1828 : : err = uarte_nrfx_init(dev);
1829 : : if (err < 0) {
1830 : : return err;
1831 : : }
1832 : : } else
1833 : : #endif
1834 : : {
1835 : : /* Enable receiver and transmitter */
1836 : 1 : nrf_uarte_enable(uarte);
1837 : :
1838 [ + - ]: 1 : if (!cfg->disable_rx) {
1839 : 1 : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1840 : :
1841 : 1 : nrf_uarte_rx_buffer_set(uarte, &data->rx_data, 1);
1842 : 1 : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
1843 : : }
1844 : : }
1845 : :
1846 [ - + ]: 1 : if (!(cfg->flags & UARTE_CFG_FLAG_PPI_ENDTX)) {
1847 : 0 : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_ENDTX_MASK);
1848 : : }
1849 : :
1850 [ - + ]: 1 : if (cfg->flags & UARTE_CFG_FLAG_LOW_POWER) {
1851 : 0 : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
1852 : : }
1853 : :
1854 : : /* Set TXSTOPPED event by requesting fake (zero-length) transfer.
1855 : : * Pointer to RAM variable (data->tx_buffer) is set because otherwise
1856 : : * such operation may result in HardFault or RAM corruption.
1857 : : */
1858 : 1 : nrf_uarte_tx_buffer_set(uarte, &data->char_out, 0);
1859 : 1 : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTTX);
1860 : :
1861 : : /* switch off transmitter to save an energy */
1862 : 1 : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPTX);
1863 : :
1864 : 1 : return 0;
1865 : : }
1866 : :
1867 : : #ifdef CONFIG_PM_DEVICE
1868 : : /** @brief Pend until TX is stopped.
1869 : : *
1870 : : * There are 2 configurations that must be handled:
1871 : : * - ENDTX->TXSTOPPED PPI enabled - just pend until TXSTOPPED event is set
1872 : : * - disable ENDTX interrupt and manually trigger STOPTX, pend for TXSTOPPED
1873 : : */
1874 : : static void wait_for_tx_stopped(const struct device *dev)
1875 : : {
1876 : : const struct uarte_nrfx_config *config = dev->config;
1877 : : bool ppi_endtx = config->flags & UARTE_CFG_FLAG_PPI_ENDTX;
1878 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1879 : : bool res;
1880 : :
1881 : : if (!ppi_endtx) {
1882 : : /* We assume here that it can be called from any context,
1883 : : * including the one that uarte interrupt will not preempt.
1884 : : * Disable endtx interrupt to ensure that it will not be triggered
1885 : : * (if in lower priority context) and stop TX if necessary.
1886 : : */
1887 : : nrf_uarte_int_disable(uarte, NRF_UARTE_INT_ENDTX_MASK);
1888 : : NRFX_WAIT_FOR(is_tx_ready(dev), 1000, 1, res);
1889 : : if (!nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED)) {
1890 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDTX);
1891 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPTX);
1892 : : }
1893 : : }
1894 : :
1895 : : NRFX_WAIT_FOR(nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED),
1896 : : 1000, 1, res);
1897 : :
1898 : : if (!ppi_endtx) {
1899 : : nrf_uarte_int_enable(uarte, NRF_UARTE_INT_ENDTX_MASK);
1900 : : }
1901 : : }
1902 : :
1903 : :
1904 : : static int uarte_nrfx_pm_action(const struct device *dev,
1905 : : enum pm_device_action action)
1906 : : {
1907 : : NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1908 : : #if defined(UARTE_ANY_ASYNC) || defined(UARTE_INTERRUPT_DRIVEN)
1909 : : struct uarte_nrfx_data *data = dev->data;
1910 : : #endif
1911 : : const struct uarte_nrfx_config *cfg = dev->config;
1912 : : #ifdef CONFIG_PINCTRL
1913 : : int ret;
1914 : : #endif
1915 : :
1916 : : switch (action) {
1917 : : case PM_DEVICE_ACTION_RESUME:
1918 : : if (cfg->flags & UARTE_CFG_FLAG_GPIO_MGMT) {
1919 : : #ifdef CONFIG_PINCTRL
1920 : : ret = pinctrl_apply_state(cfg->pcfg,
1921 : : PINCTRL_STATE_DEFAULT);
1922 : : if (ret < 0) {
1923 : : return ret;
1924 : : }
1925 : : #else
1926 : : uarte_nrfx_pins_configure(dev, false);
1927 : : #endif /* CONFIG_PINCTRL */
1928 : : }
1929 : :
1930 : : nrf_uarte_enable(uarte);
1931 : :
1932 : : #ifdef UARTE_ANY_ASYNC
1933 : : if (HW_RX_COUNTING_ENABLED(data)) {
1934 : : nrfx_timer_enable(&cfg->timer);
1935 : : }
1936 : : if (data->async) {
1937 : : return 0;
1938 : : }
1939 : : #endif
1940 : : if (!cfg->disable_rx) {
1941 : :
1942 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1943 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
1944 : : #ifdef UARTE_INTERRUPT_DRIVEN
1945 : : if (data->int_driven &&
1946 : : data->int_driven->rx_irq_enabled) {
1947 : : nrf_uarte_int_enable(uarte,
1948 : : NRF_UARTE_INT_ENDRX_MASK);
1949 : : }
1950 : : #endif
1951 : : }
1952 : : break;
1953 : : case PM_DEVICE_ACTION_SUSPEND:
1954 : : /* Disabling UART requires stopping RX, but stop RX event is
1955 : : * only sent after each RX if async UART API is used.
1956 : : */
1957 : : #ifdef UARTE_ANY_ASYNC
1958 : : if (data->async) {
1959 : : /* Entering inactive state requires device to be no
1960 : : * active asynchronous calls.
1961 : : */
1962 : : __ASSERT_NO_MSG(!data->async->rx_enabled);
1963 : : __ASSERT_NO_MSG(!data->async->tx_size);
1964 : :
1965 : : }
1966 : : #endif
1967 : : if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_RXSTARTED)) {
1968 : : #ifdef UARTE_INTERRUPT_DRIVEN
1969 : : if (data->int_driven) {
1970 : : data->int_driven->rx_irq_enabled =
1971 : : nrf_uarte_int_enable_check(uarte,
1972 : : NRF_UARTE_INT_ENDRX_MASK);
1973 : : if (data->int_driven->rx_irq_enabled) {
1974 : : nrf_uarte_int_disable(uarte,
1975 : : NRF_UARTE_INT_ENDRX_MASK);
1976 : : }
1977 : : }
1978 : : #endif
1979 : : nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPRX);
1980 : : while (!nrf_uarte_event_check(uarte,
1981 : : NRF_UARTE_EVENT_RXTO) &&
1982 : : !nrf_uarte_event_check(uarte,
1983 : : NRF_UARTE_EVENT_ERROR)) {
1984 : : /* Busy wait for event to register */
1985 : : }
1986 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXSTARTED);
1987 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_RXTO);
1988 : : nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDRX);
1989 : : }
1990 : :
1991 : : wait_for_tx_stopped(dev);
1992 : : uart_disable(dev);
1993 : :
1994 : : if (cfg->flags & UARTE_CFG_FLAG_GPIO_MGMT) {
1995 : : #ifdef CONFIG_PINCTRL
1996 : : ret = pinctrl_apply_state(cfg->pcfg,
1997 : : PINCTRL_STATE_SLEEP);
1998 : : if (ret < 0) {
1999 : : return ret;
2000 : : }
2001 : : #else
2002 : : uarte_nrfx_pins_configure(dev, true);
2003 : : #endif /* CONFIG_PINCTRL */
2004 : : }
2005 : :
2006 : : break;
2007 : : default:
2008 : : return -ENOTSUP;
2009 : : }
2010 : :
2011 : : return 0;
2012 : : }
2013 : : #endif /* CONFIG_PM_DEVICE */
2014 : :
2015 : : #define UARTE(idx) DT_NODELABEL(uart##idx)
2016 : : #define UARTE_HAS_PROP(idx, prop) DT_NODE_HAS_PROP(UARTE(idx), prop)
2017 : : #define UARTE_PROP(idx, prop) DT_PROP(UARTE(idx), prop)
2018 : :
2019 : : #define UARTE_IRQ_CONFIGURE(idx, isr_handler) \
2020 : : do { \
2021 : : IRQ_CONNECT(DT_IRQN(UARTE(idx)), DT_IRQ(UARTE(idx), priority), \
2022 : : isr_handler, DEVICE_DT_GET(UARTE(idx)), 0); \
2023 : : irq_enable(DT_IRQN(UARTE(idx))); \
2024 : : } while (0)
2025 : :
2026 : : #ifdef CONFIG_PINCTRL
2027 : : /* Low power mode is used when disable_rx is not defined or in async mode if
2028 : : * kconfig option is enabled.
2029 : : */
2030 : : #define USE_LOW_POWER(idx) \
2031 : : ((!UARTE_PROP(idx, disable_rx) && \
2032 : : COND_CODE_1(CONFIG_UART_##idx##_ASYNC, \
2033 : : (!IS_ENABLED(CONFIG_UART_##idx##_NRF_ASYNC_LOW_POWER)), \
2034 : : (1))) ? 0 : UARTE_CFG_FLAG_LOW_POWER)
2035 : :
2036 : : #define UARTE_DISABLE_RX_INIT(node_id) \
2037 : : .disable_rx = DT_PROP(node_id, disable_rx)
2038 : : #else
2039 : : /* Low power mode is used when rx pin is not defined or in async mode if
2040 : : * kconfig option is enabled.
2041 : : */
2042 : : #define USE_LOW_POWER(idx) \
2043 : : ((UARTE_HAS_PROP(idx, rx_pin) && \
2044 : : COND_CODE_1(CONFIG_UART_##idx##_ASYNC, \
2045 : : (!IS_ENABLED(CONFIG_UART_##idx##_NRF_ASYNC_LOW_POWER)), \
2046 : : (1))) ? 0 : UARTE_CFG_FLAG_LOW_POWER)
2047 : :
2048 : : #define UARTE_DISABLE_RX_INIT(node_id) \
2049 : : .disable_rx = DT_NODE_HAS_PROP(node_id, rx_pin) ? false : true
2050 : : #endif /* CONFIG_PINCTRL */
2051 : :
2052 : : #define UART_NRF_UARTE_DEVICE(idx) \
2053 : : NRF_DT_CHECK_PIN_ASSIGNMENTS(UARTE(idx), 1, \
2054 : : tx_pin, rx_pin, rts_pin, cts_pin); \
2055 : : UARTE_INT_DRIVEN(idx); \
2056 : : UARTE_ASYNC(idx); \
2057 : : IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(UARTE(idx));)) \
2058 : : static struct uarte_nrfx_data uarte_##idx##_data = { \
2059 : : UARTE_CONFIG(idx), \
2060 : : IF_ENABLED(CONFIG_UART_##idx##_ASYNC, \
2061 : : (.async = &uarte##idx##_async,)) \
2062 : : IF_ENABLED(CONFIG_UART_##idx##_INTERRUPT_DRIVEN, \
2063 : : (.int_driven = &uarte##idx##_int_driven,)) \
2064 : : }; \
2065 : : static const struct uarte_nrfx_config uarte_##idx##z_config = { \
2066 : : COND_CODE_1(CONFIG_PINCTRL, \
2067 : : (.pcfg = PINCTRL_DT_DEV_CONFIG_GET(UARTE(idx)),), \
2068 : : (.tx_pin = DT_PROP_OR(UARTE(idx), tx_pin, \
2069 : : NRF_UARTE_PSEL_DISCONNECTED), \
2070 : : .rx_pin = DT_PROP_OR(UARTE(idx), rx_pin, \
2071 : : NRF_UARTE_PSEL_DISCONNECTED), \
2072 : : .rts_pin = DT_PROP_OR(UARTE(idx), rts_pin, \
2073 : : NRF_UARTE_PSEL_DISCONNECTED), \
2074 : : .cts_pin = DT_PROP_OR(UARTE(idx), cts_pin, \
2075 : : NRF_UARTE_PSEL_DISCONNECTED), \
2076 : : .rx_pull_up = DT_PROP(UARTE(idx), rx_pull_up), \
2077 : : .cts_pull_up = DT_PROP(UARTE(idx), cts_pull_up),)) \
2078 : : .uarte_regs = (NRF_UARTE_Type *)DT_REG_ADDR(UARTE(idx)), \
2079 : : .flags = \
2080 : : (IS_ENABLED(CONFIG_UART_##idx##_GPIO_MANAGEMENT) ? \
2081 : : UARTE_CFG_FLAG_GPIO_MGMT : 0) | \
2082 : : (IS_ENABLED(CONFIG_UART_##idx##_ENHANCED_POLL_OUT) ? \
2083 : : UARTE_CFG_FLAG_PPI_ENDTX : 0) | \
2084 : : USE_LOW_POWER(idx), \
2085 : : UARTE_DISABLE_RX_INIT(UARTE(idx)), \
2086 : : IF_ENABLED(CONFIG_UART_##idx##_NRF_HW_ASYNC, \
2087 : : (.timer = NRFX_TIMER_INSTANCE( \
2088 : : CONFIG_UART_##idx##_NRF_HW_ASYNC_TIMER),)) \
2089 : : }; \
2090 : : static int uarte_##idx##_init(const struct device *dev) \
2091 : : { \
2092 : : COND_CODE_1(CONFIG_UART_##idx##_ASYNC, \
2093 : : (UARTE_IRQ_CONFIGURE(idx, uarte_nrfx_isr_async);), \
2094 : : (UARTE_IRQ_CONFIGURE(idx, uarte_nrfx_isr_int);)) \
2095 : : return uarte_instance_init( \
2096 : : dev, \
2097 : : IS_ENABLED(CONFIG_UART_##idx##_INTERRUPT_DRIVEN)); \
2098 : : } \
2099 : : \
2100 : : PM_DEVICE_DT_DEFINE(UARTE(idx), uarte_nrfx_pm_action); \
2101 : : \
2102 : : DEVICE_DT_DEFINE(UARTE(idx), \
2103 : : uarte_##idx##_init, \
2104 : : PM_DEVICE_DT_GET(UARTE(idx)), \
2105 : : &uarte_##idx##_data, \
2106 : : &uarte_##idx##z_config, \
2107 : : PRE_KERNEL_1, \
2108 : : CONFIG_SERIAL_INIT_PRIORITY, \
2109 : : &uart_nrfx_uarte_driver_api)
2110 : :
2111 : : #define UARTE_CONFIG(idx) \
2112 : : .uart_config = { \
2113 : : .baudrate = UARTE_PROP(idx, current_speed), \
2114 : : .data_bits = UART_CFG_DATA_BITS_8, \
2115 : : .stop_bits = UART_CFG_STOP_BITS_1, \
2116 : : .parity = IS_ENABLED(CONFIG_UART_##idx##_NRF_PARITY_BIT) \
2117 : : ? UART_CFG_PARITY_EVEN \
2118 : : : UART_CFG_PARITY_NONE, \
2119 : : .flow_ctrl = UARTE_PROP(idx, hw_flow_control) \
2120 : : ? UART_CFG_FLOW_CTRL_RTS_CTS \
2121 : : : UART_CFG_FLOW_CTRL_NONE, \
2122 : : }
2123 : :
2124 : : #define UARTE_ASYNC(idx) \
2125 : : IF_ENABLED(CONFIG_UART_##idx##_ASYNC, \
2126 : : (struct uarte_async_cb uarte##idx##_async = { \
2127 : : .hw_rx_counting = \
2128 : : IS_ENABLED(CONFIG_UART_##idx##_NRF_HW_ASYNC), \
2129 : : }))
2130 : :
2131 : : #define UARTE_INT_DRIVEN(idx) \
2132 : : IF_ENABLED(CONFIG_UART_##idx##_INTERRUPT_DRIVEN, \
2133 : : (static uint8_t uarte##idx##_tx_buffer[\
2134 : : MIN(CONFIG_UART_##idx##_NRF_TX_BUFFER_SIZE, \
2135 : : BIT_MASK(UARTE##idx##_EASYDMA_MAXCNT_SIZE))]; \
2136 : : static struct uarte_nrfx_int_driven \
2137 : : uarte##idx##_int_driven = { \
2138 : : .tx_buffer = uarte##idx##_tx_buffer, \
2139 : : .tx_buff_size = sizeof(uarte##idx##_tx_buffer),\
2140 : : };))
2141 : :
2142 : : #ifdef CONFIG_UART_0_NRF_UARTE
2143 : 1 : UART_NRF_UARTE_DEVICE(0);
2144 : : #endif
2145 : :
2146 : : #ifdef CONFIG_UART_1_NRF_UARTE
2147 : : UART_NRF_UARTE_DEVICE(1);
2148 : : #endif
2149 : :
2150 : : #ifdef CONFIG_UART_2_NRF_UARTE
2151 : : UART_NRF_UARTE_DEVICE(2);
2152 : : #endif
2153 : :
2154 : : #ifdef CONFIG_UART_3_NRF_UARTE
2155 : : UART_NRF_UARTE_DEVICE(3);
2156 : : #endif
|