1
1
/* mbed Microcontroller Library
2
2
*******************************************************************************
3
- * Copyright (c) 2014 , STMicroelectronics
3
+ * Copyright (c) 2016 , STMicroelectronics
4
4
* All rights reserved.
5
5
*
6
6
* Redistribution and use in source and binary forms, with or without
28
28
*******************************************************************************
29
29
*/
30
30
#include "rtc_api.h"
31
+ #include "rtc_api_hal.h"
31
32
32
33
#if DEVICE_RTC
33
34
@@ -39,10 +40,63 @@ static int rtc_inited = 0;
39
40
40
41
static RTC_HandleTypeDef RtcHandle ;
41
42
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
+
42
94
void rtc_init (void )
43
95
{
44
96
RCC_OscInitTypeDef RCC_OscInitStruct ;
97
+ #if !DEVICE_LOWPOWERTIMER
45
98
uint32_t rtc_freq = 0 ;
99
+ #endif
46
100
47
101
#if DEVICE_RTC_LSI
48
102
rtc_inited = 1 ;
@@ -58,7 +112,9 @@ void rtc_init(void)
58
112
if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) {
59
113
// Connect LSE to RTC
60
114
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
115
+ #if !DEVICE_LOWPOWERTIMER
61
116
rtc_freq = LSE_VALUE ;
117
+ #endif
62
118
}
63
119
else {
64
120
error ("RTC error: LSE clock initialization failed." );
@@ -85,23 +141,36 @@ void rtc_init(void)
85
141
// Connect LSI to RTC
86
142
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
87
143
// Note: The LSI clock can be measured precisely using a timer input capture.
144
+ #if !DEVICE_LOWPOWERTIMER
88
145
rtc_freq = LSI_VALUE ;
146
+ #endif
89
147
#endif
90
148
91
149
92
150
// Enable RTC
93
151
__HAL_RCC_RTC_ENABLE ();
94
152
95
153
RtcHandle .Init .HourFormat = RTC_HOURFORMAT_24 ;
154
+ #if !DEVICE_LOWPOWERTIMER
96
155
RtcHandle .Init .AsynchPrediv = 127 ;
97
156
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
98
161
RtcHandle .Init .OutPut = RTC_OUTPUT_DISABLE ;
99
162
RtcHandle .Init .OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;
100
163
RtcHandle .Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
101
164
102
165
if (HAL_RTC_Init (& RtcHandle ) != HAL_OK ) {
103
166
error ("RTC error: RTC initialization failed." );
104
167
}
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
105
174
}
106
175
107
176
void rtc_free (void )
@@ -218,4 +287,48 @@ void rtc_write(time_t t)
218
287
HAL_RTC_SetTime (& RtcHandle , & timeStruct , FORMAT_BIN );
219
288
}
220
289
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
+
221
334
#endif
0 commit comments