Skip to content

Commit 2e6a933

Browse files
committed
feat: add STM32WB0x support
Signed-off-by: Frederic Pillon <[email protected]>
1 parent b25fd2b commit 2e6a933

File tree

6 files changed

+78
-19
lines changed

6 files changed

+78
-19
lines changed

.github/workflows/CodeSpell.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
check_filenames: true
2424
check_hidden: true
2525
# In the event of a false positive, add the word in all lower case to this file:
26-
# ignore_words_file: ./CI/codespell/.codespellignore
26+
ignore_words_file: ./extras/.codespellignore

extras/.codespellignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hsi

src/STM32RTC.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ void STM32RTC::begin(bool resetTime, Hour_Format format)
6262
bool reinit;
6363

6464
_format = format;
65+
#if defined(RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048)
66+
reinit = RTC_init((format == HOUR_12) ? HOUR_FORMAT_12 : HOUR_FORMAT_24,
67+
(_mode == MODE_MIX) ? ::MODE_BINARY_MIX : ((_mode == MODE_BIN) ? ::MODE_BINARY_ONLY : ::MODE_BINARY_NONE),
68+
(_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
69+
(_clockSource == HSI_CLOCK) ? ::HSI_CLOCK : ::LSI_CLOCK
70+
, resetTime);
71+
#else
6572
reinit = RTC_init((format == HOUR_12) ? HOUR_FORMAT_12 : HOUR_FORMAT_24,
6673
(_mode == MODE_MIX) ? ::MODE_BINARY_MIX : ((_mode == MODE_BIN) ? ::MODE_BINARY_ONLY : ::MODE_BINARY_NONE),
6774
(_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
6875
(_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK
6976
, resetTime);
77+
#endif /* RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048 */
7078
_timeSet = !reinit;
7179

7280
syncDate();
@@ -137,8 +145,13 @@ void STM32RTC::setClockSource(Source_Clock source, uint32_t predivA, uint32_t pr
137145
{
138146
if (IS_CLOCK_SOURCE(source)) {
139147
_clockSource = source;
148+
#if defined(RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048)
149+
RTC_SetClockSource((_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
150+
(_clockSource == HSI_CLOCK) ? ::HSI_CLOCK : ::LSI_CLOCK);
151+
#else
140152
RTC_SetClockSource((_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
141153
(_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK);
154+
#endif /* RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048 */
142155
}
143156
RTC_setPrediv(predivA, predivS);
144157
}
@@ -1294,6 +1307,9 @@ void STM32RTC::configForLowPower(Source_Clock source)
12941307
#ifdef __HAL_RCC_RTCAPB_CLKAM_ENABLE
12951308
__HAL_RCC_RTCAPB_CLKAM_ENABLE();
12961309
#endif
1310+
#if defined(PWR_WAKEUP_PIN_RTC)
1311+
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN_RTC, PWR_WUP_RISIEDG);
1312+
#endif
12971313
#if defined(PWR_WAKEUP_LINE7)
12981314
HAL_PWR_EnableWakeUpLine(PWR_WAKEUP_LINE7, PWR_WAKEUP_SELECT_3, PWR_WAKEUP_POLARITY_HIGH);
12991315
#endif

src/STM32RTC.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@
6868

6969
typedef void(*voidFuncPtr)(void *);
7070

71+
#if defined(RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048)
72+
#define IS_CLOCK_SOURCE(SRC) (((SRC) == STM32RTC::LSI_CLOCK) || ((SRC) == STM32RTC::LSE_CLOCK) ||\
73+
((SRC) == STM32RTC::HSI_CLOCK))
74+
#else
7175
#define IS_CLOCK_SOURCE(SRC) (((SRC) == STM32RTC::LSI_CLOCK) || ((SRC) == STM32RTC::LSE_CLOCK) ||\
7276
((SRC) == STM32RTC::HSE_CLOCK))
77+
#endif /* RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048 */
7378
#define IS_HOUR_FORMAT(FMT) (((FMT) == STM32RTC::HOUR_12) || ((FMT) == STM32RTC::HOUR_24))
7479

7580
class STM32RTC {
@@ -107,7 +112,11 @@ class STM32RTC {
107112
enum Source_Clock : uint8_t {
108113
LSI_CLOCK = ::LSI_CLOCK,
109114
LSE_CLOCK = ::LSE_CLOCK,
115+
#if defined(RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048)
116+
HSI_CLOCK = ::HSI_CLOCK,
117+
#else
110118
HSE_CLOCK = ::HSE_CLOCK
119+
#endif /* RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048 */
111120
};
112121

113122
enum Alarm : uint32_t {

src/rtc.c

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ static voidCallbackPtr RTCSubSecondsUnderflowIrqCallback = NULL;
7171
#endif
7272
static sourceClock_t clkSrc = LSI_CLOCK;
7373
static uint32_t clkVal = LSI_VALUE;
74+
#if !defined(LL_RCC_LSCO_CLKSOURCE_HSI64M_DIV2048)
7475
static uint8_t HSEDiv = 0;
76+
#endif
7577
#if !defined(STM32F1xx)
7678
/* predividers values */
7779
static uint8_t predivSync_bits = 0xFF;
@@ -122,6 +124,13 @@ void RTC_SetClockSource(sourceClock_t source)
122124
clkSrc = source;
123125
if (source == LSE_CLOCK) {
124126
clkVal = LSE_VALUE;
127+
#if defined(LL_RCC_LSCO_CLKSOURCE_HSI64M_DIV2048)
128+
} else if (source == HSI_CLOCK) {
129+
/* HSI division factor for RTC clock must be define to ensure that
130+
* the clock supplied to the RTC is less than or equal to 1 MHz
131+
*/
132+
clkVal = 32000; /* HSI64M divided by 64 --> 1 MHz */
133+
#else
125134
} else if (source == HSE_CLOCK) {
126135
/* HSE division factor for RTC clock must be define to ensure that
127136
* the clock supplied to the RTC is less than or equal to 1 MHz
@@ -163,6 +172,7 @@ void RTC_SetClockSource(sourceClock_t source)
163172
Error_Handler();
164173
}
165174
clkVal = HSE_VALUE / HSEDiv;
175+
#endif
166176
} else if (source == LSI_CLOCK) {
167177
clkVal = LSI_VALUE;
168178
} else {
@@ -187,16 +197,20 @@ static void RTC_initClock(sourceClock_t source)
187197
if (source == LSE_CLOCK) {
188198
/* Enable the clock if not already set by user */
189199
enableClock(LSE_CLOCK);
190-
200+
#if defined(RCC_PERIPHCLK_RTC_WDG_BLEWKUP)
201+
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC_WDG_BLEWKUP;
202+
PeriphClkInit.RTCWDGBLEWKUPClockSelection = RCC_RTC_WDG_BLEWKUP_CLKSOURCE_LSE;
203+
} else if (source == HSI_CLOCK) {
204+
/* Enable the clock if not already set by user */
205+
enableClock(HSI_CLOCK);
206+
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC_WDG_BLEWKUP;
207+
PeriphClkInit.RTCWDGBLEWKUPClockSelection = RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048;
208+
#else
191209
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
192210
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
193-
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
194-
Error_Handler();
195-
}
196211
} else if (source == HSE_CLOCK) {
197212
/* Enable the clock if not already set by user */
198213
enableClock(HSE_CLOCK);
199-
200214
/* HSE division factor for RTC clock must be set to ensure that
201215
* the clock supplied to the RTC is less than or equal to 1 MHz
202216
*/
@@ -230,21 +244,27 @@ static void RTC_initClock(sourceClock_t source)
230244
#else
231245
#error "Could not define RTCClockSelection"
232246
#endif /* STM32F1xx */
233-
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
234-
Error_Handler();
235-
}
247+
#endif /* RCC_PERIPHCLK_RTC_WDG_BLEWKUP */
236248
} else if (source == LSI_CLOCK) {
237249
/* Enable the clock if not already set by user */
238250
enableClock(LSI_CLOCK);
239-
251+
#if defined(RCC_PERIPHCLK_RTC_WDG_BLEWKUP)
252+
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC_WDG_BLEWKUP;
253+
PeriphClkInit.RTCWDGBLEWKUPClockSelection = RCC_RTC_WDG_BLEWKUP_CLKSOURCE_LSI;
254+
#else
240255
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
241256
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
242-
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
243-
Error_Handler();
244-
}
257+
#endif /* RCC_PERIPHCLK_RTC_WDG_BLEWKUP */
245258
} else {
259+
/* Invalid clock source */
246260
Error_Handler();
247261
}
262+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
263+
Error_Handler();
264+
}
265+
#if defined(__HAL_RCC_RTC_CLK_ENABLE)
266+
__HAL_RCC_RTC_CLK_ENABLE();
267+
#endif
248268
}
249269

250270
/**
@@ -451,7 +471,9 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
451471
RtcHandle.Init.HourFormat = (format == HOUR_FORMAT_12) ? RTC_HOURFORMAT_12 : RTC_HOURFORMAT_24;
452472
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
453473
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
474+
#if defined(RTC_OUTPUT_TYPE_OPENDRAIN)
454475
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
476+
#endif
455477
#if defined(RTC_OUTPUT_PULLUP_NONE)
456478
RtcHandle.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
457479
#endif
@@ -507,6 +529,14 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
507529
reinit = true;
508530
} else {
509531
// RTC is already initialized
532+
#if defined(__HAL_RCC_GET_RTC_WDG_BLEWKUP_CLK_CONFIG)
533+
uint32_t oldRtcClockSource = __HAL_RCC_GET_RTC_WDG_BLEWKUP_CLK_CONFIG();
534+
oldRtcClockSource = ((oldRtcClockSource == RCC_RTC_WDG_BLEWKUP_CLKSOURCE_LSE) ? LSE_CLOCK :
535+
(oldRtcClockSource == RCC_RTC_WDG_BLEWKUP_CLKSOURCE_LSI) ? LSI_CLOCK :
536+
(oldRtcClockSource == RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048) ? HSI_CLOCK :
537+
// default case corresponding to no clock source
538+
0xFFFFFFFF);
539+
#else
510540
uint32_t oldRtcClockSource = __HAL_RCC_GET_RTC_SOURCE();
511541
oldRtcClockSource = ((oldRtcClockSource == RCC_RTCCLKSOURCE_LSE) ? LSE_CLOCK :
512542
(oldRtcClockSource == RCC_RTCCLKSOURCE_LSI) ? LSI_CLOCK :
@@ -521,7 +551,7 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
521551
#endif
522552
// default case corresponding to no clock source
523553
0xFFFFFFFF);
524-
554+
#endif
525555
#if defined(STM32F1xx)
526556
if ((RtcHandle.DateToUpdate.WeekDay == 0)
527557
&& (RtcHandle.DateToUpdate.Month == 0)
@@ -711,8 +741,8 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe
711741
/*RTC_TimeStruct.SubSeconds = subSeconds;*/
712742
/*RTC_TimeStruct.SecondFraction = 0;*/
713743
#endif /* RTC_SSR_SS */
714-
RTC_TimeStruct.DayLightSaving = RTC_STOREOPERATION_RESET;
715-
RTC_TimeStruct.StoreOperation = RTC_DAYLIGHTSAVING_NONE;
744+
RTC_TimeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
745+
RTC_TimeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
716746
#else
717747
UNUSED(period);
718748
#endif /* !STM32F1xx */
@@ -1192,7 +1222,8 @@ void RTC_Alarm_IRQHandler(void)
11921222
defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F070xB) || \
11931223
defined(STM32F030xC) || defined(STM32G0xx) || defined(STM32H5xx) || \
11941224
defined(STM32L0xx) || defined(STM32L5xx) || defined(STM32U0xx) ||\
1195-
defined(STM32U3xx) || defined(STM32U5xx) || defined(STM32WBAxx)
1225+
defined(STM32U3xx) || defined(STM32U5xx) || defined(STM32WB0x) || \
1226+
defined(STM32WBAxx)
11961227
// In some cases, the same vector is used to manage WakeupTimer,
11971228
// but with a dedicated HAL IRQHandler
11981229
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);

src/rtc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ typedef void(*voidCallbackPtr)(void *);
130130

131131
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32H5xx) || \
132132
defined(STM32L0xx) || defined(STM32L5xx) || defined(STM32U3xx) || \
133-
defined(STM32U3xx) || defined(STM32U5xx) || defined(STM32WBAxx)
133+
defined(STM32U3xx) || defined(STM32U5xx) || defined(STM32WB0x) || \
134+
defined(STM32WBAxx)
134135
#define RTC_Alarm_IRQn RTC_IRQn
135136
#define RTC_Alarm_IRQHandler RTC_IRQHandler
136137
#endif
@@ -142,7 +143,8 @@ typedef void(*voidCallbackPtr)(void *);
142143
/* mapping the IRQn for the one-second interrupt depending on the soc */
143144
#if defined(STM32F1xx) || (defined(STM32F0xx) && defined(RTC_CR_WUTE)) || \
144145
defined(STM32H5xx) || defined(STM32L0xx) || defined(STM32L5xx) || \
145-
defined(STM32U3xx) || defined(STM32U5xx) || defined(STM32WBAxx)
146+
defined(STM32U3xx) || defined(STM32U5xx) || defined(STM32WB0x) || \
147+
defined(STM32WBAxx)
146148
// specific WakeUp interrupt
147149
#define ONESECOND_IRQn RTC_IRQn
148150
#elif defined(STM32MP1xx)

0 commit comments

Comments
 (0)