stm32: Add support for USB on G0 MCUs.
Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
parent
b7ea90d4cb
commit
c7923b1139
@ -27,16 +27,42 @@
|
|||||||
#ifndef MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
|
#ifndef MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
|
||||||
#define MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
|
#define MICROPY_INCLUDED_STM32G0XX_HAL_CONF_BASE_H
|
||||||
|
|
||||||
|
// Enable various HAL modules
|
||||||
|
#define HAL_MODULE_ENABLED
|
||||||
|
#define HAL_ADC_MODULE_ENABLED
|
||||||
|
#define HAL_CORTEX_MODULE_ENABLED
|
||||||
|
#define HAL_DMA_MODULE_ENABLED
|
||||||
|
#define HAL_EXTI_MODULE_ENABLED
|
||||||
|
#define HAL_FLASH_MODULE_ENABLED
|
||||||
|
#define HAL_GPIO_MODULE_ENABLED
|
||||||
|
#define HAL_I2C_MODULE_ENABLED
|
||||||
|
#define HAL_PCD_MODULE_ENABLED
|
||||||
|
#define HAL_PWR_MODULE_ENABLED
|
||||||
|
#define HAL_RCC_MODULE_ENABLED
|
||||||
|
#define HAL_RTC_MODULE_ENABLED
|
||||||
|
#define HAL_SPI_MODULE_ENABLED
|
||||||
|
#define HAL_TIM_MODULE_ENABLED
|
||||||
|
#define HAL_UART_MODULE_ENABLED
|
||||||
|
#define HAL_USART_MODULE_ENABLED
|
||||||
|
|
||||||
// Oscillator values in Hz
|
// Oscillator values in Hz
|
||||||
// These must come before the HAL headers because stm32g0xx_ll_rcc.h will define HSI_VALUE unless already defined
|
|
||||||
#define HSI_VALUE (16000000)
|
#define HSI_VALUE (16000000)
|
||||||
#define LSI_VALUE (32000)
|
#define LSI_VALUE (32000)
|
||||||
#if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
|
#if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
|
||||||
#define HSI48_VALUE 48000000
|
#define HSI48_VALUE 48000000
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Include various HAL modules for convenience
|
// SysTick has the highest priority
|
||||||
|
#define TICK_INT_PRIORITY (0x00)
|
||||||
|
|
||||||
|
// Miscellaneous HAL settings
|
||||||
|
#define USE_RTOS 0
|
||||||
|
#define PREFETCH_ENABLE 1
|
||||||
|
#define INSTRUCTION_CACHE_ENABLE 1
|
||||||
|
#define USE_SPI_CRC 1
|
||||||
|
#define USE_HAL_CRYP_SUSPEND_RESUME 1
|
||||||
|
|
||||||
|
// Include various HAL modules for convenience
|
||||||
#include "stm32g0xx_hal_rcc.h"
|
#include "stm32g0xx_hal_rcc.h"
|
||||||
#include "stm32g0xx_hal_gpio.h"
|
#include "stm32g0xx_hal_gpio.h"
|
||||||
#include "stm32g0xx_hal_dma.h"
|
#include "stm32g0xx_hal_dma.h"
|
||||||
@ -68,38 +94,10 @@
|
|||||||
#include "stm32g0xx_hal_uart.h"
|
#include "stm32g0xx_hal_uart.h"
|
||||||
#include "stm32g0xx_hal_usart.h"
|
#include "stm32g0xx_hal_usart.h"
|
||||||
#include "stm32g0xx_hal_wwdg.h"
|
#include "stm32g0xx_hal_wwdg.h"
|
||||||
|
|
||||||
#include "stm32g0xx_ll_lpuart.h"
|
#include "stm32g0xx_ll_lpuart.h"
|
||||||
#include "stm32g0xx_ll_rtc.h"
|
#include "stm32g0xx_ll_rtc.h"
|
||||||
#include "stm32g0xx_ll_usart.h"
|
#include "stm32g0xx_ll_usart.h"
|
||||||
|
|
||||||
// Enable various HAL modules
|
|
||||||
#define HAL_MODULE_ENABLED
|
|
||||||
#define HAL_ADC_MODULE_ENABLED
|
|
||||||
#define HAL_CORTEX_MODULE_ENABLED
|
|
||||||
#define HAL_DMA_MODULE_ENABLED
|
|
||||||
#define HAL_EXTI_MODULE_ENABLED
|
|
||||||
#define HAL_FLASH_MODULE_ENABLED
|
|
||||||
#define HAL_GPIO_MODULE_ENABLED
|
|
||||||
#define HAL_I2C_MODULE_ENABLED
|
|
||||||
#define HAL_PWR_MODULE_ENABLED
|
|
||||||
#define HAL_RCC_MODULE_ENABLED
|
|
||||||
#define HAL_RTC_MODULE_ENABLED
|
|
||||||
#define HAL_SPI_MODULE_ENABLED
|
|
||||||
#define HAL_TIM_MODULE_ENABLED
|
|
||||||
#define HAL_UART_MODULE_ENABLED
|
|
||||||
#define HAL_USART_MODULE_ENABLED
|
|
||||||
|
|
||||||
// SysTick has the highest priority
|
|
||||||
#define TICK_INT_PRIORITY (0x00)
|
|
||||||
|
|
||||||
// Miscellaneous HAL settings
|
|
||||||
#define USE_RTOS 0
|
|
||||||
#define PREFETCH_ENABLE 1
|
|
||||||
#define INSTRUCTION_CACHE_ENABLE 1
|
|
||||||
#define USE_SPI_CRC 1
|
|
||||||
#define USE_HAL_CRYP_SUSPEND_RESUME 1
|
|
||||||
|
|
||||||
// HAL parameter assertions are disabled
|
// HAL parameter assertions are disabled
|
||||||
#define assert_param(expr) ((void)0)
|
#define assert_param(expr) ((void)0)
|
||||||
|
|
||||||
|
@ -173,15 +173,14 @@ void SystemClock_Config(void) {
|
|||||||
|
|
||||||
#if MICROPY_HW_ENABLE_RNG || MICROPY_HW_ENABLE_USB
|
#if MICROPY_HW_ENABLE_RNG || MICROPY_HW_ENABLE_USB
|
||||||
// Enable the 48MHz internal oscillator
|
// Enable the 48MHz internal oscillator
|
||||||
RCC->CRRCR |= RCC_CRRCR_HSI48ON;
|
RCC->CR |= RCC_CR_HSI48ON;
|
||||||
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
|
RCC->APBENR2 |= RCC_APBENR2_SYSCFGEN;
|
||||||
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENREF_HSI48;
|
while (!(RCC->CR & RCC_CR_HSI48RDY)) {
|
||||||
while (!(RCC->CRRCR & RCC_CRRCR_HSI48RDY)) {
|
|
||||||
// Wait for HSI48 to be ready
|
// Wait for HSI48 to be ready
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select RC48 as HSI48 for USB and RNG
|
// Select HSI48 for USB
|
||||||
RCC->CCIPR |= RCC_CCIPR_HSI48SEL;
|
RCC->CCIPR2 &= ~(3 << RCC_CCIPR2_USBSEL_Pos);
|
||||||
|
|
||||||
#if MICROPY_HW_ENABLE_USB
|
#if MICROPY_HW_ENABLE_USB
|
||||||
// Synchronise HSI48 with 1kHz USB SoF
|
// Synchronise HSI48 with 1kHz USB SoF
|
||||||
|
@ -296,7 +296,15 @@ void DebugMon_Handler(void) {
|
|||||||
/* file (startup_stm32f4xx.s). */
|
/* file (startup_stm32f4xx.s). */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#if defined(STM32L0) || defined(STM32L432xx)
|
#if defined(STM32G0)
|
||||||
|
|
||||||
|
#if MICROPY_HW_USB_FS
|
||||||
|
void USB_UCPD1_2_IRQHandler(void) {
|
||||||
|
HAL_PCD_IRQHandler(&pcd_fs_handle);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#elif defined(STM32L0) || defined(STM32L432xx)
|
||||||
|
|
||||||
#if MICROPY_HW_USB_FS
|
#if MICROPY_HW_USB_FS
|
||||||
void USB_IRQHandler(void) {
|
void USB_IRQHandler(void) {
|
||||||
|
@ -67,7 +67,7 @@
|
|||||||
#define MAX_ENDPOINT(dev_id) ((dev_id) == USB_PHY_FS_ID ? 3 : 5)
|
#define MAX_ENDPOINT(dev_id) ((dev_id) == USB_PHY_FS_ID ? 3 : 5)
|
||||||
#elif defined(STM32F7)
|
#elif defined(STM32F7)
|
||||||
#define MAX_ENDPOINT(dev_id) ((dev_id) == USB_PHY_FS_ID ? 5 : 8)
|
#define MAX_ENDPOINT(dev_id) ((dev_id) == USB_PHY_FS_ID ? 5 : 8)
|
||||||
#elif defined(STM32H7)
|
#elif defined(STM32G0) || defined(STM32H7)
|
||||||
#define MAX_ENDPOINT(dev_id) (8)
|
#define MAX_ENDPOINT(dev_id) (8)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -50,6 +50,15 @@
|
|||||||
|
|
||||||
#if MICROPY_HW_ENABLE_USB
|
#if MICROPY_HW_ENABLE_USB
|
||||||
|
|
||||||
|
#if !MICROPY_HW_USB_IS_MULTI_OTG
|
||||||
|
#define USE_USB_CNTR_SOFM (1)
|
||||||
|
#elif defined(STM32G0)
|
||||||
|
#define USE_USB_CNTR_SOFM (1)
|
||||||
|
#define USB USB_DRD_FS
|
||||||
|
#else
|
||||||
|
#define USE_USB_CNTR_SOFM (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
// CDC control commands
|
// CDC control commands
|
||||||
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
|
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
|
||||||
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01
|
#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
|
|||||||
// configure its serial port (in most cases to disable local echo)
|
// configure its serial port (in most cases to disable local echo)
|
||||||
cdc->connect_state = USBD_CDC_CONNECT_STATE_CONNECTING;
|
cdc->connect_state = USBD_CDC_CONNECT_STATE_CONNECTING;
|
||||||
usbd_cdc_connect_tx_timer = 8; // wait for 8 SOF IRQs
|
usbd_cdc_connect_tx_timer = 8; // wait for 8 SOF IRQs
|
||||||
#if !MICROPY_HW_USB_IS_MULTI_OTG
|
#if USE_USB_CNTR_SOFM
|
||||||
USB->CNTR |= USB_CNTR_SOFM;
|
USB->CNTR |= USB_CNTR_SOFM;
|
||||||
#else
|
#else
|
||||||
PCD_HandleTypeDef *hpcd = cdc->base.usbd->pdev->pData;
|
PCD_HandleTypeDef *hpcd = cdc->base.usbd->pdev->pData;
|
||||||
@ -263,7 +272,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
|
|||||||
--usbd_cdc_connect_tx_timer;
|
--usbd_cdc_connect_tx_timer;
|
||||||
} else {
|
} else {
|
||||||
usbd_cdc_msc_hid_state_t *usbd = ((USBD_HandleTypeDef *)hpcd->pData)->pClassData;
|
usbd_cdc_msc_hid_state_t *usbd = ((USBD_HandleTypeDef *)hpcd->pData)->pClassData;
|
||||||
#if !MICROPY_HW_USB_IS_MULTI_OTG
|
#if USE_USB_CNTR_SOFM
|
||||||
USB->CNTR &= ~USB_CNTR_SOFM;
|
USB->CNTR &= ~USB_CNTR_SOFM;
|
||||||
#else
|
#else
|
||||||
hpcd->Instance->GINTMSK &= ~USB_OTG_GINTMSK_SOFM;
|
hpcd->Instance->GINTMSK &= ~USB_OTG_GINTMSK_SOFM;
|
||||||
|
@ -49,6 +49,10 @@ PCD_HandleTypeDef pcd_hs_handle;
|
|||||||
#define USB_OTG_FS USB
|
#define USB_OTG_FS USB
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(STM32G0)
|
||||||
|
#define USB_OTG_FS USB_DRD_FS
|
||||||
|
#endif
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
PCD BSP Routines
|
PCD BSP Routines
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
@ -61,6 +65,22 @@ PCD_HandleTypeDef pcd_hs_handle;
|
|||||||
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
|
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
|
||||||
#if MICROPY_HW_USB_FS
|
#if MICROPY_HW_USB_FS
|
||||||
if (hpcd->Instance == USB_OTG_FS) {
|
if (hpcd->Instance == USB_OTG_FS) {
|
||||||
|
// Configure USB GPIO's.
|
||||||
|
|
||||||
|
#if defined(STM32G0)
|
||||||
|
|
||||||
|
// These MCUs don't have an alternate function for USB but rather require
|
||||||
|
// the pins to be disconnected from all peripherals, ie put in analog mode.
|
||||||
|
|
||||||
|
mp_hal_pin_config(pin_A11, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
|
||||||
|
mp_hal_pin_config_speed(pin_A11, GPIO_SPEED_FREQ_VERY_HIGH);
|
||||||
|
mp_hal_pin_config(pin_A12, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
|
||||||
|
mp_hal_pin_config_speed(pin_A12, GPIO_SPEED_FREQ_VERY_HIGH);
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
// Other MCUs have an alternate function for GPIO's to be in USB mode.
|
||||||
|
|
||||||
#if defined(STM32H7)
|
#if defined(STM32H7)
|
||||||
const uint32_t otg_alt = GPIO_AF10_OTG1_FS;
|
const uint32_t otg_alt = GPIO_AF10_OTG1_FS;
|
||||||
#elif defined(STM32L0)
|
#elif defined(STM32L0)
|
||||||
@ -78,6 +98,8 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
|
|||||||
mp_hal_pin_config(pin_A12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, otg_alt);
|
mp_hal_pin_config(pin_A12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, otg_alt);
|
||||||
mp_hal_pin_config_speed(pin_A12, GPIO_SPEED_FREQ_VERY_HIGH);
|
mp_hal_pin_config_speed(pin_A12, GPIO_SPEED_FREQ_VERY_HIGH);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
|
#if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
|
||||||
// USB VBUS detect pin is always A9
|
// USB VBUS detect pin is always A9
|
||||||
mp_hal_pin_config(MICROPY_HW_USB_VBUS_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0);
|
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) {
|
|||||||
mp_hal_pin_config(MICROPY_HW_USB_OTG_ID_PIN, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, otg_alt);
|
mp_hal_pin_config(MICROPY_HW_USB_OTG_ID_PIN, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, otg_alt);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(STM32H7)
|
#if defined(STM32G0)
|
||||||
|
__HAL_RCC_USB_CLK_SLEEP_ENABLE();
|
||||||
|
#elif defined(STM32H7)
|
||||||
// Keep USB clock running during sleep or else __WFI() will disable the USB
|
// Keep USB clock running during sleep or else __WFI() will disable the USB
|
||||||
__HAL_RCC_USB2_OTG_FS_CLK_SLEEP_ENABLE();
|
__HAL_RCC_USB2_OTG_FS_CLK_SLEEP_ENABLE();
|
||||||
__HAL_RCC_USB2_OTG_FS_ULPI_CLK_SLEEP_DISABLE();
|
__HAL_RCC_USB2_OTG_FS_ULPI_CLK_SLEEP_DISABLE();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Enable USB FS Clocks
|
// Enable USB FS Clocks
|
||||||
#if !MICROPY_HW_USB_IS_MULTI_OTG
|
#if !MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0)
|
||||||
__HAL_RCC_USB_CLK_ENABLE();
|
__HAL_RCC_USB_CLK_ENABLE();
|
||||||
#else
|
#else
|
||||||
__USB_OTG_FS_CLK_ENABLE();
|
__USB_OTG_FS_CLK_ENABLE();
|
||||||
@ -113,7 +137,10 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Configure and enable USB FS interrupt
|
// Configure and enable USB FS interrupt
|
||||||
#if defined(STM32L0)
|
#if defined(STM32G0)
|
||||||
|
NVIC_SetPriority(USB_UCPD1_2_IRQn, IRQ_PRI_OTG_FS);
|
||||||
|
HAL_NVIC_EnableIRQ(USB_UCPD1_2_IRQn);
|
||||||
|
#elif defined(STM32L0)
|
||||||
NVIC_SetPriority(USB_IRQn, IRQ_PRI_OTG_FS);
|
NVIC_SetPriority(USB_IRQn, IRQ_PRI_OTG_FS);
|
||||||
HAL_NVIC_EnableIRQ(USB_IRQn);
|
HAL_NVIC_EnableIRQ(USB_IRQn);
|
||||||
#elif defined(STM32L432xx)
|
#elif defined(STM32L432xx)
|
||||||
@ -235,7 +262,11 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) {
|
|||||||
#if MICROPY_HW_USB_FS
|
#if MICROPY_HW_USB_FS
|
||||||
if (hpcd->Instance == USB_OTG_FS) {
|
if (hpcd->Instance == USB_OTG_FS) {
|
||||||
/* Disable USB FS Clocks */
|
/* Disable USB FS Clocks */
|
||||||
|
#if defined(STM32G0)
|
||||||
|
__HAL_RCC_USB_CLK_DISABLE();
|
||||||
|
#else
|
||||||
__USB_OTG_FS_CLK_DISABLE();
|
__USB_OTG_FS_CLK_DISABLE();
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -413,7 +444,9 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev, int high_speed, const
|
|||||||
pcd_fs_handle.Init.lpm_enable = DISABLE;
|
pcd_fs_handle.Init.lpm_enable = DISABLE;
|
||||||
pcd_fs_handle.Init.battery_charging_enable = DISABLE;
|
pcd_fs_handle.Init.battery_charging_enable = DISABLE;
|
||||||
#if MICROPY_HW_USB_IS_MULTI_OTG
|
#if MICROPY_HW_USB_IS_MULTI_OTG
|
||||||
|
#if !defined(STM32G0)
|
||||||
pcd_fs_handle.Init.use_dedicated_ep1 = 0;
|
pcd_fs_handle.Init.use_dedicated_ep1 = 0;
|
||||||
|
#endif
|
||||||
pcd_fs_handle.Init.dma_enable = 0;
|
pcd_fs_handle.Init.dma_enable = 0;
|
||||||
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
|
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
|
||||||
pcd_fs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
|
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
|
|||||||
HAL_PCD_Init(&pcd_fs_handle);
|
HAL_PCD_Init(&pcd_fs_handle);
|
||||||
|
|
||||||
// Set FIFO buffer sizes
|
// Set FIFO buffer sizes
|
||||||
#if !MICROPY_HW_USB_IS_MULTI_OTG
|
#if !MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0)
|
||||||
uint32_t fifo_offset = USBD_PMA_RESERVE; // need to reserve some data at start of FIFO
|
uint32_t fifo_offset = USBD_PMA_RESERVE; // need to reserve some data at start of FIFO
|
||||||
for (size_t i = 0; i < USBD_PMA_NUM_FIFO; ++i) {
|
for (size_t i = 0; i < USBD_PMA_NUM_FIFO; ++i) {
|
||||||
uint16_t ep_addr = ((i & 1) * 0x80) | (i >> 1);
|
uint16_t ep_addr = ((i & 1) * 0x80) | (i >> 1);
|
||||||
|
Loading…
Reference in New Issue
Block a user