1 /*
2  * ===========================================================================================
3  *
4  *       Filename:  hal_uart.h
5  *
6  *    Description:  UART HAL definition.
7  *
8  *        Version:  Melis3.0
9  *         Create:  2019-11-14 11:11:56
10  *       Revision:  none
11  *       Compiler:  GCC:version 9.2.1 20170904 (release),SUNXI_HAL/embedded-7-branch revision 255204
12  *
13  *         Author:  bantao@allwinnertech.com
14  *   Organization:  SWC-BPD
15  *  Last Modified:  2020-04-02 19:39:41
16  *
17  * ===========================================================================================
18  */
19 
20 #ifndef SUNXI_HAL_UART_H
21 #define SUNXI_HAL_UART_H
22 
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27 
28 #include "sunxi_hal_common.h"
29 /*
30  * include the platform uart header file.
31  */
32 #include <platform-uart.h>
33 
34 /*
35  * This enum defines return status of the UART HAL public API.
36  * User should check return value after calling these APIs.
37  */
38 typedef enum
39 {
40     HAL_UART_STATUS_ERROR_PARAMETER = -4,      /**< Invalid user input parameter. */
41     HAL_UART_STATUS_ERROR_BUSY = -3,           /**< UART port is currently in use. */
42     HAL_UART_STATUS_ERROR_UNINITIALIZED = -2,  /**< UART port has not been initialized. */
43     HAL_UART_STATUS_ERROR = -1,                /**< UART driver detected a common error. */
44     HAL_UART_STATUS_OK = 0                     /**< UART function executed successfully. */
45 } hal_uart_status_t;
46 
47 typedef enum
48 {
49     UART_0 = 0,
50     UART_1,
51     UART_2,
52     UART_3,
53     UART_MAX,
54 } uart_port_t;
55 
56 #define CLI_UART_PORT  (UART_0)
57 
58 /* This enum defines baud rate of the UART frame. */
59 typedef enum
60 {
61     UART_BAUDRATE_300 = 0,
62     UART_BAUDRATE_600,
63     UART_BAUDRATE_1200,
64     UART_BAUDRATE_2400,
65     UART_BAUDRATE_4800,
66     UART_BAUDRATE_9600,
67     UART_BAUDRATE_19200,
68     UART_BAUDRATE_38400,
69     UART_BAUDRATE_57600,
70     UART_BAUDRATE_115200,
71     UART_BAUDRATE_230400,
72     UART_BAUDRATE_576000,
73     UART_BAUDRATE_921600,
74     UART_BAUDRATE_500000,
75     UART_BAUDRATE_1000000,
76     UART_BAUDRATE_1500000,
77     UART_BAUDRATE_3000000,
78     UART_BAUDRATE_4000000,
79     UART_BAUDRATE_MAX,
80 } uart_baudrate_t;
81 
82 /* This enum defines word length of the UART frame. */
83 typedef enum
84 {
85     UART_WORD_LENGTH_5 = 0,
86     UART_WORD_LENGTH_6,
87     UART_WORD_LENGTH_7,
88     UART_WORD_LENGTH_8,
89 } uart_word_length_t;
90 
91 /* This enum defines stop bit of the UART frame. */
92 typedef enum
93 {
94     UART_STOP_BIT_1 = 0,
95     UART_STOP_BIT_2,
96 } uart_stop_bit_t;
97 
98 /* This enum defines parity of the UART frame. */
99 typedef enum
100 {
101     UART_PARITY_NONE = 0,
102     UART_PARITY_ODD,
103     UART_PARITY_EVEN
104 } uart_parity_t;
105 
106 /* This struct defines UART configure parameters. */
107 typedef struct
108 {
109     uart_baudrate_t baudrate;
110     uart_word_length_t word_length;
111     uart_stop_bit_t stop_bit;
112     uart_parity_t parity;
113 } _uart_config_t;
114 
115 
116 /* UART HAL Layer API Version */
117 #define SUNXI_HAL_UART_API_VERSION               SUNXI_HAL_VERSION_MAJOR_MINOR(1, 0)
118 
119 /* Driver version */
120 #define SUNXI_HAL_UART_DRV_VERSION               SUNXI_HAL_VERSION_MAJOR_MINOR(1, 0)
121 
122 //======================================reg==========================================================//
123 #define UART_INVAL_DATA_IND     (0xffffffff)
124 
125 #undef BIT
126 #define BIT(nr)     (1UL << (nr))
127 
128 //=================================reg===================================================//
129 /*
130  * brief UART Status
131  */
132 typedef struct sunxi_hal_uart_status
133 {
134     uint32_t tx_busy          : 1;        ///< Transmitter busy flag
135     uint32_t rx_busy          : 1;        ///< Receiver busy flag
136     uint32_t tx_underflow     : 1;        ///< Transmit data underflow detected (cleared on start of next send operation)
137     uint32_t rx_overflow      : 1;        ///< Receive data overflow detected (cleared on start of next receive operation)
138     uint32_t rx_break         : 1;        ///< Break detected on receive (cleared on start of next receive operation)
139     uint32_t rx_framing_error : 1;        ///< Framing error detected on receive (cleared on start of next receive operation)
140     uint32_t rx_parity_error  : 1;        ///< Parity error detected on receive (cleared on start of next receive operation)
141     uint32_t reserved         : 25;
142 } sunxi_hal_uart_status_t;
143 
144 /*
145  *brief UART Modem Control
146  */
147 typedef enum sunxi_hal_uart_modem_control
148 {
149     SUNXI_HAL_UART_RTS_CLEAR,            ///< Deactivate RTS
150     SUNXI_HAL_UART_RTS_SET,              ///< Activate RTS
151     SUNXI_HAL_UART_DTR_CLEAR,            ///< Deactivate DTR
152     SUNXI_HAL_UART_DTR_SET               ///< Activate DTR
153 } sunxi_hal_uart_modem_control_e;
154 
155 /*
156  *brief UART Modem Status
157  */
158 typedef struct sunxi_hal_uart_modem_status
159 {
160     uint32_t cts      : 1;                ///< CTS state: 1=Active, 0=Inactive
161     uint32_t dsr      : 1;                ///< DSR state: 1=Active, 0=Inactive
162     uint32_t dcd      : 1;                ///< DCD state: 1=Active, 0=Inactive
163     uint32_t ri       : 1;                ///< RI  state: 1=Active, 0=Inactive
164     uint32_t reserved : 28;
165 } sunxi_hal_uart_modem_status_t;
166 
167 /****** UART Event *****/
168 #define SUNXI_HAL_UART_EVENT_SEND_COMPLETE       (1UL << 0)  ///< Send completed; however UART may still transmit data
169 #define SUNXI_HAL_UART_EVENT_RECEIVE_COMPLETE    (1UL << 1)  ///< Receive completed
170 #define SUNXI_HAL_UART_EVENT_TRANSFER_COMPLETE   (1UL << 2)  ///< Transfer completed
171 #define SUNXI_HAL_UART_EVENT_TX_COMPLETE         (1UL << 3)  ///< Transmit completed (optional)
172 #define SUNXI_HAL_UART_EVENT_TX_UNDERFLOW        (1UL << 4)  ///< Transmit data not available (Synchronous Slave)
173 #define SUNXI_HAL_UART_EVENT_RX_OVERFLOW         (1UL << 5)  ///< Receive data overflow
174 #define SUNXI_HAL_UART_EVENT_RX_TIMEOUT          (1UL << 6)  ///< Receive character timeout (optional)
175 #define SUNXI_HAL_UART_EVENT_RX_BREAK            (1UL << 7)  ///< Break detected on receive
176 #define SUNXI_HAL_UART_EVENT_RX_FRAMING_ERROR    (1UL << 8)  ///< Framing error detected on receive
177 #define SUNXI_HAL_UART_EVENT_RX_PARITY_ERROR     (1UL << 9)  ///< Parity error detected on receive
178 #define SUNXI_HAL_UART_EVENT_CTS                 (1UL << 10) ///< CTS state changed (optional)
179 #define SUNXI_HAL_UART_EVENT_DSR                 (1UL << 11) ///< DSR state changed (optional)
180 #define SUNXI_HAL_UART_EVENT_DCD                 (1UL << 12) ///< DCD state changed (optional)
181 #define SUNXI_HAL_UART_EVENT_RI                  (1UL << 13) ///< RI  state changed (optional)
182 
183 
184 
185 
186 /* This enum defines the UART event when an interrupt occurs. */
187 typedef enum
188 {
189     UART_EVENT_TRANSACTION_ERROR = -1,
190     UART_EVENT_RX_BUFFER_ERROR = -2,
191     UART_EVENT_TX_COMPLETE = 1,
192     UART_EVENT_RX_COMPLETE = 2,
193 } uart_callback_event_t;
194 
195 /** @brief This typedef defines user's callback function prototype.
196  *             This callback function will be called in UART interrupt handler when UART interrupt is raised.
197  *             User should call uart_register_callback() to register callbacks to UART driver explicitly.
198  *             Note, that the callback function is not appropriate for time-consuming operations. \n
199  *             parameter "event" : for more information, please refer to description of #uart_callback_event_t.
200  *             parameter "user_data" : a user defined data used in the callback function.
201  */
202 typedef void (*uart_callback_t)(uart_callback_event_t event, void *user_data);
203 
204 typedef struct
205 {
206     uint8_t *buf;
207     uint32_t len;
208     uint32_t head;
209     uint32_t tail;
210     int32_t cnt;
211 } uart_ring_buf_t;
212 
213 /* This struct defines UART private data */
214 typedef struct
215 {
216     /* basic info */
217     uart_port_t uart_port;
218     uint32_t irqn;
219 
220     /* uart register value */
221     unsigned char ier;
222     unsigned char lcr;
223     unsigned char mcr;
224     unsigned char fcr;
225     unsigned char dll;
226     unsigned char dlh;
227 
228     /* tx & rx buf */
229     const char *tx_buf;
230     uint32_t tx_buf_size;
231     /* rx ring buf */
232     uart_ring_buf_t ring_buf;
233 
234     /* user callback */
235     uart_callback_t func;
236     void *arg;
237 } uart_priv_t;
238 
239 
240 
241 /**
242   \fn          sunxi_hal_version_t SUNXI_HAL_UART_GetVersion (void)
243   \brief       Get driver version.
244   \return      \ref sunxi_hal_version_t
245 
246   \fn          SUNXI_HAL_UART_CAPABILITIES SUNXI_HAL_UART_GetCapabilities (void)
247   \brief       Get driver capabilities
248   \return      \ref SUNXI_HAL_UART_CAPABILITIES
249 
250   \fn          int32_t SUNXI_HAL_UART_Initialize (SUNXI_HAL_UART_SignalEvent_t cb_event)
251   \brief       Initialize UART Interface.
252   \param[in]   cb_event  Pointer to \ref SUNXI_HAL_UART_SignalEvent
253   \return      \ref execution_status
254 
255   \fn          int32_t SUNXI_HAL_UART_Uninitialize (void)
256   \brief       De-initialize UART Interface.
257   \return      \ref execution_status
258 
259   \fn          int32_t SUNXI_HAL_UART_PowerControl (SUNXI_HAL_POWER_STATE state)
260   \brief       Control UART Interface Power.
261   \param[in]   state  Power state
262   \return      \ref execution_status
263 
264   \fn          int32_t SUNXI_HAL_UART_Send (const void *data, uint32_t num)
265   \brief       Start sending data to UART transmitter.
266   \param[in]   data  Pointer to buffer with data to send to UART transmitter
267   \param[in]   num   Number of data items to send
268   \return      \ref execution_status
269 
270   \fn          int32_t SUNXI_HAL_UART_Receive (void *data, uint32_t num)
271   \brief       Start receiving data from UART receiver.
272   \param[out]  data  Pointer to buffer for data to receive from UART receiver
273   \param[in]   num   Number of data items to receive
274   \return      \ref execution_status
275 
276   \fn          int32_t SUNXI_HAL_UART_Transfer (const void *data_out,
277                                                  void *data_in,
278                                            uint32_t    num)
279   \brief       Start sending/receiving data to/from UART transmitter/receiver.
280   \param[in]   data_out  Pointer to buffer with data to send to UART transmitter
281   \param[out]  data_in   Pointer to buffer for data to receive from UART receiver
282   \param[in]   num       Number of data items to transfer
283   \return      \ref execution_status
284 
285   \fn          uint32_t SUNXI_HAL_UART_GetTxCount (void)
286   \brief       Get transmitted data count.
287   \return      number of data items transmitted
288 
289   \fn          uint32_t SUNXI_HAL_UART_GetRxCount (void)
290   \brief       Get received data count.
291   \return      number of data items received
292 
293   \fn          int32_t SUNXI_HAL_UART_Control (uint32_t control, uint32_t arg)
294   \brief       Control UART Interface.
295   \param[in]   control  Operation
296   \param[in]   arg      Argument of operation (optional)
297   \return      common \ref execution_status and driver specific \ref uart_execution_status
298 
299   \fn          SUNXI_HAL_UART_STATUS SUNXI_HAL_UART_GetStatus (void)
300   \brief       Get UART status.
301   \return      UART status \ref SUNXI_HAL_UART_STATUS
302 
303   \fn          int32_t SUNXI_HAL_UART_SetModemControl (SUNXI_HAL_UART_MODEM_CONTROL control)
304   \brief       Set UART Modem Control line state.
305   \param[in]   control  \ref SUNXI_HAL_UART_MODEM_CONTROL
306   \return      \ref execution_status
307 
308   \fn          SUNXI_HAL_UART_MODEM_STATUS SUNXI_HAL_UART_GetModemStatus (void)
309   \brief       Get UART Modem Status lines state.
310   \return      modem status \ref SUNXI_HAL_UART_MODEM_STATUS
311 
312   \fn          void SUNXI_HAL_UART_SignalEvent (uint32_t event)
313   \brief       Signal UART Events.
314   \param[in]   event  \ref UART_events notification mask
315   \return      none
316 */
317 
318 
319 typedef void (*sunxi_hal_uart_signal_event_t)(uint32_t event);   ///< Pointer to \ref SUNXI_HAL_UART_SignalEvent : Signal UART Event.
320 
321 
322 /**
323 \brief UART Device Driver Capabilities.
324 */
325 typedef struct sunxi_hal_uart_capabilities
326 {
327     uint32_t asynchronous       : 1;      ///< supports UART (Asynchronous) mode
328     uint32_t synchronous_master : 1;      ///< supports Synchronous Master mode
329     uint32_t synchronous_slave  : 1;      ///< supports Synchronous Slave mode
330     uint32_t single_wire        : 1;      ///< supports UART Single-wire mode
331     uint32_t irda               : 1;      ///< supports UART IrDA mode
332     uint32_t smart_card         : 1;      ///< supports UART Smart Card mode
333     uint32_t smart_card_clock   : 1;      ///< Smart Card Clock generator available
334     uint32_t flow_control_rts   : 1;      ///< RTS Flow Control available
335     uint32_t flow_control_cts   : 1;      ///< CTS Flow Control available
336     uint32_t event_tx_complete  : 1;      ///< Transmit completed event: \ref SUNXI_HAL_UART_EVENT_TX_COMPLETE
337     uint32_t event_rx_timeout   : 1;      ///< Signal receive character timeout event: \ref SUNXI_HAL_UART_EVENT_RX_TIMEOUT
338     uint32_t rts                : 1;      ///< RTS Line: 0=not available, 1=available
339     uint32_t cts                : 1;      ///< CTS Line: 0=not available, 1=available
340     uint32_t dtr                : 1;      ///< DTR Line: 0=not available, 1=available
341     uint32_t dsr                : 1;      ///< DSR Line: 0=not available, 1=available
342     uint32_t dcd                : 1;      ///< DCD Line: 0=not available, 1=available
343     uint32_t ri                 : 1;      ///< RI Line: 0=not available, 1=available
344     uint32_t event_cts          : 1;      ///< Signal CTS change event: \ref SUNXI_HAL_UART_EVENT_CTS
345     uint32_t event_dsr          : 1;      ///< Signal DSR change event: \ref SUNXI_HAL_UART_EVENT_DSR
346     uint32_t event_dcd          : 1;      ///< Signal DCD change event: \ref SUNXI_HAL_UART_EVENT_DCD
347     uint32_t event_ri           : 1;      ///< Signal RI change event: \ref SUNXI_HAL_UART_EVENT_RI
348     uint32_t reserved           : 11;     ///< Reserved (must be zero)
349 } sunxi_hal_uart_capabilities_t;
350 
351 typedef struct sunxi_hal_driver_usart
352 {
353 
354     ///< Pointer to \ref SUNXI_HAL_USART_GetVersion : Get driver version.
355     sunxi_hal_version_t (*get_version)(int32_t dev);
356 
357     ///< Pointer to \ref SUNXI_HAL_USART_GetCapabilities : Get driver capabilities.
358     sunxi_hal_uart_capabilities_t (*get_capabilities)(int32_t dev);
359 
360     ///< Pointer to \ref SUNXI_HAL_USART_Initialize : Initialize USART Interface.
361     int32_t (*initialize)(int32_t uart_port);
362 
363     ///< Pointer to \ref SUNXI_HAL_USART_Uninitialize : De-initialize USART Interface.
364     int32_t (*uninitialize)(int32_t uart_port);
365 
366     ///< Pointer to \ref SUNXI_HAL_USART_PowerControl : Control USART Interface Power.
367     int32_t (*power_control)(int32_t dev, sunxi_hal_power_state_e state);
368 
369     ///< Pointer to \ref SUNXI_HAL_USART_Send : Start sending data to USART transmitter.
370     int32_t (*send)(int32_t dev, const uint8_t *data, uint32_t num);
371 
372     ///< Pointer to \ref SUNXI_HAL_USART_Receive : Start receiving data from USART receiver.
373     int32_t (*receive)(int32_t dev, int *data, uint32_t num);
374 
375     ///< Pointer to \ref SUNXI_HAL_USART_Transfer : Start sending/receiving data to/from USART.
376     int32_t (*transfer)(int32_t dev, const void *data_out, void *data_in, uint32_t    num);
377 
378     ///< Pointer to \ref SUNXI_HAL_USART_GetTxCount : Get transmitted data count.
379     uint32_t (*get_tx_count)(int32_t dev);
380 
381     ///< Pointer to \ref SUNXI_HAL_USART_GetRxCount : Get received data count.
382     uint32_t (*get_rx_count)(int32_t dev);
383 
384     ///< Pointer to \ref SUNXI_HAL_USART_Control : Control USART Interface.
385     int32_t (*control)(int32_t uart_port, int cmd, void *args);
386 
387     ///< Pointer to \ref SUNXI_HAL_USART_GetStatus : Get USART status.
388     sunxi_hal_uart_status_t (*get_status)(int32_t dev);
389     ///< Pointer to \ref SUNXI_HAL_USART_SetModemControl : Set USART Modem Control line state.
390     int32_t (*set_modem_control)(int32_t dev, sunxi_hal_uart_modem_control_e control);
391 
392     ///< Pointer to \ref SUNXI_HAL_USART_GetModemStatus : Get USART Modem Status lines state.
393     sunxi_hal_uart_modem_status_t (*get_modem_status)(int32_t dev);
394 
395     int32_t (*receive_polling)(int32_t dev, uint8_t *data, uint32_t num);
396     sunxi_hal_poll_ops *poll_ops;
397 } const sunxi_hal_driver_usart_t;
398 #if 0
399 #define POLLIN          (0x01)
400 #define POLLRDNORM      (0x01)
401 #define POLLRDBAND      (0x01)
402 #define POLLPRI         (0x01)
403 
404 #define POLLOUT         (0x02)
405 #define POLLWRNORM      (0x02)
406 #define POLLWRBAND      (0x02)
407 
408 #define POLLERR         (0x04)
409 #define POLLHUP         (0x08)
410 #define POLLNVAL        (0x10)
411 #endif
412 sunxi_hal_version_t hal_uart_get_version(int32_t dev);
413 sunxi_hal_uart_capabilities_t hal_uart_get_capabilities(int32_t dev);
414 int32_t hal_uart_init(int32_t uart_port);
415 int32_t hal_uart_deinit(int32_t uart_port);
416 int32_t hal_uart_power_control(int32_t dev, sunxi_hal_power_state_e state);
417 int32_t hal_uart_send(int32_t dev, const uint8_t *data, uint32_t num);
418 int32_t hal_uart_put_char(int32_t dev, char c);
419 int32_t hal_uart_receive(int32_t dev, uint8_t *data, uint32_t num);
420 int32_t hal_uart_receive_no_block(int32_t dev, uint8_t *data, uint32_t num, int32_t timeout);
421 uint8_t hal_uart_get_char(int32_t dev);
422 uint32_t hal_uart_get_tx_count(int32_t dev);
423 uint32_t hal_uart_get_rx_count(int32_t dev);
424 int32_t hal_uart_control(int32_t uart_port, int cmd, void *args);
425 sunxi_hal_uart_status_t hal_uart_get_status(int32_t dev);
426 int32_t hal_uart_transfer(int32_t dev, const void *data_out,
427                         void *data_in, uint32_t num);
428 int32_t hal_uart_set_modem_control(int32_t dev,
429                         sunxi_hal_uart_modem_control_e control);
430 sunxi_hal_uart_modem_status_t hal_uart_get_modem_status(int32_t dev);
431 int32_t hal_uart_receive_polling(int32_t dev, uint8_t *data, uint32_t num);
432 int32_t hal_uart_check_poll_state(int32_t dev_id, short key);
433 int32_t hal_uart_poll_wakeup(int32_t dev_id, short key);
434 int32_t hal_uart_register_poll_wakeup(poll_wakeup_func poll_wakeup);
435 void hal_uart_set_hardware_flowcontrol(uart_port_t uart_port);
436 void hal_uart_disable_flowcontrol(uart_port_t uart_port);
437 void hal_uart_set_loopback(uart_port_t uart_port, bool enable);
438 void sunxi_driver_uart_init(void);
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif
445