| 1 |
/** |
| 2 |
****************************************************************************** |
| 3 |
* @file usbh_cdc.c |
| 4 |
* @author MCD Application Team |
| 5 |
* @version V3.0.0 |
| 6 |
* @date 18-February-2014 |
| 7 |
* @brief This file is the CDC Layer Handlers for USB Host CDC class. |
| 8 |
* |
| 9 |
* @verbatim |
| 10 |
* |
| 11 |
* =================================================================== |
| 12 |
* CDC Class Description |
| 13 |
* =================================================================== |
| 14 |
* This module manages the MSC class V1.11 following the "Device Class Definition |
| 15 |
* for Human Interface Devices (CDC) Version 1.11 Jun 27, 2001". |
| 16 |
* This driver implements the following aspects of the specification: |
| 17 |
* - The Boot Interface Subclass |
| 18 |
* - The Mouse and Keyboard protocols |
| 19 |
* |
| 20 |
* @endverbatim |
| 21 |
* |
| 22 |
****************************************************************************** |
| 23 |
* @attention |
| 24 |
* |
| 25 |
* <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> |
| 26 |
* |
| 27 |
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); |
| 28 |
* You may not use this file except in compliance with the License. |
| 29 |
* You may obtain a copy of the License at: |
| 30 |
* |
| 31 |
* http://www.st.com/software_license_agreement_liberty_v2 |
| 32 |
* |
| 33 |
* Unless required by applicable law or agreed to in writing, software |
| 34 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 35 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 36 |
* See the License for the specific language governing permissions and |
| 37 |
* limitations under the License. |
| 38 |
* |
| 39 |
****************************************************************************** |
| 40 |
*/ |
| 41 |
|
| 42 |
/* Includes ------------------------------------------------------------------*/ |
| 43 |
#include "usbh_cdc.h" |
| 44 |
|
| 45 |
/** @addtogroup USBH_LIB |
| 46 |
* @{ |
| 47 |
*/ |
| 48 |
|
| 49 |
/** @addtogroup USBH_CLASS |
| 50 |
* @{ |
| 51 |
*/ |
| 52 |
|
| 53 |
/** @addtogroup USBH_CDC_CLASS |
| 54 |
* @{ |
| 55 |
*/ |
| 56 |
|
| 57 |
/** @defgroup USBH_CDC_CORE |
| 58 |
* @brief This file includes CDC Layer Handlers for USB Host CDC class. |
| 59 |
* @{ |
| 60 |
*/ |
| 61 |
|
| 62 |
/** @defgroup USBH_CDC_CORE_Private_TypesDefinitions |
| 63 |
* @{ |
| 64 |
*/ |
| 65 |
/** |
| 66 |
* @} |
| 67 |
*/ |
| 68 |
|
| 69 |
|
| 70 |
/** @defgroup USBH_CDC_CORE_Private_Defines |
| 71 |
* @{ |
| 72 |
*/ |
| 73 |
#define USBH_CDC_BUFFER_SIZE 1024 |
| 74 |
/** |
| 75 |
* @} |
| 76 |
*/ |
| 77 |
|
| 78 |
|
| 79 |
/** @defgroup USBH_CDC_CORE_Private_Macros |
| 80 |
* @{ |
| 81 |
*/ |
| 82 |
/** |
| 83 |
* @} |
| 84 |
*/ |
| 85 |
|
| 86 |
|
| 87 |
/** @defgroup USBH_CDC_CORE_Private_Variables |
| 88 |
* @{ |
| 89 |
*/ |
| 90 |
/** |
| 91 |
* @} |
| 92 |
*/ |
| 93 |
|
| 94 |
|
| 95 |
/** @defgroup USBH_CDC_CORE_Private_FunctionPrototypes |
| 96 |
* @{ |
| 97 |
*/ |
| 98 |
|
| 99 |
static USBH_StatusTypeDef USBH_CDC_InterfaceInit (USBH_HandleTypeDef *phost); |
| 100 |
|
| 101 |
static USBH_StatusTypeDef USBH_CDC_InterfaceDeInit (USBH_HandleTypeDef *phost); |
| 102 |
|
| 103 |
static USBH_StatusTypeDef USBH_CDC_Process(USBH_HandleTypeDef *phost); |
| 104 |
|
| 105 |
static USBH_StatusTypeDef USBH_CDC_SOFProcess(USBH_HandleTypeDef *phost); |
| 106 |
|
| 107 |
static USBH_StatusTypeDef USBH_CDC_ClassRequest (USBH_HandleTypeDef *phost); |
| 108 |
|
| 109 |
static USBH_StatusTypeDef GetLineCoding(USBH_HandleTypeDef *phost, |
| 110 |
CDC_LineCodingTypeDef *linecoding); |
| 111 |
|
| 112 |
static USBH_StatusTypeDef SetLineCoding(USBH_HandleTypeDef *phost, |
| 113 |
CDC_LineCodingTypeDef *linecoding); |
| 114 |
|
| 115 |
static void CDC_ProcessTransmission(USBH_HandleTypeDef *phost); |
| 116 |
|
| 117 |
static void CDC_ProcessReception(USBH_HandleTypeDef *phost); |
| 118 |
|
| 119 |
USBH_ClassTypeDef CDC_Class = |
| 120 |
{ |
| 121 |
"CDC", |
| 122 |
USB_CDC_CLASS, |
| 123 |
USBH_CDC_InterfaceInit, |
| 124 |
USBH_CDC_InterfaceDeInit, |
| 125 |
USBH_CDC_ClassRequest, |
| 126 |
USBH_CDC_Process, |
| 127 |
USBH_CDC_SOFProcess, |
| 128 |
NULL, |
| 129 |
}; |
| 130 |
/** |
| 131 |
* @} |
| 132 |
*/ |
| 133 |
|
| 134 |
|
| 135 |
/** @defgroup USBH_CDC_CORE_Private_Functions |
| 136 |
* @{ |
| 137 |
*/ |
| 138 |
|
| 139 |
/** |
| 140 |
* @brief USBH_CDC_InterfaceInit |
| 141 |
* The function init the CDC class. |
| 142 |
* @param phost: Host handle |
| 143 |
* @retval USBH Status |
| 144 |
*/ |
| 145 |
static USBH_StatusTypeDef USBH_CDC_InterfaceInit (USBH_HandleTypeDef *phost) |
| 146 |
{ |
| 147 |
|
| 148 |
USBH_StatusTypeDef status = USBH_FAIL ; |
| 149 |
uint8_t interface; |
| 150 |
CDC_HandleTypeDef *CDC_Handle; |
| 151 |
|
| 152 |
interface = USBH_FindInterface(phost, |
| 153 |
COMMUNICATION_INTERFACE_CLASS_CODE, |
| 154 |
ABSTRACT_CONTROL_MODEL, |
| 155 |
COMMON_AT_COMMAND); |
| 156 |
|
| 157 |
if(interface == 0xFF) /* No Valid Interface */ |
| 158 |
{ |
| 159 |
USBH_DbgLog ("Cannot Find the interface for Communication Interface Class.", phost->pActiveClass->Name); |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
USBH_SelectInterface (phost, interface); |
| 164 |
phost->pActiveClass->pData = (CDC_HandleTypeDef *)USBH_malloc (sizeof(CDC_HandleTypeDef)); |
| 165 |
CDC_Handle = phost->pActiveClass->pData; |
| 166 |
|
| 167 |
/*Collect the notification endpoint address and length*/ |
| 168 |
if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80) |
| 169 |
{ |
| 170 |
CDC_Handle->CommItf.NotifEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; |
| 171 |
CDC_Handle->CommItf.NotifEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; |
| 172 |
} |
| 173 |
|
| 174 |
/*Allocate the length for host channel number in*/ |
| 175 |
CDC_Handle->CommItf.NotifPipe = USBH_AllocPipe(phost, CDC_Handle->CommItf.NotifEp); |
| 176 |
|
| 177 |
/* Open pipe for Notification endpoint */ |
| 178 |
USBH_OpenPipe (phost, |
| 179 |
CDC_Handle->CommItf.NotifPipe, |
| 180 |
CDC_Handle->CommItf.NotifEp, |
| 181 |
phost->device.address, |
| 182 |
phost->device.speed, |
| 183 |
USB_EP_TYPE_INTR, |
| 184 |
CDC_Handle->CommItf.NotifEpSize); |
| 185 |
|
| 186 |
USBH_LL_SetToggle (phost, CDC_Handle->CommItf.NotifPipe, 0); |
| 187 |
|
| 188 |
interface = USBH_FindInterface(phost, |
| 189 |
DATA_INTERFACE_CLASS_CODE, |
| 190 |
RESERVED, |
| 191 |
NO_CLASS_SPECIFIC_PROTOCOL_CODE); |
| 192 |
|
| 193 |
if(interface == 0xFF) /* No Valid Interface */ |
| 194 |
{ |
| 195 |
USBH_DbgLog ("Cannot Find the interface for Data Interface Class.", phost->pActiveClass->Name); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
/*Collect the class specific endpoint address and length*/ |
| 200 |
if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80) |
| 201 |
{ |
| 202 |
CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; |
| 203 |
CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; |
| 208 |
CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; |
| 209 |
} |
| 210 |
|
| 211 |
if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress & 0x80) |
| 212 |
{ |
| 213 |
CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress; |
| 214 |
CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize; |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress; |
| 219 |
CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize; |
| 220 |
} |
| 221 |
|
| 222 |
/*Allocate the length for host channel number out*/ |
| 223 |
CDC_Handle->DataItf.OutPipe = USBH_AllocPipe(phost, CDC_Handle->DataItf.OutEp); |
| 224 |
|
| 225 |
/*Allocate the length for host channel number in*/ |
| 226 |
CDC_Handle->DataItf.InPipe = USBH_AllocPipe(phost, CDC_Handle->DataItf.InEp); |
| 227 |
|
| 228 |
/* Open channel for OUT endpoint */ |
| 229 |
USBH_OpenPipe (phost, |
| 230 |
CDC_Handle->DataItf.OutPipe, |
| 231 |
CDC_Handle->DataItf.OutEp, |
| 232 |
phost->device.address, |
| 233 |
phost->device.speed, |
| 234 |
USB_EP_TYPE_BULK, |
| 235 |
CDC_Handle->DataItf.OutEpSize); |
| 236 |
/* Open channel for IN endpoint */ |
| 237 |
USBH_OpenPipe (phost, |
| 238 |
CDC_Handle->DataItf.InPipe, |
| 239 |
CDC_Handle->DataItf.InEp, |
| 240 |
phost->device.address, |
| 241 |
phost->device.speed, |
| 242 |
USB_EP_TYPE_BULK, |
| 243 |
CDC_Handle->DataItf.InEpSize); |
| 244 |
|
| 245 |
CDC_Handle->state = CDC_IDLE_STATE; |
| 246 |
|
| 247 |
USBH_LL_SetToggle (phost, CDC_Handle->DataItf.OutPipe,0); |
| 248 |
USBH_LL_SetToggle (phost, CDC_Handle->DataItf.InPipe,0); |
| 249 |
status = USBH_OK; |
| 250 |
} |
| 251 |
} |
| 252 |
return status; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* @brief USBH_CDC_InterfaceDeInit |
| 259 |
* The function DeInit the Pipes used for the CDC class. |
| 260 |
* @param phost: Host handle |
| 261 |
* @retval USBH Status |
| 262 |
*/ |
| 263 |
USBH_StatusTypeDef USBH_CDC_InterfaceDeInit (USBH_HandleTypeDef *phost) |
| 264 |
{ |
| 265 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 266 |
|
| 267 |
if ( CDC_Handle->CommItf.NotifPipe) |
| 268 |
{ |
| 269 |
USBH_ClosePipe(phost, CDC_Handle->CommItf.NotifPipe); |
| 270 |
USBH_FreePipe (phost, CDC_Handle->CommItf.NotifPipe); |
| 271 |
CDC_Handle->CommItf.NotifPipe = 0; /* Reset the Channel as Free */ |
| 272 |
} |
| 273 |
|
| 274 |
if ( CDC_Handle->DataItf.InPipe) |
| 275 |
{ |
| 276 |
USBH_ClosePipe(phost, CDC_Handle->DataItf.InPipe); |
| 277 |
USBH_FreePipe (phost, CDC_Handle->DataItf.InPipe); |
| 278 |
CDC_Handle->DataItf.InPipe = 0; /* Reset the Channel as Free */ |
| 279 |
} |
| 280 |
|
| 281 |
if ( CDC_Handle->DataItf.OutPipe) |
| 282 |
{ |
| 283 |
USBH_ClosePipe(phost, CDC_Handle->DataItf.OutPipe); |
| 284 |
USBH_FreePipe (phost, CDC_Handle->DataItf.OutPipe); |
| 285 |
CDC_Handle->DataItf.OutPipe = 0; /* Reset the Channel as Free */ |
| 286 |
} |
| 287 |
|
| 288 |
if(phost->pActiveClass->pData) |
| 289 |
{ |
| 290 |
USBH_free (phost->pActiveClass->pData); |
| 291 |
phost->pActiveClass->pData = 0; |
| 292 |
} |
| 293 |
|
| 294 |
return USBH_OK; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* @brief USBH_CDC_ClassRequest |
| 299 |
* The function is responsible for handling Standard requests |
| 300 |
* for CDC class. |
| 301 |
* @param phost: Host handle |
| 302 |
* @retval USBH Status |
| 303 |
*/ |
| 304 |
static USBH_StatusTypeDef USBH_CDC_ClassRequest (USBH_HandleTypeDef *phost) |
| 305 |
{ |
| 306 |
USBH_StatusTypeDef status = USBH_FAIL ; |
| 307 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 308 |
|
| 309 |
/*Issue the get line coding request*/ |
| 310 |
status = GetLineCoding(phost, &CDC_Handle->LineCoding); |
| 311 |
if(status == USBH_OK) |
| 312 |
{ |
| 313 |
phost->pUser(phost, HOST_USER_CLASS_ACTIVE); |
| 314 |
} |
| 315 |
return status; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
/** |
| 320 |
* @brief USBH_CDC_Process |
| 321 |
* The function is for managing state machine for CDC data transfers |
| 322 |
* @param phost: Host handle |
| 323 |
* @retval USBH Status |
| 324 |
*/ |
| 325 |
static USBH_StatusTypeDef USBH_CDC_Process (USBH_HandleTypeDef *phost) |
| 326 |
{ |
| 327 |
USBH_StatusTypeDef status = USBH_BUSY; |
| 328 |
USBH_StatusTypeDef req_status = USBH_OK; |
| 329 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 330 |
|
| 331 |
switch(CDC_Handle->state) |
| 332 |
{ |
| 333 |
|
| 334 |
case CDC_IDLE_STATE: |
| 335 |
status = USBH_OK; |
| 336 |
break; |
| 337 |
|
| 338 |
case CDC_SET_LINE_CODING_STATE: |
| 339 |
req_status = SetLineCoding(phost, CDC_Handle->pUserLineCoding); |
| 340 |
|
| 341 |
if(req_status == USBH_OK) |
| 342 |
{ |
| 343 |
CDC_Handle->state = CDC_GET_LAST_LINE_CODING_STATE; |
| 344 |
} |
| 345 |
|
| 346 |
else if(req_status != USBH_BUSY) |
| 347 |
{ |
| 348 |
CDC_Handle->state = CDC_ERROR_STATE; |
| 349 |
} |
| 350 |
break; |
| 351 |
|
| 352 |
|
| 353 |
case CDC_GET_LAST_LINE_CODING_STATE: |
| 354 |
req_status = GetLineCoding(phost, &(CDC_Handle->LineCoding)); |
| 355 |
|
| 356 |
if(req_status == USBH_OK) |
| 357 |
{ |
| 358 |
CDC_Handle->state = CDC_IDLE_STATE; |
| 359 |
|
| 360 |
if((CDC_Handle->LineCoding.b.bCharFormat == CDC_Handle->pUserLineCoding->b.bCharFormat) && |
| 361 |
(CDC_Handle->LineCoding.b.bDataBits == CDC_Handle->pUserLineCoding->b.bDataBits) && |
| 362 |
(CDC_Handle->LineCoding.b.bParityType == CDC_Handle->pUserLineCoding->b.bParityType) && |
| 363 |
(CDC_Handle->LineCoding.b.dwDTERate == CDC_Handle->pUserLineCoding->b.dwDTERate)) |
| 364 |
{ |
| 365 |
USBH_CDC_LineCodingChanged(phost); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
else if(req_status != USBH_BUSY) |
| 370 |
{ |
| 371 |
CDC_Handle->state = CDC_ERROR_STATE; |
| 372 |
} |
| 373 |
|
| 374 |
break; |
| 375 |
|
| 376 |
case CDC_TRANSFER_DATA: |
| 377 |
CDC_ProcessTransmission(phost); |
| 378 |
CDC_ProcessReception(phost); |
| 379 |
break; |
| 380 |
|
| 381 |
case CDC_ERROR_STATE: |
| 382 |
req_status = USBH_ClrFeature(phost, 0x00); |
| 383 |
|
| 384 |
if(req_status == USBH_OK ) |
| 385 |
{ |
| 386 |
/*Change the state to waiting*/ |
| 387 |
CDC_Handle->state = CDC_IDLE_STATE ; |
| 388 |
} |
| 389 |
break; |
| 390 |
|
| 391 |
default: |
| 392 |
break; |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
return status; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* @brief USBH_CDC_SOFProcess |
| 401 |
* The function is for managing SOF callback |
| 402 |
* @param phost: Host handle |
| 403 |
* @retval USBH Status |
| 404 |
*/ |
| 405 |
static USBH_StatusTypeDef USBH_CDC_SOFProcess (USBH_HandleTypeDef *phost) |
| 406 |
{ |
| 407 |
return USBH_OK; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/** |
| 412 |
* @brief USBH_CDC_Stop |
| 413 |
* Stop current CDC Transmission |
| 414 |
* @param phost: Host handle |
| 415 |
* @retval USBH Status |
| 416 |
*/ |
| 417 |
USBH_StatusTypeDef USBH_CDC_Stop(USBH_HandleTypeDef *phost) |
| 418 |
{ |
| 419 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 420 |
|
| 421 |
if(phost->gState == HOST_CLASS) |
| 422 |
{ |
| 423 |
CDC_Handle->state = CDC_IDLE_STATE; |
| 424 |
|
| 425 |
USBH_ClosePipe(phost, CDC_Handle->CommItf.NotifPipe); |
| 426 |
USBH_ClosePipe(phost, CDC_Handle->DataItf.InPipe); |
| 427 |
USBH_ClosePipe(phost, CDC_Handle->DataItf.OutPipe); |
| 428 |
} |
| 429 |
return USBH_OK; |
| 430 |
} |
| 431 |
/** |
| 432 |
* @brief This request allows the host to find out the currently |
| 433 |
* configured line coding. |
| 434 |
* @param pdev: Selected device |
| 435 |
* @retval USBH_StatusTypeDef : USB ctl xfer status |
| 436 |
*/ |
| 437 |
static USBH_StatusTypeDef GetLineCoding(USBH_HandleTypeDef *phost, CDC_LineCodingTypeDef *linecoding) |
| 438 |
{ |
| 439 |
|
| 440 |
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \ |
| 441 |
USB_REQ_RECIPIENT_INTERFACE; |
| 442 |
|
| 443 |
phost->Control.setup.b.bRequest = CDC_GET_LINE_CODING; |
| 444 |
phost->Control.setup.b.wValue.w = 0; |
| 445 |
phost->Control.setup.b.wIndex.w = 0; |
| 446 |
phost->Control.setup.b.wLength.w = LINE_CODING_STRUCTURE_SIZE; |
| 447 |
|
| 448 |
return USBH_CtlReq(phost, linecoding->Array, LINE_CODING_STRUCTURE_SIZE); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
/** |
| 453 |
* @brief This request allows the host to specify typical asynchronous |
| 454 |
* line-character formatting properties |
| 455 |
* This request applies to asynchronous byte stream data class interfaces |
| 456 |
* and endpoints |
| 457 |
* @param pdev: Selected device |
| 458 |
* @retval USBH_StatusTypeDef : USB ctl xfer status |
| 459 |
*/ |
| 460 |
static USBH_StatusTypeDef SetLineCoding(USBH_HandleTypeDef *phost, CDC_LineCodingTypeDef *linecodin) |
| 461 |
{ |
| 462 |
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_TYPE_CLASS | \ |
| 463 |
USB_REQ_RECIPIENT_INTERFACE; |
| 464 |
|
| 465 |
phost->Control.setup.b.bRequest = CDC_SET_LINE_CODING; |
| 466 |
phost->Control.setup.b.wValue.w = 0; |
| 467 |
|
| 468 |
phost->Control.setup.b.wIndex.w = 0; |
| 469 |
|
| 470 |
phost->Control.setup.b.wLength.w = LINE_CODING_STRUCTURE_SIZE; |
| 471 |
|
| 472 |
return USBH_CtlReq(phost, linecodin->Array , LINE_CODING_STRUCTURE_SIZE ); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* @brief This function prepares the state before issuing the class specific commands |
| 477 |
* @param None |
| 478 |
* @retval None |
| 479 |
*/ |
| 480 |
USBH_StatusTypeDef USBH_CDC_SetLineCoding(USBH_HandleTypeDef *phost, CDC_LineCodingTypeDef *linecodin) |
| 481 |
{ |
| 482 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 483 |
if(phost->gState == HOST_CLASS) |
| 484 |
{ |
| 485 |
CDC_Handle->state = CDC_SET_LINE_CODING_STATE; |
| 486 |
CDC_Handle->pUserLineCoding = linecodin; |
| 487 |
|
| 488 |
#if (USBH_USE_OS == 1) |
| 489 |
osMessagePut ( phost->os_event, USBH_CLASS_EVENT, 0); |
| 490 |
#endif |
| 491 |
} |
| 492 |
return USBH_OK; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* @brief This function prepares the state before issuing the class specific commands |
| 497 |
* @param None |
| 498 |
* @retval None |
| 499 |
*/ |
| 500 |
USBH_StatusTypeDef USBH_CDC_GetLineCoding(USBH_HandleTypeDef *phost, CDC_LineCodingTypeDef *linecodin) |
| 501 |
{ |
| 502 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 503 |
|
| 504 |
if((phost->gState == HOST_CLASS) ||(phost->gState == HOST_CLASS_REQUEST)) |
| 505 |
{ |
| 506 |
*linecodin = CDC_Handle->LineCoding; |
| 507 |
return USBH_OK; |
| 508 |
} |
| 509 |
else |
| 510 |
{ |
| 511 |
return USBH_FAIL; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* @brief This function return last recieved data size |
| 517 |
* @param None |
| 518 |
* @retval None |
| 519 |
*/ |
| 520 |
uint16_t USBH_CDC_GetLastReceivedDataSize(USBH_HandleTypeDef *phost) |
| 521 |
{ |
| 522 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 523 |
|
| 524 |
if(phost->gState == HOST_CLASS) |
| 525 |
{ |
| 526 |
return USBH_LL_GetLastXferSize(phost, CDC_Handle->DataItf.InPipe);; |
| 527 |
} |
| 528 |
else |
| 529 |
{ |
| 530 |
return 0; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* @brief This function prepares the state before issuing the class specific commands |
| 536 |
* @param None |
| 537 |
* @retval None |
| 538 |
*/ |
| 539 |
USBH_StatusTypeDef USBH_CDC_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length) |
| 540 |
{ |
| 541 |
USBH_StatusTypeDef Status = USBH_BUSY; |
| 542 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 543 |
|
| 544 |
if((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA)) |
| 545 |
{ |
| 546 |
CDC_Handle->pTxData = pbuff; |
| 547 |
CDC_Handle->TxDataLength = length; |
| 548 |
CDC_Handle->state = CDC_TRANSFER_DATA; |
| 549 |
CDC_Handle->data_tx_state = CDC_SEND_DATA; |
| 550 |
Status = USBH_OK; |
| 551 |
} |
| 552 |
return Status; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
/** |
| 557 |
* @brief This function prepares the state before issuing the class specific commands |
| 558 |
* @param None |
| 559 |
* @retval None |
| 560 |
*/ |
| 561 |
USBH_StatusTypeDef USBH_CDC_Receive(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length) |
| 562 |
{ |
| 563 |
USBH_StatusTypeDef Status = USBH_BUSY; |
| 564 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 565 |
|
| 566 |
if((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA)) |
| 567 |
{ |
| 568 |
CDC_Handle->pRxData = pbuff; |
| 569 |
CDC_Handle->RxDataLength = length; |
| 570 |
CDC_Handle->state = CDC_TRANSFER_DATA; |
| 571 |
CDC_Handle->data_rx_state = CDC_RECEIVE_DATA; |
| 572 |
Status = USBH_OK; |
| 573 |
} |
| 574 |
return Status; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* @brief The function is responsible for sending data to the device |
| 579 |
* @param pdev: Selected device |
| 580 |
* @retval None |
| 581 |
*/ |
| 582 |
static void CDC_ProcessTransmission(USBH_HandleTypeDef *phost) |
| 583 |
{ |
| 584 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 585 |
USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; |
| 586 |
|
| 587 |
switch(CDC_Handle->data_tx_state) |
| 588 |
{ |
| 589 |
|
| 590 |
case CDC_SEND_DATA: |
| 591 |
if(CDC_Handle->TxDataLength > CDC_Handle->DataItf.OutEpSize) |
| 592 |
{ |
| 593 |
USBH_BulkSendData (phost, |
| 594 |
CDC_Handle->pTxData, |
| 595 |
CDC_Handle->DataItf.OutEpSize, |
| 596 |
CDC_Handle->DataItf.OutPipe, |
| 597 |
1); |
| 598 |
} |
| 599 |
else |
| 600 |
{ |
| 601 |
USBH_BulkSendData (phost, |
| 602 |
CDC_Handle->pTxData, |
| 603 |
CDC_Handle->TxDataLength, |
| 604 |
CDC_Handle->DataItf.OutPipe, |
| 605 |
1); |
| 606 |
} |
| 607 |
|
| 608 |
CDC_Handle->data_tx_state = CDC_SEND_DATA_WAIT; |
| 609 |
|
| 610 |
break; |
| 611 |
|
| 612 |
case CDC_SEND_DATA_WAIT: |
| 613 |
|
| 614 |
URB_Status = USBH_LL_GetURBState(phost, CDC_Handle->DataItf.OutPipe); |
| 615 |
|
| 616 |
/*Check the status done for transmssion*/ |
| 617 |
if(URB_Status == USBH_URB_DONE ) |
| 618 |
{ |
| 619 |
if(CDC_Handle->TxDataLength > CDC_Handle->DataItf.OutEpSize) |
| 620 |
{ |
| 621 |
CDC_Handle->TxDataLength -= CDC_Handle->DataItf.OutEpSize ; |
| 622 |
CDC_Handle->pTxData += CDC_Handle->DataItf.OutEpSize; |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
CDC_Handle->TxDataLength = 0; |
| 627 |
} |
| 628 |
|
| 629 |
if( CDC_Handle->TxDataLength > 0) |
| 630 |
{ |
| 631 |
CDC_Handle->data_tx_state = CDC_SEND_DATA; |
| 632 |
} |
| 633 |
else |
| 634 |
{ |
| 635 |
CDC_Handle->data_tx_state = CDC_IDLE; |
| 636 |
USBH_CDC_TransmitCallback(phost); |
| 637 |
|
| 638 |
} |
| 639 |
} |
| 640 |
else if( URB_Status == USBH_URB_NOTREADY ) |
| 641 |
{ |
| 642 |
CDC_Handle->data_tx_state = CDC_SEND_DATA; |
| 643 |
} |
| 644 |
break; |
| 645 |
default: |
| 646 |
break; |
| 647 |
} |
| 648 |
} |
| 649 |
/** |
| 650 |
* @brief This function responsible for reception of data from the device |
| 651 |
* @param pdev: Selected device |
| 652 |
* @retval None |
| 653 |
*/ |
| 654 |
|
| 655 |
static void CDC_ProcessReception(USBH_HandleTypeDef *phost) |
| 656 |
{ |
| 657 |
CDC_HandleTypeDef *CDC_Handle = phost->pActiveClass->pData; |
| 658 |
USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; |
| 659 |
uint16_t length; |
| 660 |
|
| 661 |
switch(CDC_Handle->data_rx_state) |
| 662 |
{ |
| 663 |
|
| 664 |
case CDC_RECEIVE_DATA: |
| 665 |
|
| 666 |
USBH_BulkReceiveData (phost, |
| 667 |
CDC_Handle->pRxData, |
| 668 |
CDC_Handle->DataItf.InEpSize, |
| 669 |
CDC_Handle->DataItf.InPipe); |
| 670 |
|
| 671 |
CDC_Handle->data_rx_state = CDC_RECEIVE_DATA_WAIT; |
| 672 |
|
| 673 |
break; |
| 674 |
|
| 675 |
case CDC_RECEIVE_DATA_WAIT: |
| 676 |
|
| 677 |
URB_Status = USBH_LL_GetURBState(phost, CDC_Handle->DataItf.InPipe); |
| 678 |
|
| 679 |
/*Check the status done for reception*/ |
| 680 |
if(URB_Status == USBH_URB_DONE ) |
| 681 |
{ |
| 682 |
length = USBH_LL_GetLastXferSize(phost, CDC_Handle->DataItf.InPipe); |
| 683 |
|
| 684 |
if(((CDC_Handle->RxDataLength - length) > 0) && (length > CDC_Handle->DataItf.InEpSize)) |
| 685 |
{ |
| 686 |
CDC_Handle->RxDataLength -= length ; |
| 687 |
CDC_Handle->pRxData += length; |
| 688 |
CDC_Handle->data_rx_state = CDC_RECEIVE_DATA; |
| 689 |
} |
| 690 |
else |
| 691 |
{ |
| 692 |
CDC_Handle->data_rx_state = CDC_IDLE; |
| 693 |
USBH_CDC_ReceiveCallback(phost); |
| 694 |
} |
| 695 |
} |
| 696 |
break; |
| 697 |
|
| 698 |
default: |
| 699 |
break; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
/** |
| 704 |
* @brief The function informs user that data have been received |
| 705 |
* @param pdev: Selected device |
| 706 |
* @retval None |
| 707 |
*/ |
| 708 |
__weak void USBH_CDC_TransmitCallback(USBH_HandleTypeDef *phost) |
| 709 |
{ |
| 710 |
|
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* @brief The function informs user that data have been sent |
| 715 |
* @param pdev: Selected device |
| 716 |
* @retval None |
| 717 |
*/ |
| 718 |
__weak void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost) |
| 719 |
{ |
| 720 |
|
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* @brief The function informs user that Settings have been changed |
| 725 |
* @param pdev: Selected device |
| 726 |
* @retval None |
| 727 |
*/ |
| 728 |
__weak void USBH_CDC_LineCodingChanged(USBH_HandleTypeDef *phost) |
| 729 |
{ |
| 730 |
|
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* @} |
| 735 |
*/ |
| 736 |
|
| 737 |
/** |
| 738 |
* @} |
| 739 |
*/ |
| 740 |
|
| 741 |
/** |
| 742 |
* @} |
| 743 |
*/ |
| 744 |
|
| 745 |
|
| 746 |
/** |
| 747 |
* @} |
| 748 |
*/ |
| 749 |
|
| 750 |
|
| 751 |
/** |
| 752 |
* @} |
| 753 |
*/ |
| 754 |
|
| 755 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |