CARME-M4 BSP  V1.5
rtc.c
Go to the documentation of this file.
1 
71 #ifdef __cplusplus
72 extern "C"
73 {
74 #endif /* __cplusplus */
75 
76 /*----- Header-Files -------------------------------------------------------*/
77 #include <assert.h> /* Assertion handling */
78 #include <stm32f4xx.h> /* Processor STM32F407IG */
79 #include <carme.h> /* CARME Module */
80 #include <i2c.h> /* CARME I2C definitions */
81 #include <rtc_ext.h> /* CARME RTC Ext definitions */
82 #include <rtc.h> /* CARME RTC definitions */
83 
84 /*----- Macros -------------------------------------------------------------*/
85 #define CARME_RTC_STATE_REG RTC_BKP_DR0
86 #define CARME_RTC_STATE_INIT_OK 0x35AC
87 #define CARME_RTC_STATE_TIME_OK 0xA3C5
89 /*----- Data types ---------------------------------------------------------*/
90 
91 /*----- Function prototypes ------------------------------------------------*/
92 
93 /*----- Data ---------------------------------------------------------------*/
94 
95 /*----- Implementation -----------------------------------------------------*/
105 void CARME_RTC_Init(void) {
106 
107  RTC_InitTypeDef RTC_InitStruct;
108  static CARME_RTC_TIME_t time;
109 
110  /* Initialize the external rtc */
112 
113  /* Enable the PWR clock */
114  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
115 
116  /* Allow access to RTC */
117  PWR_BackupAccessCmd(ENABLE);
118 
119  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) {
120  /* RTC configuration */
121 
122  /* Enable the LSE OSC */
123  RCC_LSEConfig(RCC_LSE_ON);
124 
125  /* Wait till LSE is ready */
126  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
127  ;
128 
129  /* Select the RTC Clock Source */
130  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
131 
132  /* Enable the RTC Clock */
133  RCC_RTCCLKCmd(ENABLE);
134 
135  /* Wait for RTC APB registers synchronization */
137 
138  /* Configure the RTC data register and RTC prescaler */
139  RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
140  RTC_InitStruct.RTC_SynchPrediv = 0xFF;
141  RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
142 
143  /* Check on RTC init */
144  if (RTC_Init(&RTC_InitStruct) == ERROR) {
145  for (;;)
146  ;
147  }
148  }
149 
150  /* set the rtc from the external rtc on mainboard */
151  CARME_RTC_Ext_GetTime(&time);
152  if (CARME_RTC_SetTime(&time) == CARME_NO_ERROR) {
153  RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
154  }
155 }
156 
168 
169  static RTC_TimeTypeDef RTC_TimeStruct;
170  static RTC_DateTypeDef RTC_DateStruct;
171 
172  assert(time->year < 100 && time->year >= 0);
173  assert(time->month <= 12 && time->month >= 1);
174  assert(time->day <= 31 && time->day >= 1);
175  assert(time->wday <= 7 && time->wday >= 1);
176  assert(time->hour < 24 && time->hour >= 0);
177  assert(time->min < 60 && time->min >= 0);
178  assert(time->sec < 60 && time->sec >= 0);
179 
180  /* set the time */
181  RTC_TimeStruct.RTC_Hours = time->hour;
182  RTC_TimeStruct.RTC_Minutes = time->min;
183  RTC_TimeStruct.RTC_Seconds = time->sec;
184  RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
185 
186  /* set the date */
187  RTC_DateStruct.RTC_Date = time->day;
188  RTC_DateStruct.RTC_Month = time->month;
189  RTC_DateStruct.RTC_Year = time->year;
190  RTC_DateStruct.RTC_WeekDay = time->wday;
191  RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);
192 
193  return CARME_NO_ERROR;
194 }
195 
205 
206  static RTC_TimeTypeDef RTC_TimeStruct;
207  static RTC_DateTypeDef RTC_DateStruct;
208 
209  /* get the date and time from the internal rtc */
210  RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
211  RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
212 
213  time->year = RTC_DateStruct.RTC_Year;
214  time->month = RTC_DateStruct.RTC_Month;
215  time->day = RTC_DateStruct.RTC_Date;
216  time->wday = RTC_DateStruct.RTC_WeekDay;
217  time->hour = RTC_TimeStruct.RTC_Hours;
218  time->min = RTC_TimeStruct.RTC_Minutes;
219  time->sec = RTC_TimeStruct.RTC_Seconds;
220 }
221 
222 #if CARME_RTC_USE_WAKEUP_ISR==1
223 
231 void CARME_RTC_SetWakeUpInterrupt(CARME_RTC_WAKEUP_t wakeup) {
232 
233  NVIC_InitTypeDef NVIC_InitStructure;
234  EXTI_InitTypeDef EXTI_InitStructure;
235  static uint32_t wakeup_time = 0x7FF;
236 
237  if (wakeup == CARME_RTC_WAKEUP_STOP) { /* disable wakeup interrupt */
238 
239  RTC_WakeUpCmd(DISABLE);
240  RTC_ITConfig(RTC_IT_WUT, DISABLE); /* disable Interrupt */
241 
242  /* NVIC disable */
243  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
244  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
245  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
246  NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
247  NVIC_Init(&NVIC_InitStructure);
248 
249  /* ext Interrupt 22 disable */
251  EXTI_InitStructure.EXTI_Line = EXTI_Line22;
252  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
253  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
254  EXTI_InitStructure.EXTI_LineCmd = DISABLE;
255  EXTI_Init(&EXTI_InitStructure);
256  }
257  else { /* enable wakeup interrupt */
258 
259  /* initialize NVIC */
260  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
261  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
262  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
263  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
264  NVIC_Init(&NVIC_InitStructure);
265 
266  /* set EXT interrupt 22 for wakeup */
268  EXTI_InitStructure.EXTI_Line = EXTI_Line22;
269  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
270  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
271  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
272  EXTI_Init(&EXTI_InitStructure);
273 
274  /* disable wakeup while configuring */
275  RTC_WakeUpCmd(DISABLE);
276 
277  switch (wakeup) {
278  case RTC_WAKEUP_30s:
279  wakeup_time = 0xEFFF;
280  break;
281  case RTC_WAKEUP_10s:
282  wakeup_time = 0x4FFF;
283  break;
284  case RTC_WAKEUP_5s:
285  wakeup_time = 0x27FF;
286  break;
287  case RTC_WAKEUP_1s:
288  wakeup_time = 0x7FF;
289  break;
290  case RTC_WAKEUP_30s:
291  wakeup_time = 0x3FF;
292  break;
293  case RTC_WAKEUP_500ms:
294  wakeup_time = 0x1FF;
295  break;
296  case RTC_WAKEUP_125ms:
297  wakeup_time = 0xFF;
298  break;
299  }
300 
301  /* divider 16 => 32,768kHz/16 => 2048 Hz */
302  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16 );
303  RTC_SetWakeUpCounter(wakeup_time);
304 
305  RTC_ITConfig(RTC_IT_WUT, ENABLE); /* enable interrupt */
306  RTC_WakeUpCmd(ENABLE); /* enable wakeup */
307 
308  }
309 }
310 #endif
311 
326 uint32_t get_fattime(void) {
327 
328  static CARME_RTC_TIME_t time;
329 
330  CARME_RTC_GetTime(&time);
331 
332  return (((uint32_t) time.year + 20) << 25)
333  | ((uint32_t) time.month << 21)
334  | ((uint32_t) time.day << 16)
335  | ((uint32_t) time.hour << 11)
336  | ((uint32_t) time.min << 5)
337  | ((uint32_t) time.sec << 1);
338 }
339 
340 #ifdef __cplusplus
341 }
342 #endif /* __cplusplus */
343 
uint8_t RTC_Seconds
Definition: stm32f4xx_rtc.h:78
uint8_t ERROR_CODES
Error variable.
Definition: carme.h:255
void CARME_RTC_GetTime(CARME_RTC_TIME_t *time)
Get the date and time from the internal rtc.
Definition: rtc.c:204
RTC interface. Communication interface to use the RTC from the CARME-M4.
ErrorStatus RTC_WaitForSynchro(void)
Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock...
void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
Initializes the NVIC peripheral according to the specified parameters in the NVIC_InitStruct.
Definition: misc.c:136
uint32_t RTC_HourFormat
Definition: stm32f4xx_rtc.h:55
void PWR_BackupAccessCmd(FunctionalState NewState)
Enables or disables access to the backup domain (RTC registers, RTC backup data registers and backup ...
uint8_t day
Definition: rtc.h:97
void CARME_RTC_Init(void)
Initialize and start the internal RTC of the STM32F4xx. The date and time is set from the external R...
Definition: rtc.c:105
ErrorStatus RTC_Init(RTC_InitTypeDef *RTC_InitStruct)
Initializes the RTC registers according to the specified parameters in RTC_InitStruct.
uint8_t month
Definition: rtc.h:98
void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
Configures the RTC Wakeup clock source.
uint8_t NVIC_IRQChannelPreemptionPriority
Definition: misc.h:61
void CARME_RTC_Ext_GetTime(CARME_RTC_TIME_t *time)
Get the Time from the external RTC on mainboard.
Definition: rtc_ext.c:184
FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
Checks whether the specified RCC flag is set or not.
uint8_t min
Definition: rtc.h:95
void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
Enables or disables the specified RTC interrupts.
EXTIMode_TypeDef EXTI_Mode
RTC Date structure definition.
Definition: stm32f4xx_rtc.h:88
ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
Enables or Disables the RTC WakeUp timer.
void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
Configures the RTC clock (RTCCLK).
uint8_t sec
Definition: rtc.h:94
void EXTI_Init(EXTI_InitTypeDef *EXTI_InitStruct)
Initializes the EXTI peripheral according to the specified parameters in the EXTI_InitStruct.
uint32_t RTC_AsynchPrediv
Definition: stm32f4xx_rtc.h:58
uint8_t RTC_Minutes
Definition: stm32f4xx_rtc.h:75
ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef *RTC_TimeStruct)
Set the RTC current time.
uint32_t get_fattime(void)
Get the time from the RTC. This function is used from the FatFs and the function prototype is given ...
Definition: rtc.c:326
NVIC Init Structure definition.
Definition: misc.h:54
void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef *RTC_TimeStruct)
Get the RTC current Time.
void RCC_RTCCLKCmd(FunctionalState NewState)
Enables or disables the RTC clock.
uint8_t RTC_WeekDay
Definition: stm32f4xx_rtc.h:90
uint8_t hour
Definition: rtc.h:96
uint8_t NVIC_IRQChannel
Definition: misc.h:56
FunctionalState NVIC_IRQChannelCmd
Definition: misc.h:71
EXTI Init Structure definition.
RTC Time structure definition.
Definition: stm32f4xx_rtc.h:68
ERROR_CODES CARME_RTC_SetTime(CARME_RTC_TIME_t *time)
Set the internal rtc date and time.
Definition: rtc.c:167
uint8_t wday
Definition: rtc.h:100
uint8_t NVIC_IRQChannelSubPriority
Definition: misc.h:66
void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
Clears the EXTI's line pending bits.
void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
Configures the RTC Wakeup counter.
ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef *RTC_DateStruct)
Set the RTC current date.
#define CARME_NO_ERROR
Definition: carme.h:135
Structure to store a full date and time.
Definition: rtc.h:93
uint32_t RTC_SynchPrediv
Definition: stm32f4xx_rtc.h:61
uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
Reads data from the specified RTC Backup data Register.
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
EXTITrigger_TypeDef EXTI_Trigger
void CARME_RTC_Ext_Init(void)
Initialize the RTC on the CARME mainboard.
Definition: rtc_ext.c:100
void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef *RTC_DateStruct)
Get the RTC current date.
uint8_t year
Definition: rtc.h:99
void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
Writes a data in a specified RTC Backup data register.
Communication interface to use the RTC on the CARME mainboard.
void RCC_LSEConfig(uint8_t RCC_LSE)
Configures the External Low Speed oscillator (LSE).
RTC Init structures definition.
Definition: stm32f4xx_rtc.h:53
FunctionalState EXTI_LineCmd
I2C board support package for the CARME module.
#define EXTI_Line22