1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_STDIO_UART_H
8 #define _PICO_STDIO_UART_H
9 
10 #include "pico/stdio.h"
11 #include "hardware/uart.h"
12 
13 /** \brief Support for stdin/stdout using UART
14  *  \defgroup pico_stdio_uart pico_stdio_uart
15  *  \ingroup pico_stdio
16  *
17  *  Linking this library or calling `pico_enable_stdio_uart(TARGET ENABLED)` in the CMake (which
18  *  achieves the same thing) will add UART to the drivers used for standard input/output
19  */
20 
21 // PICO_CONFIG: PICO_STDIO_UART_DEFAULT_CRLF, Default state of CR/LF translation for UART output, type=bool, default=PICO_STDIO_DEFAULT_CRLF, group=pico_stdio_uart
22 #ifndef PICO_STDIO_UART_DEFAULT_CRLF
23 #define PICO_STDIO_UART_DEFAULT_CRLF PICO_STDIO_DEFAULT_CRLF
24 #endif
25 
26 // PICO_CONFIG: PICO_STDIO_UART_SUPPORT_CHARS_AVAILABLE_CALLBACK, Enable UART STDIO support for stdio_set_chars_available_callback. Can be disabled to make use of the uart elsewhere, type=bool, default=1, group=pico_stdio_uart
27 #ifndef PICO_STDIO_UART_SUPPORT_CHARS_AVAILABLE_CALLBACK
28 #define PICO_STDIO_UART_SUPPORT_CHARS_AVAILABLE_CALLBACK 1
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 extern stdio_driver_t stdio_uart;
36 
37 /*! \brief Explicitly initialize stdin/stdout over UART and add it to the current set of stdin/stdout drivers
38  *  \ingroup pico_stdio_uart
39  *
40  * This method sets up PICO_DEFAULT_UART_TX_PIN for UART output (if defined), PICO_DEFAULT_UART_RX_PIN for input (if defined)
41  * and configures the baud rate as PICO_DEFAULT_UART_BAUD_RATE.
42  *
43  * \note this method is automatically called by \ref stdio_init_all() if `pico_stdio_uart` is included in the build
44  */
45 void stdio_uart_init(void);
46 
47 /*! \brief Explicitly initialize stdout only (no stdin) over UART and add it to the current set of stdout drivers
48  *  \ingroup pico_stdio_uart
49  *
50  * This method sets up PICO_DEFAULT_UART_TX_PIN for UART output (if defined) , and configures the baud rate as PICO_DEFAULT_UART_BAUD_RATE
51  */
52 void stdout_uart_init(void);
53 
54 /*! \brief Explicitly initialize stdin only (no stdout) over UART and add it to the current set of stdin drivers
55  *  \ingroup pico_stdio_uart
56  *
57  * This method sets up PICO_DEFAULT_UART_RX_PIN for UART input (if defined) , and configures the baud rate as PICO_DEFAULT_UART_BAUD_RATE
58  */
59 void stdin_uart_init(void);
60 
61 /*! \brief Perform custom initialization initialize stdin/stdout over UART and add it to the current set of stdin/stdout drivers
62  *  \ingroup pico_stdio_uart
63  *
64  * \param uart the uart instance to use, \ref uart0 or \ref uart1
65  * \param baud_rate the baud rate in Hz
66  * \param tx_pin the UART pin to use for stdout (or -1 for no stdout)
67  * \param rx_pin the UART pin to use for stdin (or -1 for no stdin)
68  */
69 void stdio_uart_init_full(uart_inst_t *uart, uint baud_rate, int tx_pin, int rx_pin);
70 
71 /*! \brief Explicitly deinitialize stdin/stdout over UART and remove it from the current set of stdin/stdout drivers
72  *  \ingroup pico_stdio_uart
73  *
74  * This method disables PICO_DEFAULT_UART_TX_PIN for UART output (if defined), PICO_DEFAULT_UART_RX_PIN for input (if defined)
75  * and leaves the pads isolated.
76  *
77  * \note this method is automatically called by \ref stdio_deinit_all() if `pico_stdio_uart` is included in the build
78  */
79 void stdio_uart_deinit(void);
80 
81 /*! \brief Explicitly deinitialize stdout only (no stdin) over UART and remove it from the current set of stdout drivers
82  *  \ingroup pico_stdio_uart
83  *
84  * This method disables PICO_DEFAULT_UART_TX_PIN for UART output (if defined), and leaves the pad isolated
85  */
86 void stdout_uart_deinit(void);
87 
88 /*! \brief Explicitly deinitialize stdin only (no stdout) over UART and remove it from the current set of stdin drivers
89  *  \ingroup pico_stdio_uart
90  *
91  * This method disables PICO_DEFAULT_UART_RX_PIN for UART input (if defined), and leaves the pads isolated
92  */
93 void stdin_uart_deinit(void);
94 
95 /*! \brief Perform custom deinitialization deinitialize stdin/stdout over UART and remove it from the current set of stdin/stdout drivers
96  *  \ingroup pico_stdio_uart
97  *
98  * \param uart the uart instance to use, \ref uart0 or \ref uart1
99  * \param tx_pin the UART pin to use for stdout (or -1 for no stdout)
100  * \param rx_pin the UART pin to use for stdin (or -1 for no stdin)
101  */
102 void stdio_uart_deinit_full(uart_inst_t *uart, int tx_pin, int rx_pin);
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif
109