Skip to content

Commit a765afd

Browse files
authored
Merge pull request #2759 from svastm/lp_timer_f0
STM32F0 - Add low power timer
2 parents 3c6776f + 369ecf2 commit a765afd

File tree

5 files changed

+120
-159
lines changed

5 files changed

+120
-159
lines changed

hal/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@
615615
"inherits": ["Target"],
616616
"progen": {"target": "nucleo-f030r8"},
617617
"detect_code": ["0725"],
618-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
618+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
619619
"default_lib": "small",
620620
"release_versions": ["2"]
621621
},

hal/targets/hal/TARGET_STM/TARGET_STM32F0/lp_ticker.c

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,47 @@
3737
#include "rtc_api_hal.h"
3838

3939
static uint8_t lp_ticker_inited = 0;
40-
static uint8_t lp_ticker_reconf_presc = 0;
4140

42-
void lp_ticker_init() {
41+
void lp_ticker_init(void)
42+
{
4343
if (lp_ticker_inited) return;
4444
lp_ticker_inited = 1;
4545

4646
rtc_init();
4747
rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler);
4848
}
4949

50-
uint32_t lp_ticker_read() {
51-
uint32_t sub_secs, milis;
50+
uint32_t lp_ticker_read(void)
51+
{
52+
uint32_t usecs;
5253
time_t time;
5354

5455
lp_ticker_init();
5556

57+
do {
5658
time = rtc_read();
57-
sub_secs = rtc_read_subseconds();
58-
milis = 1000 - (sub_secs * 1000 / rtc_ticker_get_synch_presc());
59+
usecs = rtc_read_subseconds();
60+
} while (time != rtc_read());
5961

60-
return (time * 1000000) + (milis * 1000);
62+
return (time * 1000000) + usecs;
6163
}
6264

63-
void lp_ticker_set_interrupt(timestamp_t timestamp) {
64-
uint32_t sub_secs, delta, milis;
65-
time_t secs;
66-
struct tm *timeinfo;
65+
void lp_ticker_set_interrupt(timestamp_t timestamp)
66+
{
67+
uint32_t delta;
6768

68-
// Reconfigure RTC prescalers whenever the timestamp is below 30ms
69-
if (!lp_ticker_reconf_presc && timestamp < 30000) {
70-
rtc_reconfigure_prescalers();
71-
lp_ticker_reconf_presc = 1;
69+
delta = timestamp - lp_ticker_read();
70+
rtc_set_wake_up_timer(delta);
7271
}
7372

74-
milis = (timestamp % 1000000) / 1000;
75-
76-
secs = rtc_read();
77-
delta = ((timestamp / 1000000) - secs);
78-
79-
secs += delta;
80-
sub_secs = (rtc_ticker_get_synch_presc() * (1000 - milis)) / 1000;
81-
timeinfo = localtime(&secs);
82-
83-
rtc_set_alarm(timeinfo, sub_secs);
73+
void lp_ticker_disable_interrupt(void)
74+
{
75+
rtc_deactivate_wake_up_timer();
8476
}
8577

86-
void lp_ticker_disable_interrupt() {
87-
lp_ticker_reconf_presc = 0;
88-
rtc_ticker_disable_irq();
89-
}
78+
void lp_ticker_clear_interrupt(void)
79+
{
9080

91-
void lp_ticker_clear_interrupt() {
9281
}
9382

9483
#endif

hal/targets/hal/TARGET_STM/TARGET_STM32F0/rtc_api.c

Lines changed: 69 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2015, STMicroelectronics
3+
* Copyright (c) 2016, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -29,8 +29,6 @@
2929
*/
3030
#include "rtc_api.h"
3131
#include "rtc_api_hal.h"
32-
#include "stm32f0xx.h"
33-
#include "stm32f0xx_hal_rtc_ex.h"
3432

3533
#if DEVICE_RTC
3634

@@ -42,60 +40,27 @@ static int rtc_inited = 0;
4240

4341
static RTC_HandleTypeDef RtcHandle;
4442

45-
#if DEVICE_LOWPOWERTIMER
46-
static uint32_t m_synch_prediv = RTC_SYNCH_PREDIV;
47-
static uint32_t m_asynch_prediv = RTC_ASYNCH_PREDIV;
48-
49-
static void (*irq_handler)(void);
50-
51-
static void rtc_configure_time_and_date()
52-
{
53-
RTC_TimeTypeDef mTime;
54-
RTC_DateTypeDef mDate;
55-
56-
mDate.WeekDay = 1;
57-
mDate.Month = 1;
58-
mDate.Date = 1;
59-
mDate.Year = 2;
60-
if (HAL_RTC_SetDate(&RtcHandle, &mDate, RTC_FORMAT_BIN) != HAL_OK) {
61-
error("Date set failed\n");
62-
}
63-
64-
mTime.Hours = 0;
65-
mTime.Minutes = 0;
66-
mTime.Seconds = 0;
67-
mTime.TimeFormat = RTC_HOURFORMAT_24;
68-
mTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
69-
mTime.StoreOperation = RTC_STOREOPERATION_RESET;
70-
if (HAL_RTC_SetTime(&RtcHandle, &mTime, RTC_FORMAT_BIN) != HAL_OK) {
71-
error("Time set failed\n");
72-
}
73-
}
74-
75-
void RTC_IRQHandler()
76-
{
77-
HAL_RTC_AlarmIRQHandler(&RtcHandle);
78-
}
79-
80-
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
81-
{
82-
if (irq_handler)
83-
{
84-
// Fire the user callback
85-
irq_handler();
86-
}
87-
}
43+
#if DEVICE_RTC_LSI
44+
#define RTC_CLOCK LSI_VALUE
45+
#else
46+
#define RTC_CLOCK LSE_VALUE
47+
#endif
8848

89-
void rtc_set_irq_handler(uint32_t handler)
90-
{
91-
irq_handler = (void (*)(void)) handler;
92-
}
49+
#if DEVICE_LOWPOWERTIMER
50+
#define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000)
51+
#define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1)
52+
#else
53+
#define RTC_ASYNCH_PREDIV (0x007F)
54+
#define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1)
55+
#endif
9356

57+
#if DEVICE_LOWPOWERTIMER
58+
static void (*irq_handler)(void);
59+
static void RTC_IRQHandler(void);
9460
#endif
9561

9662
void rtc_init(void) {
9763
RCC_OscInitTypeDef RCC_OscInitStruct;
98-
uint32_t rtc_freq = 0;
9964

10065
#if DEVICE_RTC_LSI
10166
if (rtc_inited) return;
@@ -113,7 +78,6 @@ void rtc_init(void) {
11378
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
11479
// Connect LSE to RTC
11580
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
116-
rtc_freq = LSE_VALUE;
11781
} else {
11882
error("Cannot initialize RTC with LSE\n");
11983
}
@@ -128,32 +92,24 @@ void rtc_init(void) {
12892
__HAL_RCC_BACKUPRESET_FORCE();
12993
__HAL_RCC_BACKUPRESET_RELEASE();
13094

131-
// Enable LSI clock
132-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
133-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
134-
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
135-
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
136-
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
137-
error("Cannot initialize RTC with LSI\n");
138-
}
139-
// Connect LSI to RTC
140-
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
141-
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
142-
rtc_freq = LSI_VALUE;
95+
// Enable LSI clock
96+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
97+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
98+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
99+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
100+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
101+
error("Cannot initialize RTC with LSI\n");
102+
}
103+
// Connect LSI to RTC
104+
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
143105
#endif
144106

145107
// Enable RTC
146108
__HAL_RCC_RTC_ENABLE();
147109

148110
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
149-
#if !DEVICE_LOWPOWERTIMER
150-
RtcHandle.Init.AsynchPrediv = 127;
151-
RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
152-
#else
153-
RtcHandle.Init.AsynchPrediv = m_asynch_prediv;
154-
RtcHandle.Init.SynchPrediv = m_synch_prediv;
155-
#endif
156-
111+
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
112+
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
157113
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
158114
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
159115
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
@@ -163,9 +119,17 @@ void rtc_init(void) {
163119
}
164120

165121
#if DEVICE_LOWPOWERTIMER
166-
rtc_configure_time_and_date();
167-
NVIC_SetVector(RTC_IRQn, (uint32_t)&RTC_IRQHandler);
168-
HAL_NVIC_EnableIRQ(RTC_IRQn);
122+
#if DEVICE_RTC_LSI
123+
rtc_write(0);
124+
#else
125+
if (!rtc_isenabled()) {
126+
rtc_write(0);
127+
}
128+
#endif
129+
NVIC_ClearPendingIRQ(RTC_IRQn);
130+
NVIC_DisableIRQ(RTC_IRQn);
131+
NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler);
132+
NVIC_EnableIRQ(RTC_IRQn);
169133
#endif
170134
}
171135

@@ -203,9 +167,9 @@ int rtc_isenabled(void) {
203167
return rtc_inited;
204168
#else
205169
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) {
206-
return 1;
170+
return 1;
207171
} else {
208-
return 0;
172+
return 0;
209173
}
210174
#endif
211175
}
@@ -282,46 +246,48 @@ void rtc_write(time_t t) {
282246
}
283247

284248
#if DEVICE_LOWPOWERTIMER
285-
void rtc_set_alarm(struct tm *ti, uint32_t subsecs)
249+
250+
static void RTC_IRQHandler(void)
251+
{
252+
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
253+
}
254+
255+
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
286256
{
287-
RTC_AlarmTypeDef mAlarm;
288-
289-
mAlarm.AlarmTime.Hours = ti->tm_hour;
290-
mAlarm.AlarmTime.Minutes = ti->tm_min;
291-
mAlarm.AlarmTime.Seconds = ti->tm_sec;
292-
mAlarm.AlarmTime.SubSeconds = subsecs;
293-
mAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT_24;
294-
mAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
295-
mAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
296-
mAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
297-
mAlarm.AlarmDateWeekDay = 1;
298-
mAlarm.Alarm = RTC_ALARM_A;
299-
300-
if (HAL_RTC_SetAlarm_IT(&RtcHandle, &mAlarm, RTC_FORMAT_BIN) != HAL_OK) {
301-
error("Set Alarm failed\n");
257+
if (irq_handler) {
258+
// Fire the user callback
259+
irq_handler();
302260
}
303261
}
304262

305-
void rtc_reconfigure_prescalers()
263+
void rtc_set_irq_handler(uint32_t handler)
264+
{
265+
irq_handler = (void (*)(void))handler;
266+
}
267+
268+
uint32_t rtc_read_subseconds(void)
306269
{
307-
m_synch_prediv = 0x3FF;
308-
m_asynch_prediv = 0x1F;
309-
rtc_init();
270+
return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1));
310271
}
311272

312-
uint32_t rtc_ticker_get_synch_presc()
273+
void rtc_set_wake_up_timer(uint32_t delta)
313274
{
314-
return m_synch_prediv;
275+
uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK);
276+
277+
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter,
278+
RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) {
279+
error("Set wake up timer failed\n");
280+
}
315281
}
316282

317-
uint32_t rtc_read_subseconds()
283+
void rtc_deactivate_wake_up_timer(void)
318284
{
319-
return RTC->SSR;
285+
HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
320286
}
321287

322-
void rtc_ticker_disable_irq()
288+
void rtc_synchronize(void)
323289
{
324-
HAL_RTC_DeactivateAlarm(&RtcHandle, RTC_ALARM_A);
290+
HAL_RTC_WaitForSynchro(&RtcHandle);
325291
}
326292
#endif // DEVICE_LOWPOWERTIMER
327293

hal/targets/hal/TARGET_STM/TARGET_STM32F0/rtc_api_hal.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,36 @@ extern "C" {
4141
* Extend rtc_api.h
4242
*/
4343

44-
// Prescaler values for LSE clock
45-
#define RTC_ASYNCH_PREDIV 0x7F
46-
#define RTC_SYNCH_PREDIV 0x00FF
47-
44+
/** Set the given function as handler of wakeup timer event.
45+
*
46+
* @param handler The function to set as handler
47+
*/
4848
void rtc_set_irq_handler(uint32_t handler);
4949

50-
void rtc_ticker_disable_irq();
51-
uint32_t rtc_ticker_get_synch_presc();
50+
/** Read the subsecond register.
51+
*
52+
* @return The remaining time as microseconds (0-999999)
53+
*/
54+
uint32_t rtc_read_subseconds(void);
55+
56+
/** Program a wake up timer event in delta microseconds.
57+
*
58+
* @param delta The time to wait
59+
*/
60+
void rtc_set_wake_up_timer(uint32_t delta);
61+
62+
/** Disable the wake up timer event.
63+
*
64+
* The wake up timer use auto reload, you have to deactivate it manually.
65+
*/
66+
void rtc_deactivate_wake_up_timer(void);
67+
68+
/** Synchronise the RTC shadow registers.
69+
*
70+
* Must be called after a deepsleep.
71+
*/
72+
void rtc_synchronize(void);
5273

53-
void rtc_set_alarm(struct tm *ti, uint32_t subsecs);
54-
uint32_t rtc_read_subseconds();
55-
void rtc_reconfigure_prescalers();
5674

5775
#ifdef __cplusplus
5876
}

0 commit comments

Comments
 (0)