1 /*
2  * SPDX-FileCopyrightText: 2015, 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #pragma once
8 #include <stdint.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 typedef void cdc_acm_device;
15 extern cdc_acm_device *uart_acm_dev;
16 
17 #define ACM_BYTES_PER_TX  64
18 
19 //ACM statuses are negative to distinguish from USB_DC_* status codes
20 #define ACM_STATUS_LINESTATE_CHANGED -1
21 #define ACM_STATUS_LINECODING_CHANGED -2
22 #define ACM_STATUS_TX -3
23 #define ACM_STATUS_RX -4
24 
25 typedef void(*uart_irq_callback_t)(cdc_acm_device *dev, int status);
26 
27 /**
28  * @brief Get amount of received characters in buffer
29  *
30  * @returns character count
31  */
32 
33 int cdc_acm_rx_fifo_cnt(cdc_acm_device *dev);
34 
35 
36 /*
37  * @brief Output a character in polled mode.
38  *
39  * The UART poll method for USB UART is simulated by waiting till
40  * we get the next BULK In upcall from the USB device controller or 100 ms.
41  *
42  * @return the same character which is sent
43  */
44 unsigned char cdc_acm_poll_out(cdc_acm_device *dev, unsigned char c);
45 
46 /**
47  * @brief Fill FIFO with data
48  *
49  * @param dev     CDC ACM device struct.
50  * @param tx_data Data to transmit.
51  * @param len     Number of bytes to send.
52  *
53  * @return Number of bytes sent.
54  */
55 int cdc_acm_fifo_fill(cdc_acm_device *dev, const uint8_t *tx_data, int len);
56 
57 /**
58  * @brief Read data from FIFO
59  *
60  * @param dev     CDC ACM device struct.
61  * @param rx_data Pointer to data container.
62  * @param size    Container size.
63  *
64  * @return Number of bytes read.
65  */
66 int cdc_acm_fifo_read(cdc_acm_device *dev, uint8_t *rx_data, const int size);
67 
68 /**
69  * @brief Enable TX interrupt
70  *
71  * @param dev CDC ACM device struct.
72  *
73  * @return N/A.
74  */
75 void cdc_acm_irq_tx_enable(cdc_acm_device *dev);
76 
77 /**
78  * @brief Disable TX interrupt
79  *
80  * @param dev CDC ACM device struct.
81  *
82  * @return N/A.
83  */
84 void cdc_acm_irq_tx_disable(cdc_acm_device *dev);
85 
86 /**
87  * @brief Check if Tx IRQ has been raised
88  *
89  * @param dev CDC ACM device struct.
90  *
91  * @return 1 if a Tx IRQ is pending, 0 otherwise.
92  */
93 int cdc_acm_irq_tx_ready(cdc_acm_device *dev);
94 
95 /**
96  * @brief Enable RX interrupt
97  *
98  * @param dev CDC ACM device struct.
99  *
100  * @return N/A
101  */
102 void cdc_acm_irq_rx_enable(cdc_acm_device *dev);
103 
104 /**
105  * @brief Disable RX interrupt
106  *
107  * @param dev CDC ACM device struct.
108  *
109  * @return N/A.
110  */
111 void cdc_acm_irq_rx_disable(cdc_acm_device *dev);
112 
113 /**
114  * @brief Enable line state interrupt
115  *
116  * @param dev CDC ACM device struct.
117  *
118  * @return N/A.
119  */
120 void cdc_acm_irq_state_enable(cdc_acm_device *dev);
121 
122 /**
123  * @brief Disable line state interrupt
124  *
125  * @param dev CDC ACM device struct.
126  *
127  * @return N/A.
128  */
129 void cdc_acm_irq_state_disable(cdc_acm_device *dev);
130 
131 
132 /**
133  * @brief Check if Rx IRQ has been raised
134  *
135  * @param dev CDC ACM device struct.
136  *
137  * @return 1 if an IRQ is ready, 0 otherwise.
138  */
139 int cdc_acm_irq_rx_ready(cdc_acm_device *dev);
140 
141 /**
142  * @brief Check if Tx or Rx IRQ is pending
143  *
144  * @param dev CDC ACM device struct.
145  *
146  * @return 1 if a Tx or Rx IRQ is pending, 0 otherwise.
147  */
148 int cdc_acm_irq_is_pending(cdc_acm_device *dev);
149 
150 /**
151  * @brief Set the callback function pointer for IRQ.
152  *
153  * @param dev CDC ACM device struct.
154  * @param cb  Callback function pointer.
155  *
156  * @return N/A
157  */
158 void cdc_acm_irq_callback_set(cdc_acm_device *dev, uart_irq_callback_t cb);
159 
160 /**
161  * @brief Manipulate line control for UART.
162  *
163  * @param dev CDC ACM device struct
164  * @param ctrl The line control to be manipulated
165  * @param val Value to set the line control
166  *
167  * @return 0 if successful, failed otherwise.
168  */
169 int cdc_acm_line_ctrl_set(cdc_acm_device *dev, uint32_t ctrl, uint32_t val);
170 
171 /**
172  * @brief Manipulate line control for UART.
173  *
174  * @param dev CDC ACM device struct
175  * @param ctrl The line control to be manipulated
176  * @param val Value to set the line control
177  *
178  * @return 0 if successful, failed otherwise.
179  */
180 int cdc_acm_line_ctrl_get(cdc_acm_device *dev, uint32_t ctrl, uint32_t *val);
181 
182 
183 /**
184  * @brief Initialize UART channel
185  *
186  * This routine is called to reset the chip in a quiescent state.
187  * It is assumed that this function is called only once per UART.
188  *
189  * @param mem_chunk Memory chunk to use for internal use
190  * @param mem_chunk_size Size of the memory chunk in bytes
191  *
192  * @return dev or NULL
193  */
194 cdc_acm_device *cdc_acm_init(void *mem_chunk, int mem_chunk_size);
195 
196 
197 /** Common line controls for UART.*/
198 #define LINE_CTRL_BAUD_RATE (1 << 0)
199 #define LINE_CTRL_RTS       (1 << 1)
200 #define LINE_CTRL_DTR       (1 << 2)
201 #define LINE_CTRL_DCD       (1 << 3)
202 #define LINE_CTRL_DSR       (1 << 4)
203 
204 /* Common communication errors for UART.*/
205 
206 /** @brief Overrun error */
207 #define UART_ERROR_OVERRUN  (1 << 0)
208 
209 /** @brief Parity error */
210 #define UART_ERROR_PARITY   (1 << 1)
211 
212 /** @brief Framing error */
213 #define UART_ERROR_FRAMING  (1 << 2)
214 
215 /**
216  * @brief Break interrupt error:
217  *
218  * A break interrupt was received. This happens when the serial input is
219  * held at a logic '0' state for longer than the sum of start time + data bits
220  * + parity + stop bits.
221  */
222 #define UART_ERROR_BREAK    (1 << 3)
223 
224 #ifdef __cplusplus
225 }
226 #endif
227