Details | 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) 2020 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 | #include "libSerial/serial.h" |
||
| 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 | /* USER CODE END PRIVATE_DEFINES */ |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @} |
||
| 71 | */ |
||
| 72 | |||
| 73 | /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros |
||
| 74 | * @brief Private macros. |
||
| 75 | * @{ |
||
| 76 | */ |
||
| 77 | |||
| 78 | /* USER CODE BEGIN PRIVATE_MACRO */ |
||
| 79 | |||
| 80 | /* USER CODE END PRIVATE_MACRO */ |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @} |
||
| 84 | */ |
||
| 85 | |||
| 86 | /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables |
||
| 87 | * @brief Private variables. |
||
| 88 | * @{ |
||
| 89 | */ |
||
| 90 | /* Create buffer for reception and transmission */ |
||
| 91 | /* It's up to user to redefine and/or remove those define */ |
||
| 92 | /** Received data over USB are stored in this buffer */ |
||
| 93 | uint8_t UserRxBufferFS[APP_RX_DATA_SIZE]; |
||
| 94 | |||
| 95 | /** Data to send over USB CDC are stored in this buffer */ |
||
| 96 | uint8_t UserTxBufferFS[APP_TX_DATA_SIZE]; |
||
| 97 | |||
| 98 | /* USER CODE BEGIN PRIVATE_VARIABLES */ |
||
| 99 | |||
| 100 | /* USER CODE END PRIVATE_VARIABLES */ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @} |
||
| 104 | */ |
||
| 105 | |||
| 106 | /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables |
||
| 107 | * @brief Public variables. |
||
| 108 | * @{ |
||
| 109 | */ |
||
| 110 | |||
| 111 | extern USBD_HandleTypeDef hUsbDeviceFS; |
||
| 112 | |||
| 113 | /* USER CODE BEGIN EXPORTED_VARIABLES */ |
||
| 114 | |||
| 115 | /* USER CODE END EXPORTED_VARIABLES */ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @} |
||
| 119 | */ |
||
| 120 | |||
| 121 | /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes |
||
| 122 | * @brief Private functions declaration. |
||
| 123 | * @{ |
||
| 124 | */ |
||
| 125 | |||
| 126 | static int8_t CDC_Init_FS(void); |
||
| 127 | static int8_t CDC_DeInit_FS(void); |
||
| 128 | static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length); |
||
| 129 | static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len); |
||
| 130 | |||
| 131 | /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */ |
||
| 132 | |||
| 133 | /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @} |
||
| 137 | */ |
||
| 138 | |||
| 139 | USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = |
||
| 140 | { |
||
| 141 | CDC_Init_FS, |
||
| 142 | CDC_DeInit_FS, |
||
| 143 | CDC_Control_FS, |
||
| 144 | CDC_Receive_FS |
||
| 145 | }; |
||
| 146 | |||
| 147 | /* Private functions ---------------------------------------------------------*/ |
||
| 148 | /** |
||
| 149 | * @brief Initializes the CDC media low layer over the FS USB IP |
||
| 150 | * @retval USBD_OK if all operations are OK else USBD_FAIL |
||
| 151 | */ |
||
| 152 | static int8_t CDC_Init_FS(void) |
||
| 153 | { |
||
| 154 | /* USER CODE BEGIN 3 */ |
||
| 155 | /* Set Application Buffers */ |
||
| 156 | USBD_CDC_SetTxBuffer (&hUsbDeviceFS, UserTxBufferFS, 0); |
||
| 157 | USBD_CDC_SetRxBuffer (&hUsbDeviceFS, UserRxBufferFS); |
||
| 158 | return (USBD_OK); |
||
| 159 | /* USER CODE END 3 */ |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @brief DeInitializes the CDC media low layer |
||
| 164 | * @retval USBD_OK if all operations are OK else USBD_FAIL |
||
| 165 | */ |
||
| 166 | static int8_t CDC_DeInit_FS(void) |
||
| 167 | { |
||
| 168 | /* USER CODE BEGIN 4 */ |
||
| 169 | return (USBD_OK); |
||
| 170 | /* USER CODE END 4 */ |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @brief Manage the CDC class requests |
||
| 175 | * @param cmd: Command code |
||
| 176 | * @param pbuf: Buffer containing command data (request parameters) |
||
| 177 | * @param length: Number of data to be sent (in bytes) |
||
| 178 | * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL |
||
| 179 | */ |
||
| 180 | static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) |
||
| 181 | { |
||
| 182 | /* USER CODE BEGIN 5 */ |
||
| 183 | switch (cmd) |
||
| 184 | { |
||
| 185 | case CDC_SEND_ENCAPSULATED_COMMAND: |
||
| 186 | |||
| 187 | break; |
||
| 188 | |||
| 189 | case CDC_GET_ENCAPSULATED_RESPONSE: |
||
| 190 | |||
| 191 | break; |
||
| 192 | |||
| 193 | case CDC_SET_COMM_FEATURE: |
||
| 194 | |||
| 195 | break; |
||
| 196 | |||
| 197 | case CDC_GET_COMM_FEATURE: |
||
| 198 | |||
| 199 | break; |
||
| 200 | |||
| 201 | case CDC_CLEAR_COMM_FEATURE: |
||
| 202 | |||
| 203 | break; |
||
| 204 | |||
| 205 | /*******************************************************************************/ |
||
| 206 | /* Line Coding Structure */ |
||
| 207 | /*-----------------------------------------------------------------------------*/ |
||
| 208 | /* Offset | Field | Size | Value | Description */ |
||
| 209 | /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ |
||
| 210 | /* 4 | bCharFormat | 1 | Number | Stop bits */ |
||
| 211 | /* 0 - 1 Stop bit */ |
||
| 212 | /* 1 - 1.5 Stop bits */ |
||
| 213 | /* 2 - 2 Stop bits */ |
||
| 214 | /* 5 | bParityType | 1 | Number | Parity */ |
||
| 215 | /* 0 - None */ |
||
| 216 | /* 1 - Odd */ |
||
| 217 | /* 2 - Even */ |
||
| 218 | /* 3 - Mark */ |
||
| 219 | /* 4 - Space */ |
||
| 220 | /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ |
||
| 221 | /*******************************************************************************/ |
||
| 222 | case CDC_SET_LINE_CODING: |
||
| 223 | { |
||
| 224 | uint32_t rate = *((uint32_t*) pbuf); |
||
| 225 | if (rate > 0) |
||
| 226 | { |
||
| 227 | huart1.Init.BaudRate = rate; |
||
| 228 | __disable_irq (); |
||
| 229 | HAL_UART_Init (&huart1); |
||
| 230 | __enable_irq (); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | break; |
||
| 234 | |||
| 235 | case CDC_GET_LINE_CODING: |
||
| 236 | |||
| 237 | break; |
||
| 238 | |||
| 239 | case CDC_SET_CONTROL_LINE_STATE: |
||
| 240 | { |
||
| 241 | uint8_t control_state = *((uint8_t*) pbuf); |
||
| 242 | |||
| 243 | // DTR is control_state[0] |
||
| 244 | HAL_GPIO_WritePin (USART1_TXEN_GPIO_Port, USART1_TXEN_Pin, |
||
| 245 | control_state & 1 ? GPIO_PIN_RESET : GPIO_PIN_SET); |
||
| 246 | // |
||
| 247 | |||
| 248 | // RTS is control_state[1] |
||
| 249 | |||
| 250 | } |
||
| 251 | return (USBD_OK); |
||
| 252 | break; |
||
| 253 | |||
| 254 | case CDC_SEND_BREAK: |
||
| 255 | |||
| 256 | break; |
||
| 257 | |||
| 258 | default: |
||
| 259 | break; |
||
| 260 | } |
||
| 261 | |||
| 262 | return (USBD_OK); |
||
| 263 | /* USER CODE END 5 */ |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @brief Data received over USB OUT endpoint are sent over CDC interface |
||
| 268 | * through this function. |
||
| 269 | * |
||
| 270 | * @note |
||
| 271 | * This function will issue a NAK packet on any OUT packet received on |
||
| 272 | * USB endpoint until exiting this function. If you exit this function |
||
| 273 | * before transfer is complete on CDC interface (ie. using DMA controller) |
||
| 274 | * it will result in receiving more data while previous ones are still |
||
| 275 | * not sent. |
||
| 276 | * |
||
| 277 | * @param Buf: Buffer of data to be received |
||
| 278 | * @param Len: Number of data received (in bytes) |
||
| 279 | * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL |
||
| 280 | */ |
||
| 281 | static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) |
||
| 282 | { |
||
| 283 | /* USER CODE BEGIN 6 */ |
||
| 284 | usart_ctl *instance = &uc1; |
||
| 285 | int i = 0; |
||
| 286 | |||
| 287 | while (i < *Len) |
||
| 288 | PutCharSerial (instance, Buf[i++]); |
||
| 289 | |||
| 290 | USBD_CDC_SetRxBuffer (&hUsbDeviceFS, &Buf[0]); |
||
| 291 | USBD_CDC_ReceivePacket (&hUsbDeviceFS); |
||
| 292 | return (USBD_OK); |
||
| 293 | /* USER CODE END 6 */ |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @brief CDC_Transmit_FS |
||
| 298 | * Data to send over USB IN endpoint are sent over CDC interface |
||
| 299 | * through this function. |
||
| 300 | * @note |
||
| 301 | * |
||
| 302 | * |
||
| 303 | * @param Buf: Buffer of data to be sent |
||
| 304 | * @param Len: Number of data to be sent (in bytes) |
||
| 305 | * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY |
||
| 306 | */ |
||
| 307 | uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) |
||
| 308 | { |
||
| 309 | uint8_t result = USBD_OK; |
||
| 310 | /* USER CODE BEGIN 7 */ |
||
| 311 | USBD_CDC_HandleTypeDef *hcdc = |
||
| 312 | (USBD_CDC_HandleTypeDef*) hUsbDeviceFS.pClassData; |
||
| 313 | if (!hcdc || hcdc->TxState != 0) |
||
| 314 | { |
||
| 315 | return USBD_BUSY; |
||
| 316 | } |
||
| 317 | USBD_CDC_SetTxBuffer (&hUsbDeviceFS, Buf, Len); |
||
| 318 | result = USBD_CDC_TransmitPacket (&hUsbDeviceFS); |
||
| 319 | /* USER CODE END 7 */ |
||
| 320 | return result; |
||
| 321 | } |
||
| 322 | |||
| 323 | /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ |
||
| 324 | |||
| 325 | int16_t |
||
| 326 | CDC_Poll_UART () |
||
| 327 | { |
||
| 328 | usart_ctl *instance = &uc1; |
||
| 329 | int16_t chars = SerialCharsReceived (instance); |
||
| 330 | /* flag receiving data */ |
||
| 331 | |||
| 332 | int16_t i; |
||
| 333 | if (chars > APP_TX_DATA_SIZE) |
||
| 334 | chars = APP_TX_DATA_SIZE; |
||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | for (i = 0; i < chars; i++) |
||
| 339 | { |
||
| 340 | uint8_t c; |
||
| 341 | c = GetCharSerial (instance); |
||
| 342 | UserTxBufferFS[i] = c; |
||
| 343 | } |
||
| 344 | if (chars) |
||
| 345 | CDC_Transmit_FS (UserTxBufferFS, chars); |
||
| 346 | |||
| 347 | return chars; |
||
| 348 | } |
||
| 349 | |||
| 350 | /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @} |
||
| 354 | */ |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @} |
||
| 358 | */ |
||
| 359 | |||
| 360 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |