Skip to content

Commit

Permalink
Merge pull request #15116 from jeromecoutant/PR_USB_F3
Browse files Browse the repository at this point in the history
STM32F3 - enable USB_DEVICE for NUCLEO_F303ZE
  • Loading branch information
0xc0170 authored Oct 1, 2021
2 parents b57fe3f + 8d35879 commit 4587080
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ typedef enum {
#define LED3 PB_14 // LD3 [Red]
#define BUTTON1 PC_13 // USER_Btn [B1]

// USB
#define USB_PULLUP_CONTROL PG_6 // USB_Disconnect in the schematics

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;

/* Enable HSE oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
Expand All @@ -120,11 +119,14 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
return 0; // FAIL
}

#if DEVICE_USBDEVICE
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
RCC_PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
return 0; // FAIL
}
#endif

/* Output clock on MCO1 pin(PA8) for debugging purpose */
//if (bypass == 0)
Expand All @@ -144,7 +146,6 @@ uint8_t SetSysClock_PLL_HSI(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;

/* Enable HSI oscillator and activate PLL with HSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
Expand All @@ -169,11 +170,14 @@ uint8_t SetSysClock_PLL_HSI(void)
return 0; // FAIL
}

#if DEVICE_USBDEVICE
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
RCC_PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
return 0; // FAIL
}
#endif

/* Output clock on MCO1 pin(PA8) for debugging purpose */
//HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI, RCC_MCO_DIV1); // 8 MHz
Expand Down
4 changes: 3 additions & 1 deletion targets/TARGET_STM/USBPhyHw.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@

#if MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG

#if defined(TARGET_STM32F1) || defined(TARGET_STM32F3) || defined(TARGET_STM32L1) || defined(TARGET_STM32WB) || defined(TARGET_STM32G4)
#if defined(TARGET_STM32F1) || defined(TARGET_STM32L1) || defined(TARGET_STM32WB) || defined(TARGET_STM32G4)
#define USBHAL_IRQn USB_LP_IRQn
#elif defined(TARGET_STM32F3)
#define USBHAL_IRQn USB_LP_CAN_RX0_IRQn
#elif defined(TARGET_STM32L5)
#define USBHAL_IRQn USB_FS_IRQn
#else
Expand Down
13 changes: 9 additions & 4 deletions targets/TARGET_STM/USBPhy_STM32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ USBPhyHw::~USBPhyHw()

}

#if defined(TARGET_STM32F1) || defined(SYSCFG_PMC_USB_PU)
#if defined(TARGET_STM32F1) || defined(TARGET_STM32F3) || defined(SYSCFG_PMC_USB_PU)

#include "drivers/DigitalOut.h"

Expand All @@ -203,9 +203,14 @@ void USB_reenumerate()
LL_SYSCFG_DisableUSBPullUp();
wait_us(10000); // 10ms
LL_SYSCFG_EnableUSBPullUp();
#elif defined(USB_PULLUP_CONTROL)
mbed::DigitalOut usb_dp_pin(USB_PULLUP_CONTROL, 0);
wait_us(1000);
usb_dp_pin = 1;
wait_us(1000);
#else
// Force USB_DP pin (with external pull up) to 0
mbed::DigitalOut usb_dp_pin(USB_DP, 0) ;
mbed::DigitalOut usb_dp_pin(USB_DP, 0);
wait_us(10000); // 10ms
#endif
}
Expand Down Expand Up @@ -295,7 +300,7 @@ void USBPhyHw::init(USBPhyEvents *events)

map = PinMap_USB_FS;

#if defined(TARGET_STM32F1) || defined(SYSCFG_PMC_USB_PU)
#if defined(TARGET_STM32F1) || defined(TARGET_STM32F3) || defined(SYSCFG_PMC_USB_PU)
// USB_DevConnect is empty
USB_reenumerate();
#endif
Expand Down Expand Up @@ -418,7 +423,7 @@ void USBPhyHw::connect()
// Initializes the USB controller registers
USB_DevInit(hpcd.Instance, hpcd.Init); // hpcd.Init not used

#if defined(TARGET_STM32F1) || defined(SYSCFG_PMC_USB_PU)
#if defined(TARGET_STM32F1) || defined(TARGET_STM32F3) || defined(SYSCFG_PMC_USB_PU)
// USB_DevConnect is empty
USB_reenumerate();
#endif
Expand Down
3 changes: 3 additions & 0 deletions targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,9 @@
"detect_code": [
"0747"
],
"device_has_add": [
"USBDEVICE"
],
"device_name": "STM32F303ZETx"
},
"MCU_STM32F334x8": {
Expand Down

0 comments on commit 4587080

Please sign in to comment.