Skip to content

Commit 5ff5414

Browse files
authored
Merge pull request #3059 from cumin777/feat/stm32c5-halv2-usb
feat(usb): add STM32C5 HAL v2 USB device support
2 parents 5ac665e + 8adf1fc commit 5ff5414

10 files changed

Lines changed: 312 additions & 16 deletions

File tree

‎cores/arduino/stm32/stm32yyxx_hal_conf.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@
148148
#undef USE_HAL_PCD_MODULE
149149
#endif
150150
#define USE_HAL_PCD_MODULE 1U
151+
#if defined(USBD_CDC_USE_SINGLE_BUFFER)
152+
#define USE_HAL_PCD_USB_DOUBLE_BUFFER 0U
153+
#else
154+
#define USE_HAL_PCD_USB_DOUBLE_BUFFER 1U
155+
#endif
151156
#endif
152157

153158
#else /* HALv1 */

‎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/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_library(USBDevice_bin OBJECT EXCLUDE_FROM_ALL
2828
src/usb_device_ctlreq.c
2929
src/usb_device_ioreq.c
3030
src/usbd_conf.c
31+
src/usbd_confv2.c
3132
src/usbd_desc.c
3233
src/usbd_ep_conf.c
3334
src/usbd_if.c

‎libraries/USBDevice/inc/usbd_conf.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ extern "C" {
2929
/* Includes ------------------------------------------------------------------*/
3030
#include "stm32_def.h"
3131

32-
#if defined(USE_HALV2_DRIVER)
33-
#error "USB library is not yet compatible with HALv2 driver."
34-
#else
3532
#if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE)
3633
#error "This board does not support USB! Select 'None' in the 'Tools->USB interface' menu"
3734
#endif
@@ -40,8 +37,6 @@ extern "C" {
4037
#endif
4138
#if !defined(USB_BASE) && !defined(USB_OTG_FS) && defined(USB_OTG_HS) && !defined(USE_USB_HS)
4239
#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-
4540
#endif
4641
#include <stdio.h>
4742
#include <stdlib.h>
@@ -77,7 +72,7 @@ extern "C" {
7772
#elif defined(STM32G0B1xx) || defined(STM32G0C1xx)
7873
#define USB_IRQn USB_UCPD1_2_IRQn
7974
#define USB_IRQHandler USB_UCPD1_2_IRQHandler
80-
#elif defined(STM32C0xx) || defined(STM32H5xx) || defined(STM32U0xx)
75+
#elif defined(STM32C0xx) || defined(STM32C5xx) || defined(STM32H5xx) || defined(STM32U0xx)
8176
#define USB_IRQn USB_DRD_FS_IRQn
8277
#define USB_IRQHandler USB_DRD_FS_IRQHandler
8378
#elif defined(STM32U5xx) && !defined(USB_DRD_FS)
@@ -90,6 +85,13 @@ extern "C" {
9085

9186
#endif /* USB_BASE */
9287

88+
#if !defined(PCD_SNG_BUF) && defined(HAL_PCD_SNG_BUF)
89+
#define PCD_SNG_BUF HAL_PCD_SNG_BUF
90+
#endif
91+
#if !defined(PCD_DBL_BUF) && defined(HAL_PCD_DBL_BUF)
92+
#define PCD_DBL_BUF HAL_PCD_DBL_BUF
93+
#endif
94+
9395
#ifndef __HAL_PCD_GATE_PHYCLOCK
9496
#define __HAL_PCD_GATE_PHYCLOCK(_DUMMY_)
9597
#endif

‎libraries/USBDevice/inc/usbd_ep_conf.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <stdint.h>
2626
#include "usbd_def.h"
2727

28+
#if defined (USE_HAL_PCD_USB_DOUBLE_BUFFER) && (USE_HAL_PCD_USB_DOUBLE_BUFFER == 0)
29+
#define USBD_CDC_USE_SINGLE_BUFFER
30+
#endif
31+
2832
typedef struct {
2933
uint32_t ep_adress; /* Endpoint address */
3034
uint32_t ep_size; /* Endpoint size */
@@ -69,9 +73,14 @@ typedef struct {
6973

7074
#ifdef USBD_USE_CDC
7175
#define PMA_CDC_OUT_BASE (PMA_EP0_IN_ADDR + USB_MAX_EP0_SIZE)
76+
#if !defined(USBD_CDC_USE_SINGLE_BUFFER)
7277
#define PMA_CDC_OUT_ADDR ((PMA_CDC_OUT_BASE + USB_FS_MAX_PACKET_SIZE) | \
7378
(PMA_CDC_OUT_BASE << 16U))
7479
#define PMA_CDC_IN_ADDR (PMA_CDC_OUT_BASE + USB_FS_MAX_PACKET_SIZE * 2)
80+
#else
81+
#define PMA_CDC_OUT_ADDR PMA_CDC_OUT_BASE
82+
#define PMA_CDC_IN_ADDR (PMA_CDC_OUT_BASE + USB_FS_MAX_PACKET_SIZE)
83+
#endif
7584
#define PMA_CDC_CMD_ADDR (PMA_CDC_IN_ADDR + CDC_CMD_PACKET_SIZE)
7685
#endif /* USBD_USE_CDC */
7786
#ifdef USBD_USE_HID_COMPOSITE
@@ -86,4 +95,4 @@ extern const ep_desc_t ep_def[DEV_NUM_EP + 1];
8695
#endif /* USBCON */
8796
#endif /* __USBD_EP_CONF_H */
8897

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

‎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/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)