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