1
1
/* mbed Microcontroller Library
2
2
*******************************************************************************
3
- * Copyright (c) 2015 , 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
29
29
*/
30
30
#include "rtc_api.h"
31
31
#include "rtc_api_hal.h"
32
- #include "stm32f0xx.h"
33
- #include "stm32f0xx_hal_rtc_ex.h"
34
32
35
33
#if DEVICE_RTC
36
34
@@ -42,60 +40,27 @@ static int rtc_inited = 0;
42
40
43
41
static RTC_HandleTypeDef RtcHandle ;
44
42
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
88
48
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
93
56
57
+ #if DEVICE_LOWPOWERTIMER
58
+ static void (* irq_handler )(void );
59
+ static void RTC_IRQHandler (void );
94
60
#endif
95
61
96
62
void rtc_init (void ) {
97
63
RCC_OscInitTypeDef RCC_OscInitStruct ;
98
- uint32_t rtc_freq = 0 ;
99
64
100
65
#if DEVICE_RTC_LSI
101
66
if (rtc_inited ) return ;
@@ -113,7 +78,6 @@ void rtc_init(void) {
113
78
if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) { // Check if LSE has started correctly
114
79
// Connect LSE to RTC
115
80
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
116
- rtc_freq = LSE_VALUE ;
117
81
} else {
118
82
error ("Cannot initialize RTC with LSE\n" );
119
83
}
@@ -128,32 +92,24 @@ void rtc_init(void) {
128
92
__HAL_RCC_BACKUPRESET_FORCE ();
129
93
__HAL_RCC_BACKUPRESET_RELEASE ();
130
94
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 );
143
105
#endif
144
106
145
107
// Enable RTC
146
108
__HAL_RCC_RTC_ENABLE ();
147
109
148
110
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 ;
157
113
RtcHandle .Init .OutPut = RTC_OUTPUT_DISABLE ;
158
114
RtcHandle .Init .OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;
159
115
RtcHandle .Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
@@ -163,9 +119,17 @@ void rtc_init(void) {
163
119
}
164
120
165
121
#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 );
169
133
#endif
170
134
}
171
135
@@ -203,9 +167,9 @@ int rtc_isenabled(void) {
203
167
return rtc_inited ;
204
168
#else
205
169
if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) {
206
- return 1 ;
170
+ return 1 ;
207
171
} else {
208
- return 0 ;
172
+ return 0 ;
209
173
}
210
174
#endif
211
175
}
@@ -282,46 +246,48 @@ void rtc_write(time_t t) {
282
246
}
283
247
284
248
#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 )
286
256
{
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 ();
302
260
}
303
261
}
304
262
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 )
306
269
{
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 ));
310
271
}
311
272
312
- uint32_t rtc_ticker_get_synch_presc ( )
273
+ void rtc_set_wake_up_timer ( uint32_t delta )
313
274
{
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
+ }
315
281
}
316
282
317
- uint32_t rtc_read_subseconds ( )
283
+ void rtc_deactivate_wake_up_timer ( void )
318
284
{
319
- return RTC -> SSR ;
285
+ HAL_RTCEx_DeactivateWakeUpTimer ( & RtcHandle ) ;
320
286
}
321
287
322
- void rtc_ticker_disable_irq ( )
288
+ void rtc_synchronize ( void )
323
289
{
324
- HAL_RTC_DeactivateAlarm (& RtcHandle , RTC_ALARM_A );
290
+ HAL_RTC_WaitForSynchro (& RtcHandle );
325
291
}
326
292
#endif // DEVICE_LOWPOWERTIMER
327
293
0 commit comments