Skip to content

Commit 3801f6e

Browse files
authored
Merge pull request ARMmbed#13406 from Allmoz/master
STM32F1 USBDevice
2 parents 0db72d0 + ee4a4e9 commit 3801f6e

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

targets/TARGET_STM/TARGET_STM32F1/TARGET_STM32F103x8/PeripheralNames.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ typedef enum {
6868
CAN_1 = (int)CAN1_BASE
6969
} CANName;
7070

71+
typedef enum {
72+
USB_FS = (int)USB_BASE,
73+
} USBName;
74+
7175
#ifdef __cplusplus
7276
}
7377
#endif

targets/TARGET_STM/TARGET_STM32F1/TARGET_STM32F103x8/system_clock.c

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@
1616

1717
/**
1818
* This file configures the system clock as follows:
19-
*-----------------------------------------------------------------------------
20-
* System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
21-
* | (external 8 MHz clock) | (internal 8 MHz)
22-
* | 2- PLL_HSE_XTAL |
23-
* | (external 8 MHz xtal) |
24-
*-----------------------------------------------------------------------------
25-
* SYSCLK(MHz) | 72 | 64
26-
*-----------------------------------------------------------------------------
27-
* AHBCLK (MHz) | 72 | 64
28-
*-----------------------------------------------------------------------------
29-
* APB1CLK (MHz) | 36 | 32
30-
*-----------------------------------------------------------------------------
31-
* APB2CLK (MHz) | 72 | 64
32-
*-----------------------------------------------------------------------------
33-
* USB capable (48 MHz precise clock) | NO | NO
34-
*-----------------------------------------------------------------------------
35-
******************************************************************************
19+
*-------------------------------------------------------------------------------------------
20+
* System clock source | 1- PLL_HSE_EXTC / DEVICE_USBDEVICE | 3- PLL_HSI / DEVICE_USBDEVICE
21+
* | (external 8 MHz clock) | (internal 8 MHz)
22+
* | 2- PLL_HSE_XTAL / DEVICE_USBDEVICE |
23+
* | (external 8 MHz xtal) |
24+
*-------------------------------------------------------------------------------------------
25+
* SYSCLK(MHz) | 72 / 72 | 64 / 48
26+
*-------------------------------------------------------------------------------------------
27+
* AHBCLK (MHz) | 72 / 72 | 64 / 48
28+
*-------------------------------------------------------------------------------------------
29+
* APB1CLK (MHz) | 36 / 36 | 32 / 24
30+
*-------------------------------------------------------------------------------------------
31+
* APB2CLK (MHz) | 72 / 72 | 64 / 48
32+
*-------------------------------------------------------------------------------------------
3633
*/
3734

3835
#include "stm32f1xx.h"
@@ -95,6 +92,14 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
9592
{
9693
RCC_ClkInitTypeDef RCC_ClkInitStruct;
9794
RCC_OscInitTypeDef RCC_OscInitStruct;
95+
#if (DEVICE_USBDEVICE)
96+
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInit;
97+
#endif /* DEVICE_USBDEVICE */
98+
99+
// Select HSI as system clock source to allow modification of the PLL configuration
100+
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
101+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
102+
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
98103

99104
/* Enable HSE oscillator and activate PLL with HSE as source */
100105
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
@@ -121,6 +126,13 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
121126
return 0; // FAIL
122127
}
123128

129+
#if (DEVICE_USBDEVICE)
130+
/* USB clock selection */
131+
RCC_PeriphCLKInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
132+
RCC_PeriphCLKInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
133+
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInit);
134+
#endif /* DEVICE_USBDEVICE */
135+
124136
/* Output clock on MCO1 pin(PA8) for debugging purpose */
125137
//HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz
126138

@@ -136,6 +148,9 @@ uint8_t SetSysClock_PLL_HSI(void)
136148
{
137149
RCC_ClkInitTypeDef RCC_ClkInitStruct;
138150
RCC_OscInitTypeDef RCC_OscInitStruct;
151+
#if (DEVICE_USBDEVICE)
152+
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInit;
153+
#endif /* DEVICE_USBDEVICE */
139154

140155
/* Enable HSI oscillator and activate PLL with HSI as source */
141156
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
@@ -144,11 +159,22 @@ uint8_t SetSysClock_PLL_HSI(void)
144159
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
145160
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
146161
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
162+
#if (DEVICE_USBDEVICE)
163+
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; // 48 MHz (8 MHz/2 * 12)
164+
#else /* DEVICE_USBDEVICE */
147165
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz (8 MHz/2 * 16)
166+
#endif /* DEVICE_USBDEVICE */
148167
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
149168
return 0; // FAIL
150169
}
151170

171+
#if (DEVICE_USBDEVICE)
172+
/* USB clock selection */
173+
RCC_PeriphCLKInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
174+
RCC_PeriphCLKInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
175+
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInit);
176+
#endif /* DEVICE_USBDEVICE */
177+
152178
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
153179
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
154180
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 64 MHz

targets/TARGET_STM/USBPhyHw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
#if MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG
3838

39-
#if defined(TARGET_STM32F3) || defined(TARGET_STM32WB)
39+
#if defined(TARGET_STM32F1) || defined(TARGET_STM32F3) || defined(TARGET_STM32WB)
4040
#define USBHAL_IRQn USB_LP_IRQn
4141
#else
4242
#define USBHAL_IRQn USB_IRQn

0 commit comments

Comments
 (0)