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"
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 */
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
=
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
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 #################################*/
132 /*##-4- Start the TIM Base generation in interrupt mode ####################*/
134 if (HAL_TIM_Base_Start_IT(&TimHandle
) != HAL_OK
)
140 /*##-5- Set Application Buffers ############################################*/
141 USBD_CDC_SetTxBuffer(&USBD_Device
, UserTxBuffer
, 0);
142 USBD_CDC_SetRxBuffer(&USBD_Device
, UserRxBuffer
);
144 ctrlLineStateCb
= NULL
;
151 * @brief CDC_Itf_DeInit
152 * DeInitializes the CDC media low layer
154 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
156 static int8_t CDC_Itf_DeInit(void)
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
;
176 case CDC_SEND_ENCAPSULATED_COMMAND
:
177 /* Add your code here */
180 case CDC_GET_ENCAPSULATED_RESPONSE
:
181 /* Add your code here */
184 case CDC_SET_COMM_FEATURE
:
185 /* Add your code here */
188 case CDC_GET_COMM_FEATURE
:
189 /* Add your code here */
192 case CDC_CLEAR_COMM_FEATURE
:
193 /* Add your code here */
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
205 baudRateCb(baudRateCbContext
, LineCoding
.bitrate
);
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
;
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
));
230 /* Add your code here */
241 * @brief TIM period elapsed callback
242 * @param htim: TIM handle
245 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef
*htim
)
247 if (htim
->Instance
!= TIMusb
) return;
252 if (UserTxBufPtrOut
!= UserTxBufPtrIn
)
254 if (UserTxBufPtrOut
> UserTxBufPtrIn
) /* Roll-back */
256 buffsize
= APP_RX_DATA_SIZE
- UserTxBufPtrOut
;
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
)
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
)
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
);
300 * @brief TIM_Config: Configure TIMusb timer
304 static void TIM_Config(void)
306 /* Set TIMusb instance */
307 TimHandle
.Instance
= TIMusb
;
309 /* Initialize TIMx peripheral as follow:
311 + Prescaler = ((SystemCoreClock/2)/10000) - 1
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 */
325 /*##-6- Enable TIM peripherals Clock #######################################*/
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.
341 static void Error_Handler(void)
343 /* Add your own code here */
346 uint32_t CDC_Receive_DATA(uint8_t* recvBuf
, uint32_t len
)
349 if ( (rxBuffPtr
!= NULL
))
351 while ((rxAvailable
> 0) && count
< len
)
353 recvBuf
[count
] = rxBuffPtr
[0];
358 USBD_CDC_ReceivePacket(&USBD_Device
);
364 uint32_t CDC_Receive_BytesAvailable(void)
369 uint32_t CDC_Send_FreeBytes(void)
373 ATOMIC_BLOCK(NVIC_PRIO_VCP
) {
374 freeBytes
= ((UserTxBufPtrOut
- UserTxBufPtrIn
) + (-((int)(UserTxBufPtrOut
<= UserTxBufPtrIn
)) & APP_TX_DATA_SIZE
)) - 1;
381 * @brief CDC_Send_DATA
382 * CDC received data to be send over USB IN endpoint are managed in
384 * @param ptrBuffer: Buffer of data to be sent
385 * @param sendLength: Number of data to be sent (in bytes)
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) {
405 /*******************************************************************************
406 * Function Name : usbIsConfigured.
407 * Description : Determines if USB VCP is configured or not
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
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
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.
447 *******************************************************************************/
448 void CDC_SetBaudRateCb(void (*cb
)(void *context
, uint32_t baud
), void *context
)
450 baudRateCbContext
= context
;
454 /*******************************************************************************
455 * Function Name : CDC_SetCtrlLineStateCb
456 * Description : Set a callback to call when control line state changes
457 * Input : callback function and context.
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****/