Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
uart_internal.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Wind River Systems, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_UART_UART_INTERNAL_H_
14#define ZEPHYR_INCLUDE_DRIVERS_UART_UART_INTERNAL_H_
15
16#include <errno.h>
17#include <stddef.h>
18
19#include <zephyr/device.h>
20
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
36typedef void (*uart_irq_config_func_t)(const struct device *dev);
37
39__subsystem struct uart_driver_api {
40
41#ifdef CONFIG_UART_ASYNC_API
42
43 int (*callback_set)(const struct device *dev, uart_callback_t callback, void *user_data);
44
45 int (*tx)(const struct device *dev, const uint8_t *buf, size_t len, int32_t timeout);
46 int (*tx_abort)(const struct device *dev);
47
48 int (*rx_enable)(const struct device *dev, uint8_t *buf, size_t len, int32_t timeout);
49 int (*rx_buf_rsp)(const struct device *dev, uint8_t *buf, size_t len);
50 int (*rx_disable)(const struct device *dev);
51
52#ifdef CONFIG_UART_WIDE_DATA
53 int (*tx_u16)(const struct device *dev, const uint16_t *buf, size_t len, int32_t timeout);
54 int (*rx_enable_u16)(const struct device *dev, uint16_t *buf, size_t len, int32_t timeout);
55 int (*rx_buf_rsp_u16)(const struct device *dev, uint16_t *buf, size_t len);
56#endif
57
58#endif
59
61 int (*poll_in)(const struct device *dev, unsigned char *p_char);
62 void (*poll_out)(const struct device *dev, unsigned char out_char);
63
64#ifdef CONFIG_UART_WIDE_DATA
65 int (*poll_in_u16)(const struct device *dev, uint16_t *p_u16);
66 void (*poll_out_u16)(const struct device *dev, uint16_t out_u16);
67#endif
68
70 int (*err_check)(const struct device *dev);
71
72#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
74 int (*configure)(const struct device *dev, const struct uart_config *cfg);
75 int (*config_get)(const struct device *dev, struct uart_config *cfg);
76#endif
77
78#ifdef CONFIG_UART_INTERRUPT_DRIVEN
79
81 int (*fifo_fill)(const struct device *dev, const uint8_t *tx_data, int len);
82
83#ifdef CONFIG_UART_WIDE_DATA
84 int (*fifo_fill_u16)(const struct device *dev, const uint16_t *tx_data, int len);
85#endif
86
88 int (*fifo_read)(const struct device *dev, uint8_t *rx_data, const int size);
89
90#ifdef CONFIG_UART_WIDE_DATA
91 int (*fifo_read_u16)(const struct device *dev, uint16_t *rx_data, const int size);
92#endif
93
95 void (*irq_tx_enable)(const struct device *dev);
96
98 void (*irq_tx_disable)(const struct device *dev);
99
101 int (*irq_tx_ready)(const struct device *dev);
102
104 void (*irq_rx_enable)(const struct device *dev);
105
107 void (*irq_rx_disable)(const struct device *dev);
108
110 int (*irq_tx_complete)(const struct device *dev);
111
113 int (*irq_rx_ready)(const struct device *dev);
114
116 void (*irq_err_enable)(const struct device *dev);
117
119 void (*irq_err_disable)(const struct device *dev);
120
122 int (*irq_is_pending)(const struct device *dev);
123
125 void (*irq_update)(const struct device *dev);
126
128 void (*irq_callback_set)(const struct device *dev, uart_irq_callback_user_data_t cb,
129 void *user_data);
130
131#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
132
133#ifdef CONFIG_UART_LINE_CTRL
134 int (*line_ctrl_set)(const struct device *dev, uint32_t ctrl, uint32_t val);
135 int (*line_ctrl_get)(const struct device *dev, uint32_t ctrl, uint32_t *val);
136#endif
137
138#ifdef CONFIG_UART_DRV_CMD
139 int (*drv_cmd)(const struct device *dev, uint32_t cmd, uint32_t p);
140#endif
141};
142
143static inline int z_impl_uart_err_check(const struct device *dev)
144{
145 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
146
147 if (api->err_check == NULL) {
148 return -ENOSYS;
149 }
150
151 return api->err_check(dev);
152}
153
154static inline int z_impl_uart_poll_in(const struct device *dev, unsigned char *p_char)
155{
156 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
157
158 if (api->poll_in == NULL) {
159 return -ENOSYS;
160 }
161
162 return api->poll_in(dev, p_char);
163}
164
165static inline int z_impl_uart_poll_in_u16(const struct device *dev, uint16_t *p_u16)
166{
167#ifdef CONFIG_UART_WIDE_DATA
168 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
169
170 if (api->poll_in_u16 == NULL) {
171 return -ENOSYS;
172 }
173
174 return api->poll_in_u16(dev, p_u16);
175#else
176 ARG_UNUSED(dev);
177 ARG_UNUSED(p_u16);
178 return -ENOTSUP;
179#endif
180}
181
182static inline void z_impl_uart_poll_out(const struct device *dev, unsigned char out_char)
183{
184 DEVICE_API_GET(uart, dev)->poll_out(dev, out_char);
185}
186
187static inline void z_impl_uart_poll_out_u16(const struct device *dev, uint16_t out_u16)
188{
189#ifdef CONFIG_UART_WIDE_DATA
190 DEVICE_API_GET(uart, dev)->poll_out_u16(dev, out_u16);
191#else
192 ARG_UNUSED(dev);
193 ARG_UNUSED(out_u16);
194#endif
195}
196
197static inline int z_impl_uart_configure(const struct device *dev, const struct uart_config *cfg)
198{
199#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
200 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
201
202 if (api->configure == NULL) {
203 return -ENOSYS;
204 }
205 return api->configure(dev, cfg);
206#else
207 ARG_UNUSED(dev);
208 ARG_UNUSED(cfg);
209 return -ENOTSUP;
210#endif
211}
212
213static inline int z_impl_uart_config_get(const struct device *dev, struct uart_config *cfg)
214{
215#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
216 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
217
218 if (api->config_get == NULL) {
219 return -ENOSYS;
220 }
221
222 return api->config_get(dev, cfg);
223#else
224 ARG_UNUSED(dev);
225 ARG_UNUSED(cfg);
226 return -ENOTSUP;
227#endif
228}
229
230static inline int uart_fifo_fill(const struct device *dev, const uint8_t *tx_data, int size)
231{
232#ifdef CONFIG_UART_INTERRUPT_DRIVEN
233 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
234
235 if (api->fifo_fill == NULL) {
236 return -ENOSYS;
237 }
238
239 return api->fifo_fill(dev, tx_data, size);
240#else
241 ARG_UNUSED(dev);
242 ARG_UNUSED(tx_data);
243 ARG_UNUSED(size);
244 return -ENOTSUP;
245#endif
246}
247
248static inline int uart_fifo_fill_u16(const struct device *dev, const uint16_t *tx_data, int size)
249{
250#if defined(CONFIG_UART_INTERRUPT_DRIVEN) && defined(CONFIG_UART_WIDE_DATA)
251 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
252
253 if (api->fifo_fill_u16 == NULL) {
254 return -ENOSYS;
255 }
256
257 return api->fifo_fill_u16(dev, tx_data, size);
258#else
259 ARG_UNUSED(dev);
260 ARG_UNUSED(tx_data);
261 ARG_UNUSED(size);
262 return -ENOTSUP;
263#endif
264}
265
266static inline int uart_fifo_read(const struct device *dev, uint8_t *rx_data, const int size)
267{
268#ifdef CONFIG_UART_INTERRUPT_DRIVEN
269 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
270
271 if (api->fifo_read == NULL) {
272 return -ENOSYS;
273 }
274
275 return api->fifo_read(dev, rx_data, size);
276#else
277 ARG_UNUSED(dev);
278 ARG_UNUSED(rx_data);
279 ARG_UNUSED(size);
280 return -ENOTSUP;
281#endif
282}
283
284static inline int uart_fifo_read_u16(const struct device *dev, uint16_t *rx_data, const int size)
285{
286#if defined(CONFIG_UART_INTERRUPT_DRIVEN) && defined(CONFIG_UART_WIDE_DATA)
287 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
288
289 if (api->fifo_read_u16 == NULL) {
290 return -ENOSYS;
291 }
292
293 return api->fifo_read_u16(dev, rx_data, size);
294#else
295 ARG_UNUSED(dev);
296 ARG_UNUSED(rx_data);
297 ARG_UNUSED(size);
298 return -ENOTSUP;
299#endif
300}
301
302static inline void z_impl_uart_irq_tx_enable(const struct device *dev)
303{
304#ifdef CONFIG_UART_INTERRUPT_DRIVEN
305 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
306
307 if (api->irq_tx_enable != NULL) {
308 api->irq_tx_enable(dev);
309 }
310#else
311 ARG_UNUSED(dev);
312#endif
313}
314
315static inline void z_impl_uart_irq_tx_disable(const struct device *dev)
316{
317#ifdef CONFIG_UART_INTERRUPT_DRIVEN
318 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
319
320 if (api->irq_tx_disable != NULL) {
321 api->irq_tx_disable(dev);
322 }
323#else
324 ARG_UNUSED(dev);
325#endif
326}
327
328static inline int uart_irq_tx_ready(const struct device *dev)
329{
330#ifdef CONFIG_UART_INTERRUPT_DRIVEN
331 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
332
333 if (api->irq_tx_ready == NULL) {
334 return -ENOSYS;
335 }
336
337 return api->irq_tx_ready(dev);
338#else
339 ARG_UNUSED(dev);
340 return -ENOTSUP;
341#endif
342}
343
344static inline void z_impl_uart_irq_rx_enable(const struct device *dev)
345{
346#ifdef CONFIG_UART_INTERRUPT_DRIVEN
347 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
348
349 if (api->irq_rx_enable != NULL) {
350 api->irq_rx_enable(dev);
351 }
352#else
353 ARG_UNUSED(dev);
354#endif
355}
356
357static inline void z_impl_uart_irq_rx_disable(const struct device *dev)
358{
359#ifdef CONFIG_UART_INTERRUPT_DRIVEN
360 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
361
362 if (api->irq_rx_disable != NULL) {
363 api->irq_rx_disable(dev);
364 }
365#else
366 ARG_UNUSED(dev);
367#endif
368}
369
370static inline int uart_irq_tx_complete(const struct device *dev)
371{
372#ifdef CONFIG_UART_INTERRUPT_DRIVEN
373 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
374
375 if (api->irq_tx_complete == NULL) {
376 return -ENOSYS;
377 }
378 return api->irq_tx_complete(dev);
379#else
380 ARG_UNUSED(dev);
381 return -ENOTSUP;
382#endif
383}
384
385static inline int uart_irq_rx_ready(const struct device *dev)
386{
387#ifdef CONFIG_UART_INTERRUPT_DRIVEN
388 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
389
390 if (api->irq_rx_ready == NULL) {
391 return -ENOSYS;
392 }
393 return api->irq_rx_ready(dev);
394#else
395 ARG_UNUSED(dev);
396 return -ENOTSUP;
397#endif
398}
399
400static inline void z_impl_uart_irq_err_enable(const struct device *dev)
401{
402#ifdef CONFIG_UART_INTERRUPT_DRIVEN
403 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
404
405 if (api->irq_err_enable) {
406 api->irq_err_enable(dev);
407 }
408#else
409 ARG_UNUSED(dev);
410#endif
411}
412
413static inline void z_impl_uart_irq_err_disable(const struct device *dev)
414{
415#ifdef CONFIG_UART_INTERRUPT_DRIVEN
416 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
417
418 if (api->irq_err_disable) {
419 api->irq_err_disable(dev);
420 }
421#else
422 ARG_UNUSED(dev);
423#endif
424}
425
426static inline int uart_irq_is_pending(const struct device *dev)
427{
428#ifdef CONFIG_UART_INTERRUPT_DRIVEN
429 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
430
431 if (api->irq_is_pending == NULL) {
432 return -ENOSYS;
433 }
434 return api->irq_is_pending(dev);
435#else
436 ARG_UNUSED(dev);
437 return -ENOTSUP;
438#endif
439}
440
441static inline void uart_irq_update(const struct device *dev)
442{
443#ifdef CONFIG_UART_INTERRUPT_DRIVEN
444 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
445
446 if (api->irq_update != NULL) {
447 api->irq_update(dev);
448 }
449#endif
450}
451
452static inline int uart_irq_callback_user_data_set(const struct device *dev,
453 uart_irq_callback_user_data_t cb, void *user_data)
454{
455#ifdef CONFIG_UART_INTERRUPT_DRIVEN
456 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
457
458 if ((api != NULL) && (api->irq_callback_set != NULL)) {
459 api->irq_callback_set(dev, cb, user_data);
460 return 0;
461 } else {
462 return -ENOSYS;
463 }
464#else
465 ARG_UNUSED(dev);
466 ARG_UNUSED(cb);
467 ARG_UNUSED(user_data);
468 return -ENOTSUP;
469#endif
470}
471
472static inline int uart_irq_callback_set(const struct device *dev, uart_irq_callback_user_data_t cb)
473{
474 return uart_irq_callback_user_data_set(dev, cb, NULL);
475}
476
477static inline int uart_callback_set(const struct device *dev, uart_callback_t callback,
478 void *user_data)
479{
480#ifdef CONFIG_UART_ASYNC_API
481 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
482
483 if (api->callback_set == NULL) {
484 return -ENOSYS;
485 }
486
487 return api->callback_set(dev, callback, user_data);
488#else
489 ARG_UNUSED(dev);
490 ARG_UNUSED(callback);
491 ARG_UNUSED(user_data);
492 return -ENOTSUP;
493#endif
494}
495
496static inline int z_impl_uart_tx(const struct device *dev, const uint8_t *buf, size_t len,
497 int32_t timeout)
498
499{
500#ifdef CONFIG_UART_ASYNC_API
501 return DEVICE_API_GET(uart, dev)->tx(dev, buf, len, timeout);
502#else
503 ARG_UNUSED(dev);
504 ARG_UNUSED(buf);
505 ARG_UNUSED(len);
506 ARG_UNUSED(timeout);
507 return -ENOTSUP;
508#endif
509}
510
511static inline int z_impl_uart_tx_u16(const struct device *dev, const uint16_t *buf, size_t len,
512 int32_t timeout)
513
514{
515#if defined(CONFIG_UART_ASYNC_API) && defined(CONFIG_UART_WIDE_DATA)
516 return DEVICE_API_GET(uart, dev)->tx_u16(dev, buf, len, timeout);
517#else
518 ARG_UNUSED(dev);
519 ARG_UNUSED(buf);
520 ARG_UNUSED(len);
521 ARG_UNUSED(timeout);
522 return -ENOTSUP;
523#endif
524}
525
526static inline int z_impl_uart_tx_abort(const struct device *dev)
527{
528#ifdef CONFIG_UART_ASYNC_API
529 return DEVICE_API_GET(uart, dev)->tx_abort(dev);
530#else
531 ARG_UNUSED(dev);
532 return -ENOTSUP;
533#endif
534}
535
536static inline int z_impl_uart_rx_enable(const struct device *dev, uint8_t *buf, size_t len,
537 int32_t timeout)
538{
539#ifdef CONFIG_UART_ASYNC_API
540 return DEVICE_API_GET(uart, dev)->rx_enable(dev, buf, len, timeout);
541#else
542 ARG_UNUSED(dev);
543 ARG_UNUSED(buf);
544 ARG_UNUSED(len);
545 ARG_UNUSED(timeout);
546 return -ENOTSUP;
547#endif
548}
549
550static inline int z_impl_uart_rx_enable_u16(const struct device *dev, uint16_t *buf, size_t len,
551 int32_t timeout)
552{
553#if defined(CONFIG_UART_ASYNC_API) && defined(CONFIG_UART_WIDE_DATA)
554 return DEVICE_API_GET(uart, dev)->rx_enable_u16(dev, buf, len, timeout);
555#else
556 ARG_UNUSED(dev);
557 ARG_UNUSED(buf);
558 ARG_UNUSED(len);
559 ARG_UNUSED(timeout);
560 return -ENOTSUP;
561#endif
562}
563
564static inline int uart_rx_buf_rsp(const struct device *dev, uint8_t *buf, size_t len)
565{
566#ifdef CONFIG_UART_ASYNC_API
567 return DEVICE_API_GET(uart, dev)->rx_buf_rsp(dev, buf, len);
568#else
569 ARG_UNUSED(dev);
570 ARG_UNUSED(buf);
571 ARG_UNUSED(len);
572 return -ENOTSUP;
573#endif
574}
575
576static inline int uart_rx_buf_rsp_u16(const struct device *dev, uint16_t *buf, size_t len)
577{
578#if defined(CONFIG_UART_ASYNC_API) && defined(CONFIG_UART_WIDE_DATA)
579 return DEVICE_API_GET(uart, dev)->rx_buf_rsp_u16(dev, buf, len);
580#else
581 ARG_UNUSED(dev);
582 ARG_UNUSED(buf);
583 ARG_UNUSED(len);
584 return -ENOTSUP;
585#endif
586}
587
588static inline int z_impl_uart_rx_disable(const struct device *dev)
589{
590#ifdef CONFIG_UART_ASYNC_API
591 return DEVICE_API_GET(uart, dev)->rx_disable(dev);
592#else
593 ARG_UNUSED(dev);
594 return -ENOTSUP;
595#endif
596}
597
598static inline int z_impl_uart_line_ctrl_set(const struct device *dev, uint32_t ctrl, uint32_t val)
599{
600#ifdef CONFIG_UART_LINE_CTRL
601 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
602
603 if (api->line_ctrl_set == NULL) {
604 return -ENOSYS;
605 }
606 return api->line_ctrl_set(dev, ctrl, val);
607#else
608 ARG_UNUSED(dev);
609 ARG_UNUSED(ctrl);
610 ARG_UNUSED(val);
611 return -ENOTSUP;
612#endif
613}
614
615static inline int z_impl_uart_line_ctrl_get(const struct device *dev, uint32_t ctrl, uint32_t *val)
616{
617#ifdef CONFIG_UART_LINE_CTRL
618 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
619
620 if (api->line_ctrl_get == NULL) {
621 return -ENOSYS;
622 }
623 return api->line_ctrl_get(dev, ctrl, val);
624#else
625 ARG_UNUSED(dev);
626 ARG_UNUSED(ctrl);
627 ARG_UNUSED(val);
628 return -ENOTSUP;
629#endif
630}
631
632static inline int z_impl_uart_drv_cmd(const struct device *dev, uint32_t cmd, uint32_t p)
633{
634#ifdef CONFIG_UART_DRV_CMD
635 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
636
637 if (api->drv_cmd == NULL) {
638 return -ENOSYS;
639 }
640 return api->drv_cmd(dev, cmd, p);
641#else
642 ARG_UNUSED(dev);
643 ARG_UNUSED(cmd);
644 ARG_UNUSED(p);
645 return -ENOTSUP;
646#endif
647}
648
649#ifdef __cplusplus
650}
651#endif
652
654
655#endif /* ZEPHYR_INCLUDE_DRIVERS_UART_UART_INTERNAL_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
System error numbers.
static void cmd(uint32_t command)
Execute a display list command by co-processor engine.
Definition ft8xx_reference_api.h:153
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ENOTSUP
Unsupported value.
Definition errno.h:114
void(* uart_callback_t)(const struct device *dev, struct uart_event *evt, void *user_data)
Define the application callback function signature for uart_callback_set() function.
Definition uart.h:317
static int uart_rx_buf_rsp(const struct device *dev, uint8_t *buf, size_t len)
Provide receive buffer in response to UART_RX_BUF_REQUEST event.
static int uart_rx_buf_rsp_u16(const struct device *dev, uint16_t *buf, size_t len)
Provide wide data receive buffer in response to UART_RX_BUF_REQUEST event.
static int uart_callback_set(const struct device *dev, uart_callback_t callback, void *user_data)
Set event handler function.
static void uart_irq_update(const struct device *dev)
Start processing interrupts in ISR.
static int uart_fifo_read_u16(const struct device *dev, uint16_t *rx_data, const int size)
Read wide data from FIFO.
static int uart_irq_tx_ready(const struct device *dev)
Check if UART TX buffer can accept bytes.
static int uart_irq_callback_set(const struct device *dev, uart_irq_callback_user_data_t cb)
Set the IRQ callback function pointer (legacy).
static int uart_irq_tx_complete(const struct device *dev)
Check if UART TX block finished transmission.
static int uart_fifo_fill_u16(const struct device *dev, const uint16_t *tx_data, int size)
Fill FIFO with wide data.
static int uart_fifo_read(const struct device *dev, uint8_t *rx_data, const int size)
Read data from FIFO.
static int uart_irq_is_pending(const struct device *dev)
Check if any IRQs is pending.
static int uart_irq_rx_ready(const struct device *dev)
Check if UART RX buffer has a received char.
void(* uart_irq_callback_user_data_t)(const struct device *dev, void *user_data)
Define the application callback function signature for uart_irq_callback_user_data_set() function.
Definition uart.h:142
static int uart_irq_callback_user_data_set(const struct device *dev, uart_irq_callback_user_data_t cb, void *user_data)
Set the IRQ callback function pointer.
static int uart_fifo_fill(const struct device *dev, const uint8_t *tx_data, int size)
Fill FIFO with data.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
UART controller configuration structure.
Definition uart.h:122