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