Skip to content

Commit 23c243f

Browse files
committed
feat(usb): add STM32C5 HAL v2 USB device support
1 parent bb4b804 commit 23c243f

11 files changed

Lines changed: 318 additions & 15 deletions

File tree

libraries/SrcWrapper/inc/stm32_def.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,13 @@ __STATIC_INLINE void LL_RTC_SetBinMixBCDU(RTC_TypeDef *RTCx, uint32_t BinMixBcdU
203203
}
204204
#endif // STM32U0xx
205205

206-
/* STM32G0xx, STM32U0xx and some STM32U5xx defined USB_DRD_FS */
206+
/* Some STM32 series define USB_DRD_FS instead of the legacy USB instance. */
207207
#if !defined(USB) && defined(USB_DRD_FS)
208208
#define USB USB_DRD_FS
209209
#define PinMap_USB PinMap_USB_DRD_FS
210-
#if defined(STM32H5xx) || defined(STM32U0xx) ||\
210+
#if defined(STM32C5xx)
211+
#define USB_BASE USB_DRD_FS_BASE
212+
#elif defined(STM32H5xx) || defined(STM32U0xx) ||\
211213
defined(STM32U3xx) || defined(STM32U5xx)
212214
#define USB_BASE USB_DRD_BASE
213215
#if !defined(__HAL_RCC_USB_CLK_ENABLE)

libraries/USBDevice/inc/usbd_cdc_if.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ uint8_t CDC_getStopBits(void);
5959
uint8_t CDC_getParity(void);
6060
uint8_t CDC_getDataBits(void);
6161

62+
/* Optional board hook invoked after the host changes the CDC line coding. */
63+
void CDC_LineCodingChanged(uint32_t bitrate);
64+
6265
#ifdef __cplusplus
6366
}
6467
#endif

libraries/USBDevice/inc/usbd_conf.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ extern "C" {
3030
#include "stm32_def.h"
3131

3232
#if defined(USE_HALV2_DRIVER)
33-
#error "USB library is not yet compatible with HALv2 driver."
34-
#else
33+
#if !defined(STM32C5xx)
34+
#warning "USB device support is currently implemented only for STM32C5xx HAL v2."
35+
#endif
36+
#endif
37+
38+
#if defined(STM32C5xx)
39+
#define PCD_SNG_BUF HAL_PCD_SNG_BUF
40+
#define PCD_DBL_BUF HAL_PCD_DBL_BUF
41+
#endif
42+
3543
#if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE)
3644
#error "This board does not support USB! Select 'None' in the 'Tools->USB interface' menu"
3745
#endif
@@ -40,8 +48,6 @@ extern "C" {
4048
#endif
4149
#if !defined(USB_BASE) && !defined(USB_OTG_FS) && defined(USB_OTG_HS) && !defined(USE_USB_HS)
4250
#error "This board support only USB High Speed! Select 'High Speed' or 'High Speed in Full Speed mode' in the 'Tools->USB interface' menu"
43-
#endif
44-
4551
#endif
4652
#include <stdio.h>
4753
#include <stdlib.h>
@@ -77,7 +83,7 @@ extern "C" {
7783
#elif defined(STM32G0B1xx) || defined(STM32G0C1xx)
7884
#define USB_IRQn USB_UCPD1_2_IRQn
7985
#define USB_IRQHandler USB_UCPD1_2_IRQHandler
80-
#elif defined(STM32C0xx) || defined(STM32H5xx) || defined(STM32U0xx)
86+
#elif defined(STM32C0xx) || defined(STM32C5xx) || defined(STM32H5xx) || defined(STM32U0xx)
8187
#define USB_IRQn USB_DRD_FS_IRQn
8288
#define USB_IRQHandler USB_DRD_FS_IRQHandler
8389
#elif defined(STM32U5xx) && !defined(USB_DRD_FS)

libraries/USBDevice/inc/usbd_ep_conf.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,21 @@ typedef struct {
6969

7070
#ifdef USBD_USE_CDC
7171
#define PMA_CDC_OUT_BASE (PMA_EP0_IN_ADDR + USB_MAX_EP0_SIZE)
72+
#if defined(STM32C5xx)
73+
/*
74+
* The C5 USB DRD FS peripheral is used with bulk double buffering disabled.
75+
* Keep each CDC endpoint in its own 64-byte PMA area; in particular, the
76+
* interrupt IN endpoint must not overlap the bulk IN data buffer.
77+
*/
78+
#define PMA_CDC_OUT_ADDR PMA_CDC_OUT_BASE
79+
#define PMA_CDC_IN_ADDR (PMA_CDC_OUT_BASE + USB_FS_MAX_PACKET_SIZE)
80+
#define PMA_CDC_CMD_ADDR (PMA_CDC_IN_ADDR + USB_FS_MAX_PACKET_SIZE)
81+
#else
7282
#define PMA_CDC_OUT_ADDR ((PMA_CDC_OUT_BASE + USB_FS_MAX_PACKET_SIZE) | \
7383
(PMA_CDC_OUT_BASE << 16U))
7484
#define PMA_CDC_IN_ADDR (PMA_CDC_OUT_BASE + USB_FS_MAX_PACKET_SIZE * 2)
7585
#define PMA_CDC_CMD_ADDR (PMA_CDC_IN_ADDR + CDC_CMD_PACKET_SIZE)
86+
#endif
7687
#endif /* USBD_USE_CDC */
7788
#ifdef USBD_USE_HID_COMPOSITE
7889
#define PMA_MOUSE_IN_ADDR (PMA_EP0_IN_ADDR + HID_MOUSE_EPIN_SIZE)
@@ -86,4 +97,4 @@ extern const ep_desc_t ep_def[DEV_NUM_EP + 1];
8697
#endif /* USBCON */
8798
#endif /* __USBD_EP_CONF_H */
8899

89-
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
100+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

libraries/USBDevice/src/cdc/cdc_queue.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue,
8585
} else {
8686
*size = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read;
8787
}
88+
89+
/* Limit the queued transaction to one endpoint packet. */
90+
if (*size > CDC_QUEUE_MAX_PACKET_SIZE) {
91+
*size = CDC_QUEUE_MAX_PACKET_SIZE;
92+
}
8893
queue->reserved = *size;
8994
return &queue->buffer[queue->read];
9095
}

libraries/USBDevice/src/cdc/usbd_cdc.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,11 @@ static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
701701
static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
702702
{
703703
USBD_CDC_HandleTypeDef *hcdc;
704+
#if defined(USE_HALV2_DRIVER)
705+
hal_pcd_handle_t *hpcd = (hal_pcd_handle_t *)pdev->pData;
706+
#else
704707
PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData;
708+
#endif
705709

706710
if (pdev->pClassDataCmsit[pdev->classId] == NULL) {
707711
return (uint8_t)USBD_FAIL;
@@ -710,7 +714,13 @@ static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
710714
hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
711715

712716
if ((pdev->ep_in[epnum & 0xFU].total_length > 0U) &&
713-
((pdev->ep_in[epnum & 0xFU].total_length % hpcd->IN_ep[epnum & 0xFU].maxpacket) == 0U)) {
717+
((pdev->ep_in[epnum & 0xFU].total_length %
718+
#if defined(USE_HALV2_DRIVER)
719+
hpcd->in_ep[epnum & 0xFU].max_packet
720+
#else
721+
hpcd->IN_ep[epnum & 0xFU].maxpacket
722+
#endif
723+
) == 0U)) {
714724
/* Update the packet total length */
715725
pdev->ep_in[epnum & 0xFU].total_length = 0U;
716726

libraries/USBDevice/src/cdc/usbd_cdc_if.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ USBD_CDC_LineCodingTypeDef linecoding = {
8989
0x08 /* nb. of bits 8*/
9090
};
9191

92+
/* Boards that need a CDC line-coding side effect can override this hook. */
93+
WEAK void CDC_LineCodingChanged(uint32_t bitrate)
94+
{
95+
(void)bitrate;
96+
}
97+
9298
/* Private functions ---------------------------------------------------------*/
9399

94100
/**
@@ -175,6 +181,7 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
175181
linecoding.format = pbuf[4];
176182
linecoding.paritytype = pbuf[5];
177183
linecoding.datatype = pbuf[6];
184+
CDC_LineCodingChanged(linecoding.bitrate);
178185
break;
179186

180187
case CDC_GET_LINE_CODING:
@@ -406,4 +413,3 @@ uint8_t CDC_getDataBits(void)
406413
#endif /* USBD_USE_CDC */
407414
#endif /* USBCON */
408415
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
409-

libraries/USBDevice/src/usbd_conf.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
******************************************************************************
1818
*/
19-
#ifdef USBCON
19+
#if defined(USBCON) && !defined(USE_HALV2_DRIVER)
2020
/* Includes ------------------------------------------------------------------*/
2121
#include "usbd_core.h"
2222
#include "usbd_if.h"
@@ -718,6 +718,5 @@ void USBD_LL_Delay(uint32_t Delay)
718718
HAL_Delay(Delay);
719719
}
720720
#endif /* HAL_PCD_MODULE_ENABLED */
721-
#endif /* USBCON */
721+
#endif /* USBCON && !USE_HALV2_DRIVER */
722722
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
723-

0 commit comments

Comments
 (0)