cdc_device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. #include "tusb_option.h"
  27. #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC)
  28. #include "cdc_device.h"
  29. #include "device/usbd_pvt.h"
  30. //--------------------------------------------------------------------+
  31. // MACRO CONSTANT TYPEDEF
  32. //--------------------------------------------------------------------+
  33. typedef struct
  34. {
  35. uint8_t itf_num;
  36. uint8_t ep_notif;
  37. uint8_t ep_in;
  38. uint8_t ep_out;
  39. // Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
  40. uint8_t line_state;
  41. /*------------- From this point, data is not cleared by bus reset -------------*/
  42. char wanted_char;
  43. cdc_line_coding_t line_coding;
  44. // FIFO
  45. tu_fifo_t rx_ff;
  46. tu_fifo_t tx_ff;
  47. uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
  48. uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
  49. #if CFG_FIFO_MUTEX
  50. osal_mutex_def_t rx_ff_mutex;
  51. osal_mutex_def_t tx_ff_mutex;
  52. #endif
  53. // Endpoint Transfer buffer
  54. CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE];
  55. CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EPSIZE];
  56. }cdcd_interface_t;
  57. #define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
  58. //--------------------------------------------------------------------+
  59. // INTERNAL OBJECT & FUNCTION DECLARATION
  60. //--------------------------------------------------------------------+
  61. CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
  62. //bool pending_read_from_host; TODO remove
  63. static void _prep_out_transaction (uint8_t itf)
  64. {
  65. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  66. // skip if previous transfer not complete
  67. if ( usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_out) ) return;
  68. //if (pending_read_from_host) return;
  69. // Prepare for incoming data but only allow what we can store in the ring buffer.
  70. uint16_t max_read = tu_fifo_remaining(&p_cdc->rx_ff);
  71. if ( max_read >= CFG_TUD_CDC_EPSIZE )
  72. {
  73. usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
  74. // pending_read_from_host = true;
  75. }
  76. }
  77. //--------------------------------------------------------------------+
  78. // APPLICATION API
  79. //--------------------------------------------------------------------+
  80. bool tud_cdc_n_connected(uint8_t itf)
  81. {
  82. // DTR (bit 0) active is considered as connected
  83. return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0);
  84. }
  85. uint8_t tud_cdc_n_get_line_state (uint8_t itf)
  86. {
  87. return _cdcd_itf[itf].line_state;
  88. }
  89. void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
  90. {
  91. (*coding) = _cdcd_itf[itf].line_coding;
  92. }
  93. void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
  94. {
  95. _cdcd_itf[itf].wanted_char = wanted;
  96. }
  97. //--------------------------------------------------------------------+
  98. // READ API
  99. //--------------------------------------------------------------------+
  100. uint32_t tud_cdc_n_available(uint8_t itf)
  101. {
  102. return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
  103. }
  104. signed char tud_cdc_n_read_char(uint8_t itf)
  105. {
  106. signed char ch;
  107. return tud_cdc_n_read(itf, &ch, 1) ? ch : (-1);
  108. }
  109. uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
  110. {
  111. uint32_t num_read = tu_fifo_read_n(&_cdcd_itf[itf].rx_ff, buffer, bufsize);
  112. _prep_out_transaction(itf);
  113. return num_read;
  114. }
  115. signed char tud_cdc_n_peek(uint8_t itf, int pos)
  116. {
  117. signed char ch;
  118. return tu_fifo_peek_at(&_cdcd_itf[itf].rx_ff, pos, &ch) ? ch : (-1);
  119. }
  120. void tud_cdc_n_read_flush (uint8_t itf)
  121. {
  122. tu_fifo_clear(&_cdcd_itf[itf].rx_ff);
  123. _prep_out_transaction(itf);
  124. }
  125. //--------------------------------------------------------------------+
  126. // WRITE API
  127. //--------------------------------------------------------------------+
  128. uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
  129. {
  130. return tud_cdc_n_write(itf, &ch, 1);
  131. }
  132. uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
  133. {
  134. return tud_cdc_n_write(itf, str, strlen(str));
  135. }
  136. uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
  137. {
  138. uint16_t ret = tu_fifo_write_n(&_cdcd_itf[itf].tx_ff, buffer, bufsize);
  139. #if 0 // TODO issue with circuitpython's REPL
  140. // flush if queue more than endpoint size
  141. if ( tu_fifo_count(&_cdcd_itf[itf].tx_ff) >= CFG_TUD_CDC_EPSIZE )
  142. {
  143. tud_cdc_n_write_flush(itf);
  144. }
  145. #endif
  146. return ret;
  147. }
  148. bool tud_cdc_n_write_flush (uint8_t itf)
  149. {
  150. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  151. TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
  152. uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].tx_ff, p_cdc->epin_buf, CFG_TUD_CDC_EPSIZE);
  153. if ( count )
  154. {
  155. TU_VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected
  156. TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count) );
  157. }
  158. return true;
  159. }
  160. //--------------------------------------------------------------------+
  161. // USBD Driver API
  162. //--------------------------------------------------------------------+
  163. void cdcd_init(void)
  164. {
  165. tu_memclr(_cdcd_itf, sizeof(_cdcd_itf));
  166. for(uint8_t i=0; i<CFG_TUD_CDC; i++)
  167. {
  168. cdcd_interface_t* p_cdc = &_cdcd_itf[i];
  169. p_cdc->wanted_char = -1;
  170. // default line coding is : stop bit = 1, parity = none, data bits = 8
  171. p_cdc->line_coding.bit_rate = 115200;
  172. p_cdc->line_coding.stop_bits = 0;
  173. p_cdc->line_coding.parity = 0;
  174. p_cdc->line_coding.data_bits = 8;
  175. // config fifo
  176. tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, false);
  177. tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
  178. #if CFG_FIFO_MUTEX
  179. tu_fifo_config_mutex(&p_cdc->rx_ff, osal_mutex_create(&p_cdc->rx_ff_mutex));
  180. tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex));
  181. #endif
  182. }
  183. }
  184. void cdcd_reset(uint8_t rhport)
  185. {
  186. (void) rhport;
  187. for(uint8_t i=0; i<CFG_TUD_CDC; i++)
  188. {
  189. tu_memclr(&_cdcd_itf[i], ITF_MEM_RESET_SIZE);
  190. tu_fifo_clear(&_cdcd_itf[i].rx_ff);
  191. tu_fifo_clear(&_cdcd_itf[i].tx_ff);
  192. }
  193. }
  194. bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length)
  195. {
  196. // Only support ACM subclass
  197. TU_ASSERT ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass);
  198. // Only support AT commands, no protocol and vendor specific commands.
  199. TU_ASSERT(tu_within(CDC_COMM_PROTOCOL_NONE, itf_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) ||
  200. itf_desc->bInterfaceProtocol == 0xff);
  201. // Find available interface
  202. cdcd_interface_t * p_cdc = NULL;
  203. uint8_t cdc_id;
  204. for(cdc_id=0; cdc_id<CFG_TUD_CDC; cdc_id++)
  205. {
  206. if ( _cdcd_itf[cdc_id].ep_in == 0 )
  207. {
  208. p_cdc = &_cdcd_itf[cdc_id];
  209. break;
  210. }
  211. }
  212. TU_ASSERT(p_cdc);
  213. //------------- Control Interface -------------//
  214. p_cdc->itf_num = itf_desc->bInterfaceNumber;
  215. uint8_t const * p_desc = tu_desc_next( itf_desc );
  216. (*p_length) = sizeof(tusb_desc_interface_t);
  217. // Communication Functional Descriptors
  218. while ( TUSB_DESC_CLASS_SPECIFIC == tu_desc_type(p_desc) )
  219. {
  220. (*p_length) += tu_desc_len(p_desc);
  221. p_desc = tu_desc_next(p_desc);
  222. }
  223. if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
  224. {
  225. // notification endpoint if any
  226. TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc) );
  227. p_cdc->ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
  228. (*p_length) += p_desc[DESC_OFFSET_LEN];
  229. p_desc = tu_desc_next(p_desc);
  230. }
  231. //------------- Data Interface (if any) -------------//
  232. if ( (TUSB_DESC_INTERFACE == p_desc[DESC_OFFSET_TYPE]) &&
  233. (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
  234. {
  235. // next to endpoint descriptor
  236. p_desc = tu_desc_next(p_desc);
  237. // Open endpoint pair
  238. TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) );
  239. (*p_length) += sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
  240. }
  241. // Prepare for incoming data
  242. // pending_read_from_host = false;
  243. _prep_out_transaction(cdc_id);
  244. return true;
  245. }
  246. // Invoked when class request DATA stage is finished.
  247. // return false to stall control endpoint (e.g Host send non-sense DATA)
  248. bool cdcd_control_request_complete(uint8_t rhport, tusb_control_request_t const * request)
  249. {
  250. (void) rhport;
  251. //------------- Class Specific Request -------------//
  252. TU_VERIFY (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
  253. // TODO Support multiple interfaces
  254. uint8_t const itf = 0;
  255. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  256. // Invoke callback
  257. if ( CDC_REQUEST_SET_LINE_CODING == request->bRequest )
  258. {
  259. if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
  260. }
  261. return true;
  262. }
  263. // Handle class control request
  264. // return false to stall control endpoint (e.g unsupported request)
  265. bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request)
  266. {
  267. //------------- Class Specific Request -------------//
  268. TU_ASSERT(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
  269. // TODO Support multiple interfaces
  270. uint8_t const itf = 0;
  271. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  272. switch ( request->bRequest )
  273. {
  274. case CDC_REQUEST_SET_LINE_CODING:
  275. usbd_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
  276. break;
  277. case CDC_REQUEST_GET_LINE_CODING:
  278. usbd_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
  279. break;
  280. case CDC_REQUEST_SET_CONTROL_LINE_STATE:
  281. // CDC PSTN v1.2 section 6.3.12
  282. // Bit 0: Indicates if DTE is present or not.
  283. // This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR (Data Terminal Ready)
  284. // Bit 1: Carrier control for half-duplex modems.
  285. // This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send)
  286. p_cdc->line_state = (uint8_t) request->wValue;
  287. usbd_control_status(rhport, request);
  288. // Invoke callback
  289. if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, tu_bit_test(request->wValue, 0), tu_bit_test(request->wValue, 1));
  290. break;
  291. default: return false; // stall unsupported request
  292. }
  293. return true;
  294. }
  295. bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
  296. {
  297. (void) rhport;
  298. (void) result;
  299. // TODO Support multiple interfaces
  300. uint8_t const itf = 0;
  301. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  302. // receive new data
  303. if ( ep_addr == p_cdc->ep_out )
  304. {
  305. for(uint32_t i=0; i<xferred_bytes; i++)
  306. {
  307. tu_fifo_write(&p_cdc->rx_ff, &p_cdc->epout_buf[i]);
  308. // Check for wanted char and invoke callback if needed
  309. if ( tud_cdc_rx_wanted_cb && ( ((signed char) p_cdc->wanted_char) != -1 ) && ( p_cdc->wanted_char == p_cdc->epout_buf[i] ) )
  310. {
  311. tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
  312. }
  313. }
  314. // invoke receive callback (if there is still data)
  315. if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
  316. // prepare for OUT transaction
  317. // pending_read_from_host = false;
  318. _prep_out_transaction(itf);
  319. }
  320. // nothing to do with in and notif endpoint for now
  321. return true;
  322. }
  323. #endif