Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

396 строки
11 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_cdc_if.c
  5. * @version : v1.0_Cube
  6. * @brief : Usb device for Virtual Com Port.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2026 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. static volatile USB_CDC_RUNTIME_DIAGNOSTICS CdcRuntimeDiagnostics;
  76. /* USER CODE END PRIVATE_VARIABLES */
  77. /**
  78. * @}
  79. */
  80. /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
  81. * @brief Public variables.
  82. * @{
  83. */
  84. extern USBD_HandleTypeDef hUsbDeviceFS;
  85. /* USER CODE BEGIN EXPORTED_VARIABLES */
  86. /* USER CODE END EXPORTED_VARIABLES */
  87. /**
  88. * @}
  89. */
  90. /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
  91. * @brief Private functions declaration.
  92. * @{
  93. */
  94. static int8_t CDC_Init_FS(void);
  95. static int8_t CDC_DeInit_FS(void);
  96. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
  97. static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
  98. static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
  99. /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
  100. /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
  101. /**
  102. * @}
  103. */
  104. USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  105. {
  106. CDC_Init_FS,
  107. CDC_DeInit_FS,
  108. CDC_Control_FS,
  109. CDC_Receive_FS,
  110. CDC_TransmitCplt_FS
  111. };
  112. /* Private functions ---------------------------------------------------------*/
  113. /**
  114. * @brief Initializes the CDC media low layer over the FS USB IP
  115. * @retval USBD_OK if all operations are OK else USBD_FAIL
  116. */
  117. static int8_t CDC_Init_FS(void)
  118. {
  119. /* USER CODE BEGIN 3 */
  120. /* Set Application Buffers */
  121. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  122. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  123. CdcRuntimeDiagnostics.initialized = 1U;
  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. CdcRuntimeDiagnostics.initialized = 0U;
  135. return (USBD_OK);
  136. /* USER CODE END 4 */
  137. }
  138. /**
  139. * @brief Manage the CDC class requests
  140. * @param cmd: Command code
  141. * @param pbuf: Buffer containing command data (request parameters)
  142. * @param length: Number of data to be sent (in bytes)
  143. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  144. */
  145. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
  146. {
  147. /* USER CODE BEGIN 5 */
  148. switch(cmd)
  149. {
  150. case CDC_SEND_ENCAPSULATED_COMMAND:
  151. break;
  152. case CDC_GET_ENCAPSULATED_RESPONSE:
  153. break;
  154. case CDC_SET_COMM_FEATURE:
  155. break;
  156. case CDC_GET_COMM_FEATURE:
  157. break;
  158. case CDC_CLEAR_COMM_FEATURE:
  159. break;
  160. /*******************************************************************************/
  161. /* Line Coding Structure */
  162. /*-----------------------------------------------------------------------------*/
  163. /* Offset | Field | Size | Value | Description */
  164. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  165. /* 4 | bCharFormat | 1 | Number | Stop bits */
  166. /* 0 - 1 Stop bit */
  167. /* 1 - 1.5 Stop bits */
  168. /* 2 - 2 Stop bits */
  169. /* 5 | bParityType | 1 | Number | Parity */
  170. /* 0 - None */
  171. /* 1 - Odd */
  172. /* 2 - Even */
  173. /* 3 - Mark */
  174. /* 4 - Space */
  175. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  176. /*******************************************************************************/
  177. case CDC_SET_LINE_CODING:
  178. break;
  179. case CDC_GET_LINE_CODING:
  180. break;
  181. case CDC_SET_CONTROL_LINE_STATE:
  182. break;
  183. case CDC_SEND_BREAK:
  184. break;
  185. default:
  186. break;
  187. }
  188. return (USBD_OK);
  189. /* USER CODE END 5 */
  190. }
  191. /**
  192. * @brief Data received over USB OUT endpoint are sent over CDC interface
  193. * through this function.
  194. *
  195. * @note
  196. * This function will issue a NAK packet on any OUT packet received on
  197. * USB endpoint until exiting this function. If you exit this function
  198. * before transfer is complete on CDC interface (ie. using DMA controller)
  199. * it will result in receiving more data while previous ones are still
  200. * not sent.
  201. *
  202. * @param Buf: Buffer of data to be received
  203. * @param Len: Number of data received (in bytes)
  204. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  205. */
  206. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  207. {
  208. /* USER CODE BEGIN 6 */
  209. uint8_t result;
  210. if ((Buf == NULL) || (Len == NULL))
  211. {
  212. CdcRuntimeDiagnostics.rxRearmFailureCount++;
  213. return (USBD_FAIL);
  214. }
  215. CdcRuntimeDiagnostics.rxPacketCount++;
  216. CdcRuntimeDiagnostics.rxByteCount += *Len;
  217. result = USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  218. if (result == USBD_OK)
  219. {
  220. result = USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  221. }
  222. if (result != USBD_OK)
  223. {
  224. CdcRuntimeDiagnostics.rxRearmFailureCount++;
  225. }
  226. return (int8_t)result;
  227. /* USER CODE END 6 */
  228. }
  229. /**
  230. * @brief CDC_Transmit_FS
  231. * Data to send over USB IN endpoint are sent over CDC interface
  232. * through this function.
  233. * @note
  234. *
  235. *
  236. * @param Buf: Buffer of data to be sent
  237. * @param Len: Number of data to be sent (in bytes)
  238. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  239. */
  240. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  241. {
  242. uint8_t result = USBD_OK;
  243. /* USER CODE BEGIN 7 */
  244. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  245. CdcRuntimeDiagnostics.txRequestCount++;
  246. if ((Buf == NULL) || (Len == 0U) || (hcdc == NULL))
  247. {
  248. CdcRuntimeDiagnostics.txFailureCount++;
  249. return USBD_FAIL;
  250. }
  251. if (hcdc->TxState != 0U){
  252. CdcRuntimeDiagnostics.txBusyCount++;
  253. return USBD_BUSY;
  254. }
  255. result = USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  256. if (result == USBD_OK)
  257. {
  258. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  259. }
  260. if (result == USBD_OK)
  261. {
  262. CdcRuntimeDiagnostics.txByteCount += Len;
  263. }
  264. else if (result == USBD_BUSY)
  265. {
  266. CdcRuntimeDiagnostics.txBusyCount++;
  267. }
  268. else
  269. {
  270. CdcRuntimeDiagnostics.txFailureCount++;
  271. }
  272. /* USER CODE END 7 */
  273. return result;
  274. }
  275. /**
  276. * @brief CDC_TransmitCplt_FS
  277. * Data transmitted callback
  278. *
  279. * @note
  280. * This function is IN transfer complete callback used to inform user that
  281. * the submitted Data is successfully sent over USB.
  282. *
  283. * @param Buf: Buffer of data to be received
  284. * @param Len: Number of data received (in bytes)
  285. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  286. */
  287. static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
  288. {
  289. uint8_t result = USBD_OK;
  290. /* USER CODE BEGIN 13 */
  291. UNUSED(Buf);
  292. UNUSED(Len);
  293. UNUSED(epnum);
  294. CdcRuntimeDiagnostics.txCompleteCount++;
  295. /* USER CODE END 13 */
  296. return result;
  297. }
  298. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  299. uint8_t CDC_GetRuntimeDiagnostics(USB_CDC_RUNTIME_DIAGNOSTICS *diagnostics)
  300. {
  301. if (diagnostics == NULL)
  302. {
  303. return 0U;
  304. }
  305. /* Aligned word reads are atomic on Cortex-M4. Do not mask the motion
  306. * interrupts merely to make unrelated USB counters mutually consistent. */
  307. __DMB();
  308. diagnostics->rxPacketCount = CdcRuntimeDiagnostics.rxPacketCount;
  309. diagnostics->rxByteCount = CdcRuntimeDiagnostics.rxByteCount;
  310. diagnostics->rxRearmFailureCount =
  311. CdcRuntimeDiagnostics.rxRearmFailureCount;
  312. diagnostics->txRequestCount = CdcRuntimeDiagnostics.txRequestCount;
  313. diagnostics->txByteCount = CdcRuntimeDiagnostics.txByteCount;
  314. diagnostics->txBusyCount = CdcRuntimeDiagnostics.txBusyCount;
  315. diagnostics->txFailureCount = CdcRuntimeDiagnostics.txFailureCount;
  316. diagnostics->txCompleteCount = CdcRuntimeDiagnostics.txCompleteCount;
  317. diagnostics->initialized = CdcRuntimeDiagnostics.initialized;
  318. __DMB();
  319. return 1U;
  320. }
  321. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  322. /**
  323. * @}
  324. */
  325. /**
  326. * @}
  327. */