]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/udi_cdc.h
Massdrop keyboard support (#3780)
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / usb / udi_cdc.h
1 /**
2  * \file
3  *
4  * \brief USB Device Communication Device Class (CDC) interface definitions.
5  *
6  * Copyright (c) 2009-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46
47 #ifndef _UDI_CDC_H_
48 #define _UDI_CDC_H_
49
50 #ifdef CDC
51
52 #include "conf_usb.h"
53 #include "usb_protocol.h"
54 #include "usb_protocol_cdc.h"
55 #include "udd.h"
56 #include "udc_desc.h"
57 #include "udi.h"
58
59 // Check the number of port
60 #ifndef  UDI_CDC_PORT_NB
61 # define  UDI_CDC_PORT_NB 1
62 #endif
63 #if (UDI_CDC_PORT_NB > 1)
64 # error UDI_CDC_PORT_NB must be at most 1
65 #endif
66
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70
71 /**
72  * \addtogroup udi_cdc_group_udc
73  * @{
74  */
75
76 //! Global structure which contains standard UDI API for UDC
77 extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_comm;
78 extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_data;
79 //@}
80
81 //#define CDC_ACM_SIZE  64  see usb_protocol_cdc.h
82 //#define CDC_RX_SIZE   64
83
84 //! CDC communication endpoints size for all speeds
85 #define UDI_CDC_COMM_EP_SIZE        CDC_ACM_SIZE
86 //! CDC data endpoints size for FS speed (8B, 16B, 32B, 64B)
87 #define UDI_CDC_DATA_EPS_FS_SIZE    CDC_RX_SIZE
88
89 #define CDC_PRINT_BUF_SIZE  256
90 extern char printbuf[CDC_PRINT_BUF_SIZE];
91
92 //@}
93
94 /**
95  * \ingroup udi_group
96  * \defgroup udi_cdc_group USB Device Interface (UDI) for Communication Class Device (CDC)
97  *
98  * Common APIs used by high level application to use this USB class.
99  *
100  * These routines are used to transfer and control data
101  * to/from USB CDC endpoint.
102  *
103  * See \ref udi_cdc_quickstart.
104  * @{
105  */
106
107 /**
108  * \name Interface for application with single CDC interface support
109  */
110 //@{
111
112 /**
113  * \brief Notify a state change of DCD signal
114  *
115  * \param b_set      DCD is enabled if true, else disabled
116  */
117 void udi_cdc_ctrl_signal_dcd(bool b_set);
118
119 /**
120  * \brief Notify a state change of DSR signal
121  *
122  * \param b_set      DSR is enabled if true, else disabled
123  */
124 void udi_cdc_ctrl_signal_dsr(bool b_set);
125
126 /**
127  * \brief Notify a framing error
128  */
129 void udi_cdc_signal_framing_error(void);
130
131 /**
132  * \brief Notify a parity error
133  */
134 void udi_cdc_signal_parity_error(void);
135
136 /**
137  * \brief Notify a overrun
138  */
139 void udi_cdc_signal_overrun(void);
140
141 /**
142  * \brief Gets the number of byte received
143  *
144  * \return the number of data available
145  */
146 iram_size_t udi_cdc_get_nb_received_data(void);
147
148 /**
149  * \brief This function checks if a character has been received on the CDC line
150  *
151  * \return \c 1 if a byte is ready to be read.
152  */
153 bool udi_cdc_is_rx_ready(void);
154
155 /**
156  * \brief Waits and gets a value on CDC line
157  *
158  * \return value read on CDC line
159  */
160 int udi_cdc_getc(void);
161
162 /**
163  * \brief Reads a RAM buffer on CDC line
164  *
165  * \param buf       Values read
166  * \param size      Number of value read
167  *
168  * \return the number of data remaining
169  */
170 iram_size_t udi_cdc_read_buf(void* buf, iram_size_t size);
171
172 /**
173  * \brief Non polling reads of a up to 'size' data from CDC line
174  *
175  * \param port      Communication port number to manage
176  * \param buf       Buffer where to store read data
177  * \param size      Maximum number of data to read (size of buffer)
178  *
179  * \return the number of data effectively read
180  */
181 iram_size_t udi_cdc_read_no_polling(void* buf, iram_size_t size);
182
183 /**
184  * \brief Gets the number of free byte in TX buffer
185  *
186  * \return the number of free byte in TX buffer
187  */
188 iram_size_t udi_cdc_get_free_tx_buffer(void);
189
190 /**
191  * \brief This function checks if a new character sent is possible
192  * The type int is used to support scanf redirection from compiler LIB.
193  *
194  * \return \c 1 if a new character can be sent
195  */
196 bool udi_cdc_is_tx_ready(void);
197
198 /**
199  * \brief Puts a byte on CDC line
200  * The type int is used to support printf redirection from compiler LIB.
201  *
202  * \param value      Value to put
203  *
204  * \return \c 1 if function was successfully done, otherwise \c 0.
205  */
206 int udi_cdc_putc(int value);
207
208 /**
209  * \brief Writes a RAM buffer on CDC line
210  *
211  * \param buf       Values to write
212  * \param size      Number of value to write
213  *
214  * \return the number of data remaining
215  */
216 iram_size_t udi_cdc_write_buf(const void* buf, iram_size_t size);
217 //@}
218
219 /**
220  * \name Interface for application with multi CDC interfaces support
221  */
222 //@{
223
224 /**
225  * \brief Notify a state change of DCD signal
226  *
227  * \param port       Communication port number to manage
228  * \param b_set      DCD is enabled if true, else disabled
229  */
230 void udi_cdc_multi_ctrl_signal_dcd(uint8_t port, bool b_set);
231
232 /**
233  * \brief Notify a state change of DSR signal
234  *
235  * \param port       Communication port number to manage
236  * \param b_set      DSR is enabled if true, else disabled
237  */
238 void udi_cdc_multi_ctrl_signal_dsr(uint8_t port, bool b_set);
239
240 /**
241  * \brief Notify a framing error
242  *
243  * \param port       Communication port number to manage
244  */
245 void udi_cdc_multi_signal_framing_error(uint8_t port);
246
247 /**
248  * \brief Notify a parity error
249  *
250  * \param port       Communication port number to manage
251  */
252 void udi_cdc_multi_signal_parity_error(uint8_t port);
253
254 /**
255  * \brief Notify a overrun
256  *
257  * \param port       Communication port number to manage
258  */
259 void udi_cdc_multi_signal_overrun(uint8_t port);
260
261 /**
262  * \brief Gets the number of byte received
263  *
264  * \param port       Communication port number to manage
265  *
266  * \return the number of data available
267  */
268 iram_size_t udi_cdc_multi_get_nb_received_data(uint8_t port);
269
270 /**
271  * \brief This function checks if a character has been received on the CDC line
272  *
273  * \param port       Communication port number to manage
274  *
275  * \return \c 1 if a byte is ready to be read.
276  */
277 bool udi_cdc_multi_is_rx_ready(uint8_t port);
278
279 /**
280  * \brief Waits and gets a value on CDC line
281  *
282  * \param port       Communication port number to manage
283  *
284  * \return value read on CDC line
285  */
286 int udi_cdc_multi_getc(uint8_t port);
287
288 /**
289  * \brief Reads a RAM buffer on CDC line
290  *
291  * \param port       Communication port number to manage
292  * \param buf       Values read
293  * \param size      Number of values read
294  *
295  * \return the number of data remaining
296  */
297 iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size);
298
299 /**
300  * \brief Gets the number of free byte in TX buffer
301  *
302  * \param port       Communication port number to manage
303  *
304  * \return the number of free byte in TX buffer
305  */
306 iram_size_t udi_cdc_multi_get_free_tx_buffer(uint8_t port);
307
308 /**
309  * \brief This function checks if a new character sent is possible
310  * The type int is used to support scanf redirection from compiler LIB.
311  *
312  * \param port       Communication port number to manage
313  *
314  * \return \c 1 if a new character can be sent
315  */
316 bool udi_cdc_multi_is_tx_ready(uint8_t port);
317
318 /**
319  * \brief Puts a byte on CDC line
320  * The type int is used to support printf redirection from compiler LIB.
321  *
322  * \param port       Communication port number to manage
323  * \param value      Value to put
324  *
325  * \return \c 1 if function was successfully done, otherwise \c 0.
326  */
327 int udi_cdc_multi_putc(uint8_t port, int value);
328
329 /**
330  * \brief Writes a RAM buffer on CDC line
331  *
332  * \param port       Communication port number to manage
333  * \param buf       Values to write
334  * \param size      Number of value to write
335  *
336  * \return the number of data remaining
337  */
338 iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t size);
339 //@}
340
341 #define CDC_PRINTBUF_SIZE  256
342 extern char printbuf[CDC_PRINTBUF_SIZE];
343
344 #define CDC_INBUF_SIZE 256
345
346 typedef struct {
347     uint32_t count;
348     uint32_t lastcount;
349     char buf[CDC_INBUF_SIZE];
350 } inbuf_t;
351
352 #else //CDC
353
354 // keep these to accommodate calls if remaining
355 #define CDC_PRINTBUF_SIZE  1
356 extern char printbuf[CDC_PRINTBUF_SIZE];
357
358 #define CDC_INBUF_SIZE 1
359
360 typedef struct {
361     uint32_t count;
362     uint32_t lastcount;
363     char buf[CDC_INBUF_SIZE];
364 } inbuf_t;
365
366 extern inbuf_t inbuf;
367
368 #endif //CDC
369
370 uint32_t CDC_print(char *printbuf);
371 uint32_t CDC_input(void);
372 void CDC_init(void);
373
374 #define __xprintf dpf
375 int dpf(const char *_Format, ...);
376
377 #ifdef __cplusplus
378 }
379 #endif
380
381 #endif // _UDI_CDC_H_