usbd_cdc_if.c 8.9 KB

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