Fixed USB for STM32C0xx (for HAL conf v9 and RT conf v8)

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
Engemil
Posts: 34
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 11 times
Been thanked: 8 times

Fixed USB for STM32C0xx (for HAL conf v9 and RT conf v8)

Postby Engemil » Tue Dec 16, 2025 10:56 pm

Hi!

Based on development version of ChibiOS (github commit c474a2d75d31ce7e551c6ae69ba75dfdc95a9b7c) (to become version 21.11.5??), I fixed problems with USB for STM32C0xx, with for HAL config file version 9, RT config file version 8, and newest mcuconf for STM32C071RB.

Updated STM32C0xx port files.

I confirm it works on STM32C071RB. Hopefully there isn't anything else I missed, which might be important.

Code: Select all

From d1ab6f5d4087e1e4954484593e50ebb841b2d159 Mon Sep 17 00:00:00 2001
From: engemil <engemil@users.noreply.github.com>
Date: Tue, 16 Dec 2025 22:38:41 +0100
Subject: [PATCH] STM32C0xx USB fix

---
 os/hal/ports/STM32/STM32C0xx/hal_lld.c        |  2 +-
 os/hal/ports/STM32/STM32C0xx/hal_lld.h        |  4 +--
 os/hal/ports/STM32/STM32C0xx/stm32_isr.c      |  6 ++++
 os/hal/ports/STM32/STM32C0xx/stm32_isr.h      | 30 ++++++++++---------
 os/hal/ports/STM32/STM32C0xx/stm32_registry.h | 28 +++++++++--------
 5 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/os/hal/ports/STM32/STM32C0xx/hal_lld.c b/os/hal/ports/STM32/STM32C0xx/hal_lld.c
index 298e06b869..cc74465857 100644
--- a/os/hal/ports/STM32/STM32C0xx/hal_lld.c
+++ b/os/hal/ports/STM32/STM32C0xx/hal_lld.c
@@ -205,7 +205,7 @@ static void hal_lld_set_static_clocks(void) {
                  STM32_ADCSEL | STM32_I2S1SEL | STM32_I2C1SEL | STM32_FDCAN1SEL | STM32_USART1SEL,
                  true);
 
-#if STM32_HAS_USB
+#if STM32_HAS_USB1
   /* CCIPR2 register initialization.*/
   halRegWrite32X(&RCC->CCIPR2, STM32_USBSEL, true);
 #endif
diff --git a/os/hal/ports/STM32/STM32C0xx/hal_lld.h b/os/hal/ports/STM32/STM32C0xx/hal_lld.h
index 563f336ec5..d22855ad61 100644
--- a/os/hal/ports/STM32/STM32C0xx/hal_lld.h
+++ b/os/hal/ports/STM32/STM32C0xx/hal_lld.h
@@ -785,7 +785,7 @@
     #error "HSE not enabled, required by STM32_RTCSEL"
   #endif
 
-  #if STM32_HAS_USB
+  #if STM32_HAS_USB1
     #if STM32_USBSEL == RCC_CCIPR2_USBSEL_HSE
       #error "HSE not enabled, required by STM32_USBSEL"
     #endif
@@ -1216,7 +1216,7 @@
  * @brief   USB clock frequency.
  */
 #if (STM32_USBSEL == RCC_CCIPR2_USBSEL_HSIUSB48) || defined(__DOXYGEN__)
-  #define STM32_USBCLK                      hal_lld_get_clock_point(CLK_HSIUSB48CLK)
+  #define STM32_USBCLK                      hal_lld_get_clock_point(CLK_HSIUSB48)
 
 #elif STM32_USBSEL == RCC_CCIPR2_USBSEL_HSE
   #define STM32_USBCLK                      STM32_HSECLK
diff --git a/os/hal/ports/STM32/STM32C0xx/stm32_isr.c b/os/hal/ports/STM32/STM32C0xx/stm32_isr.c
index bb2bca8166..ef463fa39b 100644
--- a/os/hal/ports/STM32/STM32C0xx/stm32_isr.c
+++ b/os/hal/ports/STM32/STM32C0xx/stm32_isr.c
@@ -68,6 +68,8 @@
 #include "stm32_tim16.inc"
 #include "stm32_tim17.inc"
 
+#include "stm32_usb1.inc"
+
 /*===========================================================================*/
 /* Driver exported functions.                                                */
 /*===========================================================================*/
@@ -93,6 +95,8 @@ void irqInit(void) {
 
   usart1_irq_init();
   usart2_irq_init();
+
+  usb1_irq_init();
 }
 
 /**
@@ -116,6 +120,8 @@ void irqDeinit(void) {
 
   usart1_irq_deinit();
   usart2_irq_deinit();
+
+  usb1_irq_deinit();
 }
 
 /** @} */
diff --git a/os/hal/ports/STM32/STM32C0xx/stm32_isr.h b/os/hal/ports/STM32/STM32C0xx/stm32_isr.h
index 2eaf50b098..9890e1c5d1 100644
--- a/os/hal/ports/STM32/STM32C0xx/stm32_isr.h
+++ b/os/hal/ports/STM32/STM32C0xx/stm32_isr.h
@@ -123,20 +123,22 @@
  */
 #define STM32_USART1_HANDLER                VectorAC
 #define STM32_USART2_HANDLER                VectorB0
-
-#define STM32_USART1_NUMBER                 27
-#define STM32_USART2_NUMBER                 28
-
-/*
- * USB units.
- */
-#define STM32_USB1_HP_HANDLER               Vector60
-#define STM32_USB1_LP_HANDLER               Vector60
-#define STM32_USB1_HP_NUMBER                8
-#define STM32_USB1_LP_NUMBER                8
-/** @} */
-
-/*===========================================================================*/
+
+#define STM32_USART1_NUMBER                 27
+#define STM32_USART2_NUMBER                 28
+
+/*
+ * USB units.
+ */
+//#define STM32_USB1_HP_HANDLER               Vector60
+//#define STM32_USB1_LP_HANDLER               Vector60
+//#define STM32_USB1_HP_NUMBER                8
+//#define STM32_USB1_LP_NUMBER                8
+#define STM32_USB1_HANDLER               Vector60
+#define STM32_USB1_NUMBER                8
+/** @} */
+
+/*===========================================================================*/
 /* Driver pre-compile time settings.                                         */
 /*===========================================================================*/
 
diff --git a/os/hal/ports/STM32/STM32C0xx/stm32_registry.h b/os/hal/ports/STM32/STM32C0xx/stm32_registry.h
index 6e1125f0e9..f24113bbbf 100644
--- a/os/hal/ports/STM32/STM32C0xx/stm32_registry.h
+++ b/os/hal/ports/STM32/STM32C0xx/stm32_registry.h
@@ -174,9 +174,10 @@
 #define STM32_HAS_I2C4                      FALSE
 
 /* OTG/USB attributes.*/
-#define STM32_HAS_OTG1                      FALSE
-#define STM32_HAS_OTG2                      FALSE
-#define STM32_HAS_USB                       FALSE
+//#define STM32_HAS_OTG1                      FALSE
+//#define STM32_HAS_OTG2                      FALSE
+//#define STM32_HAS_USB                       FALSE
+#define STM32_HAS_USB1                       FALSE
 
 /* RCC attributes.*/
 #define STM32_RCC_HAS_HSIUSB48              FALSE
@@ -325,9 +326,10 @@
 #define STM32_HAS_I2C4                      FALSE
 
 /* OTG/USB attributes.*/
-#define STM32_HAS_OTG1                      FALSE
-#define STM32_HAS_OTG2                      FALSE
-#define STM32_HAS_USB                       FALSE
+//#define STM32_HAS_OTG1                      FALSE
+//#define STM32_HAS_OTG2                      FALSE
+//#define STM32_HAS_USB                       FALSE
+#define STM32_HAS_USB1                       FALSE
 
 /* RCC attributes.*/
 #define STM32_RCC_HAS_HSIUSB48              FALSE
@@ -481,10 +483,11 @@
 #define STM32_HAS_I2C4                      FALSE
 
 /* OTG/USB attributes.*/
-#define STM32_HAS_OTG1                      FALSE
-#define STM32_HAS_OTG2                      FALSE
+//#define STM32_HAS_OTG1                      FALSE
+//#define STM32_HAS_OTG2                      FALSE
 
-#define STM32_HAS_USB                       TRUE
+#define STM32_HAS_USB1                      TRUE
+//#define STM32_HAS_USB                       TRUE
 #define STM32_USB_ACCESS_SCHEME_2x16        TRUE
 #define STM32_USB_PMA_SIZE                  2048
 #define STM32_USB_HAS_BCDR                  TRUE
@@ -645,9 +648,10 @@
 #define STM32_HAS_I2C4                      FALSE
 
 /* OTG/USB attributes.*/
-#define STM32_HAS_OTG1                      FALSE
-#define STM32_HAS_OTG2                      FALSE
-#define STM32_HAS_USB                       FALSE
+//#define STM32_HAS_OTG1                      FALSE
+//#define STM32_HAS_OTG2                      FALSE
+//#define STM32_HAS_USB                       FALSE
+#define STM32_HAS_USB1                       FALSE
 
 /* RCC attributes.*/
 #define STM32_RCC_HAS_HSIUSB48              FALSE

User avatar
Giovanni
Site Admin
Posts: 14779
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1174 times
Been thanked: 976 times

Re: Fixed USB for STM32C0xx (for HAL conf v9 and RT conf v8)

Postby Giovanni » Wed Dec 17, 2025 8:26 am

Hi,

Thanks for the patch and testing, I cannot test on C071 right now because no board.

I confirm, the current trunk HAL is what you will see in 21.11.5, hopefully next month.

Giovanni


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 30 guests