cdc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * ${file_name}
  3. *
  4. * Created on: ${date}
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2012, hathach (tinyusb.net)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * 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. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tinyUSB stack.
  35. */
  36. #include "cdc.h"
  37. #include "common/fifo.h"
  38. #if defined TUSB_CFG_DEVICE_CDC && defined TUSB_CFG_DEVICE
  39. static USBD_HANDLE_T g_hCdc;
  40. static CDC_LINE_CODING line_coding;
  41. static uint8_t qBuffer[2][CDC_BUFFER_SIZE]; /* TX and RX buffers */
  42. static fifo_t ffTX, ffRX;
  43. void usb_cdc_recv_isr(void) ATTR_WEAK ATTR_ALIAS(usb_cdc_recv_isr_stub);
  44. /**************************************************************************/
  45. /*!
  46. @brief Stub for the optional CDC receive ISR that can be used
  47. to perform some action when data arrives via USB CDC
  48. */
  49. /**************************************************************************/
  50. void usb_cdc_recv_isr_stub (void)
  51. {
  52. return;
  53. }
  54. /**************************************************************************/
  55. /*!
  56. @brief Adds a single byte to the transmit buffer for USB CDC
  57. @param[in] c
  58. The byte to send
  59. @returns TRUE if the byte was successfully added to the TX buffer
  60. @note See example for tusb_cdc_getc
  61. */
  62. /**************************************************************************/
  63. bool tusb_cdc_putc (uint8_t c)
  64. {
  65. return fifo_write(&ffTX, c);
  66. }
  67. /**************************************************************************/
  68. /*!
  69. @brief Reads a single byte from the USB CDC buffer
  70. @param[in] c
  71. Pointer to the location where the byte should be written
  72. @returns TRUE if a byte was read from the buffer
  73. @section EXAMPLE
  74. @code
  75. // Convert incoming characters to upper case and send back via CDC
  76. if (usb_isConfigured())
  77. {
  78. uint8_t cdc_char;
  79. if( tusb_cdc_getc(&cdc_char) )
  80. {
  81. switch (cdc_char)
  82. {
  83. default :
  84. cdc_char = toupper(cdc_char);
  85. tusb_cdc_putc(cdc_char);
  86. break;
  87. }
  88. }
  89. }
  90. @endcode
  91. */
  92. /**************************************************************************/
  93. bool tusb_cdc_getc(uint8_t *c)
  94. {
  95. ASSERT(c, false); // not empty pointer
  96. return fifo_read(&ffRX, c);
  97. }
  98. /**************************************************************************/
  99. /*!
  100. @brief Writes the supplied buffer to the USB CDC device
  101. @param[in] buffer
  102. Pointer to the buffer that should be written via USB CDC
  103. @param[in] count
  104. The number of bytes to write
  105. @returns The actual number of bytes sent out via USB CDC
  106. @section EXAMPLE
  107. @code
  108. // Capture printf output (in Red Suite) and send it to USB CDC
  109. // (Note: With newlib this function should be renamed to _write)
  110. int __sys_write(int file, char *ptr, int len)
  111. {
  112. #ifdef CFG_PRINTF_USBCDC
  113. // Handle USB CDC output
  114. if (usb_isConfigured())
  115. {
  116. int length = len;
  117. while(length > 0)
  118. {
  119. uint16_t transferredCount;
  120. transferredCount = tusb_cdc_send( (uint8_t*) ptr, length);
  121. ptr += transferredCount;
  122. length -= transferredCount;
  123. }
  124. }
  125. #endif
  126. return len;
  127. }
  128. @endcode
  129. */
  130. /**************************************************************************/
  131. uint16_t tusb_cdc_send(uint8_t* buffer, uint16_t count)
  132. {
  133. uint16_t i=0;
  134. ASSERT(buffer && count, 0);
  135. while (i < count && fifo_write(&ffTX, buffer[i]) )
  136. {
  137. i++;
  138. }
  139. return i;
  140. }
  141. /**************************************************************************/
  142. /*!
  143. @brief Reads the incoming CDC buffer up to a maximum number of bytes
  144. @param[in] buffer
  145. Pointer to the buffer where data should be written
  146. @param[in] max
  147. The maximum number of bytes to read
  148. @returns The actual number of bytes received
  149. */
  150. /**************************************************************************/
  151. uint16_t tusb_cdc_recv(uint8_t* buffer, uint16_t max)
  152. {
  153. ASSERT(buffer && max, 0);
  154. return fifo_read_n(&ffRX, buffer, max);
  155. }
  156. // ROM driver bug: cannot hook this to CIC_GetRequest
  157. // Need this to implement GetLineCode & detect
  158. //ErrorCode_t CDC_Control_GetRequest(USBD_HANDLE_T hUsb, USB_SETUP_PACKET *pSetup, uint8_t **pBuffer, uint16_t *length)
  159. //{
  160. // return LPC_OK;
  161. //}
  162. /**************************************************************************/
  163. /*!
  164. @brief TODO Add description
  165. */
  166. /**************************************************************************/
  167. ErrorCode_t CDC_SetLineCoding(USBD_HANDLE_T hUsb, CDC_LINE_CODING *lineCoding)
  168. {
  169. ASSERT(lineCoding, ERR_FAILED);
  170. memcpy(&line_coding, lineCoding, sizeof(CDC_LINE_CODING));
  171. return LPC_OK;
  172. }
  173. /**************************************************************************/
  174. /*!
  175. @brief TODO Add description
  176. */
  177. /**************************************************************************/
  178. ErrorCode_t CDC_SendBreak(USBD_HANDLE_T hCDC, uint16_t mstime)
  179. {
  180. return LPC_OK;
  181. }
  182. /**************************************************************************/
  183. /*!
  184. @brief Bulk Out handler for the USB ROM drivers (UART TX)
  185. */
  186. /**************************************************************************/
  187. ErrorCode_t CDC_BulkIn_Hdlr(USBD_HANDLE_T hUsb, void* data, uint32_t event)
  188. {
  189. if (USB_EVT_IN == event)
  190. {
  191. uint8_t buffer[CDC_DATA_EP_MAXPACKET_SIZE];
  192. uint16_t count;
  193. count = fifo_read_n(&ffTX, buffer, CDC_DATA_EP_MAXPACKET_SIZE);
  194. USBD_API->hw->WriteEP(hUsb, CDC_DATA_EP_IN, buffer, count); // write data to EP
  195. }
  196. return LPC_OK;
  197. }
  198. /**************************************************************************/
  199. /*!
  200. @brief Bulk Out handler for the USB ROM drivers (UART RX)
  201. */
  202. /**************************************************************************/
  203. ErrorCode_t CDC_BulkOut_Hdlr(USBD_HANDLE_T hUsb, void* data, uint32_t event)
  204. {
  205. if (USB_EVT_OUT == event)
  206. {
  207. uint16_t count, i;
  208. uint8_t buffer[CDC_DATA_EP_MAXPACKET_SIZE];
  209. count = USBD_API->hw->ReadEP(hUsb, CDC_DATA_EP_OUT, buffer);
  210. for (i=0; i<count; i++)
  211. {
  212. fifo_write(&ffRX, buffer[i]);
  213. }
  214. usb_cdc_recv_isr();
  215. }
  216. return LPC_OK;
  217. }
  218. /**************************************************************************/
  219. /*!
  220. @brief Initialises USB CDC using the ROM driver
  221. */
  222. /**************************************************************************/
  223. TUSB_Error_t tusb_cdc_init(USBD_HANDLE_T hUsb, USB_INTERFACE_DESCRIPTOR const *const pControlIntfDesc, USB_INTERFACE_DESCRIPTOR const *const pDataIntfDesc, uint32_t* mem_base, uint32_t* mem_size)
  224. {
  225. USBD_CDC_INIT_PARAM_T cdc_param =
  226. {
  227. .mem_base = *mem_base,
  228. .mem_size = *mem_size,
  229. .cif_intf_desc = (uint8_t*) pControlIntfDesc,
  230. .dif_intf_desc = (uint8_t*) pDataIntfDesc,
  231. .SetLineCode = CDC_SetLineCoding,
  232. .SendBreak = CDC_SendBreak,
  233. // .CIC_GetRequest = CDC_Control_GetRequest, // bug from romdrive cannot hook to this handler
  234. // Bug from ROM driver: can not hook bulk in & out handler here, must use USBD API register instead
  235. // .CDC_BulkIN_Hdlr = CDC_BulkIn_Hdlr,
  236. // .CDC_BulkOUT_Hdlr = CDC_BulkOut_Hdlr,
  237. };
  238. ASSERT (pControlIntfDesc && pDataIntfDesc, ERR_FAILED);
  239. /* register Bulk IN & OUT endpoint interrupt handler */
  240. ASSERT ( LPC_OK == USBD_API->core->RegisterEpHandler (hUsb , ((CDC_DATA_EP_IN & 0x0F) << 1) +1 , CDC_BulkIn_Hdlr , NULL), tERROR_FAILED );
  241. ASSERT ( LPC_OK == USBD_API->core->RegisterEpHandler (hUsb , (CDC_DATA_EP_OUT & 0x0F) << 1 , CDC_BulkOut_Hdlr , NULL), tERROR_FAILED );
  242. ASSERT ( LPC_OK == USBD_API->cdc->init(hUsb, &cdc_param, &g_hCdc), tERROR_FAILED);
  243. /* update memory variables */
  244. *mem_base = cdc_param.mem_base;
  245. *mem_size = cdc_param.mem_size;
  246. return tERROR_NONE;
  247. }
  248. /**************************************************************************/
  249. /*!
  250. @brief TODO Add description
  251. */
  252. /**************************************************************************/
  253. TUSB_Error_t tusb_cdc_configured(USBD_HANDLE_T hUsb)
  254. {
  255. uint8_t dummy=0;
  256. USBD_API->hw->WriteEP(hUsb, CDC_DATA_EP_IN, &dummy, 1); // initial packet for IN endpoint, will not work if omitted
  257. // FIXME abstract to hal
  258. #if MCU == MCU_LPC11UXX
  259. fifo_init (&ffTX, qBuffer[0], CDC_BUFFER_SIZE, false, USB_IRQn); // TX is non-overwritable
  260. fifo_init (&ffRX, qBuffer[1], CDC_BUFFER_SIZE, true, USB_IRQn); // RX is overwritable
  261. #elif MCU == MCU_LPC13UXX
  262. fifo_init (&ffTX, qBuffer[0], CDC_BUFFER_SIZE, false, USB_IRQ_IRQn); // TX is non-overwritable
  263. fifo_init (&ffRX, qBuffer[1], CDC_BUFFER_SIZE, true, USB_IRQ_IRQn); // RX is overwritable
  264. #elif MCU == MCU_LPC43XX
  265. fifo_init (&ffTX, qBuffer[0], CDC_BUFFER_SIZE, false, USB0_IRQn); // TODO USB1 TX is non-overwritable
  266. fifo_init (&ffRX, qBuffer[1], CDC_BUFFER_SIZE, true, USB0_IRQn); // RX is overwritable
  267. #else
  268. #error No MCU defined" // TODO asbtract MCU
  269. #endif
  270. return tERROR_NONE;
  271. }
  272. #endif