IOTON boards API using mbed SDK - http://ioton.cc/plataforma-ton

Dependents:   ton-bot_teste ton-bot_seguidor_linha ton-bot_seguidor_parede

Fork of IOTON-API by Kleber Silva

Committer:
krebyy
Date:
Thu Jun 29 20:20:49 2017 +0000
Revision:
3:9c7195d31602
Parent:
1:3a73d77c2cef
Update to TON Board V1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krebyy 1:3a73d77c2cef 1 /**
krebyy 1:3a73d77c2cef 2 ******************************************************************************
krebyy 1:3a73d77c2cef 3 * @file usbd_cdc_if_template.c
krebyy 1:3a73d77c2cef 4 * @author MCD Application Team
krebyy 1:3a73d77c2cef 5 * @version V2.3.0
krebyy 1:3a73d77c2cef 6 * @date 04-November-2014
krebyy 1:3a73d77c2cef 7 * @brief Generic media access Layer.
krebyy 1:3a73d77c2cef 8 ******************************************************************************
krebyy 1:3a73d77c2cef 9 * @attention
krebyy 1:3a73d77c2cef 10 *
krebyy 1:3a73d77c2cef 11 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
krebyy 1:3a73d77c2cef 12 *
krebyy 1:3a73d77c2cef 13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
krebyy 1:3a73d77c2cef 14 * You may not use this file except in compliance with the License.
krebyy 1:3a73d77c2cef 15 * You may obtain a copy of the License at:
krebyy 1:3a73d77c2cef 16 *
krebyy 1:3a73d77c2cef 17 * http://www.st.com/software_license_agreement_liberty_v2
krebyy 1:3a73d77c2cef 18 *
krebyy 1:3a73d77c2cef 19 * Unless required by applicable law or agreed to in writing, software
krebyy 1:3a73d77c2cef 20 * distributed under the License is distributed on an "AS IS" BASIS,
krebyy 1:3a73d77c2cef 21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
krebyy 1:3a73d77c2cef 22 * See the License for the specific language governing permissions and
krebyy 1:3a73d77c2cef 23 * limitations under the License.
krebyy 1:3a73d77c2cef 24 *
krebyy 1:3a73d77c2cef 25 ******************************************************************************
krebyy 1:3a73d77c2cef 26 * @remark
krebyy 1:3a73d77c2cef 27 * Fun��es TEMPLATE_Init e TEMPLATE_Receive modificadas; Fun��es VCP_read e
krebyy 1:3a73d77c2cef 28 * VCP_write criadas por Kleber Lima da Silva (kleber.ufu@hotmail.com)
krebyy 1:3a73d77c2cef 29 * em 26-Abril-2015
krebyy 1:3a73d77c2cef 30 *
krebyy 1:3a73d77c2cef 31 */
krebyy 1:3a73d77c2cef 32
krebyy 1:3a73d77c2cef 33 /* Includes ------------------------------------------------------------------*/
krebyy 1:3a73d77c2cef 34 #include "usbd_cdc_if_template.h"
krebyy 1:3a73d77c2cef 35
krebyy 1:3a73d77c2cef 36 /** @addtogroup STM32_USB_DEVICE_LIBRARY
krebyy 1:3a73d77c2cef 37 * @{
krebyy 1:3a73d77c2cef 38 */
krebyy 1:3a73d77c2cef 39
krebyy 1:3a73d77c2cef 40
krebyy 1:3a73d77c2cef 41 /** @defgroup USBD_CDC
krebyy 1:3a73d77c2cef 42 * @brief usbd core module
krebyy 1:3a73d77c2cef 43 * @{
krebyy 1:3a73d77c2cef 44 */
krebyy 1:3a73d77c2cef 45
krebyy 1:3a73d77c2cef 46 /** @defgroup USBD_CDC_Private_TypesDefinitions
krebyy 1:3a73d77c2cef 47 * @{
krebyy 1:3a73d77c2cef 48 */
krebyy 1:3a73d77c2cef 49 static struct
krebyy 1:3a73d77c2cef 50 {
krebyy 1:3a73d77c2cef 51 uint8_t Buffer[CDC_DATA_FS_IN_PACKET_SIZE];
krebyy 1:3a73d77c2cef 52 int Position, Size;
krebyy 1:3a73d77c2cef 53 char ReadDone;
krebyy 1:3a73d77c2cef 54 } s_RxBuffer;
krebyy 1:3a73d77c2cef 55
krebyy 1:3a73d77c2cef 56
krebyy 1:3a73d77c2cef 57 extern USBD_HandleTypeDef USBD_Device;
krebyy 1:3a73d77c2cef 58 /**
krebyy 1:3a73d77c2cef 59 * @}
krebyy 1:3a73d77c2cef 60 */
krebyy 1:3a73d77c2cef 61
krebyy 1:3a73d77c2cef 62
krebyy 1:3a73d77c2cef 63 /** @defgroup USBD_CDC_Private_Defines
krebyy 1:3a73d77c2cef 64 * @{
krebyy 1:3a73d77c2cef 65 */
krebyy 1:3a73d77c2cef 66 /**
krebyy 1:3a73d77c2cef 67 * @}
krebyy 1:3a73d77c2cef 68 */
krebyy 1:3a73d77c2cef 69
krebyy 1:3a73d77c2cef 70
krebyy 1:3a73d77c2cef 71 /** @defgroup USBD_CDC_Private_Macros
krebyy 1:3a73d77c2cef 72 * @{
krebyy 1:3a73d77c2cef 73 */
krebyy 1:3a73d77c2cef 74
krebyy 1:3a73d77c2cef 75 /**
krebyy 1:3a73d77c2cef 76 * @}
krebyy 1:3a73d77c2cef 77 */
krebyy 1:3a73d77c2cef 78
krebyy 1:3a73d77c2cef 79
krebyy 1:3a73d77c2cef 80 /** @defgroup USBD_CDC_Private_FunctionPrototypes
krebyy 1:3a73d77c2cef 81 * @{
krebyy 1:3a73d77c2cef 82 */
krebyy 1:3a73d77c2cef 83
krebyy 1:3a73d77c2cef 84 static int8_t TEMPLATE_Init (void);
krebyy 1:3a73d77c2cef 85 static int8_t TEMPLATE_DeInit (void);
krebyy 1:3a73d77c2cef 86 static int8_t TEMPLATE_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length);
krebyy 1:3a73d77c2cef 87 static int8_t TEMPLATE_Receive (uint8_t* pbuf, uint32_t *Len);
krebyy 1:3a73d77c2cef 88
krebyy 1:3a73d77c2cef 89 int VCP_read(void *pBuffer, int size);
krebyy 1:3a73d77c2cef 90 int VCP_write(const void *pBuffer, int size);
krebyy 1:3a73d77c2cef 91
krebyy 1:3a73d77c2cef 92 USBD_CDC_ItfTypeDef USBD_CDC_Template_fops =
krebyy 1:3a73d77c2cef 93 {
krebyy 1:3a73d77c2cef 94 TEMPLATE_Init,
krebyy 1:3a73d77c2cef 95 TEMPLATE_DeInit,
krebyy 1:3a73d77c2cef 96 TEMPLATE_Control,
krebyy 1:3a73d77c2cef 97 TEMPLATE_Receive
krebyy 1:3a73d77c2cef 98 };
krebyy 1:3a73d77c2cef 99
krebyy 1:3a73d77c2cef 100 USBD_CDC_LineCodingTypeDef linecoding =
krebyy 1:3a73d77c2cef 101 {
krebyy 1:3a73d77c2cef 102 115200, /* baud rate*/
krebyy 1:3a73d77c2cef 103 0x00, /* stop bits-1*/
krebyy 1:3a73d77c2cef 104 0x00, /* parity - none*/
krebyy 1:3a73d77c2cef 105 0x08 /* nb. of bits 8*/
krebyy 1:3a73d77c2cef 106 };
krebyy 1:3a73d77c2cef 107
krebyy 1:3a73d77c2cef 108 /* Private functions ---------------------------------------------------------*/
krebyy 1:3a73d77c2cef 109
krebyy 1:3a73d77c2cef 110 /**
krebyy 1:3a73d77c2cef 111 * @brief TEMPLATE_Init
krebyy 1:3a73d77c2cef 112 * Initializes the CDC media low layer
krebyy 1:3a73d77c2cef 113 * @param None
krebyy 1:3a73d77c2cef 114 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
krebyy 1:3a73d77c2cef 115 */
krebyy 1:3a73d77c2cef 116 static int8_t TEMPLATE_Init(void)
krebyy 1:3a73d77c2cef 117 {
krebyy 1:3a73d77c2cef 118 /*
krebyy 1:3a73d77c2cef 119 Add your initialization code here
krebyy 1:3a73d77c2cef 120 */
krebyy 1:3a73d77c2cef 121
krebyy 1:3a73d77c2cef 122 USBD_CDC_SetRxBuffer(&USBD_Device, s_RxBuffer.Buffer);
krebyy 1:3a73d77c2cef 123
krebyy 1:3a73d77c2cef 124 return (USBD_OK);
krebyy 1:3a73d77c2cef 125 }
krebyy 1:3a73d77c2cef 126
krebyy 1:3a73d77c2cef 127 /**
krebyy 1:3a73d77c2cef 128 * @brief TEMPLATE_DeInit
krebyy 1:3a73d77c2cef 129 * DeInitializes the CDC media low layer
krebyy 1:3a73d77c2cef 130 * @param None
krebyy 1:3a73d77c2cef 131 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
krebyy 1:3a73d77c2cef 132 */
krebyy 1:3a73d77c2cef 133 static int8_t TEMPLATE_DeInit(void)
krebyy 1:3a73d77c2cef 134 {
krebyy 1:3a73d77c2cef 135 /*
krebyy 1:3a73d77c2cef 136 Add your deinitialization code here
krebyy 1:3a73d77c2cef 137 */
krebyy 1:3a73d77c2cef 138 return (0);
krebyy 1:3a73d77c2cef 139 }
krebyy 1:3a73d77c2cef 140
krebyy 1:3a73d77c2cef 141
krebyy 1:3a73d77c2cef 142 /**
krebyy 1:3a73d77c2cef 143 * @brief TEMPLATE_Control
krebyy 1:3a73d77c2cef 144 * Manage the CDC class requests
krebyy 1:3a73d77c2cef 145 * @param Cmd: Command code
krebyy 1:3a73d77c2cef 146 * @param Buf: Buffer containing command data (request parameters)
krebyy 1:3a73d77c2cef 147 * @param Len: Number of data to be sent (in bytes)
krebyy 1:3a73d77c2cef 148 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
krebyy 1:3a73d77c2cef 149 */
krebyy 1:3a73d77c2cef 150 static int8_t TEMPLATE_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
krebyy 1:3a73d77c2cef 151 {
krebyy 1:3a73d77c2cef 152 switch (cmd)
krebyy 1:3a73d77c2cef 153 {
krebyy 1:3a73d77c2cef 154 case CDC_SEND_ENCAPSULATED_COMMAND:
krebyy 1:3a73d77c2cef 155 /* Add your code here */
krebyy 1:3a73d77c2cef 156 break;
krebyy 1:3a73d77c2cef 157
krebyy 1:3a73d77c2cef 158 case CDC_GET_ENCAPSULATED_RESPONSE:
krebyy 1:3a73d77c2cef 159 /* Add your code here */
krebyy 1:3a73d77c2cef 160 break;
krebyy 1:3a73d77c2cef 161
krebyy 1:3a73d77c2cef 162 case CDC_SET_COMM_FEATURE:
krebyy 1:3a73d77c2cef 163 /* Add your code here */
krebyy 1:3a73d77c2cef 164 break;
krebyy 1:3a73d77c2cef 165
krebyy 1:3a73d77c2cef 166 case CDC_GET_COMM_FEATURE:
krebyy 1:3a73d77c2cef 167 /* Add your code here */
krebyy 1:3a73d77c2cef 168 break;
krebyy 1:3a73d77c2cef 169
krebyy 1:3a73d77c2cef 170 case CDC_CLEAR_COMM_FEATURE:
krebyy 1:3a73d77c2cef 171 /* Add your code here */
krebyy 1:3a73d77c2cef 172 break;
krebyy 1:3a73d77c2cef 173
krebyy 1:3a73d77c2cef 174 case CDC_SET_LINE_CODING:
krebyy 1:3a73d77c2cef 175 linecoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
krebyy 1:3a73d77c2cef 176 (pbuf[2] << 16) | (pbuf[3] << 24));
krebyy 1:3a73d77c2cef 177 linecoding.format = pbuf[4];
krebyy 1:3a73d77c2cef 178 linecoding.paritytype = pbuf[5];
krebyy 1:3a73d77c2cef 179 linecoding.datatype = pbuf[6];
krebyy 1:3a73d77c2cef 180
krebyy 1:3a73d77c2cef 181 /* Add your code here */
krebyy 1:3a73d77c2cef 182 break;
krebyy 1:3a73d77c2cef 183
krebyy 1:3a73d77c2cef 184 case CDC_GET_LINE_CODING:
krebyy 1:3a73d77c2cef 185 pbuf[0] = (uint8_t)(linecoding.bitrate);
krebyy 1:3a73d77c2cef 186 pbuf[1] = (uint8_t)(linecoding.bitrate >> 8);
krebyy 1:3a73d77c2cef 187 pbuf[2] = (uint8_t)(linecoding.bitrate >> 16);
krebyy 1:3a73d77c2cef 188 pbuf[3] = (uint8_t)(linecoding.bitrate >> 24);
krebyy 1:3a73d77c2cef 189 pbuf[4] = linecoding.format;
krebyy 1:3a73d77c2cef 190 pbuf[5] = linecoding.paritytype;
krebyy 1:3a73d77c2cef 191 pbuf[6] = linecoding.datatype;
krebyy 1:3a73d77c2cef 192
krebyy 1:3a73d77c2cef 193 /* Add your code here */
krebyy 1:3a73d77c2cef 194 break;
krebyy 1:3a73d77c2cef 195
krebyy 1:3a73d77c2cef 196 case CDC_SET_CONTROL_LINE_STATE:
krebyy 1:3a73d77c2cef 197 /* Add your code here */
krebyy 1:3a73d77c2cef 198 break;
krebyy 1:3a73d77c2cef 199
krebyy 1:3a73d77c2cef 200 case CDC_SEND_BREAK:
krebyy 1:3a73d77c2cef 201 /* Add your code here */
krebyy 1:3a73d77c2cef 202 break;
krebyy 1:3a73d77c2cef 203
krebyy 1:3a73d77c2cef 204 default:
krebyy 1:3a73d77c2cef 205 break;
krebyy 1:3a73d77c2cef 206 }
krebyy 1:3a73d77c2cef 207
krebyy 1:3a73d77c2cef 208 return (0);
krebyy 1:3a73d77c2cef 209 }
krebyy 1:3a73d77c2cef 210
krebyy 1:3a73d77c2cef 211 /**
krebyy 1:3a73d77c2cef 212 * @brief TEMPLATE_Receive
krebyy 1:3a73d77c2cef 213 * Data received over USB OUT endpoint are sent over CDC interface
krebyy 1:3a73d77c2cef 214 * through this function.
krebyy 1:3a73d77c2cef 215 *
krebyy 1:3a73d77c2cef 216 * @note
krebyy 1:3a73d77c2cef 217 * This function will issue a NAK packet on any OUT packet received on
krebyy 1:3a73d77c2cef 218 * USB endpoint untill exiting this function. If you exit this function
krebyy 1:3a73d77c2cef 219 * before transfer is complete on CDC interface (ie. using DMA controller)
krebyy 1:3a73d77c2cef 220 * it will result in receiving more data while previous ones are still
krebyy 1:3a73d77c2cef 221 * not sent.
krebyy 1:3a73d77c2cef 222 *
krebyy 1:3a73d77c2cef 223 * @param Buf: Buffer of data to be received
krebyy 1:3a73d77c2cef 224 * @param Len: Number of data received (in bytes)
krebyy 1:3a73d77c2cef 225 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
krebyy 1:3a73d77c2cef 226 */
krebyy 1:3a73d77c2cef 227 static int8_t TEMPLATE_Receive (uint8_t* Buf, uint32_t *Len)
krebyy 1:3a73d77c2cef 228 {
krebyy 1:3a73d77c2cef 229 s_RxBuffer.Position = 0;
krebyy 1:3a73d77c2cef 230 s_RxBuffer.Size = *Len;
krebyy 1:3a73d77c2cef 231 s_RxBuffer.ReadDone = 1;
krebyy 1:3a73d77c2cef 232
krebyy 1:3a73d77c2cef 233 return (0);
krebyy 1:3a73d77c2cef 234 }
krebyy 1:3a73d77c2cef 235
krebyy 1:3a73d77c2cef 236
krebyy 1:3a73d77c2cef 237 /**
krebyy 1:3a73d77c2cef 238 * @brief Fun��o de leitura da Virtual COM Port
krebyy 1:3a73d77c2cef 239 */
krebyy 1:3a73d77c2cef 240 int VCP_read(void *pBuffer, int size)
krebyy 1:3a73d77c2cef 241 {
krebyy 1:3a73d77c2cef 242 if (!s_RxBuffer.ReadDone)
krebyy 1:3a73d77c2cef 243 return 0;
krebyy 1:3a73d77c2cef 244
krebyy 1:3a73d77c2cef 245 int remaining = s_RxBuffer.Size - s_RxBuffer.Position;
krebyy 1:3a73d77c2cef 246 int todo = MIN(remaining, size);
krebyy 1:3a73d77c2cef 247 if (todo <= 0)
krebyy 1:3a73d77c2cef 248 return 0;
krebyy 1:3a73d77c2cef 249
krebyy 1:3a73d77c2cef 250 memcpy(pBuffer, s_RxBuffer.Buffer + s_RxBuffer.Position, todo);
krebyy 1:3a73d77c2cef 251 s_RxBuffer.Position += todo;
krebyy 1:3a73d77c2cef 252 if (s_RxBuffer.Position >= s_RxBuffer.Size)
krebyy 1:3a73d77c2cef 253 {
krebyy 1:3a73d77c2cef 254 s_RxBuffer.ReadDone = 0;
krebyy 1:3a73d77c2cef 255 USBD_CDC_ReceivePacket(&USBD_Device);
krebyy 1:3a73d77c2cef 256 }
krebyy 1:3a73d77c2cef 257
krebyy 1:3a73d77c2cef 258 return todo;
krebyy 1:3a73d77c2cef 259 }
krebyy 1:3a73d77c2cef 260
krebyy 1:3a73d77c2cef 261
krebyy 1:3a73d77c2cef 262 /**
krebyy 1:3a73d77c2cef 263 * @brief Fun��o de escrita na Virtual COM Port
krebyy 1:3a73d77c2cef 264 */
krebyy 1:3a73d77c2cef 265 int VCP_write(const void *pBuffer, int size)
krebyy 1:3a73d77c2cef 266 {
krebyy 1:3a73d77c2cef 267 uint16_t timeout = 10000;
krebyy 1:3a73d77c2cef 268
krebyy 1:3a73d77c2cef 269 if (size > CDC_DATA_FS_OUT_PACKET_SIZE)
krebyy 1:3a73d77c2cef 270 {
krebyy 1:3a73d77c2cef 271 int offset;
krebyy 1:3a73d77c2cef 272 for (offset = 0; offset < size; offset++)
krebyy 1:3a73d77c2cef 273 {
krebyy 1:3a73d77c2cef 274 int todo = MIN(CDC_DATA_HS_OUT_PACKET_SIZE,
krebyy 1:3a73d77c2cef 275 size - offset);
krebyy 1:3a73d77c2cef 276 int done = VCP_write(((char *)pBuffer) + offset, todo);
krebyy 1:3a73d77c2cef 277 if (done != todo)
krebyy 1:3a73d77c2cef 278 return offset + done;
krebyy 1:3a73d77c2cef 279 }
krebyy 1:3a73d77c2cef 280
krebyy 1:3a73d77c2cef 281 return size;
krebyy 1:3a73d77c2cef 282 }
krebyy 1:3a73d77c2cef 283
krebyy 1:3a73d77c2cef 284 USBD_CDC_HandleTypeDef *pCDC =
krebyy 1:3a73d77c2cef 285 (USBD_CDC_HandleTypeDef *)USBD_Device.pClassData;
krebyy 1:3a73d77c2cef 286
krebyy 1:3a73d77c2cef 287 USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t *)pBuffer, size);
krebyy 1:3a73d77c2cef 288 if (USBD_CDC_TransmitPacket(&USBD_Device) != USBD_OK)
krebyy 1:3a73d77c2cef 289 return 0;
krebyy 1:3a73d77c2cef 290
krebyy 1:3a73d77c2cef 291 while(pCDC->TxState)
krebyy 1:3a73d77c2cef 292 {
krebyy 1:3a73d77c2cef 293 if ((timeout--) == 0) return 0;
krebyy 1:3a73d77c2cef 294 }
krebyy 1:3a73d77c2cef 295
krebyy 1:3a73d77c2cef 296 return size;
krebyy 1:3a73d77c2cef 297 }
krebyy 1:3a73d77c2cef 298
krebyy 1:3a73d77c2cef 299
krebyy 1:3a73d77c2cef 300 /**
krebyy 1:3a73d77c2cef 301 * @}
krebyy 1:3a73d77c2cef 302 */
krebyy 1:3a73d77c2cef 303
krebyy 1:3a73d77c2cef 304 /**
krebyy 1:3a73d77c2cef 305 * @}
krebyy 1:3a73d77c2cef 306 */
krebyy 1:3a73d77c2cef 307
krebyy 1:3a73d77c2cef 308 /**
krebyy 1:3a73d77c2cef 309 * @}
krebyy 1:3a73d77c2cef 310 */
krebyy 1:3a73d77c2cef 311
krebyy 1:3a73d77c2cef 312 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
krebyy 1:3a73d77c2cef 313