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 int (*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
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 z_impl_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 int z_impl_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 return -ENOSYS;
448 }
449 return api->irq_update(dev);
450#else
451 ARG_UNUSED(dev);
452 return -ENOTSUP;
453#endif
454}
455
456static inline int uart_irq_callback_user_data_set(const struct device *dev,
457 uart_irq_callback_user_data_t cb, void *user_data)
458{
459#ifdef CONFIG_UART_INTERRUPT_DRIVEN
460 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
461
462 if ((api != NULL) && (api->irq_callback_set != NULL)) {
463 api->irq_callback_set(dev, cb, user_data);
464 return 0;
465 } else {
466 return -ENOSYS;
467 }
468#else
469 ARG_UNUSED(dev);
470 ARG_UNUSED(cb);
471 ARG_UNUSED(user_data);
472 return -ENOTSUP;
473#endif
474}
475
476static inline int uart_irq_callback_set(const struct device *dev, uart_irq_callback_user_data_t cb)
477{
478 return uart_irq_callback_user_data_set(dev, cb, NULL);
479}
480
481static inline int uart_callback_set(const struct device *dev, uart_callback_t callback,
482 void *user_data)
483{
484#ifdef CONFIG_UART_ASYNC_API
485 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
486
487 if (api->callback_set == NULL) {
488 return -ENOSYS;
489 }
490
491 return api->callback_set(dev, callback, user_data);
492#else
493 ARG_UNUSED(dev);
494 ARG_UNUSED(callback);
495 ARG_UNUSED(user_data);
496 return -ENOTSUP;
497#endif
498}
499
500static inline int z_impl_uart_tx(const struct device *dev, const uint8_t *buf, size_t len,
501 int32_t timeout)
502
503{
504#ifdef CONFIG_UART_ASYNC_API
505 return DEVICE_API_GET(uart, dev)->tx(dev, buf, len, timeout);
506#else
507 ARG_UNUSED(dev);
508 ARG_UNUSED(buf);
509 ARG_UNUSED(len);
510 ARG_UNUSED(timeout);
511 return -ENOTSUP;
512#endif
513}
514
515static inline int z_impl_uart_tx_u16(const struct device *dev, const uint16_t *buf, size_t len,
516 int32_t timeout)
517
518{
519#if defined(CONFIG_UART_ASYNC_API) && defined(CONFIG_UART_WIDE_DATA)
520 return DEVICE_API_GET(uart, dev)->tx_u16(dev, buf, len, timeout);
521#else
522 ARG_UNUSED(dev);
523 ARG_UNUSED(buf);
524 ARG_UNUSED(len);
525 ARG_UNUSED(timeout);
526 return -ENOTSUP;
527#endif
528}
529
530static inline int z_impl_uart_tx_abort(const struct device *dev)
531{
532#ifdef CONFIG_UART_ASYNC_API
533 return DEVICE_API_GET(uart, dev)->tx_abort(dev);
534#else
535 ARG_UNUSED(dev);
536 return -ENOTSUP;
537#endif
538}
539
540static inline int z_impl_uart_rx_enable(const struct device *dev, uint8_t *buf, size_t len,
541 int32_t timeout)
542{
543#ifdef CONFIG_UART_ASYNC_API
544 return DEVICE_API_GET(uart, dev)->rx_enable(dev, buf, len, timeout);
545#else
546 ARG_UNUSED(dev);
547 ARG_UNUSED(buf);
548 ARG_UNUSED(len);
549 ARG_UNUSED(timeout);
550 return -ENOTSUP;
551#endif
552}
553
554static inline int z_impl_uart_rx_enable_u16(const struct device *dev, uint16_t *buf, size_t len,
555 int32_t timeout)
556{
557#if defined(CONFIG_UART_ASYNC_API) && defined(CONFIG_UART_WIDE_DATA)
558 return DEVICE_API_GET(uart, dev)->rx_enable_u16(dev, buf, len, timeout);
559#else
560 ARG_UNUSED(dev);
561 ARG_UNUSED(buf);
562 ARG_UNUSED(len);
563 ARG_UNUSED(timeout);
564 return -ENOTSUP;
565#endif
566}
567
568static inline int uart_rx_buf_rsp(const struct device *dev, uint8_t *buf, size_t len)
569{
570#ifdef CONFIG_UART_ASYNC_API
571 return DEVICE_API_GET(uart, dev)->rx_buf_rsp(dev, buf, len);
572#else
573 ARG_UNUSED(dev);
574 ARG_UNUSED(buf);
575 ARG_UNUSED(len);
576 return -ENOTSUP;
577#endif
578}
579
580static inline int uart_rx_buf_rsp_u16(const struct device *dev, uint16_t *buf, size_t len)
581{
582#if defined(CONFIG_UART_ASYNC_API) && defined(CONFIG_UART_WIDE_DATA)
583 return DEVICE_API_GET(uart, dev)->rx_buf_rsp_u16(dev, buf, len);
584#else
585 ARG_UNUSED(dev);
586 ARG_UNUSED(buf);
587 ARG_UNUSED(len);
588 return -ENOTSUP;
589#endif
590}
591
592static inline int z_impl_uart_rx_disable(const struct device *dev)
593{
594#ifdef CONFIG_UART_ASYNC_API
595 return DEVICE_API_GET(uart, dev)->rx_disable(dev);
596#else
597 ARG_UNUSED(dev);
598 return -ENOTSUP;
599#endif
600}
601
602static inline int z_impl_uart_line_ctrl_set(const struct device *dev, uint32_t ctrl, uint32_t val)
603{
604#ifdef CONFIG_UART_LINE_CTRL
605 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
606
607 if (api->line_ctrl_set == NULL) {
608 return -ENOSYS;
609 }
610 return api->line_ctrl_set(dev, ctrl, val);
611#else
612 ARG_UNUSED(dev);
613 ARG_UNUSED(ctrl);
614 ARG_UNUSED(val);
615 return -ENOTSUP;
616#endif
617}
618
619static inline int z_impl_uart_line_ctrl_get(const struct device *dev, uint32_t ctrl, uint32_t *val)
620{
621#ifdef CONFIG_UART_LINE_CTRL
622 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
623
624 if (api->line_ctrl_get == NULL) {
625 return -ENOSYS;
626 }
627 return api->line_ctrl_get(dev, ctrl, val);
628#else
629 ARG_UNUSED(dev);
630 ARG_UNUSED(ctrl);
631 ARG_UNUSED(val);
632 return -ENOTSUP;
633#endif
634}
635
636static inline int z_impl_uart_drv_cmd(const struct device *dev, uint32_t cmd, uint32_t p)
637{
638#ifdef CONFIG_UART_DRV_CMD
639 const struct uart_driver_api *api = DEVICE_API_GET(uart, dev);
640
641 if (api->drv_cmd == NULL) {
642 return -ENOSYS;
643 }
644 return api->drv_cmd(dev, cmd, p);
645#else
646 ARG_UNUSED(dev);
647 ARG_UNUSED(cmd);
648 ARG_UNUSED(p);
649 return -ENOTSUP;
650#endif
651}
652
653#ifdef __cplusplus
654}
655#endif
656
658
659#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:1375
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:318
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 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_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.
#define NULL
Definition iar_missing_defs.h:20
__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