Skip to content

Commit 66c51c9

Browse files
committed
STM32F3 - Add low power timer functions in RTC api files
1 parent 75f5609 commit 66c51c9

File tree

2 files changed

+175
-1
lines changed

2 files changed

+175
-1
lines changed

hal/targets/hal/TARGET_STM/TARGET_STM32F3/rtc_api.c

Lines changed: 114 additions & 1 deletion
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,10 +40,63 @@ static int rtc_inited = 0;
3940

4041
static RTC_HandleTypeDef RtcHandle;
4142

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

47101
#if DEVICE_RTC_LSI
48102
rtc_inited = 1;
@@ -58,7 +112,9 @@ void rtc_init(void)
58112
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
59113
// Connect LSE to RTC
60114
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
115+
#if !DEVICE_LOWPOWERTIMER
61116
rtc_freq = LSE_VALUE;
117+
#endif
62118
}
63119
else {
64120
error("RTC error: LSE clock initialization failed.");
@@ -85,23 +141,36 @@ void rtc_init(void)
85141
// Connect LSI to RTC
86142
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
87143
// Note: The LSI clock can be measured precisely using a timer input capture.
144+
#if !DEVICE_LOWPOWERTIMER
88145
rtc_freq = LSI_VALUE;
146+
#endif
89147
#endif
90148

91149

92150
// Enable RTC
93151
__HAL_RCC_RTC_ENABLE();
94152

95153
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
154+
#if !DEVICE_LOWPOWERTIMER
96155
RtcHandle.Init.AsynchPrediv = 127;
97156
RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
157+
#else
158+
RtcHandle.Init.AsynchPrediv = m_asynch_prediv;
159+
RtcHandle.Init.SynchPrediv = m_synch_prediv;
160+
#endif
98161
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
99162
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
100163
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
101164

102165
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
103166
error("RTC error: RTC initialization failed.");
104167
}
168+
169+
#if DEVICE_LOWPOWERTIMER
170+
rtc_configure_time_and_date();
171+
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)&RTC_IRQHandler);
172+
HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
173+
#endif
105174
}
106175

107176
void rtc_free(void)
@@ -218,4 +287,48 @@ void rtc_write(time_t t)
218287
HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
219288
}
220289

290+
#if DEVICE_LOWPOWERTIMER
291+
void rtc_set_alarm(struct tm *ti, uint32_t subsecs)
292+
{
293+
RTC_AlarmTypeDef mAlarm;
294+
295+
mAlarm.AlarmTime.Hours = ti->tm_hour;
296+
mAlarm.AlarmTime.Minutes = ti->tm_min;
297+
mAlarm.AlarmTime.Seconds = ti->tm_sec;
298+
mAlarm.AlarmTime.SubSeconds = subsecs;
299+
mAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT_24;
300+
mAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
301+
mAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
302+
mAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
303+
mAlarm.AlarmDateWeekDay = 1;
304+
mAlarm.Alarm = RTC_ALARM_A;
305+
306+
if (HAL_RTC_SetAlarm_IT(&RtcHandle, &mAlarm, RTC_FORMAT_BIN) != HAL_OK) {
307+
error("Set Alarm failed\n");
308+
}
309+
}
310+
311+
void rtc_reconfigure_prescalers(void)
312+
{
313+
m_synch_prediv = 0x3FF;
314+
m_asynch_prediv = 0x1F;
315+
rtc_init();
316+
}
317+
318+
uint32_t rtc_ticker_get_synch_presc(void)
319+
{
320+
return m_synch_prediv;
321+
}
322+
323+
uint32_t rtc_read_subseconds(void)
324+
{
325+
return RTC->SSR;
326+
}
327+
328+
void rtc_ticker_disable_irq(void)
329+
{
330+
HAL_RTC_DeactivateAlarm(&RtcHandle, RTC_ALARM_A);
331+
}
332+
#endif // DEVICE_LOWPOWERTIMER
333+
221334
#endif
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// Prescaler values for LSE clock
45+
#define RTC_ASYNCH_PREDIV 0x7F
46+
#define RTC_SYNCH_PREDIV 0x00FF
47+
48+
void rtc_set_irq_handler(uint32_t handler);
49+
50+
void rtc_ticker_disable_irq(void);
51+
uint32_t rtc_ticker_get_synch_presc(void);
52+
53+
void rtc_set_alarm(struct tm *ti, uint32_t subsecs);
54+
uint32_t rtc_read_subseconds(void);
55+
void rtc_reconfigure_prescalers(void);
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
60+
61+
#endif

0 commit comments

Comments
 (0)