Skip to content

Commit 35a8b1f

Browse files
authored
Merge pull request #2764 from svastm/lp_timer_l4
STM32L4 - Add low power timer
2 parents 7d464b1 + 883ff3a commit 35a8b1f

File tree

5 files changed

+252
-9
lines changed

5 files changed

+252
-9
lines changed

hal/targets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@
983983
"inherits": ["Target"],
984984
"progen": {"target": "nucleo-l432kc"},
985985
"detect_code": ["0770"],
986-
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "CAN", "STDIO_MESSAGES"],
986+
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "CAN", "STDIO_MESSAGES"],
987987
"release_versions": ["2", "5"]
988988
},
989989
"NUCLEO_L476RG": {
@@ -995,7 +995,7 @@
995995
"inherits": ["Target"],
996996
"progen": {"target": "nucleo-l476rg"},
997997
"detect_code": ["0765"],
998-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
998+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
999999
"release_versions": ["2", "5"]
10001000
},
10011001
"STM32F3XX": {
@@ -1138,7 +1138,7 @@
11381138
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
11391139
"progen": {"target": "disco-l476vg"},
11401140
"detect_code": ["0820"],
1141-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
1141+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
11421142
"release_versions": ["2", "5"]
11431143
},
11441144
"MTS_MDOT_F405RG": {
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_STM32L4/rtc_api.c

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*******************************************************************************
2929
*/
3030
#include "rtc_api.h"
31+
#include "rtc_api_hal.h"
3132

3233
#if DEVICE_RTC
3334

@@ -39,11 +40,29 @@ 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(void);
60+
#endif
61+
4262
void rtc_init(void)
4363
{
4464
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
4565
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
46-
uint32_t rtc_freq = 0;
4766

4867
#if DEVICE_RTC_LSI
4968
if (rtc_inited) return;
@@ -63,7 +82,6 @@ void rtc_init(void)
6382
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
6483
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
6584
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
66-
rtc_freq = LSE_VALUE;
6785
} else {
6886
error("Cannot initialize RTC with LSE\n");
6987
}
@@ -92,8 +110,6 @@ void rtc_init(void)
92110
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
93111
error("Cannot initialize RTC with LSI\n");
94112
}
95-
// This value is LSI typical value (see device datasheet)
96-
rtc_freq = 32000;
97113
#endif
98114

99115
// Check if RTC is already initialized
@@ -103,15 +119,29 @@ void rtc_init(void)
103119
__HAL_RCC_RTC_ENABLE();
104120

105121
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
106-
RtcHandle.Init.AsynchPrediv = 127;
107-
RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
122+
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
123+
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
108124
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
109125
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
110126
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
111127

112128
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
113129
error("Cannot initialize RTC\n");
114130
}
131+
132+
#if DEVICE_LOWPOWERTIMER
133+
#if DEVICE_RTC_LSI
134+
rtc_write(0);
135+
#else
136+
if (!rtc_isenabled()) {
137+
rtc_write(0);
138+
}
139+
#endif
140+
NVIC_ClearPendingIRQ(RTC_WKUP_IRQn);
141+
NVIC_DisableIRQ(RTC_WKUP_IRQn);
142+
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
143+
NVIC_EnableIRQ(RTC_WKUP_IRQn);
144+
#endif
115145
}
116146

117147
void rtc_free(void)
@@ -231,4 +261,50 @@ void rtc_write(time_t t)
231261
HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
232262
}
233263

264+
#if DEVICE_LOWPOWERTIMER
265+
266+
static void RTC_IRQHandler(void)
267+
{
268+
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
269+
}
270+
271+
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
272+
{
273+
if (irq_handler) {
274+
// Fire the user callback
275+
irq_handler();
276+
}
277+
}
278+
279+
void rtc_set_irq_handler(uint32_t handler)
280+
{
281+
irq_handler = (void (*)(void))handler;
282+
}
283+
284+
uint32_t rtc_read_subseconds(void)
285+
{
286+
return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1));
287+
}
288+
289+
void rtc_set_wake_up_timer(uint32_t delta)
290+
{
291+
uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK);
292+
293+
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter,
294+
RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) {
295+
error("Set wake up timer failed\n");
296+
}
297+
}
298+
299+
void rtc_deactivate_wake_up_timer(void)
300+
{
301+
HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
302+
}
303+
304+
void rtc_synchronize(void)
305+
{
306+
HAL_RTC_WaitForSynchro(&RtcHandle);
307+
}
308+
#endif // DEVICE_LOWPOWERTIMER
309+
234310
#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

hal/targets/hal/TARGET_STM/TARGET_STM32L4/sleep.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*******************************************************************************
2929
*/
3030
#include "sleep_api.h"
31+
#include "rtc_api_hal.h"
3132

3233
#if DEVICE_SLEEP
3334

@@ -55,6 +56,10 @@ void deepsleep(void)
5556

5657
// Restart HAL systick
5758
HAL_ResumeTick();
59+
60+
#if DEVICE_LOWPOWERTIMER
61+
rtc_synchronize();
62+
#endif
5863
}
5964

6065
#endif

0 commit comments

Comments
 (0)