1 | // #define __INTERNAL_CLOCK__
|
2 | void RTC_Config(void)
|
3 | {
|
4 | EXTI_InitTypeDef EXTI_InitStruct;
|
5 | NVIC_InitTypeDef NVIC_InitStruct;
|
6 |
|
7 | /* Enable the PWR clock */
|
8 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
9 | /* Allow access to RTC */
|
10 | PWR_BackupAccessCmd(ENABLE);
|
11 |
|
12 | #ifdef __INTERNAL_CLOCK__
|
13 | // LSI used as RTC source clock
|
14 | RCC_LSICmd(ENABLE);
|
15 | while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {}
|
16 | RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
|
17 | #else
|
18 | // LSE used as RTC source clock
|
19 | RCC_LSEConfig(RCC_LSE_ON);
|
20 | while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}
|
21 | RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
|
22 | #endif
|
23 | /* Enable the RTC Clock */
|
24 | RCC_RTCCLKCmd(ENABLE);
|
25 | /* Wait for RTC APB registers synchronisation */
|
26 | RTC_WaitForSynchro();
|
27 |
|
28 | RTC_ClearITPendingBit(RTC_IT_WUT);
|
29 | EXTI_ClearITPendingBit(EXTI_Line22);
|
30 |
|
31 | /* Disable wakeup interrupt */
|
32 | RTC_WakeUpCmd(DISABLE);
|
33 |
|
34 | /* Disable RTC interrupt flag */
|
35 | RTC_ITConfig(RTC_IT_WUT, DISABLE);
|
36 |
|
37 | /* NVIC init for RTC */
|
38 | NVIC_InitStruct.NVIC_IRQChannel = RTC_WKUP_IRQn;
|
39 | NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = RTC_PRIORITY;
|
40 | NVIC_InitStruct.NVIC_IRQChannelSubPriority = RTC_WAKEUP_SUBPRIORITY;
|
41 | NVIC_InitStruct.NVIC_IRQChannelCmd = DISABLE;
|
42 | NVIC_Init(&NVIC_InitStruct);
|
43 |
|
44 | /* RTC connected to EXTI_Line22 */
|
45 | EXTI_InitStruct.EXTI_Line = EXTI_Line22;
|
46 | EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
|
47 | EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
|
48 | EXTI_InitStruct.EXTI_LineCmd = DISABLE;
|
49 | EXTI_Init(&EXTI_InitStruct);
|
50 |
|
51 | NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
|
52 | NVIC_Init(&NVIC_InitStruct);
|
53 | /* Enable EXT1 interrupt */
|
54 | EXTI_InitStruct.EXTI_LineCmd = ENABLE;
|
55 | EXTI_Init(&EXTI_InitStruct);
|
56 |
|
57 | RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div8);
|
58 |
|
59 | RTC_SetWakeUpCounter(0x0FFF);
|
60 | /* Enable wakeup interrupt */
|
61 | RTC_ITConfig(RTC_IT_WUT, ENABLE);
|
62 | /* Enable wakeup command */
|
63 | RTC_WakeUpCmd(ENABLE);
|
64 |
|
65 | RTC_TimeStructInit(&RTC_Time);
|
66 | RTC_DateStructInit(&RTC_Date);
|
67 | RTC_SetTime(RTC_Format_BIN, &RTC_Time);
|
68 | RTC_SetDate(RTC_Format_BIN, &RTC_Date);
|
69 | }
|