cdc_device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**************************************************************************/
  2. /*!
  3. @file cdc_device.c
  4. @author hathach (tinyusb.org)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, hathach (tinyusb.org)
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. This file is part of the tinyusb stack.
  30. */
  31. /**************************************************************************/
  32. #include "tusb_option.h"
  33. #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC)
  34. #define _TINY_USB_SOURCE_FILE_
  35. //--------------------------------------------------------------------+
  36. // INCLUDE
  37. //--------------------------------------------------------------------+
  38. #include "cdc_device.h"
  39. #include "device/usbd_pvt.h"
  40. //--------------------------------------------------------------------+
  41. // MACRO CONSTANT TYPEDEF
  42. //--------------------------------------------------------------------+
  43. typedef struct
  44. {
  45. uint8_t itf_num;
  46. uint8_t ep_notif;
  47. uint8_t ep_in;
  48. uint8_t ep_out;
  49. // Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
  50. uint8_t line_state;
  51. /*------------- From this point, data is not cleared by bus reset -------------*/
  52. char wanted_char;
  53. CFG_TUSB_MEM_ALIGN cdc_line_coding_t line_coding;
  54. // FIFO
  55. tu_fifo_t rx_ff;
  56. tu_fifo_t tx_ff;
  57. uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
  58. uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
  59. #if CFG_FIFO_MUTEX
  60. osal_mutex_def_t rx_ff_mutex;
  61. osal_mutex_def_t tx_ff_mutex;
  62. #endif
  63. // Endpoint Transfer buffer
  64. CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE];
  65. CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EPSIZE];
  66. }cdcd_interface_t;
  67. #define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
  68. //--------------------------------------------------------------------+
  69. // INTERNAL OBJECT & FUNCTION DECLARATION
  70. //--------------------------------------------------------------------+
  71. CFG_TUSB_ATTR_USBRAM static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
  72. //--------------------------------------------------------------------+
  73. // APPLICATION API
  74. //--------------------------------------------------------------------+
  75. bool tud_cdc_n_connected(uint8_t itf)
  76. {
  77. // DTR (bit 0) active is considered as connected
  78. return BIT_TEST_(_cdcd_itf[itf].line_state, 0);
  79. }
  80. uint8_t tud_cdc_n_get_line_state (uint8_t itf)
  81. {
  82. return _cdcd_itf[itf].line_state;
  83. }
  84. void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
  85. {
  86. (*coding) = _cdcd_itf[itf].line_coding;
  87. }
  88. void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
  89. {
  90. _cdcd_itf[itf].wanted_char = wanted;
  91. }
  92. //--------------------------------------------------------------------+
  93. // READ API
  94. //--------------------------------------------------------------------+
  95. uint32_t tud_cdc_n_available(uint8_t itf)
  96. {
  97. return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
  98. }
  99. char tud_cdc_n_read_char(uint8_t itf)
  100. {
  101. char ch;
  102. return tu_fifo_read(&_cdcd_itf[itf].rx_ff, &ch) ? ch : (-1);
  103. }
  104. uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
  105. {
  106. return tu_fifo_read_n(&_cdcd_itf[itf].rx_ff, buffer, bufsize);
  107. }
  108. char tud_cdc_n_peek(uint8_t itf, int pos)
  109. {
  110. char ch;
  111. return tu_fifo_peek_at(&_cdcd_itf[itf].rx_ff, pos, &ch) ? ch : (-1);
  112. }
  113. void tud_cdc_n_read_flush (uint8_t itf)
  114. {
  115. tu_fifo_clear(&_cdcd_itf[itf].rx_ff);
  116. }
  117. //--------------------------------------------------------------------+
  118. // WRITE API
  119. //--------------------------------------------------------------------+
  120. uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
  121. {
  122. return tud_cdc_n_write(itf, &ch, 1);
  123. }
  124. uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
  125. {
  126. return tud_cdc_n_write(itf, str, strlen(str));
  127. }
  128. uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
  129. {
  130. uint16_t ret = tu_fifo_write_n(&_cdcd_itf[itf].tx_ff, buffer, bufsize);
  131. #if 0 // TODO issue with circuitpython's REPL
  132. // flush if queue more than endpoint size
  133. if ( tu_fifo_count(&_cdcd_itf[itf].tx_ff) >= CFG_TUD_CDC_EPSIZE )
  134. {
  135. tud_cdc_n_write_flush(itf);
  136. }
  137. #endif
  138. return ret;
  139. }
  140. bool tud_cdc_n_write_flush (uint8_t itf)
  141. {
  142. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  143. TU_VERIFY( !dcd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
  144. uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].tx_ff, p_cdc->epin_buf, CFG_TUD_CDC_EPSIZE);
  145. if ( count )
  146. {
  147. TU_VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected
  148. TU_ASSERT( dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count) );
  149. }
  150. return true;
  151. }
  152. //--------------------------------------------------------------------+
  153. // USBD Driver API
  154. //--------------------------------------------------------------------+
  155. void cdcd_init(void)
  156. {
  157. tu_memclr(_cdcd_itf, sizeof(_cdcd_itf));
  158. for(uint8_t i=0; i<CFG_TUD_CDC; i++)
  159. {
  160. cdcd_interface_t* ser = &_cdcd_itf[i];
  161. ser->wanted_char = -1;
  162. // default line coding is : stop bit = 1, parity = none, data bits = 8
  163. ser->line_coding.bit_rate = 115200;
  164. ser->line_coding.stop_bits = 0;
  165. ser->line_coding.parity = 0;
  166. ser->line_coding.data_bits = 8;
  167. // config fifo
  168. tu_fifo_config(&ser->rx_ff, ser->rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
  169. tu_fifo_config(&ser->tx_ff, ser->tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
  170. #if CFG_FIFO_MUTEX
  171. tu_fifo_config_mutex(&ser->rx_ff, osal_mutex_create(&ser->rx_ff_mutex));
  172. tu_fifo_config_mutex(&ser->tx_ff, osal_mutex_create(&ser->tx_ff_mutex));
  173. #endif
  174. }
  175. }
  176. void cdcd_reset(uint8_t rhport)
  177. {
  178. (void) rhport;
  179. for(uint8_t i=0; i<CFG_TUD_CDC; i++)
  180. {
  181. tu_memclr(&_cdcd_itf[i], ITF_MEM_RESET_SIZE);
  182. tu_fifo_clear(&_cdcd_itf[i].rx_ff);
  183. tu_fifo_clear(&_cdcd_itf[i].tx_ff);
  184. }
  185. }
  186. tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length)
  187. {
  188. if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != p_interface_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS;
  189. if ( !(tu_within(CDC_COMM_PROTOCOL_ATCOMMAND, p_interface_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) ||
  190. 0xff == p_interface_desc->bInterfaceProtocol) )
  191. {
  192. return TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL;
  193. }
  194. // Find available interface
  195. cdcd_interface_t * p_cdc = NULL;
  196. for(uint8_t i=0; i<CFG_TUD_CDC; i++)
  197. {
  198. if ( _cdcd_itf[i].ep_in == 0 )
  199. {
  200. p_cdc = &_cdcd_itf[i];
  201. break;
  202. }
  203. }
  204. //------------- Control Interface -------------//
  205. p_cdc->itf_num = p_interface_desc->bInterfaceNumber;
  206. uint8_t const * p_desc = descriptor_next ( (uint8_t const *) p_interface_desc );
  207. (*p_length) = sizeof(tusb_desc_interface_t);
  208. // Communication Functional Descriptors
  209. while( TUSB_DESC_CLASS_SPECIFIC == p_desc[DESC_OFFSET_TYPE] )
  210. {
  211. (*p_length) += p_desc[DESC_OFFSET_LEN];
  212. p_desc = descriptor_next(p_desc);
  213. }
  214. if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE])
  215. { // notification endpoint if any
  216. TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), TUSB_ERROR_DCD_OPEN_PIPE_FAILED);
  217. p_cdc->ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
  218. (*p_length) += p_desc[DESC_OFFSET_LEN];
  219. p_desc = descriptor_next(p_desc);
  220. }
  221. //------------- Data Interface (if any) -------------//
  222. if ( (TUSB_DESC_INTERFACE == p_desc[DESC_OFFSET_TYPE]) &&
  223. (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
  224. {
  225. // next to endpoint descritpor
  226. (*p_length) += p_desc[DESC_OFFSET_LEN];
  227. p_desc = descriptor_next(p_desc);
  228. // Open endpoint pair with usbd helper
  229. tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) p_desc;
  230. TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) );
  231. (*p_length) += 2*sizeof(tusb_desc_endpoint_t);
  232. }
  233. // Prepare for incoming data
  234. TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE), TUSB_ERROR_DCD_EDPT_XFER);
  235. return TUSB_ERROR_NONE;
  236. }
  237. tusb_error_t cdcd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request)
  238. {
  239. OSAL_SUBTASK_BEGIN
  240. //------------- Class Specific Request -------------//
  241. if (p_request->bmRequestType_bit.type != TUSB_REQ_TYPE_CLASS) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
  242. // TODO Support multiple interfaces
  243. uint8_t const itf = 0;
  244. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  245. if ( (CDC_REQUEST_GET_LINE_CODING == p_request->bRequest) || (CDC_REQUEST_SET_LINE_CODING == p_request->bRequest) )
  246. {
  247. uint16_t len = tu_min16(sizeof(cdc_line_coding_t), p_request->wLength);
  248. usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, &p_cdc->line_coding, len);
  249. // Invoke callback
  250. if (CDC_REQUEST_SET_LINE_CODING == p_request->bRequest)
  251. {
  252. if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
  253. }
  254. }
  255. else if (CDC_REQUEST_SET_CONTROL_LINE_STATE == p_request->bRequest )
  256. {
  257. dcd_control_status(rhport, p_request->bmRequestType_bit.direction); // ACK control request
  258. // CDC PSTN v1.2 section 6.3.12
  259. // Bit 0: Indicates if DTE is present or not.
  260. // This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR (Data Terminal Ready)
  261. // Bit 1: Carrier control for half-duplex modems.
  262. // This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send)
  263. p_cdc->line_state = (uint8_t) p_request->wValue;
  264. // Invoke callback
  265. if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, BIT_TEST_(p_request->wValue, 0), BIT_TEST_(p_request->wValue, 1));
  266. }
  267. else
  268. {
  269. dcd_control_stall(rhport); // stall unsupported request
  270. }
  271. OSAL_SUBTASK_END
  272. }
  273. tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes)
  274. {
  275. // TODO Support multiple interfaces
  276. uint8_t const itf = 0;
  277. cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
  278. // receive new data
  279. if ( ep_addr == p_cdc->ep_out )
  280. {
  281. char const wanted = p_cdc->wanted_char;
  282. for(uint32_t i=0; i<xferred_bytes; i++)
  283. {
  284. tu_fifo_write(&p_cdc->rx_ff, &p_cdc->epout_buf[i]);
  285. // Check for wanted char and invoke callback if needed
  286. if ( tud_cdc_rx_wanted_cb && ( wanted != -1 ) && ( wanted == p_cdc->epout_buf[i] ) )
  287. {
  288. tud_cdc_rx_wanted_cb(itf, wanted);
  289. }
  290. }
  291. // invoke receive callback (if there is still data)
  292. if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
  293. // prepare for next
  294. TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE), TUSB_ERROR_DCD_EDPT_XFER );
  295. }
  296. // nothing to do with in and notif endpoint
  297. return TUSB_ERROR_NONE;
  298. }
  299. #endif