Skip to content

Commit 50ccd99

Browse files
committed
[clock] Add new api enableClock
Handle properly clock activation. Signed-off-by: Frederic Pillon <[email protected]>
1 parent f9b701b commit 50ccd99

File tree

5 files changed

+76
-67
lines changed

5 files changed

+76
-67
lines changed

cores/arduino/stm32/clock.c

+59-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
******************************************************************************
3737
*/
38-
#include "stm32_def.h"
38+
#include "clock.h"
3939

4040
#ifdef __cplusplus
4141
extern "C" {
@@ -120,6 +120,64 @@ void delayInsideIT(uint32_t delay_us)
120120
#endif
121121
}
122122

123+
/**
124+
* @brief Enable the specified clock if not already set
125+
* @param source: clock source: LSE_CLOCK, LSI_CLOCK, HSI_CLOCK or HSE_CLOCK
126+
* @retval None
127+
*/
128+
void enableClock(sourceClock_t source)
129+
{
130+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
131+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
132+
133+
switch(source) {
134+
case LSI_CLOCK:
135+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) {
136+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
137+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
138+
}
139+
break;
140+
case HSI_CLOCK:
141+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) {
142+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
143+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
144+
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
145+
}
146+
break;
147+
case LSE_CLOCK:
148+
/* Enable Power Clock */
149+
if(__HAL_RCC_PWR_IS_CLK_DISABLED()) {
150+
__HAL_RCC_PWR_CLK_ENABLE();
151+
}
152+
#ifdef HAL_PWR_MODULE_ENABLED
153+
/* Allow access to Backup domain */
154+
HAL_PWR_EnableBkUpAccess();
155+
#endif
156+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
157+
#ifdef __HAL_RCC_LSEDRIVE_CONFIG
158+
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
159+
#endif
160+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
161+
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
162+
}
163+
break;
164+
case HSE_CLOCK:
165+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) {
166+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
167+
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
168+
}
169+
break;
170+
default:
171+
/* No valid clock to enable */
172+
break;
173+
}
174+
if(RCC_OscInitStruct.OscillatorType != RCC_OSCILLATORTYPE_NONE) {
175+
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
176+
Error_Handler();
177+
}
178+
}
179+
}
180+
123181
#ifdef __cplusplus
124182
}
125183
#endif

cores/arduino/stm32/clock.h

+9
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,22 @@
4747
#endif
4848

4949
/* Exported types ------------------------------------------------------------*/
50+
/* Clock source selection */
51+
typedef enum {
52+
LSI_CLOCK,
53+
HSI_CLOCK,
54+
LSE_CLOCK,
55+
HSE_CLOCK
56+
} sourceClock_t;
57+
5058
/* Exported constants --------------------------------------------------------*/
5159
/* Exported macro ------------------------------------------------------------*/
5260
/* Exported functions ------------------------------------------------------- */
5361
uint32_t GetCurrentMilli(void);
5462
uint32_t GetCurrentMicro(void);
5563
void delayInsideIT(uint32_t delay_us);
5664

65+
void enableClock(sourceClock_t source);
5766
#ifdef __cplusplus
5867
}
5968
#endif

cores/arduino/stm32/rtc.c

+4-36
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,11 @@ void RTC_SetClockSource(sourceClock_t source)
100100
*/
101101
static void RTC_initClock(sourceClock_t source)
102102
{
103-
RCC_OscInitTypeDef RCC_OscInitStruct;
104103
RCC_PeriphCLKInitTypeDef PeriphClkInit;
105104

106105
if(source == LSE_CLOCK) {
107106
/* Enable the clock if not already set by user */
108-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
109-
#ifdef __HAL_RCC_LSEDRIVE_CONFIG
110-
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
111-
#endif
112-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
113-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
114-
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
115-
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
116-
Error_Handler();
117-
}
118-
}
107+
enableClock(LSE_CLOCK);
119108

120109
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
121110
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
@@ -125,14 +114,8 @@ static void RTC_initClock(sourceClock_t source)
125114
clkSrc = LSE_CLOCK;
126115
} else if(source == HSE_CLOCK) {
127116
/* Enable the clock if not already set by user */
128-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) {
129-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
130-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
131-
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
132-
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
133-
Error_Handler();
134-
}
135-
}
117+
enableClock(HSE_CLOCK);
118+
136119
/* HSE division factor for RTC clock must be set to ensure that
137120
* the clock supplied to the RTC is less than or equal to 1 MHz
138121
*/
@@ -182,14 +165,7 @@ static void RTC_initClock(sourceClock_t source)
182165
clkSrc = HSE_CLOCK;
183166
} else if(source == LSI_CLOCK) {
184167
/* Enable the clock if not already set by user */
185-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) {
186-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
187-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
188-
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
189-
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
190-
Error_Handler();
191-
}
192-
}
168+
enableClock(LSI_CLOCK);
193169

194170
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
195171
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
@@ -314,14 +290,6 @@ void RTC_init(hourFormat_t format, sourceClock_t source)
314290
{
315291
initFormat = format;
316292

317-
/* Enable Power Clock */
318-
__HAL_RCC_PWR_CLK_ENABLE();
319-
320-
#ifdef HAL_PWR_MODULE_ENABLED
321-
/* Allow access to Backup domain */
322-
HAL_PWR_EnableBkUpAccess();
323-
#endif
324-
325293
/* Init RTC clock */
326294
RTC_initClock(source);
327295

cores/arduino/stm32/rtc.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define __RTC_H
4141

4242
/* Includes ------------------------------------------------------------------*/
43-
#include "stm32_def.h"
43+
#include "clock.h"
4444

4545
#ifdef HAL_RTC_MODULE_ENABLED
4646

@@ -72,13 +72,6 @@ typedef enum {
7272
Y_MSK = 32
7373
} alarmMask_t;
7474

75-
/* Clock source selection */
76-
typedef enum {
77-
LSI_CLOCK,
78-
LSE_CLOCK,
79-
HSE_CLOCK
80-
} sourceClock_t;
81-
8275
typedef void(*voidCallbackPtr)(void *);
8376

8477
/* Exported constants --------------------------------------------------------*/

cores/arduino/stm32/uart.c

+3-22
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,7 @@ void uart_init(serial_t *obj)
328328
/* If baudrate is lower than or equal to 9600 try to change to LSE */
329329
if(obj->baudrate <= 9600) {
330330
/* Enable the clock if not already set by user */
331-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
332-
#ifdef __HAL_RCC_LSEDRIVE_CONFIG
333-
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
334-
#endif
335-
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
336-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
337-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
338-
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
339-
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
340-
Error_Handler();
341-
}
342-
}
331+
enableClock(LSE_CLOCK);
343332

344333
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_LSE);
345334
if (HAL_UART_Init(huart) == HAL_OK) {
@@ -496,17 +485,9 @@ void uart_config_lowpower(serial_t *obj)
496485
if(obj == NULL) {
497486
return;
498487
}
499-
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
500488
/* Ensure HSI clock is enable */
501-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) {
502-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
503-
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
504-
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
505-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
506-
if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK) {
507-
Error_Handler();
508-
}
509-
}
489+
enableClock(HSI_CLOCK);
490+
510491
/* Configure HSI as source clock for low power wakeup clock */
511492
switch (obj->index) {
512493
#if defined(USART1_BASE)

0 commit comments

Comments
 (0)