]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/udi_cdc.h
Massdrop keyboard updates for SEND_STRING, syscalls, stdio, debug prints, Auto Shift...
[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 //@}
90
91 /**
92  * \ingroup udi_group
93  * \defgroup udi_cdc_group USB Device Interface (UDI) for Communication Class Device (CDC)
94  *
95  * Common APIs used by high level application to use this USB class.
96  *
97  * These routines are used to transfer and control data
98  * to/from USB CDC endpoint.
99  *
100  * See \ref udi_cdc_quickstart.
101  * @{
102  */
103
104 /**
105  * \name Interface for application with single CDC interface support
106  */
107 //@{
108
109 /**
110  * \brief Notify a state change of DCD signal
111  *
112  * \param b_set      DCD is enabled if true, else disabled
113  */
114 void udi_cdc_ctrl_signal_dcd(bool b_set);
115
116 /**
117  * \brief Notify a state change of DSR signal
118  *
119  * \param b_set      DSR is enabled if true, else disabled
120  */
121 void udi_cdc_ctrl_signal_dsr(bool b_set);
122
123 /**
124  * \brief Notify a framing error
125  */
126 void udi_cdc_signal_framing_error(void);
127
128 /**
129  * \brief Notify a parity error
130  */
131 void udi_cdc_signal_parity_error(void);
132
133 /**
134  * \brief Notify a overrun
135  */
136 void udi_cdc_signal_overrun(void);
137
138 /**
139  * \brief Gets the number of byte received
140  *
141  * \return the number of data available
142  */
143 iram_size_t udi_cdc_get_nb_received_data(void);
144
145 /**
146  * \brief This function checks if a character has been received on the CDC line
147  *
148  * \return \c 1 if a byte is ready to be read.
149  */
150 bool udi_cdc_is_rx_ready(void);
151
152 /**
153  * \brief Waits and gets a value on CDC line
154  *
155  * \return value read on CDC line
156  */
157 int udi_cdc_getc(void);
158
159 /**
160  * \brief Reads a RAM buffer on CDC line
161  *
162  * \param buf       Values read
163  * \param size      Number of value read
164  *
165  * \return the number of data remaining
166  */
167 iram_size_t udi_cdc_read_buf(void* buf, iram_size_t size);
168
169 /**
170  * \brief Non polling reads of a up to 'size' data from CDC line
171  *
172  * \param port      Communication port number to manage
173  * \param buf       Buffer where to store read data
174  * \param size      Maximum number of data to read (size of buffer)
175  *
176  * \return the number of data effectively read
177  */
178 iram_size_t udi_cdc_read_no_polling(void* buf, iram_size_t size);
179
180 /**
181  * \brief Gets the number of free byte in TX buffer
182  *
183  * \return the number of free byte in TX buffer
184  */
185 iram_size_t udi_cdc_get_free_tx_buffer(void);
186
187 /**
188  * \brief This function checks if a new character sent is possible
189  * The type int is used to support scanf redirection from compiler LIB.
190  *
191  * \return \c 1 if a new character can be sent
192  */
193 bool udi_cdc_is_tx_ready(void);
194
195 /**
196  * \brief Puts a byte on CDC line
197  * The type int is used to support printf redirection from compiler LIB.
198  *
199  * \param value      Value to put
200  *
201  * \return \c 1 if function was successfully done, otherwise \c 0.
202  */
203 int udi_cdc_putc(int value);
204
205 /**
206  * \brief Writes a RAM buffer on CDC line
207  *
208  * \param buf       Values to write
209  * \param size      Number of value to write
210  *
211  * \return the number of data remaining
212  */
213 iram_size_t udi_cdc_write_buf(const void* buf, iram_size_t size);
214 //@}
215
216 /**
217  * \name Interface for application with multi CDC interfaces support
218  */
219 //@{
220
221 /**
222  * \brief Notify a state change of DCD signal
223  *
224  * \param port       Communication port number to manage
225  * \param b_set      DCD is enabled if true, else disabled
226  */
227 void udi_cdc_multi_ctrl_signal_dcd(uint8_t port, bool b_set);
228
229 /**
230  * \brief Notify a state change of DSR signal
231  *
232  * \param port       Communication port number to manage
233  * \param b_set      DSR is enabled if true, else disabled
234  */
235 void udi_cdc_multi_ctrl_signal_dsr(uint8_t port, bool b_set);
236
237 /**
238  * \brief Notify a framing error
239  *
240  * \param port       Communication port number to manage
241  */
242 void udi_cdc_multi_signal_framing_error(uint8_t port);
243
244 /**
245  * \brief Notify a parity error
246  *
247  * \param port       Communication port number to manage
248  */
249 void udi_cdc_multi_signal_parity_error(uint8_t port);
250
251 /**
252  * \brief Notify a overrun
253  *
254  * \param port       Communication port number to manage
255  */
256 void udi_cdc_multi_signal_overrun(uint8_t port);
257
258 /**
259  * \brief Gets the number of byte received
260  *
261  * \param port       Communication port number to manage
262  *
263  * \return the number of data available
264  */
265 iram_size_t udi_cdc_multi_get_nb_received_data(uint8_t port);
266
267 /**
268  * \brief This function checks if a character has been received on the CDC line
269  *
270  * \param port       Communication port number to manage
271  *
272  * \return \c 1 if a byte is ready to be read.
273  */
274 bool udi_cdc_multi_is_rx_ready(uint8_t port);
275
276 /**
277  * \brief Waits and gets a value on CDC line
278  *
279  * \param port       Communication port number to manage
280  *
281  * \return value read on CDC line
282  */
283 int udi_cdc_multi_getc(uint8_t port);
284
285 /**
286  * \brief Reads a RAM buffer on CDC line
287  *
288  * \param port       Communication port number to manage
289  * \param buf       Values read
290  * \param size      Number of values read
291  *
292  * \return the number of data remaining
293  */
294 iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size);
295
296 /**
297  * \brief Gets the number of free byte in TX buffer
298  *
299  * \param port       Communication port number to manage
300  *
301  * \return the number of free byte in TX buffer
302  */
303 iram_size_t udi_cdc_multi_get_free_tx_buffer(uint8_t port);
304
305 /**
306  * \brief This function checks if a new character sent is possible
307  * The type int is used to support scanf redirection from compiler LIB.
308  *
309  * \param port       Communication port number to manage
310  *
311  * \return \c 1 if a new character can be sent
312  */
313 bool udi_cdc_multi_is_tx_ready(uint8_t port);
314
315 /**
316  * \brief Puts a byte on CDC line
317  * The type int is used to support printf redirection from compiler LIB.
318  *
319  * \param port       Communication port number to manage
320  * \param value      Value to put
321  *
322  * \return \c 1 if function was successfully done, otherwise \c 0.
323  */
324 int udi_cdc_multi_putc(uint8_t port, int value);
325
326 /**
327  * \brief Writes a RAM buffer on CDC line
328  *
329  * \param port       Communication port number to manage
330  * \param buf       Values to write
331  * \param size      Number of value to write
332  *
333  * \return the number of data remaining
334  */
335 iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t size);
336 //@}
337
338 #define CDC_PRINTBUF_SIZE  256
339 extern char printbuf[CDC_PRINTBUF_SIZE];
340
341 #define CDC_INBUF_SIZE 256
342
343 typedef struct {
344     uint32_t count;
345     uint32_t lastcount;
346     char buf[CDC_INBUF_SIZE];
347 } inbuf_t;
348
349 #else //CDC
350
351 // keep these to accommodate calls if remaining
352 #define CDC_PRINTBUF_SIZE  1
353 extern char printbuf[CDC_PRINTBUF_SIZE];
354
355 #define CDC_INBUF_SIZE 1
356
357 typedef struct {
358     uint32_t count;
359     uint32_t lastcount;
360     char buf[CDC_INBUF_SIZE];
361 } inbuf_t;
362
363 extern inbuf_t inbuf;
364
365 #endif //CDC
366
367 uint32_t CDC_print(char *printbuf);
368 uint32_t CDC_input(void);
369 void CDC_init(void);
370
371 #ifdef __cplusplus
372 }
373 #endif
374
375 #endif // _UDI_CDC_H_