1 /*!
2  * \file      uart.h
3  *
4  * \brief     UART driver implementation
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  */
23 #ifndef __UART_H__
24 #define __UART_H__
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31 #include "fifo.h"
32 #include "gpio.h"
33 
34 /*!
35  * UART peripheral ID
36  */
37 typedef enum
38 {
39     UART_1,
40     UART_2,
41     UART_USB_CDC = 255,
42 }UartId_t;
43 
44 /*!
45  * UART notification identifier
46  */
47 typedef enum
48 {
49     UART_NOTIFY_TX,
50     UART_NOTIFY_RX
51 }UartNotifyId_t;
52 
53 /*!
54  * UART object type definition
55  */
56 typedef struct
57 {
58     UartId_t UartId;
59     bool IsInitialized;
60     Gpio_t Tx;
61     Gpio_t Rx;
62     Fifo_t FifoTx;
63     Fifo_t FifoRx;
64     /*!
65      * IRQ user notification callback prototype.
66      */
67     void ( *IrqNotify )( UartNotifyId_t id );
68 }Uart_t;
69 
70 /*!
71  * Operation Mode for the UART
72  */
73 typedef enum
74 {
75     TX_ONLY = 0,
76     RX_ONLY,
77     RX_TX
78 }UartMode_t;
79 
80 /*!
81  * UART word length
82  */
83 typedef enum
84 {
85     UART_8_BIT = 0,
86     UART_9_BIT
87 }WordLength_t;
88 
89 /*!
90  * UART stop bits
91  */
92 typedef enum
93 {
94     UART_1_STOP_BIT = 0,
95     UART_0_5_STOP_BIT,
96     UART_2_STOP_BIT,
97     UART_1_5_STOP_BIT
98 }StopBits_t;
99 
100 /*!
101  * UART parity
102  */
103 typedef enum
104 {
105     NO_PARITY = 0,
106     EVEN_PARITY,
107     ODD_PARITY
108 }Parity_t;
109 
110 /*!
111  * UART flow control
112  */
113 typedef enum
114 {
115     NO_FLOW_CTRL = 0,
116     RTS_FLOW_CTRL,
117     CTS_FLOW_CTRL,
118     RTS_CTS_FLOW_CTRL
119 }FlowCtrl_t;
120 
121 /*!
122  * \brief Initializes the UART object and MCU peripheral
123  *
124  * \param [IN] obj  UART object
125  * \param [IN] tx   UART Tx pin name to be used
126  * \param [IN] rx   UART Rx pin name to be used
127  */
128 void UartInit( Uart_t *obj, UartId_t uartId, PinNames tx, PinNames rx );
129 
130 /*!
131  * \brief Configures the UART object and MCU peripheral
132  *
133  * \remark UartInit function must be called first.
134  *
135  * \param [IN] obj          UART object
136  * \param [IN] mode         Mode of operation for the UART
137  * \param [IN] baudrate     UART baudrate
138  * \param [IN] wordLength   packet length
139  * \param [IN] stopBits     stop bits setup
140  * \param [IN] parity       packet parity
141  * \param [IN] flowCtrl     UART flow control
142  */
143 void UartConfig( Uart_t *obj, UartMode_t mode, uint32_t baudrate, WordLength_t wordLength, StopBits_t stopBits, Parity_t parity, FlowCtrl_t flowCtrl );
144 
145 /*!
146  * \brief DeInitializes the UART object and MCU pins
147  *
148  * \param [IN] obj  UART object
149  */
150 void UartDeInit( Uart_t *obj );
151 
152 /*!
153  * \brief Sends a character to the UART
154  *
155  * \param [IN] obj   UART object
156  * \param [IN] data  Character to be sent
157  * \retval status    [0: OK, 1: Busy]
158  */
159 uint8_t UartPutChar( Uart_t *obj, uint8_t data );
160 
161 /*!
162  * \brief Sends a buffer to the UART
163  *
164  * \param [IN] obj    UART object
165  * \param [IN] buffer Buffer to be sent
166  * \param [IN] size   Buffer size
167  * \retval status     [0: OK, 1: Busy]
168  */
169 uint8_t UartPutBuffer( Uart_t *obj, uint8_t *buffer, uint16_t size );
170 
171 /*!
172  * \brief Gets a character from the UART
173  *
174  * \param [IN] obj   UART object
175  * \param [IN] data  Received character
176  * \retval status    [0: OK, 1: Busy]
177  */
178 uint8_t UartGetChar( Uart_t *obj, uint8_t *data );
179 
180 /*!
181  * \brief Gets a character from the UART
182  *
183  * \param [IN] obj          UART object
184  * \param [IN] buffer       Received buffer
185  * \param [IN] size         Number of bytes to be received
186  * \param [OUT] nbReadBytes Number of bytes really read
187  * \retval status           [0: OK, 1: Busy]
188  */
189 uint8_t UartGetBuffer( Uart_t *obj, uint8_t *buffer, uint16_t size, uint16_t *nbReadBytes );
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif // __UART_H__
196