Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

946 linhas
28 KiB

  1. /**
  2. ******************************************************************************
  3. * @file usbd_cdc.c
  4. * @author MCD Application Team
  5. * @brief This file provides the high layer firmware functions to manage the
  6. * following functionalities of the USB CDC Class:
  7. * - Initialization and Configuration of high and low layer
  8. * - Enumeration as CDC Device (and enumeration for each implemented memory interface)
  9. * - OUT/IN data transfer
  10. * - Command IN transfer (class requests management)
  11. * - Error management
  12. *
  13. * @verbatim
  14. *
  15. * ===================================================================
  16. * CDC Class Driver Description
  17. * ===================================================================
  18. * This driver manages the "Universal Serial Bus Class Definitions for Communications Devices
  19. * Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus
  20. * Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007"
  21. * This driver implements the following aspects of the specification:
  22. * - Device descriptor management
  23. * - Configuration descriptor management
  24. * - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN)
  25. * - Requests management (as described in section 6.2 in specification)
  26. * - Abstract Control Model compliant
  27. * - Union Functional collection (using 1 IN endpoint for control)
  28. * - Data interface class
  29. *
  30. * These aspects may be enriched or modified for a specific user application.
  31. *
  32. * This driver doesn't implement the following aspects of the specification
  33. * (but it is possible to manage these features with some modifications on this driver):
  34. * - Any class-specific aspect relative to communication classes should be managed by user application.
  35. * - All communication classes other than PSTN are not managed
  36. *
  37. * @endverbatim
  38. *
  39. ******************************************************************************
  40. * @attention
  41. *
  42. * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
  43. * All rights reserved.</center></h2>
  44. *
  45. * This software component is licensed by ST under Ultimate Liberty license
  46. * SLA0044, the "License"; You may not use this file except in compliance with
  47. * the License. You may obtain a copy of the License at:
  48. * http://www.st.com/SLA0044
  49. *
  50. ******************************************************************************
  51. */
  52. /* BSPDependencies
  53. - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c"
  54. - "stm32xxxxx_{eval}{discovery}_io.c"
  55. EndBSPDependencies */
  56. /* Includes ------------------------------------------------------------------*/
  57. #include "usbd_cdc.h"
  58. #include "usbd_ctlreq.h"
  59. /** @addtogroup STM32_USB_DEVICE_LIBRARY
  60. * @{
  61. */
  62. /** @defgroup USBD_CDC
  63. * @brief usbd core module
  64. * @{
  65. */
  66. /** @defgroup USBD_CDC_Private_TypesDefinitions
  67. * @{
  68. */
  69. /**
  70. * @}
  71. */
  72. /** @defgroup USBD_CDC_Private_Defines
  73. * @{
  74. */
  75. /**
  76. * @}
  77. */
  78. /** @defgroup USBD_CDC_Private_Macros
  79. * @{
  80. */
  81. /**
  82. * @}
  83. */
  84. /** @defgroup USBD_CDC_Private_FunctionPrototypes
  85. * @{
  86. */
  87. static uint8_t USBD_CDC_Init (USBD_HandleTypeDef *pdev,
  88. uint8_t cfgidx);
  89. static uint8_t USBD_CDC_DeInit (USBD_HandleTypeDef *pdev,
  90. uint8_t cfgidx);
  91. static uint8_t USBD_CDC_Setup (USBD_HandleTypeDef *pdev,
  92. USBD_SetupReqTypedef *req);
  93. static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev,
  94. uint8_t epnum);
  95. static uint8_t USBD_CDC_DataOut (USBD_HandleTypeDef *pdev,
  96. uint8_t epnum);
  97. static uint8_t USBD_CDC_EP0_RxReady (USBD_HandleTypeDef *pdev);
  98. static uint8_t *USBD_CDC_GetFSCfgDesc (uint16_t *length);
  99. static uint8_t *USBD_CDC_GetHSCfgDesc (uint16_t *length);
  100. static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length);
  101. static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length);
  102. uint8_t *USBD_CDC_GetDeviceQualifierDescriptor (uint16_t *length);
  103. /* USB Standard Device Descriptor */
  104. __ALIGN_BEGIN static uint8_t USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
  105. {
  106. USB_LEN_DEV_QUALIFIER_DESC,
  107. USB_DESC_TYPE_DEVICE_QUALIFIER,
  108. 0x00,
  109. 0x02,
  110. 0x00,
  111. 0x00,
  112. 0x00,
  113. 0x40,
  114. 0x01,
  115. 0x00,
  116. };
  117. /**
  118. * @}
  119. */
  120. /** @defgroup USBD_CDC_Private_Variables
  121. * @{
  122. */
  123. /* CDC interface class callbacks structure */
  124. USBD_ClassTypeDef USBD_CDC =
  125. {
  126. USBD_CDC_Init,
  127. USBD_CDC_DeInit,
  128. USBD_CDC_Setup,
  129. NULL, /* EP0_TxSent, */
  130. USBD_CDC_EP0_RxReady,
  131. USBD_CDC_DataIn,
  132. USBD_CDC_DataOut,
  133. NULL,
  134. NULL,
  135. NULL,
  136. USBD_CDC_GetHSCfgDesc,
  137. USBD_CDC_GetFSCfgDesc,
  138. USBD_CDC_GetOtherSpeedCfgDesc,
  139. USBD_CDC_GetDeviceQualifierDescriptor,
  140. };
  141. /* USB CDC device Configuration Descriptor */
  142. __ALIGN_BEGIN uint8_t USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
  143. {
  144. /*Configuration Descriptor*/
  145. 0x09, /* bLength: Configuration Descriptor size */
  146. USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
  147. USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */
  148. 0x00,
  149. 0x02, /* bNumInterfaces: 2 interface */
  150. 0x01, /* bConfigurationValue: Configuration value */
  151. 0x00, /* iConfiguration: Index of string descriptor describing the configuration */
  152. 0xC0, /* bmAttributes: self powered */
  153. 0x32, /* MaxPower 0 mA */
  154. /*---------------------------------------------------------------------------*/
  155. /*Interface Descriptor */
  156. 0x09, /* bLength: Interface Descriptor size */
  157. USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
  158. /* Interface descriptor type */
  159. 0x00, /* bInterfaceNumber: Number of Interface */
  160. 0x00, /* bAlternateSetting: Alternate setting */
  161. 0x01, /* bNumEndpoints: One endpoints used */
  162. 0x02, /* bInterfaceClass: Communication Interface Class */
  163. 0x02, /* bInterfaceSubClass: Abstract Control Model */
  164. 0x01, /* bInterfaceProtocol: Common AT commands */
  165. 0x00, /* iInterface: */
  166. /*Header Functional Descriptor*/
  167. 0x05, /* bLength: Endpoint Descriptor size */
  168. 0x24, /* bDescriptorType: CS_INTERFACE */
  169. 0x00, /* bDescriptorSubtype: Header Func Desc */
  170. 0x10, /* bcdCDC: spec release number */
  171. 0x01,
  172. /*Call Management Functional Descriptor*/
  173. 0x05, /* bFunctionLength */
  174. 0x24, /* bDescriptorType: CS_INTERFACE */
  175. 0x01, /* bDescriptorSubtype: Call Management Func Desc */
  176. 0x00, /* bmCapabilities: D0+D1 */
  177. 0x01, /* bDataInterface: 1 */
  178. /*ACM Functional Descriptor*/
  179. 0x04, /* bFunctionLength */
  180. 0x24, /* bDescriptorType: CS_INTERFACE */
  181. 0x02, /* bDescriptorSubtype: Abstract Control Management desc */
  182. 0x02, /* bmCapabilities */
  183. /*Union Functional Descriptor*/
  184. 0x05, /* bFunctionLength */
  185. 0x24, /* bDescriptorType: CS_INTERFACE */
  186. 0x06, /* bDescriptorSubtype: Union func desc */
  187. 0x00, /* bMasterInterface: Communication class interface */
  188. 0x01, /* bSlaveInterface0: Data Class Interface */
  189. /*Endpoint 2 Descriptor*/
  190. 0x07, /* bLength: Endpoint Descriptor size */
  191. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  192. CDC_CMD_EP, /* bEndpointAddress */
  193. 0x03, /* bmAttributes: Interrupt */
  194. LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
  195. HIBYTE(CDC_CMD_PACKET_SIZE),
  196. CDC_HS_BINTERVAL, /* bInterval: */
  197. /*---------------------------------------------------------------------------*/
  198. /*Data class interface descriptor*/
  199. 0x09, /* bLength: Endpoint Descriptor size */
  200. USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
  201. 0x01, /* bInterfaceNumber: Number of Interface */
  202. 0x00, /* bAlternateSetting: Alternate setting */
  203. 0x02, /* bNumEndpoints: Two endpoints used */
  204. 0x0A, /* bInterfaceClass: CDC */
  205. 0x00, /* bInterfaceSubClass: */
  206. 0x00, /* bInterfaceProtocol: */
  207. 0x00, /* iInterface: */
  208. /*Endpoint OUT Descriptor*/
  209. 0x07, /* bLength: Endpoint Descriptor size */
  210. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  211. CDC_OUT_EP, /* bEndpointAddress */
  212. 0x02, /* bmAttributes: Bulk */
  213. LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
  214. HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
  215. 0x00, /* bInterval: ignore for Bulk transfer */
  216. /*Endpoint IN Descriptor*/
  217. 0x07, /* bLength: Endpoint Descriptor size */
  218. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  219. CDC_IN_EP, /* bEndpointAddress */
  220. 0x02, /* bmAttributes: Bulk */
  221. LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
  222. HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
  223. 0x00 /* bInterval: ignore for Bulk transfer */
  224. } ;
  225. /* USB CDC device Configuration Descriptor */
  226. __ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
  227. {
  228. /*Configuration Descriptor*/
  229. 0x09, /* bLength: Configuration Descriptor size */
  230. USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
  231. USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */
  232. 0x00,
  233. 0x02, /* bNumInterfaces: 2 interface */
  234. 0x01, /* bConfigurationValue: Configuration value */
  235. 0x00, /* iConfiguration: Index of string descriptor describing the configuration */
  236. 0xC0, /* bmAttributes: self powered */
  237. 0x32, /* MaxPower 0 mA */
  238. /*---------------------------------------------------------------------------*/
  239. /*Interface Descriptor */
  240. 0x09, /* bLength: Interface Descriptor size */
  241. USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
  242. /* Interface descriptor type */
  243. 0x00, /* bInterfaceNumber: Number of Interface */
  244. 0x00, /* bAlternateSetting: Alternate setting */
  245. 0x01, /* bNumEndpoints: One endpoints used */
  246. 0x02, /* bInterfaceClass: Communication Interface Class */
  247. 0x02, /* bInterfaceSubClass: Abstract Control Model */
  248. 0x01, /* bInterfaceProtocol: Common AT commands */
  249. 0x00, /* iInterface: */
  250. /*Header Functional Descriptor*/
  251. 0x05, /* bLength: Endpoint Descriptor size */
  252. 0x24, /* bDescriptorType: CS_INTERFACE */
  253. 0x00, /* bDescriptorSubtype: Header Func Desc */
  254. 0x10, /* bcdCDC: spec release number */
  255. 0x01,
  256. /*Call Management Functional Descriptor*/
  257. 0x05, /* bFunctionLength */
  258. 0x24, /* bDescriptorType: CS_INTERFACE */
  259. 0x01, /* bDescriptorSubtype: Call Management Func Desc */
  260. 0x00, /* bmCapabilities: D0+D1 */
  261. 0x01, /* bDataInterface: 1 */
  262. /*ACM Functional Descriptor*/
  263. 0x04, /* bFunctionLength */
  264. 0x24, /* bDescriptorType: CS_INTERFACE */
  265. 0x02, /* bDescriptorSubtype: Abstract Control Management desc */
  266. 0x02, /* bmCapabilities */
  267. /*Union Functional Descriptor*/
  268. 0x05, /* bFunctionLength */
  269. 0x24, /* bDescriptorType: CS_INTERFACE */
  270. 0x06, /* bDescriptorSubtype: Union func desc */
  271. 0x00, /* bMasterInterface: Communication class interface */
  272. 0x01, /* bSlaveInterface0: Data Class Interface */
  273. /*Endpoint 2 Descriptor*/
  274. 0x07, /* bLength: Endpoint Descriptor size */
  275. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  276. CDC_CMD_EP, /* bEndpointAddress */
  277. 0x03, /* bmAttributes: Interrupt */
  278. LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
  279. HIBYTE(CDC_CMD_PACKET_SIZE),
  280. CDC_FS_BINTERVAL, /* bInterval: */
  281. /*---------------------------------------------------------------------------*/
  282. /*Data class interface descriptor*/
  283. 0x09, /* bLength: Endpoint Descriptor size */
  284. USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
  285. 0x01, /* bInterfaceNumber: Number of Interface */
  286. 0x00, /* bAlternateSetting: Alternate setting */
  287. 0x02, /* bNumEndpoints: Two endpoints used */
  288. 0x0A, /* bInterfaceClass: CDC */
  289. 0x00, /* bInterfaceSubClass: */
  290. 0x00, /* bInterfaceProtocol: */
  291. 0x00, /* iInterface: */
  292. /*Endpoint OUT Descriptor*/
  293. 0x07, /* bLength: Endpoint Descriptor size */
  294. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  295. CDC_OUT_EP, /* bEndpointAddress */
  296. 0x02, /* bmAttributes: Bulk */
  297. LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
  298. HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
  299. 0x00, /* bInterval: ignore for Bulk transfer */
  300. /*Endpoint IN Descriptor*/
  301. 0x07, /* bLength: Endpoint Descriptor size */
  302. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  303. CDC_IN_EP, /* bEndpointAddress */
  304. 0x02, /* bmAttributes: Bulk */
  305. LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
  306. HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
  307. 0x00 /* bInterval: ignore for Bulk transfer */
  308. } ;
  309. __ALIGN_BEGIN uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
  310. {
  311. 0x09, /* bLength: Configuation Descriptor size */
  312. USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION,
  313. USB_CDC_CONFIG_DESC_SIZ,
  314. 0x00,
  315. 0x02, /* bNumInterfaces: 2 interfaces */
  316. 0x01, /* bConfigurationValue: */
  317. 0x04, /* iConfiguration: */
  318. 0xC0, /* bmAttributes: */
  319. 0x32, /* MaxPower 100 mA */
  320. /*Interface Descriptor */
  321. 0x09, /* bLength: Interface Descriptor size */
  322. USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
  323. /* Interface descriptor type */
  324. 0x00, /* bInterfaceNumber: Number of Interface */
  325. 0x00, /* bAlternateSetting: Alternate setting */
  326. 0x01, /* bNumEndpoints: One endpoints used */
  327. 0x02, /* bInterfaceClass: Communication Interface Class */
  328. 0x02, /* bInterfaceSubClass: Abstract Control Model */
  329. 0x01, /* bInterfaceProtocol: Common AT commands */
  330. 0x00, /* iInterface: */
  331. /*Header Functional Descriptor*/
  332. 0x05, /* bLength: Endpoint Descriptor size */
  333. 0x24, /* bDescriptorType: CS_INTERFACE */
  334. 0x00, /* bDescriptorSubtype: Header Func Desc */
  335. 0x10, /* bcdCDC: spec release number */
  336. 0x01,
  337. /*Call Management Functional Descriptor*/
  338. 0x05, /* bFunctionLength */
  339. 0x24, /* bDescriptorType: CS_INTERFACE */
  340. 0x01, /* bDescriptorSubtype: Call Management Func Desc */
  341. 0x00, /* bmCapabilities: D0+D1 */
  342. 0x01, /* bDataInterface: 1 */
  343. /*ACM Functional Descriptor*/
  344. 0x04, /* bFunctionLength */
  345. 0x24, /* bDescriptorType: CS_INTERFACE */
  346. 0x02, /* bDescriptorSubtype: Abstract Control Management desc */
  347. 0x02, /* bmCapabilities */
  348. /*Union Functional Descriptor*/
  349. 0x05, /* bFunctionLength */
  350. 0x24, /* bDescriptorType: CS_INTERFACE */
  351. 0x06, /* bDescriptorSubtype: Union func desc */
  352. 0x00, /* bMasterInterface: Communication class interface */
  353. 0x01, /* bSlaveInterface0: Data Class Interface */
  354. /*Endpoint 2 Descriptor*/
  355. 0x07, /* bLength: Endpoint Descriptor size */
  356. USB_DESC_TYPE_ENDPOINT , /* bDescriptorType: Endpoint */
  357. CDC_CMD_EP, /* bEndpointAddress */
  358. 0x03, /* bmAttributes: Interrupt */
  359. LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
  360. HIBYTE(CDC_CMD_PACKET_SIZE),
  361. CDC_FS_BINTERVAL, /* bInterval: */
  362. /*---------------------------------------------------------------------------*/
  363. /*Data class interface descriptor*/
  364. 0x09, /* bLength: Endpoint Descriptor size */
  365. USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
  366. 0x01, /* bInterfaceNumber: Number of Interface */
  367. 0x00, /* bAlternateSetting: Alternate setting */
  368. 0x02, /* bNumEndpoints: Two endpoints used */
  369. 0x0A, /* bInterfaceClass: CDC */
  370. 0x00, /* bInterfaceSubClass: */
  371. 0x00, /* bInterfaceProtocol: */
  372. 0x00, /* iInterface: */
  373. /*Endpoint OUT Descriptor*/
  374. 0x07, /* bLength: Endpoint Descriptor size */
  375. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  376. CDC_OUT_EP, /* bEndpointAddress */
  377. 0x02, /* bmAttributes: Bulk */
  378. 0x40, /* wMaxPacketSize: */
  379. 0x00,
  380. 0x00, /* bInterval: ignore for Bulk transfer */
  381. /*Endpoint IN Descriptor*/
  382. 0x07, /* bLength: Endpoint Descriptor size */
  383. USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
  384. CDC_IN_EP, /* bEndpointAddress */
  385. 0x02, /* bmAttributes: Bulk */
  386. 0x40, /* wMaxPacketSize: */
  387. 0x00,
  388. 0x00 /* bInterval */
  389. };
  390. /**
  391. * @}
  392. */
  393. /** @defgroup USBD_CDC_Private_Functions
  394. * @{
  395. */
  396. /**
  397. * @brief USBD_CDC_Init
  398. * Initialize the CDC interface
  399. * @param pdev: device instance
  400. * @param cfgidx: Configuration index
  401. * @retval status
  402. */
  403. static uint8_t USBD_CDC_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
  404. {
  405. uint8_t ret = 0U;
  406. USBD_CDC_HandleTypeDef *hcdc;
  407. if(pdev->dev_speed == USBD_SPEED_HIGH)
  408. {
  409. /* Open EP IN */
  410. USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK,
  411. CDC_DATA_HS_IN_PACKET_SIZE);
  412. pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U;
  413. /* Open EP OUT */
  414. USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK,
  415. CDC_DATA_HS_OUT_PACKET_SIZE);
  416. pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U;
  417. }
  418. else
  419. {
  420. /* Open EP IN */
  421. USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK,
  422. CDC_DATA_FS_IN_PACKET_SIZE);
  423. pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U;
  424. /* Open EP OUT */
  425. USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK,
  426. CDC_DATA_FS_OUT_PACKET_SIZE);
  427. pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U;
  428. }
  429. /* Open Command IN EP */
  430. USBD_LL_OpenEP(pdev, CDC_CMD_EP, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE);
  431. pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U;
  432. pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));
  433. if(pdev->pClassData == NULL)
  434. {
  435. ret = 1U;
  436. }
  437. else
  438. {
  439. hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  440. /* Init physical Interface components */
  441. ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();
  442. /* Init Xfer states */
  443. hcdc->TxState = 0U;
  444. hcdc->RxState = 0U;
  445. if(pdev->dev_speed == USBD_SPEED_HIGH)
  446. {
  447. /* Prepare Out endpoint to receive next packet */
  448. USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
  449. CDC_DATA_HS_OUT_PACKET_SIZE);
  450. }
  451. else
  452. {
  453. /* Prepare Out endpoint to receive next packet */
  454. USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
  455. CDC_DATA_FS_OUT_PACKET_SIZE);
  456. }
  457. }
  458. return ret;
  459. }
  460. /**
  461. * @brief USBD_CDC_Init
  462. * DeInitialize the CDC layer
  463. * @param pdev: device instance
  464. * @param cfgidx: Configuration index
  465. * @retval status
  466. */
  467. static uint8_t USBD_CDC_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
  468. {
  469. uint8_t ret = 0U;
  470. /* Close EP IN */
  471. USBD_LL_CloseEP(pdev, CDC_IN_EP);
  472. pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 0U;
  473. /* Close EP OUT */
  474. USBD_LL_CloseEP(pdev, CDC_OUT_EP);
  475. pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 0U;
  476. /* Close Command IN EP */
  477. USBD_LL_CloseEP(pdev, CDC_CMD_EP);
  478. pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 0U;
  479. /* DeInit physical Interface components */
  480. if(pdev->pClassData != NULL)
  481. {
  482. ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit();
  483. USBD_free(pdev->pClassData);
  484. pdev->pClassData = NULL;
  485. }
  486. return ret;
  487. }
  488. /**
  489. * @brief USBD_CDC_Setup
  490. * Handle the CDC specific requests
  491. * @param pdev: instance
  492. * @param req: usb requests
  493. * @retval status
  494. */
  495. static uint8_t USBD_CDC_Setup (USBD_HandleTypeDef *pdev,
  496. USBD_SetupReqTypedef *req)
  497. {
  498. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  499. uint8_t ifalt = 0U;
  500. uint16_t status_info = 0U;
  501. uint8_t ret = USBD_OK;
  502. switch (req->bmRequest & USB_REQ_TYPE_MASK)
  503. {
  504. case USB_REQ_TYPE_CLASS :
  505. if (req->wLength)
  506. {
  507. if (req->bmRequest & 0x80U)
  508. {
  509. ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest,
  510. (uint8_t *)(void *)hcdc->data,
  511. req->wLength);
  512. USBD_CtlSendData (pdev, (uint8_t *)(void *)hcdc->data, req->wLength);
  513. }
  514. else
  515. {
  516. hcdc->CmdOpCode = req->bRequest;
  517. hcdc->CmdLength = (uint8_t)req->wLength;
  518. USBD_CtlPrepareRx (pdev, (uint8_t *)(void *)hcdc->data, req->wLength);
  519. }
  520. }
  521. else
  522. {
  523. ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest,
  524. (uint8_t *)(void *)req, 0U);
  525. }
  526. break;
  527. case USB_REQ_TYPE_STANDARD:
  528. switch (req->bRequest)
  529. {
  530. case USB_REQ_GET_STATUS:
  531. if (pdev->dev_state == USBD_STATE_CONFIGURED)
  532. {
  533. USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U);
  534. }
  535. else
  536. {
  537. USBD_CtlError (pdev, req);
  538. ret = USBD_FAIL;
  539. }
  540. break;
  541. case USB_REQ_GET_INTERFACE:
  542. if (pdev->dev_state == USBD_STATE_CONFIGURED)
  543. {
  544. USBD_CtlSendData (pdev, &ifalt, 1U);
  545. }
  546. else
  547. {
  548. USBD_CtlError (pdev, req);
  549. ret = USBD_FAIL;
  550. }
  551. break;
  552. case USB_REQ_SET_INTERFACE:
  553. if (pdev->dev_state != USBD_STATE_CONFIGURED)
  554. {
  555. USBD_CtlError (pdev, req);
  556. ret = USBD_FAIL;
  557. }
  558. break;
  559. default:
  560. USBD_CtlError (pdev, req);
  561. ret = USBD_FAIL;
  562. break;
  563. }
  564. break;
  565. default:
  566. USBD_CtlError (pdev, req);
  567. ret = USBD_FAIL;
  568. break;
  569. }
  570. return ret;
  571. }
  572. /**
  573. * @brief USBD_CDC_DataIn
  574. * Data sent on non-control IN endpoint
  575. * @param pdev: device instance
  576. * @param epnum: endpoint number
  577. * @retval status
  578. */
  579. static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
  580. {
  581. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)pdev->pClassData;
  582. PCD_HandleTypeDef *hpcd = pdev->pData;
  583. if(pdev->pClassData != NULL)
  584. {
  585. if((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U))
  586. {
  587. /* Update the packet total length */
  588. pdev->ep_in[epnum].total_length = 0U;
  589. /* Send ZLP */
  590. USBD_LL_Transmit (pdev, epnum, NULL, 0U);
  591. }
  592. else
  593. {
  594. hcdc->TxState = 0U;
  595. }
  596. return USBD_OK;
  597. }
  598. else
  599. {
  600. return USBD_FAIL;
  601. }
  602. }
  603. /**
  604. * @brief USBD_CDC_DataOut
  605. * Data received on non-control Out endpoint
  606. * @param pdev: device instance
  607. * @param epnum: endpoint number
  608. * @retval status
  609. */
  610. static uint8_t USBD_CDC_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum)
  611. {
  612. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  613. /* Get the received data length */
  614. hcdc->RxLength = USBD_LL_GetRxDataSize (pdev, epnum);
  615. /* USB data will be immediately processed, this allow next USB traffic being
  616. NAKed till the end of the application Xfer */
  617. if(pdev->pClassData != NULL)
  618. {
  619. ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Receive(hcdc->RxBuffer, &hcdc->RxLength);
  620. return USBD_OK;
  621. }
  622. else
  623. {
  624. return USBD_FAIL;
  625. }
  626. }
  627. /**
  628. * @brief USBD_CDC_EP0_RxReady
  629. * Handle EP0 Rx Ready event
  630. * @param pdev: device instance
  631. * @retval status
  632. */
  633. static uint8_t USBD_CDC_EP0_RxReady (USBD_HandleTypeDef *pdev)
  634. {
  635. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  636. if((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU))
  637. {
  638. ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(hcdc->CmdOpCode,
  639. (uint8_t *)(void *)hcdc->data,
  640. (uint16_t)hcdc->CmdLength);
  641. hcdc->CmdOpCode = 0xFFU;
  642. }
  643. return USBD_OK;
  644. }
  645. /**
  646. * @brief USBD_CDC_GetFSCfgDesc
  647. * Return configuration descriptor
  648. * @param speed : current device speed
  649. * @param length : pointer data length
  650. * @retval pointer to descriptor buffer
  651. */
  652. static uint8_t *USBD_CDC_GetFSCfgDesc (uint16_t *length)
  653. {
  654. *length = sizeof (USBD_CDC_CfgFSDesc);
  655. return USBD_CDC_CfgFSDesc;
  656. }
  657. /**
  658. * @brief USBD_CDC_GetHSCfgDesc
  659. * Return configuration descriptor
  660. * @param speed : current device speed
  661. * @param length : pointer data length
  662. * @retval pointer to descriptor buffer
  663. */
  664. static uint8_t *USBD_CDC_GetHSCfgDesc (uint16_t *length)
  665. {
  666. *length = sizeof (USBD_CDC_CfgHSDesc);
  667. return USBD_CDC_CfgHSDesc;
  668. }
  669. /**
  670. * @brief USBD_CDC_GetCfgDesc
  671. * Return configuration descriptor
  672. * @param speed : current device speed
  673. * @param length : pointer data length
  674. * @retval pointer to descriptor buffer
  675. */
  676. static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length)
  677. {
  678. *length = sizeof (USBD_CDC_OtherSpeedCfgDesc);
  679. return USBD_CDC_OtherSpeedCfgDesc;
  680. }
  681. /**
  682. * @brief DeviceQualifierDescriptor
  683. * return Device Qualifier descriptor
  684. * @param length : pointer data length
  685. * @retval pointer to descriptor buffer
  686. */
  687. uint8_t *USBD_CDC_GetDeviceQualifierDescriptor (uint16_t *length)
  688. {
  689. *length = sizeof (USBD_CDC_DeviceQualifierDesc);
  690. return USBD_CDC_DeviceQualifierDesc;
  691. }
  692. /**
  693. * @brief USBD_CDC_RegisterInterface
  694. * @param pdev: device instance
  695. * @param fops: CD Interface callback
  696. * @retval status
  697. */
  698. uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev,
  699. USBD_CDC_ItfTypeDef *fops)
  700. {
  701. uint8_t ret = USBD_FAIL;
  702. if(fops != NULL)
  703. {
  704. pdev->pUserData= fops;
  705. ret = USBD_OK;
  706. }
  707. return ret;
  708. }
  709. /**
  710. * @brief USBD_CDC_SetTxBuffer
  711. * @param pdev: device instance
  712. * @param pbuff: Tx Buffer
  713. * @retval status
  714. */
  715. uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev,
  716. uint8_t *pbuff,
  717. uint16_t length)
  718. {
  719. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  720. hcdc->TxBuffer = pbuff;
  721. hcdc->TxLength = length;
  722. return USBD_OK;
  723. }
  724. /**
  725. * @brief USBD_CDC_SetRxBuffer
  726. * @param pdev: device instance
  727. * @param pbuff: Rx Buffer
  728. * @retval status
  729. */
  730. uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef *pdev,
  731. uint8_t *pbuff)
  732. {
  733. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  734. hcdc->RxBuffer = pbuff;
  735. return USBD_OK;
  736. }
  737. /**
  738. * @brief USBD_CDC_TransmitPacket
  739. * Transmit packet on IN endpoint
  740. * @param pdev: device instance
  741. * @retval status
  742. */
  743. uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
  744. {
  745. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  746. if(pdev->pClassData != NULL)
  747. {
  748. if(hcdc->TxState == 0U)
  749. {
  750. /* Tx Transfer in progress */
  751. hcdc->TxState = 1U;
  752. /* Update the packet total length */
  753. pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength;
  754. /* Transmit next packet */
  755. USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer,
  756. (uint16_t)hcdc->TxLength);
  757. return USBD_OK;
  758. }
  759. else
  760. {
  761. return USBD_BUSY;
  762. }
  763. }
  764. else
  765. {
  766. return USBD_FAIL;
  767. }
  768. }
  769. /**
  770. * @brief USBD_CDC_ReceivePacket
  771. * prepare OUT Endpoint for reception
  772. * @param pdev: device instance
  773. * @retval status
  774. */
  775. uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
  776. {
  777. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
  778. /* Suspend or Resume USB Out process */
  779. if(pdev->pClassData != NULL)
  780. {
  781. if(pdev->dev_speed == USBD_SPEED_HIGH )
  782. {
  783. /* Prepare Out endpoint to receive next packet */
  784. USBD_LL_PrepareReceive(pdev,
  785. CDC_OUT_EP,
  786. hcdc->RxBuffer,
  787. CDC_DATA_HS_OUT_PACKET_SIZE);
  788. }
  789. else
  790. {
  791. /* Prepare Out endpoint to receive next packet */
  792. USBD_LL_PrepareReceive(pdev,
  793. CDC_OUT_EP,
  794. hcdc->RxBuffer,
  795. CDC_DATA_FS_OUT_PACKET_SIZE);
  796. }
  797. return USBD_OK;
  798. }
  799. else
  800. {
  801. return USBD_FAIL;
  802. }
  803. }
  804. /**
  805. * @}
  806. */
  807. /**
  808. * @}
  809. */
  810. /**
  811. * @}
  812. */
  813. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/