Skip to content

Commit c7923b1

Browse files
committed
stm32: Add support for USB on G0 MCUs.
Signed-off-by: Damien George <damien@micropython.org>
1 parent b7ea90d commit c7923b1

File tree

6 files changed

+91
-44
lines changed

6 files changed

+91
-44
lines changed

ports/stm32/boards/stm32g0xx_hal_conf_base.h

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,42 @@
2727
#ifndef MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
2828
#define MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
2929

30+
// Enable various HAL modules
31+
#define HAL_MODULE_ENABLED
32+
#define HAL_ADC_MODULE_ENABLED
33+
#define HAL_CORTEX_MODULE_ENABLED
34+
#define HAL_DMA_MODULE_ENABLED
35+
#define HAL_EXTI_MODULE_ENABLED
36+
#define HAL_FLASH_MODULE_ENABLED
37+
#define HAL_GPIO_MODULE_ENABLED
38+
#define HAL_I2C_MODULE_ENABLED
39+
#define HAL_PCD_MODULE_ENABLED
40+
#define HAL_PWR_MODULE_ENABLED
41+
#define HAL_RCC_MODULE_ENABLED
42+
#define HAL_RTC_MODULE_ENABLED
43+
#define HAL_SPI_MODULE_ENABLED
44+
#define HAL_TIM_MODULE_ENABLED
45+
#define HAL_UART_MODULE_ENABLED
46+
#define HAL_USART_MODULE_ENABLED
47+
3048
// Oscillator values in Hz
31-
// These must come before the HAL headers because stm32g0xx_ll_rcc.h will define HSI_VALUE unless already defined
3249
#define HSI_VALUE (16000000)
3350
#define LSI_VALUE (32000)
3451
#if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
3552
#define HSI48_VALUE 48000000
3653
#endif
3754

38-
// Include various HAL modules for convenience
55+
// SysTick has the highest priority
56+
#define TICK_INT_PRIORITY (0x00)
57+
58+
// Miscellaneous HAL settings
59+
#define USE_RTOS 0
60+
#define PREFETCH_ENABLE 1
61+
#define INSTRUCTION_CACHE_ENABLE 1
62+
#define USE_SPI_CRC 1
63+
#define USE_HAL_CRYP_SUSPEND_RESUME 1
3964

65+
// Include various HAL modules for convenience
4066
#include "stm32g0xx_hal_rcc.h"
4167
#include "stm32g0xx_hal_gpio.h"
4268
#include "stm32g0xx_hal_dma.h"
@@ -68,38 +94,10 @@
6894
#include "stm32g0xx_hal_uart.h"
6995
#include "stm32g0xx_hal_usart.h"
7096
#include "stm32g0xx_hal_wwdg.h"
71-
7297
#include "stm32g0xx_ll_lpuart.h"
7398
#include "stm32g0xx_ll_rtc.h"
7499
#include "stm32g0xx_ll_usart.h"
75100

76-
// Enable various HAL modules
77-
#define HAL_MODULE_ENABLED
78-
#define HAL_ADC_MODULE_ENABLED
79-
#define HAL_CORTEX_MODULE_ENABLED
80-
#define HAL_DMA_MODULE_ENABLED
81-
#define HAL_EXTI_MODULE_ENABLED
82-
#define HAL_FLASH_MODULE_ENABLED
83-
#define HAL_GPIO_MODULE_ENABLED
84-
#define HAL_I2C_MODULE_ENABLED
85-
#define HAL_PWR_MODULE_ENABLED
86-
#define HAL_RCC_MODULE_ENABLED
87-
#define HAL_RTC_MODULE_ENABLED
88-
#define HAL_SPI_MODULE_ENABLED
89-
#define HAL_TIM_MODULE_ENABLED
90-
#define HAL_UART_MODULE_ENABLED
91-
#define HAL_USART_MODULE_ENABLED
92-
93-
// SysTick has the highest priority
94-
#define TICK_INT_PRIORITY (0x00)
95-
96-
// Miscellaneous HAL settings
97-
#define USE_RTOS 0
98-
#define PREFETCH_ENABLE 1
99-
#define INSTRUCTION_CACHE_ENABLE 1
100-
#define USE_SPI_CRC 1
101-
#define USE_HAL_CRYP_SUSPEND_RESUME 1
102-
103101
// HAL parameter assertions are disabled
104102
#define assert_param(expr) ((void)0)
105103

ports/stm32/powerctrlboot.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,14 @@ void SystemClock_Config(void) {
173173

174174
#if MICROPY_HW_ENABLE_RNG || MICROPY_HW_ENABLE_USB
175175
// Enable the 48MHz internal oscillator
176-
RCC->CRRCR |= RCC_CRRCR_HSI48ON;
177-
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
178-
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENREF_HSI48;
179-
while (!(RCC->CRRCR & RCC_CRRCR_HSI48RDY)) {
176+
RCC->CR |= RCC_CR_HSI48ON;
177+
RCC->APBENR2 |= RCC_APBENR2_SYSCFGEN;
178+
while (!(RCC->CR & RCC_CR_HSI48RDY)) {
180179
// Wait for HSI48 to be ready
181180
}
182181

183-
// Select RC48 as HSI48 for USB and RNG
184-
RCC->CCIPR |= RCC_CCIPR_HSI48SEL;
182+
// Select HSI48 for USB
183+
RCC->CCIPR2 &= ~(3 << RCC_CCIPR2_USBSEL_Pos);
185184

186185
#if MICROPY_HW_ENABLE_USB
187186
// Synchronise HSI48 with 1kHz USB SoF

ports/stm32/stm32_it.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,15 @@ void DebugMon_Handler(void) {
296296
/* file (startup_stm32f4xx.s). */
297297
/******************************************************************************/
298298

299-
#if defined(STM32L0) || defined(STM32L432xx)
299+
#if defined(STM32G0)
300+
301+
#if MICROPY_HW_USB_FS
302+
void USB_UCPD1_2_IRQHandler(void) {
303+
HAL_PCD_IRQHandler(&pcd_fs_handle);
304+
}
305+
#endif
306+
307+
#elif defined(STM32L0) || defined(STM32L432xx)
300308

301309
#if MICROPY_HW_USB_FS
302310
void USB_IRQHandler(void) {

ports/stm32/usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
#define MAX_ENDPOINT(dev_id) ((dev_id) == USB_PHY_FS_ID ? 3 : 5)
6868
#elif defined(STM32F7)
6969
#define MAX_ENDPOINT(dev_id) ((dev_id) == USB_PHY_FS_ID ? 5 : 8)
70-
#elif defined(STM32H7)
70+
#elif defined(STM32G0) || defined(STM32H7)
7171
#define MAX_ENDPOINT(dev_id) (8)
7272
#endif
7373

ports/stm32/usbd_cdc_interface.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050

5151
#if MICROPY_HW_ENABLE_USB
5252

53+
#if !MICROPY_HW_USB_IS_MULTI_OTG
54+
#define USE_USB_CNTR_SOFM (1)
55+
#elif defined(STM32G0)
56+
#define USE_USB_CNTR_SOFM (1)
57+
#define USB USB_DRD_FS
58+
#else
59+
#define USE_USB_CNTR_SOFM (0)
60+
#endif
61+
5362
// CDC control commands
5463
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
5564
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01
@@ -152,7 +161,7 @@ int8_t usbd_cdc_control(usbd_cdc_state_t *cdc_in, uint8_t cmd, uint8_t *pbuf, ui
152161
// configure its serial port (in most cases to disable local echo)
153162
cdc->connect_state = USBD_CDC_CONNECT_STATE_CONNECTING;
154163
usbd_cdc_connect_tx_timer = 8; // wait for 8 SOF IRQs
155-
#if !MICROPY_HW_USB_IS_MULTI_OTG
164+
#if USE_USB_CNTR_SOFM
156165
USB->CNTR |= USB_CNTR_SOFM;
157166
#else
158167
PCD_HandleTypeDef *hpcd = cdc->base.usbd->pdev->pData;
@@ -263,7 +272,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
263272
--usbd_cdc_connect_tx_timer;
264273
} else {
265274
usbd_cdc_msc_hid_state_t *usbd = ((USBD_HandleTypeDef *)hpcd->pData)->pClassData;
266-
#if !MICROPY_HW_USB_IS_MULTI_OTG
275+
#if USE_USB_CNTR_SOFM
267276
USB->CNTR &= ~USB_CNTR_SOFM;
268277
#else
269278
hpcd->Instance->GINTMSK &= ~USB_OTG_GINTMSK_SOFM;

ports/stm32/usbd_conf.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ PCD_HandleTypeDef pcd_hs_handle;
4949
#define USB_OTG_FS USB
5050
#endif
5151

52+
#if defined(STM32G0)
53+
#define USB_OTG_FS USB_DRD_FS
54+
#endif
55+
5256
/*******************************************************************************
5357
PCD BSP Routines
5458
*******************************************************************************/
@@ -61,6 +65,22 @@ PCD_HandleTypeDef pcd_hs_handle;
6165
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
6266
#if MICROPY_HW_USB_FS
6367
if (hpcd->Instance == USB_OTG_FS) {
68+
// Configure USB GPIO's.
69+
70+
#if defined(STM32G0)
71+
72+
// These MCUs don't have an alternate function for USB but rather require
73+
// the pins to be disconnected from all peripherals, ie put in analog mode.
74+
75+
mp_hal_pin_config(pin_A11, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
76+
mp_hal_pin_config_speed(pin_A11, GPIO_SPEED_FREQ_VERY_HIGH);
77+
mp_hal_pin_config(pin_A12, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
78+
mp_hal_pin_config_speed(pin_A12, GPIO_SPEED_FREQ_VERY_HIGH);
79+
80+
#else
81+
82+
// Other MCUs have an alternate function for GPIO's to be in USB mode.
83+
6484
#if defined(STM32H7)
6585
const uint32_t otg_alt = GPIO_AF10_OTG1_FS;
6686
#elif defined(STM32L0)
@@ -78,6 +98,8 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
7898
mp_hal_pin_config(pin_A12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, otg_alt);
7999
mp_hal_pin_config_speed(pin_A12, GPIO_SPEED_FREQ_VERY_HIGH);
80100

101+
#endif
102+
81103
#if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
82104
// USB VBUS detect pin is always A9
83105
mp_hal_pin_config(MICROPY_HW_USB_VBUS_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0);
@@ -88,14 +110,16 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
88110
mp_hal_pin_config(MICROPY_HW_USB_OTG_ID_PIN, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, otg_alt);
89111
#endif
90112

91-
#if defined(STM32H7)
113+
#if defined(STM32G0)
114+
__HAL_RCC_USB_CLK_SLEEP_ENABLE();
115+
#elif defined(STM32H7)
92116
// Keep USB clock running during sleep or else __WFI() will disable the USB
93117
__HAL_RCC_USB2_OTG_FS_CLK_SLEEP_ENABLE();
94118
__HAL_RCC_USB2_OTG_FS_ULPI_CLK_SLEEP_DISABLE();
95119
#endif
96120

97121
// Enable USB FS Clocks
98-
#if !MICROPY_HW_USB_IS_MULTI_OTG
122+
#if !MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0)
99123
__HAL_RCC_USB_CLK_ENABLE();
100124
#else
101125
__USB_OTG_FS_CLK_ENABLE();
@@ -113,7 +137,10 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
113137
#endif
114138

115139
// Configure and enable USB FS interrupt
116-
#if defined(STM32L0)
140+
#if defined(STM32G0)
141+
NVIC_SetPriority(USB_UCPD1_2_IRQn, IRQ_PRI_OTG_FS);
142+
HAL_NVIC_EnableIRQ(USB_UCPD1_2_IRQn);
143+
#elif defined(STM32L0)
117144
NVIC_SetPriority(USB_IRQn, IRQ_PRI_OTG_FS);
118145
HAL_NVIC_EnableIRQ(USB_IRQn);
119146
#elif defined(STM32L432xx)
@@ -235,7 +262,11 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) {
235262
#if MICROPY_HW_USB_FS
236263
if (hpcd->Instance == USB_OTG_FS) {
237264
/* Disable USB FS Clocks */
265+
#if defined(STM32G0)
266+
__HAL_RCC_USB_CLK_DISABLE();
267+
#else
238268
__USB_OTG_FS_CLK_DISABLE();
269+
#endif
239270
return;
240271
}
241272
#endif
@@ -413,7 +444,9 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev, int high_speed, const
413444
pcd_fs_handle.Init.lpm_enable = DISABLE;
414445
pcd_fs_handle.Init.battery_charging_enable = DISABLE;
415446
#if MICROPY_HW_USB_IS_MULTI_OTG
447+
#if !defined(STM32G0)
416448
pcd_fs_handle.Init.use_dedicated_ep1 = 0;
449+
#endif
417450
pcd_fs_handle.Init.dma_enable = 0;
418451
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
419452
pcd_fs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
@@ -430,7 +463,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev, int high_speed, const
430463
HAL_PCD_Init(&pcd_fs_handle);
431464

432465
// Set FIFO buffer sizes
433-
#if !MICROPY_HW_USB_IS_MULTI_OTG
466+
#if !MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0)
434467
uint32_t fifo_offset = USBD_PMA_RESERVE; // need to reserve some data at start of FIFO
435468
for (size_t i = 0; i < USBD_PMA_NUM_FIFO; ++i) {
436469
uint16_t ep_addr = ((i & 1) * 0x80) | (i >> 1);

0 commit comments

Comments
 (0)