Damian Gabino / picoGW_mcu
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbd_cdc_if.cpp Source File

usbd_cdc_if.cpp

00001 /**
00002   ******************************************************************************
00003   * @file           : usbd_cdc_if.c
00004   * @brief          :
00005   ******************************************************************************
00006   * COPYRIGHT(c) 2016 STMicroelectronics
00007   *
00008   * Redistribution and use in source and binary forms, with or without modification,
00009   * are permitted provided that the following conditions are met:
00010   * 1. Redistributions of source code must retain the above copyright notice,
00011   * this list of conditions and the following disclaimer.
00012   * 2. Redistributions in binary form must reproduce the above copyright notice,
00013   * this list of conditions and the following disclaimer in the documentation
00014   * and/or other materials provided with the distribution.
00015   * 3. Neither the name of STMicroelectronics nor the names of its contributors
00016   * may be used to endorse or promote products derived from this software
00017   * without specific prior written permission.
00018   *
00019   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00023   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029   *
00030   ******************************************************************************
00031 */
00032 
00033 /* Includes ------------------------------------------------------------------*/
00034 #include "usbd_cdc_if.h"
00035 /* USER CODE BEGIN INCLUDE */
00036 /* USER CODE END INCLUDE */
00037 
00038 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
00039   * @{
00040   */
00041 
00042 /** @defgroup USBD_CDC 
00043   * @brief usbd core module
00044   * @{
00045   */ 
00046 
00047 /** @defgroup USBD_CDC_Private_TypesDefinitions
00048   * @{
00049   */ 
00050 /* USER CODE BEGIN PRIVATE_TYPES */
00051 /* USER CODE END PRIVATE_TYPES */ 
00052 /**
00053   * @}
00054   */ 
00055 
00056 /** @defgroup USBD_CDC_Private_Defines
00057   * @{
00058   */ 
00059 /* USER CODE BEGIN PRIVATE_DEFINES */
00060 /* Define size for the receive and transmit buffer over CDC */
00061 /* It's up to user to redefine and/or remove those define */
00062 #define APP_RX_DATA_SIZE  8200
00063 #define APP_TX_DATA_SIZE  8200
00064 /* USER CODE END PRIVATE_DEFINES */
00065 /**
00066   * @}
00067   */ 
00068 
00069 /** @defgroup USBD_CDC_Private_Macros
00070   * @{
00071   */ 
00072 /* USER CODE BEGIN PRIVATE_MACRO */
00073 /* USER CODE END PRIVATE_MACRO */
00074 
00075 /**
00076   * @}
00077   */ 
00078   
00079 /** @defgroup USBD_CDC_Private_Variables
00080   * @{
00081   */
00082 /* Create buffer for reception and transmission           */
00083 /* It's up to user to redefine and/or remove those define */
00084 /* Received Data over USB are stored in this buffer       */
00085 uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
00086 
00087 /* Send Data over USB CDC are stored in this buffer       */
00088 uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
00089 
00090 /* USER CODE BEGIN PRIVATE_VARIABLES */
00091 /* USER CODE END PRIVATE_VARIABLES */
00092 
00093 /**
00094   * @}
00095   */ 
00096   
00097 /** @defgroup USBD_CDC_IF_Exported_Variables
00098   * @{
00099   */ 
00100   extern USBD_HandleTypeDef hUsbDeviceFS;
00101 /* USER CODE BEGIN EXPORTED_VARIABLES */
00102 /* USER CODE END EXPORTED_VARIABLES */
00103 
00104 /**
00105   * @}
00106   */ 
00107   
00108 /** @defgroup USBD_CDC_Private_FunctionPrototypes
00109   * @{
00110   */
00111 static int8_t CDC_Init_FS     (void);
00112 static int8_t CDC_DeInit_FS   (void);
00113 static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length);
00114 static int8_t CDC_Receive_FS  (uint8_t* pbuf, uint32_t *Len);
00115 
00116 /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
00117 /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
00118 
00119 /**
00120   * @}
00121   */ 
00122   
00123 USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = 
00124 {
00125   CDC_Init_FS,
00126   CDC_DeInit_FS,
00127   CDC_Control_FS,  
00128   CDC_Receive_FS
00129 };
00130 
00131 /* Private functions ---------------------------------------------------------*/
00132 /**
00133   * @brief  CDC_Init_FS
00134   *         Initializes the CDC media low layer over the FS USB IP
00135   * @param  None
00136   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00137   */
00138 static int8_t CDC_Init_FS(void)
00139 { 
00140   /* USER CODE BEGIN 3 */ 
00141   /* Set Application Buffers */
00142   USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
00143   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
00144   return (USBD_OK);
00145   /* USER CODE END 3 */ 
00146 }
00147 
00148 /**
00149   * @brief  CDC_DeInit_FS
00150   *         DeInitializes the CDC media low layer
00151   * @param  None
00152   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00153   */
00154 static int8_t CDC_DeInit_FS(void)
00155 {
00156   /* USER CODE BEGIN 4 */ 
00157   return (USBD_OK);
00158   /* USER CODE END 4 */ 
00159 }
00160 
00161 /**
00162   * @brief  CDC_Control_FS
00163   *         Manage the CDC class requests
00164   * @param  cmd: Command code            
00165   * @param  pbuf: Buffer containing command data (request parameters)
00166   * @param  length: Number of data to be sent (in bytes)
00167   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00168   */
00169 static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length)
00170 { 
00171   /* USER CODE BEGIN 5 */
00172   switch (cmd)
00173   {
00174   case CDC_SEND_ENCAPSULATED_COMMAND:
00175  
00176     break;
00177 
00178   case CDC_GET_ENCAPSULATED_RESPONSE:
00179  
00180     break;
00181 
00182   case CDC_SET_COMM_FEATURE:
00183  
00184     break;
00185 
00186   case CDC_GET_COMM_FEATURE:
00187 
00188     break;
00189 
00190   case CDC_CLEAR_COMM_FEATURE:
00191 
00192     break;
00193 
00194   /*******************************************************************************/
00195   /* Line Coding Structure                                                       */
00196   /*-----------------------------------------------------------------------------*/
00197   /* Offset | Field       | Size | Value  | Description                          */
00198   /* 0      | dwDTERate   |   4  | Number |Data terminal rate, in bits per second*/
00199   /* 4      | bCharFormat |   1  | Number | Stop bits                            */
00200   /*                                        0 - 1 Stop bit                       */
00201   /*                                        1 - 1.5 Stop bits                    */
00202   /*                                        2 - 2 Stop bits                      */
00203   /* 5      | bParityType |  1   | Number | Parity                               */
00204   /*                                        0 - None                             */
00205   /*                                        1 - Odd                              */ 
00206   /*                                        2 - Even                             */
00207   /*                                        3 - Mark                             */
00208   /*                                        4 - Space                            */
00209   /* 6      | bDataBits  |   1   | Number Data bits (5, 6, 7, 8 or 16).          */
00210   /*******************************************************************************/
00211   case CDC_SET_LINE_CODING:   
00212     
00213     break;
00214 
00215   case CDC_GET_LINE_CODING:     
00216 
00217     break;
00218 
00219   case CDC_SET_CONTROL_LINE_STATE:
00220 
00221     break;
00222 
00223   case CDC_SEND_BREAK:
00224  
00225     break;    
00226     
00227   default:
00228     break;
00229   }
00230 
00231   return (USBD_OK);
00232   /* USER CODE END 5 */
00233 }
00234 
00235 /**
00236   * @brief  CDC_Receive_FS
00237   *         Data received over USB OUT endpoint are sent over CDC interface 
00238   *         through this function.
00239   *           
00240   *         @note
00241   *         This function will block any OUT packet reception on USB endpoint 
00242   *         untill exiting this function. If you exit this function before transfer
00243   *         is complete on CDC interface (ie. using DMA controller) it will result 
00244   *         in receiving more data while previous ones are still not sent.
00245   *                 
00246   * @param  Buf: Buffer of data to be received
00247   * @param  Len: Number of data received (in bytes)
00248   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00249   */
00250 static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
00251 {
00252   /* USER CODE BEGIN 6 */
00253   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
00254   USBD_CDC_ReceivePacket(&hUsbDeviceFS);
00255   return (USBD_OK);
00256   /* USER CODE END 6 */ 
00257 }
00258 
00259 /**
00260   * @brief  CDC_Transmit_FS
00261   *         Data send over USB IN endpoint are sent over CDC interface 
00262   *         through this function.           
00263   *         @note
00264   *         
00265   *                 
00266   * @param  Buf: Buffer of data to be send
00267   * @param  Len: Number of data to be send (in bytes)
00268   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
00269   */
00270 uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
00271 {
00272   uint8_t result = USBD_OK;
00273   /* USER CODE BEGIN 7 */ 
00274   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
00275   if (hcdc->TxState != 0){
00276     return USBD_BUSY;
00277   }
00278   USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
00279   result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
00280   /* USER CODE END 7 */ 
00281   return result;
00282 }
00283 
00284  int8_t CDC_Receive_FSP(uint8_t* Buf, uint32_t *Len)
00285 {
00286   /* USER CODE BEGIN 6 */
00287   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
00288   USBD_CDC_ReceivePacket(&hUsbDeviceFS);
00289   return (USBD_OK);
00290   /* USER CODE END 6 */ 
00291 }
00292 /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
00293 /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
00294 
00295 /**
00296   * @}
00297   */ 
00298 
00299 /**
00300   * @}
00301   */ 
00302 
00303 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00304 
00305 
00306