Skip to content

Commit 3f9a04c

Browse files
authored
Merge pull request #2760 from svastm/lp_timer_f4
STM32F4 - Add low power timer
2 parents b481da4 + 0766d39 commit 3f9a04c

File tree

5 files changed

+261
-16
lines changed

5 files changed

+261
-16
lines changed

hal/targets.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@
795795
"progen": {"target": "nucleo-f410rb"},
796796
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
797797
"detect_code": ["0740"],
798-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
798+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
799799
"release_versions": ["2", "5"]
800800
},
801801
"NUCLEO_F411RE": {
@@ -808,7 +808,7 @@
808808
"progen": {"target": "nucleo-f411re"},
809809
"detect_code": ["0740"],
810810
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
811-
"device_has": ["ANALOGIN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
811+
"device_has": ["ANALOGIN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
812812
"release_versions": ["2", "5"]
813813
},
814814
"ELMO_F411RE": {
@@ -833,7 +833,7 @@
833833
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
834834
"progen": {"target": "nucleo-f429zi"},
835835
"macros": ["DEVICE_RTC_LSI=1", "TRANSACTION_QUEUE_SIZE_SPI=2"],
836-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
836+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
837837
"detect_code": ["0796"],
838838
"features": ["IPV4"],
839839
"release_versions": ["2", "5"]
@@ -848,7 +848,7 @@
848848
"progen": {"target": "nucleo-f446re"},
849849
"detect_code": ["0777"],
850850
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
851-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
851+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
852852
"release_versions": ["2", "5"]
853853
},
854854
"NUCLEO_F446ZE": {
@@ -861,7 +861,7 @@
861861
"progen": {"target": "nucleo-f446ze"},
862862
"detect_code": ["0778"],
863863
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
864-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
864+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
865865
"release_versions": ["2", "5"]
866866
},
867867

@@ -874,7 +874,7 @@
874874
"inherits": ["Target"],
875875
"detect_code": ["0840"],
876876
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
877-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
877+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
878878
"release_versions": ["2", "5"]
879879
},
880880
"NUCLEO_F746ZG": {
@@ -1088,7 +1088,7 @@
10881088
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
10891089
"progen": {"target": "disco-f469ni"},
10901090
"detect_code": ["0788"],
1091-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
1091+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
10921092
"release_versions": ["2", "5"]
10931093
},
10941094
"DISCO_L053C8": {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2016, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
#include "device.h"
31+
32+
#if DEVICE_LOWPOWERTIMER
33+
34+
#include "ticker_api.h"
35+
#include "lp_ticker_api.h"
36+
#include "rtc_api.h"
37+
#include "rtc_api_hal.h"
38+
39+
static uint8_t lp_ticker_inited = 0;
40+
41+
void lp_ticker_init(void)
42+
{
43+
if (lp_ticker_inited) return;
44+
lp_ticker_inited = 1;
45+
46+
rtc_init();
47+
rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler);
48+
}
49+
50+
uint32_t lp_ticker_read(void)
51+
{
52+
uint32_t usecs;
53+
time_t time;
54+
55+
lp_ticker_init();
56+
57+
do {
58+
time = rtc_read();
59+
usecs = rtc_read_subseconds();
60+
} while (time != rtc_read());
61+
62+
return (time * 1000000) + usecs;
63+
}
64+
65+
void lp_ticker_set_interrupt(timestamp_t timestamp)
66+
{
67+
uint32_t delta;
68+
69+
delta = timestamp - lp_ticker_read();
70+
rtc_set_wake_up_timer(delta);
71+
}
72+
73+
void lp_ticker_disable_interrupt(void)
74+
{
75+
rtc_deactivate_wake_up_timer();
76+
}
77+
78+
void lp_ticker_clear_interrupt(void)
79+
{
80+
81+
}
82+
83+
#endif

hal/targets/hal/TARGET_STM/TARGET_STM32F4/rtc_api.c

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2014, STMicroelectronics
3+
* Copyright (c) 2016, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -28,6 +28,7 @@
2828
*******************************************************************************
2929
*/
3030
#include "rtc_api.h"
31+
#include "rtc_api_hal.h"
3132

3233
#if DEVICE_RTC
3334

@@ -39,12 +40,31 @@ static int rtc_inited = 0;
3940

4041
static RTC_HandleTypeDef RtcHandle;
4142

43+
#if DEVICE_RTC_LSI
44+
#define RTC_CLOCK LSI_VALUE
45+
#else
46+
#define RTC_CLOCK LSE_VALUE
47+
#endif
48+
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
56+
57+
#if DEVICE_LOWPOWERTIMER
58+
static void (*irq_handler)(void);
59+
static void RTC_IRQHandler();
60+
#endif
61+
4262
void rtc_init(void)
4363
{
4464
RCC_OscInitTypeDef RCC_OscInitStruct;
45-
uint32_t rtc_freq = 0;
4665

4766
#if DEVICE_RTC_LSI
67+
if (rtc_inited) return;
4868
rtc_inited = 1;
4969
#endif
5070

@@ -59,9 +79,7 @@ void rtc_init(void)
5979
// Connect LSE to RTC
6080
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
6181
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
62-
rtc_freq = LSE_VALUE;
63-
}
64-
else {
82+
} else {
6583
error("RTC error: LSE clock initialization failed.");
6684
}
6785
#else
@@ -86,23 +104,35 @@ void rtc_init(void)
86104
// Connect LSI to RTC
87105
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
88106
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
89-
// [TODO] This value is LSI typical value. To be measured precisely using a timer input capture
90-
rtc_freq = LSI_VALUE;
91107
#endif
92108

93109
// Enable RTC
94110
__HAL_RCC_RTC_ENABLE();
95111

96112
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
97-
RtcHandle.Init.AsynchPrediv = 127;
98-
RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
113+
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
114+
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
99115
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
100116
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
101117
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
102118

103119
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
104120
error("RTC error: RTC initialization failed.");
105121
}
122+
123+
#if DEVICE_LOWPOWERTIMER
124+
#if DEVICE_RTC_LSI
125+
rtc_write(0);
126+
#else
127+
if (!rtc_isenabled()) {
128+
rtc_write(0);
129+
}
130+
#endif
131+
NVIC_ClearPendingIRQ(RTC_WKUP_IRQn);
132+
NVIC_DisableIRQ(RTC_WKUP_IRQn);
133+
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
134+
NVIC_EnableIRQ(RTC_WKUP_IRQn);
135+
#endif
106136
}
107137

108138
void rtc_free(void)
@@ -129,6 +159,7 @@ void rtc_free(void)
129159
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
130160
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
131161
HAL_RCC_OscConfig(&RCC_OscInitStruct);
162+
132163
#if DEVICE_RTC_LSI
133164
rtc_inited = 0;
134165
#endif
@@ -218,4 +249,50 @@ void rtc_write(time_t t)
218249
HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
219250
}
220251

252+
#if DEVICE_LOWPOWERTIMER
253+
254+
static void RTC_IRQHandler()
255+
{
256+
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
257+
}
258+
259+
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
260+
{
261+
if (irq_handler) {
262+
// Fire the user callback
263+
irq_handler();
264+
}
265+
}
266+
267+
void rtc_set_irq_handler(uint32_t handler)
268+
{
269+
irq_handler = (void (*)(void))handler;
270+
}
271+
272+
uint32_t rtc_read_subseconds(void)
273+
{
274+
return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1));
275+
}
276+
277+
void rtc_set_wake_up_timer(uint32_t delta)
278+
{
279+
uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK);
280+
281+
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter,
282+
RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) {
283+
error("Set wake up timer failed\n");
284+
}
285+
}
286+
287+
void rtc_deactivate_wake_up_timer(void)
288+
{
289+
HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
290+
}
291+
292+
void rtc_synchronize(void)
293+
{
294+
HAL_RTC_WaitForSynchro(&RtcHandle);
295+
}
296+
#endif // DEVICE_LOWPOWERTIMER
297+
221298
#endif
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2016, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
31+
#ifndef MBED_RTC_API_HAL_H
32+
#define MBED_RTC_API_HAL_H
33+
34+
#include <stdint.h>
35+
#include "rtc_api.h"
36+
37+
#ifdef __cplusplus
38+
extern "C" {
39+
#endif
40+
/*
41+
* Extend rtc_api.h
42+
*/
43+
44+
/** Set the given function as handler of wakeup timer event.
45+
*
46+
* @param handler The function to set as handler
47+
*/
48+
void rtc_set_irq_handler(uint32_t handler);
49+
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);
73+
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif

0 commit comments

Comments
 (0)