Kleber Silva / IOTON-API

Dependents:   ton_demo ton_template

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbd_cdc_if_template.c Source File

usbd_cdc_if_template.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    usbd_cdc_if_template.c
00004   * @author  MCD Application Team
00005   * @version V2.3.0
00006   * @date    04-November-2014
00007   * @brief   Generic media access Layer.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   * @remark
00027   * Fun��es TEMPLATE_Init e TEMPLATE_Receive modificadas; Fun��es VCP_read e
00028   * VCP_write criadas por Kleber Lima da Silva (kleber.ufu@hotmail.com)
00029   * em 26-Abril-2015
00030   *
00031   */
00032 
00033 /* Includes ------------------------------------------------------------------*/
00034 #include "usbd_cdc_if_template.h"
00035 
00036 /** @addtogroup STM32_USB_DEVICE_LIBRARY
00037   * @{
00038   */
00039 
00040 
00041 /** @defgroup USBD_CDC 
00042   * @brief usbd core module
00043   * @{
00044   */ 
00045 
00046 /** @defgroup USBD_CDC_Private_TypesDefinitions
00047   * @{
00048   */ 
00049 static struct
00050 {
00051     uint8_t Buffer[CDC_DATA_FS_IN_PACKET_SIZE];
00052     int Position, Size;
00053     char ReadDone;
00054 } s_RxBuffer;
00055 
00056 
00057 extern USBD_HandleTypeDef USBD_Device;
00058 /**
00059   * @}
00060   */ 
00061 
00062 
00063 /** @defgroup USBD_CDC_Private_Defines
00064   * @{
00065   */ 
00066 /**
00067   * @}
00068   */ 
00069 
00070 
00071 /** @defgroup USBD_CDC_Private_Macros
00072   * @{
00073   */ 
00074 
00075 /**
00076   * @}
00077   */ 
00078 
00079 
00080 /** @defgroup USBD_CDC_Private_FunctionPrototypes
00081   * @{
00082   */
00083 
00084 static int8_t TEMPLATE_Init     (void);
00085 static int8_t TEMPLATE_DeInit   (void);
00086 static int8_t TEMPLATE_Control  (uint8_t cmd, uint8_t* pbuf, uint16_t length);
00087 static int8_t TEMPLATE_Receive  (uint8_t* pbuf, uint32_t *Len);
00088 
00089 int VCP_read(void *pBuffer, int size);
00090 int VCP_write(const void *pBuffer, int size);
00091 
00092 USBD_CDC_ItfTypeDef USBD_CDC_Template_fops =
00093 {
00094   TEMPLATE_Init,
00095   TEMPLATE_DeInit,
00096   TEMPLATE_Control,
00097   TEMPLATE_Receive
00098 };
00099 
00100 USBD_CDC_LineCodingTypeDef linecoding =
00101   {
00102     115200, /* baud rate*/
00103     0x00,   /* stop bits-1*/
00104     0x00,   /* parity - none*/
00105     0x08    /* nb. of bits 8*/
00106   };
00107 
00108 /* Private functions ---------------------------------------------------------*/
00109 
00110 /**
00111   * @brief  TEMPLATE_Init
00112   *         Initializes the CDC media low layer
00113   * @param  None
00114   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00115   */
00116 static int8_t TEMPLATE_Init(void)
00117 {
00118   /*
00119      Add your initialization code here 
00120   */  
00121 
00122   USBD_CDC_SetRxBuffer(&USBD_Device, s_RxBuffer.Buffer);
00123 
00124   return (USBD_OK);
00125 }
00126 
00127 /**
00128   * @brief  TEMPLATE_DeInit
00129   *         DeInitializes the CDC media low layer
00130   * @param  None
00131   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00132   */
00133 static int8_t TEMPLATE_DeInit(void)
00134 {
00135   /*
00136      Add your deinitialization code here 
00137   */  
00138   return (0);
00139 }
00140 
00141 
00142 /**
00143   * @brief  TEMPLATE_Control
00144   *         Manage the CDC class requests
00145   * @param  Cmd: Command code            
00146   * @param  Buf: Buffer containing command data (request parameters)
00147   * @param  Len: Number of data to be sent (in bytes)
00148   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00149   */
00150 static int8_t TEMPLATE_Control  (uint8_t cmd, uint8_t* pbuf, uint16_t length)
00151 { 
00152   switch (cmd)
00153   {
00154   case CDC_SEND_ENCAPSULATED_COMMAND:
00155     /* Add your code here */
00156     break;
00157 
00158   case CDC_GET_ENCAPSULATED_RESPONSE:
00159     /* Add your code here */
00160     break;
00161 
00162   case CDC_SET_COMM_FEATURE:
00163     /* Add your code here */
00164     break;
00165 
00166   case CDC_GET_COMM_FEATURE:
00167     /* Add your code here */
00168     break;
00169 
00170   case CDC_CLEAR_COMM_FEATURE:
00171     /* Add your code here */
00172     break;
00173 
00174   case CDC_SET_LINE_CODING:
00175     linecoding.bitrate    = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
00176                             (pbuf[2] << 16) | (pbuf[3] << 24));
00177     linecoding.format     = pbuf[4];
00178     linecoding.paritytype = pbuf[5];
00179     linecoding.datatype   = pbuf[6];
00180     
00181     /* Add your code here */
00182     break;
00183 
00184   case CDC_GET_LINE_CODING:
00185     pbuf[0] = (uint8_t)(linecoding.bitrate);
00186     pbuf[1] = (uint8_t)(linecoding.bitrate >> 8);
00187     pbuf[2] = (uint8_t)(linecoding.bitrate >> 16);
00188     pbuf[3] = (uint8_t)(linecoding.bitrate >> 24);
00189     pbuf[4] = linecoding.format;
00190     pbuf[5] = linecoding.paritytype;
00191     pbuf[6] = linecoding.datatype;     
00192     
00193     /* Add your code here */
00194     break;
00195 
00196   case CDC_SET_CONTROL_LINE_STATE:
00197     /* Add your code here */
00198     break;
00199 
00200   case CDC_SEND_BREAK:
00201      /* Add your code here */
00202     break;    
00203     
00204   default:
00205     break;
00206   }
00207 
00208   return (0);
00209 }
00210 
00211 /**
00212   * @brief  TEMPLATE_Receive
00213   *         Data received over USB OUT endpoint are sent over CDC interface 
00214   *         through this function.
00215   *           
00216   *         @note
00217   *         This function will issue a NAK packet on any OUT packet received on 
00218   *         USB endpoint untill exiting this function. If you exit this function
00219   *         before transfer is complete on CDC interface (ie. using DMA controller)
00220   *         it will result in receiving more data while previous ones are still 
00221   *         not sent.
00222   *                 
00223   * @param  Buf: Buffer of data to be received
00224   * @param  Len: Number of data received (in bytes)
00225   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
00226   */
00227 static int8_t TEMPLATE_Receive (uint8_t* Buf, uint32_t *Len)
00228 {
00229   s_RxBuffer.Position = 0;
00230   s_RxBuffer.Size = *Len;
00231   s_RxBuffer.ReadDone = 1;
00232 
00233   return (0);
00234 }
00235 
00236 
00237 /**
00238   * @brief Fun��o de leitura da Virtual COM Port
00239   */
00240 int VCP_read(void *pBuffer, int size)
00241 {
00242     if (!s_RxBuffer.ReadDone)
00243         return 0;
00244 
00245     int remaining = s_RxBuffer.Size - s_RxBuffer.Position;
00246     int todo = MIN(remaining, size);
00247     if (todo <= 0)
00248         return 0;
00249 
00250     memcpy(pBuffer, s_RxBuffer.Buffer + s_RxBuffer.Position, todo);
00251     s_RxBuffer.Position += todo;
00252     if (s_RxBuffer.Position >= s_RxBuffer.Size)
00253     {
00254         s_RxBuffer.ReadDone = 0;
00255         USBD_CDC_ReceivePacket(&USBD_Device);
00256     }
00257 
00258     return todo;
00259 }
00260 
00261 
00262 /**
00263   * @brief Fun��o de escrita na Virtual COM Port
00264   */
00265 int VCP_write(const void *pBuffer, int size)
00266 {
00267     uint16_t timeout = 10000;
00268 
00269     if (size > CDC_DATA_FS_OUT_PACKET_SIZE)
00270     {
00271         int offset;
00272         for (offset = 0; offset < size; offset++)
00273         {
00274             int todo = MIN(CDC_DATA_HS_OUT_PACKET_SIZE,
00275                            size - offset);
00276             int done = VCP_write(((char *)pBuffer) + offset, todo);
00277             if (done != todo)
00278                 return offset + done;
00279         }
00280 
00281         return size;
00282     }
00283 
00284     USBD_CDC_HandleTypeDef *pCDC =
00285             (USBD_CDC_HandleTypeDef *)USBD_Device.pClassData;
00286 
00287     USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t *)pBuffer, size);
00288     if (USBD_CDC_TransmitPacket(&USBD_Device) != USBD_OK)
00289         return 0;
00290 
00291     while(pCDC->TxState)
00292     {
00293         if ((timeout--) == 0) return 0;
00294     }
00295 
00296     return size;
00297 }
00298 
00299 
00300 /**
00301   * @}
00302   */ 
00303 
00304 /**
00305   * @}
00306   */ 
00307 
00308 /**
00309   * @}
00310   */ 
00311 
00312 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00313