msp: expose number of vtx power levels, bands and channels
[inav.git] / src / main / vcp_hal / usbd_cdc_interface.c
blob2489d74fb26c5f8e32011f2a508e308cd5c612b6
1 /**
2 ******************************************************************************
3 * @file USB_Device/CDC_Standalone/Src/usbd_cdc_interface.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 22-April-2016
7 * @brief Source file for USBD CDC interface
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics International N.V.
12 * All rights reserved.</center></h2>
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted, provided that the following conditions are met:
17 * 1. Redistribution of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. Neither the name of STMicroelectronics nor the names of other
23 * contributors to this software may be used to endorse or promote products
24 * derived from this software without specific written permission.
25 * 4. This software, including modifications and/or derivative works of this
26 * software, must execute solely and exclusively on microcontroller or
27 * microprocessor devices manufactured by or for STMicroelectronics.
28 * 5. Redistribution and use of this software other than as permitted under
29 * this license is void and will automatically terminate your rights under
30 * this license.
32 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
35 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
36 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
37 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
40 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 ******************************************************************************
48 /* Includes ------------------------------------------------------------------*/
49 #include "platform.h"
51 #include "usbd_core.h"
52 #include "usbd_desc.h"
53 #include "usbd_cdc.h"
54 #include "usbd_cdc_interface.h"
55 #include "stdbool.h"
56 #include "drivers/time.h"
57 #include "drivers/nvic.h"
58 #include "build/atomic.h"
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 #define APP_RX_DATA_SIZE 4096
63 #define APP_TX_DATA_SIZE 4096
65 /* Private macro -------------------------------------------------------------*/
66 /* Private variables ---------------------------------------------------------*/
67 USBD_CDC_LineCodingTypeDef LineCoding =
69 115200, /* baud rate*/
70 0x00, /* stop bits-1*/
71 0x00, /* parity - none*/
72 0x08 /* nb. of bits 8*/
75 uint8_t UserRxBuffer[APP_RX_DATA_SIZE];/* Received Data over USB are stored in this buffer */
76 uint8_t UserTxBuffer[APP_TX_DATA_SIZE];/* Received Data over UART (CDC interface) are stored in this buffer */
77 uint32_t BuffLength;
78 uint32_t UserTxBufPtrIn = 0;/* Increment this pointer or roll it back to
79 start address when data are received over USART */
80 uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
81 start address when data are sent over USB */
83 uint32_t rxAvailable = 0;
84 uint8_t* rxBuffPtr = NULL;
86 /* TIM handler declaration */
87 TIM_HandleTypeDef TimHandle;
88 /* USB handler declaration */
89 extern USBD_HandleTypeDef USBD_Device;
91 static void (*ctrlLineStateCb)(void *context, uint16_t ctrlLineState);
92 static void *ctrlLineStateCbContext;
93 static void (*baudRateCb)(void *context, uint32_t baud);
94 static void *baudRateCbContext;
96 /* Private function prototypes -----------------------------------------------*/
97 static int8_t CDC_Itf_Init(void);
98 static int8_t CDC_Itf_DeInit(void);
99 static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length);
100 static int8_t CDC_Itf_Receive(uint8_t* pbuf, uint32_t *Len);
102 static void TIM_Config(void);
103 static void Error_Handler(void);
105 USBD_CDC_ItfTypeDef USBD_CDC_fops =
107 CDC_Itf_Init,
108 CDC_Itf_DeInit,
109 CDC_Itf_Control,
110 CDC_Itf_Receive
114 void TIMx_IRQHandler(void)
116 HAL_TIM_IRQHandler(&TimHandle);
119 /* Private functions ---------------------------------------------------------*/
122 * @brief CDC_Itf_Init
123 * Initializes the CDC media low layer
124 * @param None
125 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
127 static int8_t CDC_Itf_Init(void)
129 /*##-3- Configure the TIM Base generation #################################*/
130 TIM_Config();
132 /*##-4- Start the TIM Base generation in interrupt mode ####################*/
133 /* Start Channel1 */
134 if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
136 /* Starting Error */
137 Error_Handler();
140 /*##-5- Set Application Buffers ############################################*/
141 USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0);
142 USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer);
144 ctrlLineStateCb = NULL;
145 baudRateCb = NULL;
147 return (USBD_OK);
151 * @brief CDC_Itf_DeInit
152 * DeInitializes the CDC media low layer
153 * @param None
154 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
156 static int8_t CDC_Itf_DeInit(void)
159 return (USBD_OK);
163 * @brief CDC_Itf_Control
164 * Manage the CDC class requests
165 * @param Cmd: Command code
166 * @param Buf: Buffer containing command data (request parameters)
167 * @param Len: Number of data to be sent (in bytes)
168 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
170 static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
172 LINE_CODING* plc = (LINE_CODING*)pbuf;
174 switch (cmd)
176 case CDC_SEND_ENCAPSULATED_COMMAND:
177 /* Add your code here */
178 break;
180 case CDC_GET_ENCAPSULATED_RESPONSE:
181 /* Add your code here */
182 break;
184 case CDC_SET_COMM_FEATURE:
185 /* Add your code here */
186 break;
188 case CDC_GET_COMM_FEATURE:
189 /* Add your code here */
190 break;
192 case CDC_CLEAR_COMM_FEATURE:
193 /* Add your code here */
194 break;
196 case CDC_SET_LINE_CODING:
197 if (pbuf && (length == sizeof (*plc))) {
198 LineCoding.bitrate = plc->bitrate;
199 LineCoding.format = plc->format;
200 LineCoding.paritytype = plc->paritytype;
201 LineCoding.datatype = plc->datatype;
203 // If a callback is provided, tell the upper driver of changes in baud rate
204 if (baudRateCb) {
205 baudRateCb(baudRateCbContext, LineCoding.bitrate);
209 break;
211 case CDC_GET_LINE_CODING:
212 if (pbuf && (length == sizeof (*plc))) {
213 plc->bitrate = LineCoding.bitrate;
214 plc->format = LineCoding.format;
215 plc->paritytype = LineCoding.paritytype;
216 plc->datatype = LineCoding.datatype;
218 break;
220 case CDC_SET_CONTROL_LINE_STATE:
221 // If a callback is provided, tell the upper driver of changes in DTR/RTS state
222 if (pbuf && (length == sizeof (uint16_t))) {
223 if (ctrlLineStateCb) {
224 ctrlLineStateCb(ctrlLineStateCbContext, *((uint16_t *)pbuf));
227 break;
229 case CDC_SEND_BREAK:
230 /* Add your code here */
231 break;
233 default:
234 break;
237 return (USBD_OK);
241 * @brief TIM period elapsed callback
242 * @param htim: TIM handle
243 * @retval None
245 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
247 if (htim->Instance != TIMusb) return;
249 uint32_t buffptr;
250 uint32_t buffsize;
252 if (UserTxBufPtrOut != UserTxBufPtrIn)
254 if (UserTxBufPtrOut > UserTxBufPtrIn) /* Roll-back */
256 buffsize = APP_RX_DATA_SIZE - UserTxBufPtrOut;
258 else
260 buffsize = UserTxBufPtrIn - UserTxBufPtrOut;
263 buffptr = UserTxBufPtrOut;
265 USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t*)&UserTxBuffer[buffptr], buffsize);
267 if (USBD_CDC_TransmitPacket(&USBD_Device) == USBD_OK)
269 UserTxBufPtrOut += buffsize;
270 if (UserTxBufPtrOut == APP_TX_DATA_SIZE)
272 UserTxBufPtrOut = 0;
279 * @brief CDC_Itf_DataRx
280 * Data received over USB OUT endpoint are sent over CDC interface
281 * through this function.
282 * @param Buf: Buffer of data to be transmitted
283 * @param Len: Number of data received (in bytes)
284 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
286 static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len)
288 rxAvailable = *Len;
289 rxBuffPtr = Buf;
290 if (!rxAvailable) {
291 // Received an empty packet, trigger receiving the next packet.
292 // This will happen after a packet that's exactly 64 bytes is received.
293 // The USB protocol requires that an empty (0 byte) packet immediately follow.
294 USBD_CDC_ReceivePacket(&USBD_Device);
296 return (USBD_OK);
300 * @brief TIM_Config: Configure TIMusb timer
301 * @param None.
302 * @retval None
304 static void TIM_Config(void)
306 /* Set TIMusb instance */
307 TimHandle.Instance = TIMusb;
309 /* Initialize TIMx peripheral as follow:
310 + Period = 10000 - 1
311 + Prescaler = ((SystemCoreClock/2)/10000) - 1
312 + ClockDivision = 0
313 + Counter direction = Up
315 TimHandle.Init.Period = (CDC_POLLING_INTERVAL*1000) - 1;
316 TimHandle.Init.Prescaler = (SystemCoreClock / 2 / (1000000)) - 1;
317 TimHandle.Init.ClockDivision = 0;
318 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
319 if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
321 /* Initialization Error */
322 Error_Handler();
325 /*##-6- Enable TIM peripherals Clock #######################################*/
326 TIMx_CLK_ENABLE();
328 /*##-7- Configure the NVIC for TIMx ########################################*/
329 /* Set Interrupt Group Priority */
330 HAL_NVIC_SetPriority(TIMx_IRQn, 6, 0);
332 /* Enable the TIMx global Interrupt */
333 HAL_NVIC_EnableIRQ(TIMx_IRQn);
337 * @brief This function is executed in case of error occurrence.
338 * @param None
339 * @retval None
341 static void Error_Handler(void)
343 /* Add your own code here */
346 uint32_t CDC_Receive_DATA(uint8_t* recvBuf, uint32_t len)
348 uint32_t count = 0;
349 if ( (rxBuffPtr != NULL))
351 while ((rxAvailable > 0) && count < len)
353 recvBuf[count] = rxBuffPtr[0];
354 rxBuffPtr++;
355 rxAvailable--;
356 count++;
357 if (rxAvailable < 1)
358 USBD_CDC_ReceivePacket(&USBD_Device);
361 return count;
364 uint32_t CDC_Receive_BytesAvailable(void)
366 return rxAvailable;
369 uint32_t CDC_Send_FreeBytes(void)
371 uint32_t freeBytes;
373 ATOMIC_BLOCK(NVIC_PRIO_VCP) {
374 freeBytes = ((UserTxBufPtrOut - UserTxBufPtrIn) + (-((int)(UserTxBufPtrOut <= UserTxBufPtrIn)) & APP_TX_DATA_SIZE)) - 1;
377 return freeBytes;
381 * @brief CDC_Send_DATA
382 * CDC received data to be send over USB IN endpoint are managed in
383 * this function.
384 * @param ptrBuffer: Buffer of data to be sent
385 * @param sendLength: Number of data to be sent (in bytes)
386 * @retval Bytes sent
388 uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
390 USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)USBD_Device.pCDC_ClassData;
391 while (hcdc->TxState != 0);
393 for (uint32_t i = 0; i < sendLength; i++)
395 UserTxBuffer[UserTxBufPtrIn] = ptrBuffer[i];
396 UserTxBufPtrIn = (UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE;
397 while (CDC_Send_FreeBytes() == 0) {
398 delay(1);
401 return sendLength;
405 /*******************************************************************************
406 * Function Name : usbIsConfigured.
407 * Description : Determines if USB VCP is configured or not
408 * Input : None.
409 * Output : None.
410 * Return : True if configured.
411 *******************************************************************************/
412 uint8_t usbIsConfigured(void)
414 return (USBD_Device.dev_state == USBD_STATE_CONFIGURED);
417 /*******************************************************************************
418 * Function Name : usbIsConnected.
419 * Description : Determines if USB VCP is connected ot not
420 * Input : None.
421 * Output : None.
422 * Return : True if connected.
423 *******************************************************************************/
424 uint8_t usbIsConnected(void)
426 return (USBD_Device.dev_state != USBD_STATE_DEFAULT);
429 /*******************************************************************************
430 * Function Name : CDC_BaudRate.
431 * Description : Get the current baud rate
432 * Input : None.
433 * Output : None.
434 * Return : Baud rate in bps
435 *******************************************************************************/
436 uint32_t CDC_BaudRate(void)
438 return LineCoding.bitrate;
441 /*******************************************************************************
442 * Function Name : CDC_SetBaudRateCb
443 * Description : Set a callback to call when baud rate changes
444 * Input : callback function and context.
445 * Output : None.
446 * Return : None.
447 *******************************************************************************/
448 void CDC_SetBaudRateCb(void (*cb)(void *context, uint32_t baud), void *context)
450 baudRateCbContext = context;
451 baudRateCb = cb;
454 /*******************************************************************************
455 * Function Name : CDC_SetCtrlLineStateCb
456 * Description : Set a callback to call when control line state changes
457 * Input : callback function and context.
458 * Output : None.
459 * Return : None.
460 *******************************************************************************/
461 void CDC_SetCtrlLineStateCb(void (*cb)(void *context, uint16_t ctrlLineState), void *context)
463 ctrlLineStateCbContext = context;
464 ctrlLineStateCb = cb;
467 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/