1 /*
2  * Copyright (C) 2017-2020 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv/usi_usart.h
7  * @brief    Header File for USART Driver
8  * @version  V1.0
9  * @date     02. June 2020
10  * @model    usi_usart
11  ******************************************************************************/
12 
13 #ifndef _DRV_USI_USART_H_
14 #define _DRV_USI_USART_H_
15 
16 #include <drv/common.h>
17 #include <drv/uart.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24   \brief       Initialize UART Interface. 1. Initializes the resources needed for the UART interface 2.registers event callback function
25   \param[in]   uart      Operate handle.
26   \param[in]   idx       The device idx
27   \param[in]   cb_event  Event call back function \ref uart_event_cb_t
28   \param[in]   arg       User can define it by himself
29   \return      error code
30 */
31 csi_error_t csi_usi_uart_init(csi_uart_t *uart, uint32_t idx);
32 
33 /**
34   \brief       De-initialize UART Interface. stops operation and releases the software resources used by the interface
35   \param[in]   uart  Operate handle.
36   \return      Error code
37 */
38 void csi_usi_uart_uninit(csi_uart_t *uart);
39 
40 /**
41   \brief       Attach the callback handler to UART
42   \param[in]   uart  Operate handle.
43   \param[in]   cb    Callback function
44   \param[in]   arg   User can define it by himself as callback's param
45   \return      Error code
46 */
47 csi_error_t csi_usi_uart_attach_callback(csi_uart_t *uart, void * cb, void *arg);
48 
49 /**
50   \brief       Detach the callback handler
51   \param[in]   uart  Operate handle.
52 */
53 void  csi_usi_uart_detach_callback(csi_uart_t *uart);
54 
55 /**
56   \brief       Config the baudrate.
57   \param[in]   uart  UART handle to operate.
58   \param[in]   baud  UART baudrate
59   \return      Error code
60 */
61 csi_error_t csi_usi_uart_baud(csi_uart_t *uart, uint32_t baud);
62 
63 /**
64   \brief       Config the uart format.
65   \param[in]   uart      UART handle to operate.
66   \param[in]   data_bit  UART data bits
67   \param[in]   parity    UART data parity
68   \param[in]   stop_bit  UART stop bits
69   \return      Error code
70 */
71 csi_error_t csi_usi_uart_format(csi_uart_t *uart,  csi_uart_data_bits_t data_bits,
72                             csi_uart_parity_t parity, csi_uart_stop_bits_t stop_bits);
73 
74 /**
75   \brief       Config the uart flow control.
76   \param[in]   uart      UART handle to operate.
77   \param[in]   flowctrl  UART flow control
78   \return      Error code
79 */
80 csi_error_t csi_usi_uart_flowctrl(csi_uart_t *uart,  csi_uart_flowctrl_t flowctrl);
81 
82 /**
83   \brief       Start sending data to UART transmitter.
84   \param[in]   uart     UART handle to operate.
85   \param[in]   data     Pointer to buffer with data to send to UART transmitter. data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
86   \param[in]   num      Number of data items to send (byte)
87   \param[in]   Timeout  is the number of queries, not time
88   \return      The num of data witch is send successful
89 */
90 int32_t csi_usi_uart_send(csi_uart_t *uart, const void *data, uint32_t size, uint32_t timeout);
91 
92 /**
93   \brief       Start sending data to UART transmitter (interrupt mode).
94   \param[in]   uart   UART handle to operate.
95   \param[in]   data     Pointer to buffer with data to send to UART transmitter. data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
96   \param[in]   num      Number of data items to send
97   \return      The status of send func
98 */
99 csi_error_t csi_usi_uart_send_async(csi_uart_t *uart, const void *data, uint32_t size);
100 
101 /**
102   \brief       Get the num of data in RX_FIFO.
103   \param[in]   uart   UART handle to operate.
104   \return      The num of data in RX_FIFO
105 */
106 uint32_t csi_usi_uart_get_recvfifo_waiting_num(csi_uart_t *uart);
107 
108 /**
109   \brief       Start receiving data from UART receiver. \n
110                This function is non-blocking,\ref uart_event_e is signaled when operation completes or error happens.
111                \ref csi_uart_get_status can get operation status.
112   \param[in]   uart  UART handle to operate.
113   \param[out]  data  Pointer to buffer for data to receive from UART receiver.data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
114   \param[in]   num   Number of data items to receive
115   \return      Error code
116 */
117 csi_error_t csi_usi_uart_receive_async(csi_uart_t *uart, void *data, uint32_t size);
118 
119 /**
120   \brief       Query data from UART receiver FIFO.
121   \param[in]   uart  UART handle to operate.
122   \param[out]  data  Pointer to buffer for data to receive from UART receiver
123   \param[in]   num   Number of data items to receive
124   \param[in]   Timeout  is the number of queries, not time
125   \return      FIFO data num to receive
126 */
127 int32_t csi_usi_uart_receive(csi_uart_t *uart, void *data, uint32_t size, uint32_t timeout);
128 
129 /**
130   \brief       Get character in query mode.
131   \param[in]   uart  UART handle to operate.
132   \param[out]  ch The pointer to the received character.
133   \return      Error code
134 */
135 uint8_t  csi_usi_uart_getchar(csi_uart_t *uart);
136 
137 /**
138   \brief       Transmit character in query mode.
139   \param[in]   uart  UART handle to operate.
140   \param[in]   ch  The input character
141   \return      Error code
142 */
143 void csi_usi_uart_putchar(csi_uart_t *uart, uint8_t ch);
144 
145 /**
146   \brief       Link DMA channel to uart device
147   \param[in]   uart  UART handle to operate.
148   \param[in]   tx_dma The DMA channel handle for send, when it is NULL means to unlink the channel
149   \param[in]   rx_dma The DMA channel handle for receive, when it is NULL means to unlink the channel
150   \return      Error code
151 */
152 csi_error_t csi_usi_uart_link_dma(csi_uart_t *uart, csi_dma_ch_t *tx_dma, csi_dma_ch_t *rx_dma);
153 
154 /**
155   \brief       Get the state of uart device.
156   \param[in]   uart   UART handle to operate.
157   \param[out]  state  The state of uart device.
158   \return      Error code.
159 */
160 csi_error_t csi_usi_uart_get_state(csi_uart_t *uart, csi_state_t *state);
161 
162 /**
163   \brief       Enable uart power manage.
164   \param[in]   uart   UART handle to operate.
165   \return      Error code.
166 */
167 csi_error_t csi_usi_uart_enable_pm(csi_uart_t *uart);
168 
169 /**
170   \brief       Disable uart power manage.
171   \param[in]   uart   UART handle to operate.
172 */
173 void csi_usi_uart_disable_pm(csi_uart_t *uart);
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 #endif /* _DRV_USI_USART_H_ */
179