My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

udi_cdc.h 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. #ifndef _UDI_CDC_H_
  47. #define _UDI_CDC_H_
  48. #include "conf_usb.h"
  49. #include "usb_protocol.h"
  50. #include "usb_protocol_cdc.h"
  51. #include "udd.h"
  52. #include "udc_desc.h"
  53. #include "udi.h"
  54. // Check the number of port
  55. #ifndef UDI_CDC_PORT_NB
  56. # define UDI_CDC_PORT_NB 1
  57. #endif
  58. #if (UDI_CDC_PORT_NB < 1) || (UDI_CDC_PORT_NB > 7)
  59. # error UDI_CDC_PORT_NB must be between 1 and 7
  60. #endif
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /**
  65. * \addtogroup udi_cdc_group_udc
  66. * @{
  67. */
  68. //! Global structure which contains standard UDI API for UDC
  69. extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_comm;
  70. extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_data;
  71. //@}
  72. /**
  73. * \ingroup udi_cdc_group
  74. * \defgroup udi_cdc_group_desc USB interface descriptors
  75. *
  76. * The following structures provide predefined USB interface descriptors.
  77. * It must be used to define the final USB descriptors.
  78. */
  79. //@{
  80. /**
  81. * \brief Communication Class interface descriptor
  82. *
  83. * Interface descriptor with associated functional and endpoint
  84. * descriptors for the CDC Communication Class interface.
  85. */
  86. typedef struct {
  87. //! Standard interface descriptor
  88. usb_iface_desc_t iface;
  89. //! CDC Header functional descriptor
  90. usb_cdc_hdr_desc_t header;
  91. //! CDC Abstract Control Model functional descriptor
  92. usb_cdc_acm_desc_t acm;
  93. //! CDC Union functional descriptor
  94. usb_cdc_union_desc_t union_desc;
  95. //! CDC Call Management functional descriptor
  96. usb_cdc_call_mgmt_desc_t call_mgmt;
  97. //! Notification endpoint descriptor
  98. usb_ep_desc_t ep_notify;
  99. } udi_cdc_comm_desc_t;
  100. /**
  101. * \brief Data Class interface descriptor
  102. *
  103. * Interface descriptor with associated endpoint descriptors for the
  104. * CDC Data Class interface.
  105. */
  106. typedef struct {
  107. //! Standard interface descriptor
  108. usb_iface_desc_t iface;
  109. //! Data IN/OUT endpoint descriptors
  110. usb_ep_desc_t ep_in;
  111. usb_ep_desc_t ep_out;
  112. } udi_cdc_data_desc_t;
  113. //! CDC communication endpoints size for all speeds
  114. #define UDI_CDC_COMM_EP_SIZE 64
  115. //! CDC data endpoints size for FS speed (8B, 16B, 32B, 64B)
  116. #define UDI_CDC_DATA_EPS_FS_SIZE 64
  117. //! CDC data endpoints size for HS speed (512B only)
  118. #define UDI_CDC_DATA_EPS_HS_SIZE 512
  119. /**
  120. * \name Content of interface descriptors
  121. * Up to 7 CDC interfaces can be implemented on a USB device.
  122. */
  123. //@{
  124. //! By default no string associated to these interfaces
  125. #ifndef UDI_CDC_IAD_STRING_ID_0
  126. #define UDI_CDC_IAD_STRING_ID_0 0
  127. #endif
  128. #ifndef UDI_CDC_COMM_STRING_ID_0
  129. #define UDI_CDC_COMM_STRING_ID_0 0
  130. #endif
  131. #ifndef UDI_CDC_DATA_STRING_ID_0
  132. #define UDI_CDC_DATA_STRING_ID_0 0
  133. #endif
  134. #define UDI_CDC_IAD_DESC_0 UDI_CDC_IAD_DESC(0)
  135. #define UDI_CDC_COMM_DESC_0 UDI_CDC_COMM_DESC(0)
  136. #define UDI_CDC_DATA_DESC_0_FS UDI_CDC_DATA_DESC_FS(0)
  137. #define UDI_CDC_DATA_DESC_0_HS UDI_CDC_DATA_DESC_HS(0)
  138. //! By default no string associated to these interfaces
  139. #ifndef UDI_CDC_IAD_STRING_ID_1
  140. #define UDI_CDC_IAD_STRING_ID_1 0
  141. #endif
  142. #ifndef UDI_CDC_COMM_STRING_ID_1
  143. #define UDI_CDC_COMM_STRING_ID_1 0
  144. #endif
  145. #ifndef UDI_CDC_DATA_STRING_ID_1
  146. #define UDI_CDC_DATA_STRING_ID_1 0
  147. #endif
  148. #define UDI_CDC_IAD_DESC_1 UDI_CDC_IAD_DESC(1)
  149. #define UDI_CDC_COMM_DESC_1 UDI_CDC_COMM_DESC(1)
  150. #define UDI_CDC_DATA_DESC_1_FS UDI_CDC_DATA_DESC_FS(1)
  151. #define UDI_CDC_DATA_DESC_1_HS UDI_CDC_DATA_DESC_HS(1)
  152. //! By default no string associated to these interfaces
  153. #ifndef UDI_CDC_IAD_STRING_ID_2
  154. #define UDI_CDC_IAD_STRING_ID_2 0
  155. #endif
  156. #ifndef UDI_CDC_COMM_STRING_ID_2
  157. #define UDI_CDC_COMM_STRING_ID_2 0
  158. #endif
  159. #ifndef UDI_CDC_DATA_STRING_ID_2
  160. #define UDI_CDC_DATA_STRING_ID_2 0
  161. #endif
  162. #define UDI_CDC_IAD_DESC_2 UDI_CDC_IAD_DESC(2)
  163. #define UDI_CDC_COMM_DESC_2 UDI_CDC_COMM_DESC(2)
  164. #define UDI_CDC_DATA_DESC_2_FS UDI_CDC_DATA_DESC_FS(2)
  165. #define UDI_CDC_DATA_DESC_2_HS UDI_CDC_DATA_DESC_HS(2)
  166. //! By default no string associated to these interfaces
  167. #ifndef UDI_CDC_IAD_STRING_ID_3
  168. #define UDI_CDC_IAD_STRING_ID_3 0
  169. #endif
  170. #ifndef UDI_CDC_COMM_STRING_ID_3
  171. #define UDI_CDC_COMM_STRING_ID_3 0
  172. #endif
  173. #ifndef UDI_CDC_DATA_STRING_ID_3
  174. #define UDI_CDC_DATA_STRING_ID_3 0
  175. #endif
  176. #define UDI_CDC_IAD_DESC_3 UDI_CDC_IAD_DESC(3)
  177. #define UDI_CDC_COMM_DESC_3 UDI_CDC_COMM_DESC(3)
  178. #define UDI_CDC_DATA_DESC_3_FS UDI_CDC_DATA_DESC_FS(3)
  179. #define UDI_CDC_DATA_DESC_3_HS UDI_CDC_DATA_DESC_HS(3)
  180. //! By default no string associated to these interfaces
  181. #ifndef UDI_CDC_IAD_STRING_ID_4
  182. #define UDI_CDC_IAD_STRING_ID_4 0
  183. #endif
  184. #ifndef UDI_CDC_COMM_STRING_ID_4
  185. #define UDI_CDC_COMM_STRING_ID_4 0
  186. #endif
  187. #ifndef UDI_CDC_DATA_STRING_ID_4
  188. #define UDI_CDC_DATA_STRING_ID_4 0
  189. #endif
  190. #define UDI_CDC_IAD_DESC_4 UDI_CDC_IAD_DESC(4)
  191. #define UDI_CDC_COMM_DESC_4 UDI_CDC_COMM_DESC(4)
  192. #define UDI_CDC_DATA_DESC_4_FS UDI_CDC_DATA_DESC_FS(4)
  193. #define UDI_CDC_DATA_DESC_4_HS UDI_CDC_DATA_DESC_HS(4)
  194. //! By default no string associated to these interfaces
  195. #ifndef UDI_CDC_IAD_STRING_ID_5
  196. #define UDI_CDC_IAD_STRING_ID_5 0
  197. #endif
  198. #ifndef UDI_CDC_COMM_STRING_ID_5
  199. #define UDI_CDC_COMM_STRING_ID_5 0
  200. #endif
  201. #ifndef UDI_CDC_DATA_STRING_ID_5
  202. #define UDI_CDC_DATA_STRING_ID_5 0
  203. #endif
  204. #define UDI_CDC_IAD_DESC_5 UDI_CDC_IAD_DESC(5)
  205. #define UDI_CDC_COMM_DESC_5 UDI_CDC_COMM_DESC(5)
  206. #define UDI_CDC_DATA_DESC_5_FS UDI_CDC_DATA_DESC_FS(5)
  207. #define UDI_CDC_DATA_DESC_5_HS UDI_CDC_DATA_DESC_HS(5)
  208. //! By default no string associated to these interfaces
  209. #ifndef UDI_CDC_IAD_STRING_ID_6
  210. #define UDI_CDC_IAD_STRING_ID_6 0
  211. #endif
  212. #ifndef UDI_CDC_COMM_STRING_ID_6
  213. #define UDI_CDC_COMM_STRING_ID_6 0
  214. #endif
  215. #ifndef UDI_CDC_DATA_STRING_ID_6
  216. #define UDI_CDC_DATA_STRING_ID_6 0
  217. #endif
  218. #define UDI_CDC_IAD_DESC_6 UDI_CDC_IAD_DESC(6)
  219. #define UDI_CDC_COMM_DESC_6 UDI_CDC_COMM_DESC(6)
  220. #define UDI_CDC_DATA_DESC_6_FS UDI_CDC_DATA_DESC_FS(6)
  221. #define UDI_CDC_DATA_DESC_6_HS UDI_CDC_DATA_DESC_HS(6)
  222. //@}
  223. //! Content of CDC IAD interface descriptor for all speeds
  224. #define UDI_CDC_IAD_DESC(port) { \
  225. .bLength = sizeof(usb_iad_desc_t),\
  226. .bDescriptorType = USB_DT_IAD,\
  227. .bInterfaceCount = 2,\
  228. .bFunctionClass = CDC_CLASS_COMM,\
  229. .bFunctionSubClass = CDC_SUBCLASS_ACM,\
  230. .bFunctionProtocol = CDC_PROTOCOL_V25TER,\
  231. .bFirstInterface = UDI_CDC_COMM_IFACE_NUMBER_##port,\
  232. .iFunction = UDI_CDC_IAD_STRING_ID_##port,\
  233. }
  234. //! Content of CDC COMM interface descriptor for all speeds
  235. #define UDI_CDC_COMM_DESC(port) { \
  236. .iface.bLength = sizeof(usb_iface_desc_t),\
  237. .iface.bDescriptorType = USB_DT_INTERFACE,\
  238. .iface.bAlternateSetting = 0,\
  239. .iface.bNumEndpoints = 1,\
  240. .iface.bInterfaceClass = CDC_CLASS_COMM,\
  241. .iface.bInterfaceSubClass = CDC_SUBCLASS_ACM,\
  242. .iface.bInterfaceProtocol = CDC_PROTOCOL_V25TER,\
  243. .header.bFunctionLength = sizeof(usb_cdc_hdr_desc_t),\
  244. .header.bDescriptorType = CDC_CS_INTERFACE,\
  245. .header.bDescriptorSubtype = CDC_SCS_HEADER,\
  246. .header.bcdCDC = LE16(0x0110),\
  247. .call_mgmt.bFunctionLength = sizeof(usb_cdc_call_mgmt_desc_t),\
  248. .call_mgmt.bDescriptorType = CDC_CS_INTERFACE,\
  249. .call_mgmt.bDescriptorSubtype = CDC_SCS_CALL_MGMT,\
  250. .call_mgmt.bmCapabilities = \
  251. CDC_CALL_MGMT_SUPPORTED | CDC_CALL_MGMT_OVER_DCI,\
  252. .acm.bFunctionLength = sizeof(usb_cdc_acm_desc_t),\
  253. .acm.bDescriptorType = CDC_CS_INTERFACE,\
  254. .acm.bDescriptorSubtype = CDC_SCS_ACM,\
  255. .acm.bmCapabilities = CDC_ACM_SUPPORT_LINE_REQUESTS,\
  256. .union_desc.bFunctionLength = sizeof(usb_cdc_union_desc_t),\
  257. .union_desc.bDescriptorType = CDC_CS_INTERFACE,\
  258. .union_desc.bDescriptorSubtype= CDC_SCS_UNION,\
  259. .ep_notify.bLength = sizeof(usb_ep_desc_t),\
  260. .ep_notify.bDescriptorType = USB_DT_ENDPOINT,\
  261. .ep_notify.bmAttributes = USB_EP_TYPE_INTERRUPT,\
  262. .ep_notify.wMaxPacketSize = LE16(UDI_CDC_COMM_EP_SIZE),\
  263. .ep_notify.bInterval = 0x10,\
  264. .ep_notify.bEndpointAddress = UDI_CDC_COMM_EP_##port,\
  265. .iface.bInterfaceNumber = UDI_CDC_COMM_IFACE_NUMBER_##port,\
  266. .call_mgmt.bDataInterface = UDI_CDC_DATA_IFACE_NUMBER_##port,\
  267. .union_desc.bMasterInterface = UDI_CDC_COMM_IFACE_NUMBER_##port,\
  268. .union_desc.bSlaveInterface0 = UDI_CDC_DATA_IFACE_NUMBER_##port,\
  269. .iface.iInterface = UDI_CDC_COMM_STRING_ID_##port,\
  270. }
  271. //! Content of CDC DATA interface descriptors
  272. #define UDI_CDC_DATA_DESC_COMMON \
  273. .iface.bLength = sizeof(usb_iface_desc_t),\
  274. .iface.bDescriptorType = USB_DT_INTERFACE,\
  275. .iface.bAlternateSetting = 0,\
  276. .iface.bNumEndpoints = 2,\
  277. .iface.bInterfaceClass = CDC_CLASS_DATA,\
  278. .iface.bInterfaceSubClass = 0,\
  279. .iface.bInterfaceProtocol = 0,\
  280. .ep_in.bLength = sizeof(usb_ep_desc_t),\
  281. .ep_in.bDescriptorType = USB_DT_ENDPOINT,\
  282. .ep_in.bmAttributes = USB_EP_TYPE_BULK,\
  283. .ep_in.bInterval = 0,\
  284. .ep_out.bLength = sizeof(usb_ep_desc_t),\
  285. .ep_out.bDescriptorType = USB_DT_ENDPOINT,\
  286. .ep_out.bmAttributes = USB_EP_TYPE_BULK,\
  287. .ep_out.bInterval = 0,
  288. #define UDI_CDC_DATA_DESC_FS(port) { \
  289. UDI_CDC_DATA_DESC_COMMON \
  290. .ep_in.wMaxPacketSize = LE16(UDI_CDC_DATA_EPS_FS_SIZE),\
  291. .ep_out.wMaxPacketSize = LE16(UDI_CDC_DATA_EPS_FS_SIZE),\
  292. .ep_in.bEndpointAddress = UDI_CDC_DATA_EP_IN_##port,\
  293. .ep_out.bEndpointAddress = UDI_CDC_DATA_EP_OUT_##port,\
  294. .iface.bInterfaceNumber = UDI_CDC_DATA_IFACE_NUMBER_##port,\
  295. .iface.iInterface = UDI_CDC_DATA_STRING_ID_##port,\
  296. }
  297. #define UDI_CDC_DATA_DESC_HS(port) { \
  298. UDI_CDC_DATA_DESC_COMMON \
  299. .ep_in.wMaxPacketSize = LE16(UDI_CDC_DATA_EPS_HS_SIZE),\
  300. .ep_out.wMaxPacketSize = LE16(UDI_CDC_DATA_EPS_HS_SIZE),\
  301. .ep_in.bEndpointAddress = UDI_CDC_DATA_EP_IN_##port,\
  302. .ep_out.bEndpointAddress = UDI_CDC_DATA_EP_OUT_##port,\
  303. .iface.bInterfaceNumber = UDI_CDC_DATA_IFACE_NUMBER_##port,\
  304. .iface.iInterface = UDI_CDC_DATA_STRING_ID_##port,\
  305. }
  306. //@}
  307. /**
  308. * \ingroup udi_group
  309. * \defgroup udi_cdc_group USB Device Interface (UDI) for Communication Class Device (CDC)
  310. *
  311. * Common APIs used by high level application to use this USB class.
  312. *
  313. * These routines are used to transfer and control data
  314. * to/from USB CDC endpoint.
  315. *
  316. * See \ref udi_cdc_quickstart.
  317. * @{
  318. */
  319. /**
  320. * \name Interface for application with single CDC interface support
  321. */
  322. //@{
  323. /**
  324. * \brief Notify a state change of DCD signal
  325. *
  326. * \param b_set DCD is enabled if true, else disabled
  327. */
  328. void udi_cdc_ctrl_signal_dcd(bool b_set);
  329. /**
  330. * \brief Notify a state change of DSR signal
  331. *
  332. * \param b_set DSR is enabled if true, else disabled
  333. */
  334. void udi_cdc_ctrl_signal_dsr(bool b_set);
  335. /**
  336. * \brief Notify a framing error
  337. */
  338. void udi_cdc_signal_framing_error(void);
  339. /**
  340. * \brief Notify a parity error
  341. */
  342. void udi_cdc_signal_parity_error(void);
  343. /**
  344. * \brief Notify a overrun
  345. */
  346. void udi_cdc_signal_overrun(void);
  347. /**
  348. * \brief Gets the number of byte received
  349. *
  350. * \return the number of data available
  351. */
  352. iram_size_t udi_cdc_get_nb_received_data(void);
  353. /**
  354. * \brief This function checks if a character has been received on the CDC line
  355. *
  356. * \return \c 1 if a byte is ready to be read.
  357. */
  358. bool udi_cdc_is_rx_ready(void);
  359. /**
  360. * \brief Waits and gets a value on CDC line
  361. *
  362. * \return value read on CDC line
  363. */
  364. int udi_cdc_getc(void);
  365. /**
  366. * \brief Reads a RAM buffer on CDC line
  367. *
  368. * \param buf Values read
  369. * \param size Number of value read
  370. *
  371. * \return the number of data remaining
  372. */
  373. iram_size_t udi_cdc_read_buf(void* buf, iram_size_t size);
  374. /**
  375. * \brief Non polling reads of a up to 'size' data from CDC line
  376. *
  377. * \param port Communication port number to manage
  378. * \param buf Buffer where to store read data
  379. * \param size Maximum number of data to read (size of buffer)
  380. *
  381. * \return the number of data effectively read
  382. */
  383. iram_size_t udi_cdc_read_no_polling(void* buf, iram_size_t size);
  384. /**
  385. * \brief Gets the number of free byte in TX buffer
  386. *
  387. * \return the number of free byte in TX buffer
  388. */
  389. iram_size_t udi_cdc_get_free_tx_buffer(void);
  390. /**
  391. * \brief This function checks if a new character sent is possible
  392. * The type int is used to support scanf redirection from compiler LIB.
  393. *
  394. * \return \c 1 if a new character can be sent
  395. */
  396. bool udi_cdc_is_tx_ready(void);
  397. /**
  398. * \brief Puts a byte on CDC line
  399. * The type int is used to support printf redirection from compiler LIB.
  400. *
  401. * \param value Value to put
  402. *
  403. * \return \c 1 if function was successfully done, otherwise \c 0.
  404. */
  405. int udi_cdc_putc(int value);
  406. /**
  407. * \brief Writes a RAM buffer on CDC line
  408. *
  409. * \param buf Values to write
  410. * \param size Number of value to write
  411. *
  412. * \return the number of data remaining
  413. */
  414. iram_size_t udi_cdc_write_buf(const void* buf, iram_size_t size);
  415. //@}
  416. /**
  417. * \name Interface for application with multi CDC interfaces support
  418. */
  419. //@{
  420. /**
  421. * \brief Notify a state change of DCD signal
  422. *
  423. * \param port Communication port number to manage
  424. * \param b_set DCD is enabled if true, else disabled
  425. */
  426. void udi_cdc_multi_ctrl_signal_dcd(uint8_t port, bool b_set);
  427. /**
  428. * \brief Notify a state change of DSR signal
  429. *
  430. * \param port Communication port number to manage
  431. * \param b_set DSR is enabled if true, else disabled
  432. */
  433. void udi_cdc_multi_ctrl_signal_dsr(uint8_t port, bool b_set);
  434. /**
  435. * \brief Notify a framing error
  436. *
  437. * \param port Communication port number to manage
  438. */
  439. void udi_cdc_multi_signal_framing_error(uint8_t port);
  440. /**
  441. * \brief Notify a parity error
  442. *
  443. * \param port Communication port number to manage
  444. */
  445. void udi_cdc_multi_signal_parity_error(uint8_t port);
  446. /**
  447. * \brief Notify a overrun
  448. *
  449. * \param port Communication port number to manage
  450. */
  451. void udi_cdc_multi_signal_overrun(uint8_t port);
  452. /**
  453. * \brief Gets the number of byte received
  454. *
  455. * \param port Communication port number to manage
  456. *
  457. * \return the number of data available
  458. */
  459. iram_size_t udi_cdc_multi_get_nb_received_data(uint8_t port);
  460. /**
  461. * \brief This function checks if a character has been received on the CDC line
  462. *
  463. * \param port Communication port number to manage
  464. *
  465. * \return \c 1 if a byte is ready to be read.
  466. */
  467. bool udi_cdc_multi_is_rx_ready(uint8_t port);
  468. /**
  469. * \brief Waits and gets a value on CDC line
  470. *
  471. * \param port Communication port number to manage
  472. *
  473. * \return value read on CDC line
  474. */
  475. int udi_cdc_multi_getc(uint8_t port);
  476. /**
  477. * \brief Reads a RAM buffer on CDC line
  478. *
  479. * \param port Communication port number to manage
  480. * \param buf Values read
  481. * \param size Number of values read
  482. *
  483. * \return the number of data remaining
  484. */
  485. iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size);
  486. /**
  487. * \brief Gets the number of free byte in TX buffer
  488. *
  489. * \param port Communication port number to manage
  490. *
  491. * \return the number of free byte in TX buffer
  492. */
  493. iram_size_t udi_cdc_multi_get_free_tx_buffer(uint8_t port);
  494. /**
  495. * \brief This function checks if a new character sent is possible
  496. * The type int is used to support scanf redirection from compiler LIB.
  497. *
  498. * \param port Communication port number to manage
  499. *
  500. * \return \c 1 if a new character can be sent
  501. */
  502. bool udi_cdc_multi_is_tx_ready(uint8_t port);
  503. /**
  504. * \brief Puts a byte on CDC line
  505. * The type int is used to support printf redirection from compiler LIB.
  506. *
  507. * \param port Communication port number to manage
  508. * \param value Value to put
  509. *
  510. * \return \c 1 if function was successfully done, otherwise \c 0.
  511. */
  512. int udi_cdc_multi_putc(uint8_t port, int value);
  513. /**
  514. * \brief Writes a RAM buffer on CDC line
  515. *
  516. * \param port Communication port number to manage
  517. * \param buf Values to write
  518. * \param size Number of value to write
  519. *
  520. * \return the number of data remaining
  521. */
  522. iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t size);
  523. //@}
  524. //@}
  525. /**
  526. * \page udi_cdc_quickstart Quick start guide for USB device Communication Class Device module (UDI CDC)
  527. *
  528. * This is the quick start guide for the \ref udi_cdc_group
  529. * "USB device interface CDC module (UDI CDC)" with step-by-step instructions on
  530. * how to configure and use the modules in a selection of use cases.
  531. *
  532. * The use cases contain several code fragments. The code fragments in the
  533. * steps for setup can be copied into a custom initialization function, while
  534. * the steps for usage can be copied into, e.g., the main application function.
  535. *
  536. * \section udi_cdc_basic_use_case Basic use case
  537. * In this basic use case, the "USB CDC (Single Interface Device)" module is used
  538. * with only one communication port.
  539. * The "USB CDC (Composite Device)" module usage is described in \ref udi_cdc_use_cases
  540. * "Advanced use cases".
  541. *
  542. * \section udi_cdc_basic_use_case_setup Setup steps
  543. * \subsection udi_cdc_basic_use_case_setup_prereq Prerequisites
  544. * \copydetails udc_basic_use_case_setup_prereq
  545. * \subsection udi_cdc_basic_use_case_setup_code Example code
  546. * \copydetails udc_basic_use_case_setup_code
  547. * \subsection udi_cdc_basic_use_case_setup_flow Workflow
  548. * \copydetails udc_basic_use_case_setup_flow
  549. *
  550. * \section udi_cdc_basic_use_case_usage Usage steps
  551. *
  552. * \subsection udi_cdc_basic_use_case_usage_code Example code
  553. * Content of conf_usb.h:
  554. * \code
  555. #define UDI_CDC_ENABLE_EXT(port) my_callback_cdc_enable()
  556. extern bool my_callback_cdc_enable(void);
  557. #define UDI_CDC_DISABLE_EXT(port) my_callback_cdc_disable()
  558. extern void my_callback_cdc_disable(void);
  559. #define UDI_CDC_LOW_RATE
  560. #define UDI_CDC_DEFAULT_RATE 115200
  561. #define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1
  562. #define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE
  563. #define UDI_CDC_DEFAULT_DATABITS 8
  564. #include "udi_cdc_conf.h" // At the end of conf_usb.h file
  565. \endcode
  566. *
  567. * Add to application C-file:
  568. * \code
  569. static bool my_flag_autorize_cdc_transfert = false;
  570. bool my_callback_cdc_enable(void)
  571. {
  572. my_flag_autorize_cdc_transfert = true;
  573. return true;
  574. }
  575. void my_callback_cdc_disable(void)
  576. {
  577. my_flag_autorize_cdc_transfert = false;
  578. }
  579. void task(void)
  580. {
  581. if (my_flag_autorize_cdc_transfert) {
  582. udi_cdc_putc('A');
  583. udi_cdc_getc();
  584. }
  585. }
  586. \endcode
  587. *
  588. * \subsection udi_cdc_basic_use_case_setup_flow Workflow
  589. * -# Ensure that conf_usb.h is available and contains the following configuration,
  590. * which is the USB device CDC configuration:
  591. * - \code #define USB_DEVICE_SERIAL_NAME "12...EF" // Disk SN for CDC \endcode
  592. * \note The USB serial number is mandatory when a CDC interface is used.
  593. * - \code #define UDI_CDC_ENABLE_EXT(port) my_callback_cdc_enable()
  594. extern bool my_callback_cdc_enable(void); \endcode
  595. * \note After the device enumeration (detecting and identifying USB devices),
  596. * the USB host starts the device configuration. When the USB CDC interface
  597. * from the device is accepted by the host, the USB host enables this interface and the
  598. * UDI_CDC_ENABLE_EXT() callback function is called and return true.
  599. * Thus, when this event is received, the data transfer on CDC interface are authorized.
  600. * - \code #define UDI_CDC_DISABLE_EXT(port) my_callback_cdc_disable()
  601. extern void my_callback_cdc_disable(void); \endcode
  602. * \note When the USB device is unplugged or is reset by the USB host, the USB
  603. * interface is disabled and the UDI_CDC_DISABLE_EXT() callback function
  604. * is called. Thus, the data transfer must be stopped on CDC interface.
  605. * - \code #define UDI_CDC_LOW_RATE \endcode
  606. * \note Define it when the transfer CDC Device to Host is a low rate
  607. * (<512000 bauds) to reduce CDC buffers size.
  608. * - \code #define UDI_CDC_DEFAULT_RATE 115200
  609. #define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1
  610. #define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE
  611. #define UDI_CDC_DEFAULT_DATABITS 8 \endcode
  612. * \note Default configuration of communication port at startup.
  613. * -# Send or wait data on CDC line:
  614. * - \code // Waits and gets a value on CDC line
  615. int udi_cdc_getc(void);
  616. // Reads a RAM buffer on CDC line
  617. iram_size_t udi_cdc_read_buf(int* buf, iram_size_t size);
  618. // Puts a byte on CDC line
  619. int udi_cdc_putc(int value);
  620. // Writes a RAM buffer on CDC line
  621. iram_size_t udi_cdc_write_buf(const int* buf, iram_size_t size); \endcode
  622. *
  623. * \section udi_cdc_use_cases Advanced use cases
  624. * For more advanced use of the UDI CDC module, see the following use cases:
  625. * - \subpage udi_cdc_use_case_composite
  626. * - \subpage udc_use_case_1
  627. * - \subpage udc_use_case_2
  628. * - \subpage udc_use_case_3
  629. * - \subpage udc_use_case_4
  630. * - \subpage udc_use_case_5
  631. * - \subpage udc_use_case_6
  632. */
  633. /**
  634. * \page udi_cdc_use_case_composite CDC in a composite device
  635. *
  636. * A USB Composite Device is a USB Device which uses more than one USB class.
  637. * In this use case, the "USB CDC (Composite Device)" module is used to
  638. * create a USB composite device. Thus, this USB module can be associated with
  639. * another "Composite Device" module, like "USB HID Mouse (Composite Device)".
  640. *
  641. * Also, you can refer to application note
  642. * <A href="http://www.atmel.com/dyn/resources/prod_documents/doc8445.pdf">
  643. * AVR4902 ASF - USB Composite Device</A>.
  644. *
  645. * \section udi_cdc_use_case_composite_setup Setup steps
  646. * For the setup code of this use case to work, the
  647. * \ref udi_cdc_basic_use_case "basic use case" must be followed.
  648. *
  649. * \section udi_cdc_use_case_composite_usage Usage steps
  650. *
  651. * \subsection udi_cdc_use_case_composite_usage_code Example code
  652. * Content of conf_usb.h:
  653. * \code
  654. #define USB_DEVICE_EP_CTRL_SIZE 64
  655. #define USB_DEVICE_NB_INTERFACE (X+2)
  656. #define USB_DEVICE_MAX_EP (X+3)
  657. #define UDI_CDC_DATA_EP_IN_0 (1 | USB_EP_DIR_IN) // TX
  658. #define UDI_CDC_DATA_EP_OUT_0 (2 | USB_EP_DIR_OUT) // RX
  659. #define UDI_CDC_COMM_EP_0 (3 | USB_EP_DIR_IN) // Notify endpoint
  660. #define UDI_CDC_COMM_IFACE_NUMBER_0 X+0
  661. #define UDI_CDC_DATA_IFACE_NUMBER_0 X+1
  662. #define UDI_COMPOSITE_DESC_T \
  663. usb_iad_desc_t udi_cdc_iad; \
  664. udi_cdc_comm_desc_t udi_cdc_comm; \
  665. udi_cdc_data_desc_t udi_cdc_data; \
  666. ...
  667. #define UDI_COMPOSITE_DESC_FS \
  668. .udi_cdc_iad = UDI_CDC_IAD_DESC_0, \
  669. .udi_cdc_comm = UDI_CDC_COMM_DESC_0, \
  670. .udi_cdc_data = UDI_CDC_DATA_DESC_0_FS, \
  671. ...
  672. #define UDI_COMPOSITE_DESC_HS \
  673. .udi_cdc_iad = UDI_CDC_IAD_DESC_0, \
  674. .udi_cdc_comm = UDI_CDC_COMM_DESC_0, \
  675. .udi_cdc_data = UDI_CDC_DATA_DESC_0_HS, \
  676. ...
  677. #define UDI_COMPOSITE_API \
  678. &udi_api_cdc_comm, \
  679. &udi_api_cdc_data, \
  680. ...
  681. \endcode
  682. *
  683. * \subsection udi_cdc_use_case_composite_usage_flow Workflow
  684. * -# Ensure that conf_usb.h is available and contains the following parameters
  685. * required for a USB composite device configuration:
  686. * - \code // Endpoint control size, This must be:
  687. // - 8, 16, 32 or 64 for full speed device (8 is recommended to save RAM)
  688. // - 64 for a high speed device
  689. #define USB_DEVICE_EP_CTRL_SIZE 64
  690. // Total Number of interfaces on this USB device.
  691. // Add 2 for CDC.
  692. #define USB_DEVICE_NB_INTERFACE (X+2)
  693. // Total number of endpoints on this USB device.
  694. // This must include each endpoint for each interface.
  695. // Add 3 for CDC.
  696. #define USB_DEVICE_MAX_EP (X+3) \endcode
  697. * -# Ensure that conf_usb.h contains the description of
  698. * composite device:
  699. * - \code // The endpoint numbers chosen by you for the CDC.
  700. // The endpoint numbers starting from 1.
  701. #define UDI_CDC_DATA_EP_IN_0 (1 | USB_EP_DIR_IN) // TX
  702. #define UDI_CDC_DATA_EP_OUT_0 (2 | USB_EP_DIR_OUT) // RX
  703. #define UDI_CDC_COMM_EP_0 (3 | USB_EP_DIR_IN) // Notify endpoint
  704. // The interface index of an interface starting from 0
  705. #define UDI_CDC_COMM_IFACE_NUMBER_0 X+0
  706. #define UDI_CDC_DATA_IFACE_NUMBER_0 X+1 \endcode
  707. * -# Ensure that conf_usb.h contains the following parameters
  708. * required for a USB composite device configuration:
  709. * - \code // USB Interfaces descriptor structure
  710. #define UDI_COMPOSITE_DESC_T \
  711. ...
  712. usb_iad_desc_t udi_cdc_iad; \
  713. udi_cdc_comm_desc_t udi_cdc_comm; \
  714. udi_cdc_data_desc_t udi_cdc_data; \
  715. ...
  716. // USB Interfaces descriptor value for Full Speed
  717. #define UDI_COMPOSITE_DESC_FS \
  718. ...
  719. .udi_cdc_iad = UDI_CDC_IAD_DESC_0, \
  720. .udi_cdc_comm = UDI_CDC_COMM_DESC_0, \
  721. .udi_cdc_data = UDI_CDC_DATA_DESC_0_FS, \
  722. ...
  723. // USB Interfaces descriptor value for High Speed
  724. #define UDI_COMPOSITE_DESC_HS \
  725. ...
  726. .udi_cdc_iad = UDI_CDC_IAD_DESC_0, \
  727. .udi_cdc_comm = UDI_CDC_COMM_DESC_0, \
  728. .udi_cdc_data = UDI_CDC_DATA_DESC_0_HS, \
  729. ...
  730. // USB Interface APIs
  731. #define UDI_COMPOSITE_API \
  732. ...
  733. &udi_api_cdc_comm, \
  734. &udi_api_cdc_data, \
  735. ... \endcode
  736. * - \note The descriptors order given in the four lists above must be the
  737. * same as the order defined by all interface indexes. The interface index
  738. * orders are defined through UDI_X_IFACE_NUMBER defines.\n
  739. * Also, the CDC requires a USB Interface Association Descriptor (IAD) for
  740. * composite device.
  741. */
  742. #ifdef __cplusplus
  743. }
  744. #endif
  745. #endif // _UDI_CDC_H_