usbd_cdc_if.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_cdc_if.c
  5. * @version : v3.0_Cube
  6. * @brief : Usb device for Virtual Com Port.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usbd_cdc_if.h"
  22. /* USER CODE BEGIN INCLUDE */
  23. /* USER CODE END INCLUDE */
  24. /* Private typedef -----------------------------------------------------------*/
  25. /* Private define ------------------------------------------------------------*/
  26. /* Private macro -------------------------------------------------------------*/
  27. /* USER CODE BEGIN PV */
  28. /* Private variables ---------------------------------------------------------*/
  29. /* USER CODE END PV */
  30. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  31. * @brief Usb device library.
  32. * @{
  33. */
  34. /** @addtogroup USBD_CDC_IF
  35. * @{
  36. */
  37. /** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions
  38. * @brief Private types.
  39. * @{
  40. */
  41. /* USER CODE BEGIN PRIVATE_TYPES */
  42. /* USER CODE END PRIVATE_TYPES */
  43. /**
  44. * @}
  45. */
  46. /** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines
  47. * @brief Private defines.
  48. * @{
  49. */
  50. /* USER CODE BEGIN PRIVATE_DEFINES */
  51. /* USER CODE END PRIVATE_DEFINES */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
  56. * @brief Private macros.
  57. * @{
  58. */
  59. /* USER CODE BEGIN PRIVATE_MACRO */
  60. /* USER CODE END PRIVATE_MACRO */
  61. /**
  62. * @}
  63. */
  64. /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
  65. * @brief Private variables.
  66. * @{
  67. */
  68. /* Create buffer for reception and transmission */
  69. /* It's up to user to redefine and/or remove those define */
  70. /** Received data over USB are stored in this buffer */
  71. uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  72. /** Data to send over USB CDC are stored in this buffer */
  73. uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  74. /* USER CODE BEGIN PRIVATE_VARIABLES */
  75. /* USER CODE END PRIVATE_VARIABLES */
  76. /**
  77. * @}
  78. */
  79. /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
  80. * @brief Public variables.
  81. * @{
  82. */
  83. extern USBD_HandleTypeDef hUsbDeviceFS;
  84. /* USER CODE BEGIN EXPORTED_VARIABLES */
  85. /* USER CODE END EXPORTED_VARIABLES */
  86. /**
  87. * @}
  88. */
  89. /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
  90. * @brief Private functions declaration.
  91. * @{
  92. */
  93. static int8_t CDC_Init_FS(void);
  94. static int8_t CDC_DeInit_FS(void);
  95. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
  96. static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
  97. static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
  98. /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
  99. /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
  100. /**
  101. * @}
  102. */
  103. USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  104. {
  105. CDC_Init_FS,
  106. CDC_DeInit_FS,
  107. CDC_Control_FS,
  108. CDC_Receive_FS,
  109. CDC_TransmitCplt_FS
  110. };
  111. /* Private functions ---------------------------------------------------------*/
  112. /**
  113. * @brief Initializes the CDC media low layer over the FS USB IP
  114. * @retval USBD_OK if all operations are OK else USBD_FAIL
  115. */
  116. static int8_t CDC_Init_FS(void)
  117. {
  118. /* USER CODE BEGIN 3 */
  119. /* Set Application Buffers */
  120. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  121. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  122. return (USBD_OK);
  123. /* USER CODE END 3 */
  124. }
  125. /**
  126. * @brief DeInitializes the CDC media low layer
  127. * @retval USBD_OK if all operations are OK else USBD_FAIL
  128. */
  129. static int8_t CDC_DeInit_FS(void)
  130. {
  131. /* USER CODE BEGIN 4 */
  132. return (USBD_OK);
  133. /* USER CODE END 4 */
  134. }
  135. /**
  136. * @brief Manage the CDC class requests
  137. * @param cmd: Command code
  138. * @param pbuf: Buffer containing command data (request parameters)
  139. * @param length: Number of data to be sent (in bytes)
  140. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  141. */
  142. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
  143. {
  144. /* USER CODE BEGIN 5 */
  145. switch(cmd)
  146. {
  147. case CDC_SEND_ENCAPSULATED_COMMAND:
  148. break;
  149. case CDC_GET_ENCAPSULATED_RESPONSE:
  150. break;
  151. case CDC_SET_COMM_FEATURE:
  152. break;
  153. case CDC_GET_COMM_FEATURE:
  154. break;
  155. case CDC_CLEAR_COMM_FEATURE:
  156. break;
  157. /*******************************************************************************/
  158. /* Line Coding Structure */
  159. /*-----------------------------------------------------------------------------*/
  160. /* Offset | Field | Size | Value | Description */
  161. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  162. /* 4 | bCharFormat | 1 | Number | Stop bits */
  163. /* 0 - 1 Stop bit */
  164. /* 1 - 1.5 Stop bits */
  165. /* 2 - 2 Stop bits */
  166. /* 5 | bParityType | 1 | Number | Parity */
  167. /* 0 - None */
  168. /* 1 - Odd */
  169. /* 2 - Even */
  170. /* 3 - Mark */
  171. /* 4 - Space */
  172. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  173. /*******************************************************************************/
  174. case CDC_SET_LINE_CODING:
  175. break;
  176. case CDC_GET_LINE_CODING:
  177. break;
  178. case CDC_SET_CONTROL_LINE_STATE:
  179. break;
  180. case CDC_SEND_BREAK:
  181. break;
  182. default:
  183. break;
  184. }
  185. return (USBD_OK);
  186. /* USER CODE END 5 */
  187. }
  188. /**
  189. * @brief Data received over USB OUT endpoint are sent over CDC interface
  190. * through this function.
  191. *
  192. * @note
  193. * This function will issue a NAK packet on any OUT packet received on
  194. * USB endpoint until exiting this function. If you exit this function
  195. * before transfer is complete on CDC interface (ie. using DMA controller)
  196. * it will result in receiving more data while previous ones are still
  197. * not sent.
  198. *
  199. * @param Buf: Buffer of data to be received
  200. * @param Len: Number of data received (in bytes)
  201. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  202. */
  203. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  204. {
  205. /* USER CODE BEGIN 6 */
  206. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  207. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  208. return (USBD_OK);
  209. /* USER CODE END 6 */
  210. }
  211. /**
  212. * @brief CDC_Transmit_FS
  213. * Data to send over USB IN endpoint are sent over CDC interface
  214. * through this function.
  215. * @note
  216. *
  217. *
  218. * @param Buf: Buffer of data to be sent
  219. * @param Len: Number of data to be sent (in bytes)
  220. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  221. */
  222. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  223. {
  224. uint8_t result = USBD_OK;
  225. /* USER CODE BEGIN 7 */
  226. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  227. if (hcdc->TxState != 0){
  228. return USBD_BUSY;
  229. }
  230. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  231. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  232. /* USER CODE END 7 */
  233. return result;
  234. }
  235. /**
  236. * @brief CDC_TransmitCplt_FS
  237. * Data transmitted callback
  238. *
  239. * @note
  240. * This function is IN transfer complete callback used to inform user that
  241. * the submitted Data is successfully sent over USB.
  242. *
  243. * @param Buf: Buffer of data to be received
  244. * @param Len: Number of data received (in bytes)
  245. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  246. */
  247. static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
  248. {
  249. uint8_t result = USBD_OK;
  250. /* USER CODE BEGIN 13 */
  251. UNUSED(Buf);
  252. UNUSED(Len);
  253. UNUSED(epnum);
  254. /* USER CODE END 13 */
  255. return result;
  256. }
  257. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  258. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */