2 ******************************************************************************
3 * @file USB_Device/CDC_Standalone/Src/usbd_cdc_interface.c
4 * @author MCD Application Team
7 * @brief Source file for USBD CDC interface
8 ******************************************************************************
11 * <h2><center>© 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
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 ------------------------------------------------------------------*/
51 #include "usbd_core.h"
52 #include "usbd_desc.h"
54 #include "usbd_cdc_interface.h"
56 #include "drivers/time.h"
58 /* Private typedef -----------------------------------------------------------*/
59 /* Private define ------------------------------------------------------------*/
60 #define APP_RX_DATA_SIZE 4096
61 #define APP_TX_DATA_SIZE 4096
63 /* Private macro -------------------------------------------------------------*/
64 /* Private variables ---------------------------------------------------------*/
65 USBD_CDC_LineCodingTypeDef LineCoding
=
67 115200, /* baud rate*/
68 0x00, /* stop bits-1*/
69 0x00, /* parity - none*/
70 0x08 /* nb. of bits 8*/
73 uint8_t UserRxBuffer
[APP_RX_DATA_SIZE
];/* Received Data over USB are stored in this buffer */
74 uint8_t UserTxBuffer
[APP_TX_DATA_SIZE
];/* Received Data over UART (CDC interface) are stored in this buffer */
76 uint32_t UserTxBufPtrIn
= 0;/* Increment this pointer or roll it back to
77 start address when data are received over USART */
78 uint32_t UserTxBufPtrOut
= 0; /* Increment this pointer or roll it back to
79 start address when data are sent over USB */
81 uint32_t rxAvailable
= 0;
82 uint8_t* rxBuffPtr
= NULL
;
84 /* TIM handler declaration */
85 TIM_HandleTypeDef TimHandle
;
86 /* USB handler declaration */
87 extern USBD_HandleTypeDef USBD_Device
;
89 static void (*ctrlLineStateCb
)(void *context
, uint16_t ctrlLineState
);
90 static void *ctrlLineStateCbContext
;
91 static void (*baudRateCb
)(void *context
, uint32_t baud
);
92 static void *baudRateCbContext
;
94 /* Private function prototypes -----------------------------------------------*/
95 static int8_t CDC_Itf_Init(void);
96 static int8_t CDC_Itf_DeInit(void);
97 static int8_t CDC_Itf_Control(uint8_t cmd
, uint8_t* pbuf
, uint16_t length
);
98 static int8_t CDC_Itf_Receive(uint8_t* pbuf
, uint32_t *Len
);
100 static void TIM_Config(void);
101 static void Error_Handler(void);
103 USBD_CDC_ItfTypeDef USBD_CDC_fops
=
112 void TIMx_IRQHandler(void)
114 HAL_TIM_IRQHandler(&TimHandle
);
117 /* Private functions ---------------------------------------------------------*/
120 * @brief CDC_Itf_Init
121 * Initializes the CDC media low layer
123 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
125 static int8_t CDC_Itf_Init(void)
127 /*##-3- Configure the TIM Base generation #################################*/
130 /*##-4- Start the TIM Base generation in interrupt mode ####################*/
132 if (HAL_TIM_Base_Start_IT(&TimHandle
) != HAL_OK
)
138 /*##-5- Set Application Buffers ############################################*/
139 USBD_CDC_SetTxBuffer(&USBD_Device
, UserTxBuffer
, 0);
140 USBD_CDC_SetRxBuffer(&USBD_Device
, UserRxBuffer
);
142 ctrlLineStateCb
= NULL
;
149 * @brief CDC_Itf_DeInit
150 * DeInitializes the CDC media low layer
152 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
154 static int8_t CDC_Itf_DeInit(void)
161 * @brief CDC_Itf_Control
162 * Manage the CDC class requests
163 * @param Cmd: Command code
164 * @param Buf: Buffer containing command data (request parameters)
165 * @param Len: Number of data to be sent (in bytes)
166 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
168 static int8_t CDC_Itf_Control (uint8_t cmd
, uint8_t* pbuf
, uint16_t length
)
170 LINE_CODING
* plc
= (LINE_CODING
*)pbuf
;
174 case CDC_SEND_ENCAPSULATED_COMMAND
:
175 /* Add your code here */
178 case CDC_GET_ENCAPSULATED_RESPONSE
:
179 /* Add your code here */
182 case CDC_SET_COMM_FEATURE
:
183 /* Add your code here */
186 case CDC_GET_COMM_FEATURE
:
187 /* Add your code here */
190 case CDC_CLEAR_COMM_FEATURE
:
191 /* Add your code here */
194 case CDC_SET_LINE_CODING
:
195 if (pbuf
&& (length
== sizeof (*plc
))) {
196 LineCoding
.bitrate
= plc
->bitrate
;
197 LineCoding
.format
= plc
->format
;
198 LineCoding
.paritytype
= plc
->paritytype
;
199 LineCoding
.datatype
= plc
->datatype
;
201 // If a callback is provided, tell the upper driver of changes in baud rate
203 baudRateCb(baudRateCbContext
, LineCoding
.bitrate
);
209 case CDC_GET_LINE_CODING
:
210 if (pbuf
&& (length
== sizeof (*plc
))) {
211 plc
->bitrate
= LineCoding
.bitrate
;
212 plc
->format
= LineCoding
.format
;
213 plc
->paritytype
= LineCoding
.paritytype
;
214 plc
->datatype
= LineCoding
.datatype
;
218 case CDC_SET_CONTROL_LINE_STATE
:
219 // If a callback is provided, tell the upper driver of changes in DTR/RTS state
220 if (pbuf
&& (length
== sizeof (uint16_t))) {
221 if (ctrlLineStateCb
) {
222 ctrlLineStateCb(ctrlLineStateCbContext
, *((uint16_t *)pbuf
));
228 /* Add your code here */
239 * @brief TIM period elapsed callback
240 * @param htim: TIM handle
243 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef
*htim
)
245 if (htim
->Instance
!= TIMusb
) return;
250 if (UserTxBufPtrOut
!= UserTxBufPtrIn
)
252 if (UserTxBufPtrOut
> UserTxBufPtrIn
) /* Roll-back */
254 buffsize
= APP_RX_DATA_SIZE
- UserTxBufPtrOut
;
258 buffsize
= UserTxBufPtrIn
- UserTxBufPtrOut
;
261 buffptr
= UserTxBufPtrOut
;
263 USBD_CDC_SetTxBuffer(&USBD_Device
, (uint8_t*)&UserTxBuffer
[buffptr
], buffsize
);
265 if (USBD_CDC_TransmitPacket(&USBD_Device
) == USBD_OK
)
267 UserTxBufPtrOut
+= buffsize
;
268 if (UserTxBufPtrOut
== APP_TX_DATA_SIZE
)
277 * @brief CDC_Itf_DataRx
278 * Data received over USB OUT endpoint are sent over CDC interface
279 * through this function.
280 * @param Buf: Buffer of data to be transmitted
281 * @param Len: Number of data received (in bytes)
282 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
284 static int8_t CDC_Itf_Receive(uint8_t* Buf
, uint32_t *Len
)
289 // Received an empty packet, trigger receiving the next packet.
290 // This will happen after a packet that's exactly 64 bytes is received.
291 // The USB protocol requires that an empty (0 byte) packet immediately follow.
292 USBD_CDC_ReceivePacket(&USBD_Device
);
298 * @brief TIM_Config: Configure TIMusb timer
302 static void TIM_Config(void)
304 /* Set TIMusb instance */
305 TimHandle
.Instance
= TIMusb
;
307 /* Initialize TIMx peripheral as follow:
309 + Prescaler = ((SystemCoreClock/2)/10000) - 1
311 + Counter direction = Up
313 TimHandle
.Init
.Period
= (CDC_POLLING_INTERVAL
*1000) - 1;
314 TimHandle
.Init
.Prescaler
= (SystemCoreClock
/ 2 / (1000000)) - 1;
315 TimHandle
.Init
.ClockDivision
= 0;
316 TimHandle
.Init
.CounterMode
= TIM_COUNTERMODE_UP
;
317 if (HAL_TIM_Base_Init(&TimHandle
) != HAL_OK
)
319 /* Initialization Error */
323 /*##-6- Enable TIM peripherals Clock #######################################*/
326 /*##-7- Configure the NVIC for TIMx ########################################*/
327 /* Set Interrupt Group Priority */
328 HAL_NVIC_SetPriority(TIMx_IRQn
, 6, 0);
330 /* Enable the TIMx global Interrupt */
331 HAL_NVIC_EnableIRQ(TIMx_IRQn
);
335 * @brief This function is executed in case of error occurrence.
339 static void Error_Handler(void)
341 /* Add your own code here */
344 uint32_t CDC_Receive_DATA(uint8_t* recvBuf
, uint32_t len
)
347 if ( (rxBuffPtr
!= NULL
))
349 while ((rxAvailable
> 0) && count
< len
)
351 recvBuf
[count
] = rxBuffPtr
[0];
356 USBD_CDC_ReceivePacket(&USBD_Device
);
362 uint32_t CDC_Receive_BytesAvailable(void)
367 uint32_t CDC_Send_FreeBytes(void)
370 return the bytes free in the circular buffer
372 functionally equivalent to:
373 (APP_Rx_ptr_out > APP_Rx_ptr_in ? APP_Rx_ptr_out - APP_Rx_ptr_in : APP_RX_DATA_SIZE - APP_Rx_ptr_in + APP_Rx_ptr_in)
374 but without the impact of the condition check.
376 return ((UserTxBufPtrOut
- UserTxBufPtrIn
) + (-((int)(UserTxBufPtrOut
<= UserTxBufPtrIn
)) & APP_TX_DATA_SIZE
)) - 1;
380 * @brief CDC_Send_DATA
381 * CDC received data to be send over USB IN endpoint are managed in
383 * @param ptrBuffer: Buffer of data to be sent
384 * @param sendLength: Number of data to be sent (in bytes)
387 uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer
, uint32_t sendLength
)
389 USBD_CDC_HandleTypeDef
*hcdc
= (USBD_CDC_HandleTypeDef
*)USBD_Device
.pCDC_ClassData
;
390 while (hcdc
->TxState
!= 0);
392 for (uint32_t i
= 0; i
< sendLength
; i
++)
394 UserTxBuffer
[UserTxBufPtrIn
] = ptrBuffer
[i
];
395 UserTxBufPtrIn
= (UserTxBufPtrIn
+ 1) % APP_TX_DATA_SIZE
;
396 while (CDC_Send_FreeBytes() == 0) {
404 /*******************************************************************************
405 * Function Name : usbIsConfigured.
406 * Description : Determines if USB VCP is configured or not
409 * Return : True if configured.
410 *******************************************************************************/
411 uint8_t usbIsConfigured(void)
413 return (USBD_Device
.dev_state
== USBD_STATE_CONFIGURED
);
416 /*******************************************************************************
417 * Function Name : usbIsConnected.
418 * Description : Determines if USB VCP is connected ot not
421 * Return : True if connected.
422 *******************************************************************************/
423 uint8_t usbIsConnected(void)
425 return (USBD_Device
.dev_state
!= USBD_STATE_DEFAULT
);
428 /*******************************************************************************
429 * Function Name : CDC_BaudRate.
430 * Description : Get the current baud rate
433 * Return : Baud rate in bps
434 *******************************************************************************/
435 uint32_t CDC_BaudRate(void)
437 return LineCoding
.bitrate
;
440 /*******************************************************************************
441 * Function Name : CDC_SetBaudRateCb
442 * Description : Set a callback to call when baud rate changes
443 * Input : callback function and context.
446 *******************************************************************************/
447 void CDC_SetBaudRateCb(void (*cb
)(void *context
, uint32_t baud
), void *context
)
449 baudRateCbContext
= context
;
453 /*******************************************************************************
454 * Function Name : CDC_SetCtrlLineStateCb
455 * Description : Set a callback to call when control line state changes
456 * Input : callback function and context.
459 *******************************************************************************/
460 void CDC_SetCtrlLineStateCb(void (*cb
)(void *context
, uint16_t ctrlLineState
), void *context
)
462 ctrlLineStateCbContext
= context
;
463 ctrlLineStateCb
= cb
;
466 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/