Skip to content

Commit e1b8dd6

Browse files
authored
Merge pull request #13797 from JojoS62/PR_fix_LSE-drive-load-setting
STM32: add setting for LSE drive load level
2 parents fad7f34 + 6264e0a commit e1b8dd6

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

targets/TARGET_STM/TARGET_STM32L5/system_clock.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ uint8_t SetSysClock_PLL_MSI(void)
134134
__HAL_RCC_RTCAPB_CLK_ENABLE();
135135

136136
#if MBED_CONF_TARGET_LSE_AVAILABLE
137-
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
138137
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
139138
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
140139
RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT

targets/TARGET_STM/TARGET_STM32WB/TARGET_STM32WB55xx/TARGET_NUCLEO_WB55RG/system_clock.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ void SetSysClock(void)
7272

7373
Config_HSE();
7474

75-
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
7675
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
7776

7877
/* This prevents the CPU2 (M0+) to disable the HSI48 oscillator */

targets/TARGET_STM/mbed_overrides.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,69 @@
2727
*/
2828
#include "cmsis.h"
2929
#include "objects.h"
30+
#include "platform/mbed_error.h"
3031

3132
int mbed_sdk_inited = 0;
3233
extern void SetSysClock(void);
3334

35+
#if defined(RCC_LSE_HIGHDRIVE_MODE) || defined(RCC_LSEDRIVE_HIGH)
36+
# define LSE_CONFIG_AVAILABLE
37+
#endif
38+
39+
// set defaults for LSE drive load level
40+
#if defined(LSE_CONFIG_AVAILABLE)
41+
42+
# if defined(MBED_CONF_TARGET_LSE_DRIVE_LOAD_LEVEL)
43+
# define LSE_DRIVE_LOAD_LEVEL MBED_CONF_TARGET_LSE_DRIVE_LOAD_LEVEL
44+
# else
45+
# if defined(RCC_LSE_HIGHDRIVE_MODE)
46+
# define LSE_DRIVE_LOAD_LEVEL RCC_LSE_LOWPOWER_MODE
47+
# else
48+
# define LSE_DRIVE_LOAD_LEVEL RCC_LSEDRIVE_LOW
49+
# endif
50+
# endif
51+
52+
53+
/**
54+
* @brief configure the LSE crystal driver load
55+
* This settings ist target hardware dependend and
56+
* depends on the crystal that is used for LSE clock.
57+
* For low power requirements, crystals with low load capacitors can be used and
58+
* driver setting is RCC_LSEDRIVE_LOW.
59+
* For higher stablity, crystals with higher load capacitys can be used and
60+
* driver setting is RCC_LSEDRIVE_HIGH.
61+
*
62+
* A detailed description about this setting can be found here:
63+
* https://www.st.com/resource/en/application_note/cd00221665-oscillator-design-guide-for-stm8afals-stm32-mcus-and-mpus-stmicroelectronics.pdf
64+
*
65+
* LSE maybe used later, but crystal load drive setting is necessary before
66+
* enabling LSE.
67+
*
68+
* @param None
69+
* @retval None
70+
*/
71+
72+
static void LSEDriveConfig(void) {
73+
// this config can be changed only when LSE is stopped
74+
// LSE could be enabled before a reset and will remain running, disable first
75+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
76+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
77+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
78+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
79+
{
80+
error("LSEDriveConfig : failed to disable LSE\n");
81+
}
82+
83+
// set LSE drive level. Exception only for F4_g2 series
84+
HAL_PWR_EnableBkUpAccess();
85+
#if defined(__HAL_RCC_LSEDRIVE_CONFIG)
86+
__HAL_RCC_LSEDRIVE_CONFIG(LSE_DRIVE_LOAD_LEVEL);
87+
#else
88+
HAL_RCCEx_SelectLSEMode(LSE_DRIVE_LOAD_LEVEL);
89+
#endif
90+
}
91+
#endif // LSE_CONFIG_AVAILABLE
92+
3493
/**
3594
* @brief Setup the target board-specific configuration
3695
* of the microcontroller
@@ -120,6 +179,11 @@ void mbed_sdk_init()
120179

121180
/* Configure the System clock source, PLL Multiplier and Divider factors,
122181
AHB/APBx prescalers and Flash settings */
182+
#if defined(LSE_CONFIG_AVAILABLE)
183+
// LSE maybe used later, but crystal load drive setting is necessary before
184+
// enabling LSE
185+
LSEDriveConfig();
186+
#endif
123187
SetSysClock();
124188
SystemCoreClockUpdate();
125189

@@ -142,6 +206,9 @@ void mbed_sdk_init()
142206

143207
/* Configure the System clock source, PLL Multiplier and Divider factors,
144208
AHB/APBx prescalers and Flash settings */
209+
#if defined(LSE_CONFIG_AVAILABLE)
210+
LSEDriveConfig();
211+
#endif
145212
SetSysClock();
146213
SystemCoreClockUpdate();
147214
#endif /* DUAL_CORE */

targets/targets.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,11 @@
11981198
"help": "Mask value : USE_PLL_HSE_EXTC | USE_PLL_HSE_XTAL (need HW patch) | USE_PLL_HSI",
11991199
"value": "USE_PLL_HSE_EXTC|USE_PLL_HSI",
12001200
"macro_name": "CLOCK_SOURCE"
1201+
},
1202+
"lse_drive_load_level": {
1203+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
1204+
"value": "RCC_LSEDRIVE_LOW",
1205+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
12011206
}
12021207
},
12031208
"macros_add": [
@@ -1407,6 +1412,11 @@
14071412
"help": "Mask value : USE_PLL_HSE_EXTC | USE_PLL_HSE_XTAL (need HW patch) | USE_PLL_HSI",
14081413
"value": "USE_PLL_HSE_EXTC|USE_PLL_HSI",
14091414
"macro_name": "CLOCK_SOURCE"
1415+
},
1416+
"lse_drive_load_level": {
1417+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
1418+
"value": "RCC_LSEDRIVE_LOW",
1419+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
14101420
}
14111421
},
14121422
"device_has_add": [
@@ -2165,6 +2175,11 @@
21652175
"lpticker_lptim": {
21662176
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
21672177
"value": 1
2178+
},
2179+
"lse_drive_load_level": {
2180+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
2181+
"value": "RCC_LSEDRIVE_LOW",
2182+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
21682183
}
21692184
},
21702185
"overrides": {
@@ -2408,6 +2423,11 @@
24082423
"lpticker_lptim": {
24092424
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
24102425
"value": 1
2426+
},
2427+
"lse_drive_load_level": {
2428+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
2429+
"value": "RCC_LSEDRIVE_LOW",
2430+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
24112431
}
24122432
},
24132433
"extra_labels_add": [
@@ -2511,6 +2531,11 @@
25112531
"lpticker_lptim": {
25122532
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
25132533
"value": 1
2534+
},
2535+
"lse_drive_load_level": {
2536+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
2537+
"value": "RCC_LSEDRIVE_LOW",
2538+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
25142539
}
25152540
},
25162541
"extra_labels_add": [
@@ -2674,6 +2699,11 @@
26742699
"lpticker_lptim": {
26752700
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
26762701
"value": 1
2702+
},
2703+
"lse_drive_load_level": {
2704+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
2705+
"value": "RCC_LSEDRIVE_LOW",
2706+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
26772707
}
26782708
},
26792709
"components_add": [
@@ -2937,6 +2967,11 @@
29372967
"lpticker_lptim": {
29382968
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
29392969
"value": 1
2970+
},
2971+
"lse_drive_load_level": {
2972+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
2973+
"value": "RCC_LSEDRIVE_LOW",
2974+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
29402975
}
29412976
},
29422977
"macros_add": [
@@ -3170,6 +3205,11 @@
31703205
"lpticker_lptim": {
31713206
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
31723207
"value": 1
3208+
},
3209+
"lse_drive_load_level": {
3210+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
3211+
"value": "RCC_LSEDRIVE_LOW",
3212+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
31733213
}
31743214
},
31753215
"macros_add": [
@@ -3701,6 +3741,11 @@
37013741
"lpticker_lptim": {
37023742
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
37033743
"value": 1
3744+
},
3745+
"lse_drive_load_level": {
3746+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
3747+
"value": "RCC_LSEDRIVE_LOW",
3748+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
37043749
}
37053750
},
37063751
"overrides": {
@@ -3800,6 +3845,11 @@
38003845
"lpticker_lptim": {
38013846
"help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer",
38023847
"value": 1
3848+
},
3849+
"lse_drive_load_level": {
3850+
"help": "HSE drive load level RCC_LSEDRIVE_LOW | RCC_LSEDRIVE_MEDIUMLOW | RCC_LSEDRIVE_MEDIUMHIGH | RCC_LSEDRIVE_HIGH",
3851+
"value": "RCC_LSEDRIVE_LOW",
3852+
"macro_name": "TARGET_LSE_DRIVE_LOAD_LEVEL"
38033853
}
38043854
},
38053855
"macros_add": [

0 commit comments

Comments
 (0)