1 | void init()
|
2 | {
|
3 | /* Enable the PWR clock */
|
4 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
5 |
|
6 | /* Allow access to RTC */
|
7 | PWR_BackupAccessCmd(ENABLE);
|
8 |
|
9 | /* Enable the LSI OSC */
|
10 | RCC_LSICmd(ENABLE);
|
11 |
|
12 | /* Wait till LSI is ready */
|
13 | while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
|
14 | {
|
15 | }
|
16 |
|
17 | /* Select the RTC Clock Source */
|
18 | RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
|
19 |
|
20 |
|
21 | /* Configure the RTC data register and RTC prescaler */
|
22 | RTC_InitTypeDef RTC_InitStructure;
|
23 | RTC_InitStructure.RTC_AsynchPrediv = 0x7F; // prescaler set to optain a 1 Hz signal of the 32kHz LSI
|
24 | RTC_InitStructure.RTC_SynchPrediv = 0xFF; // prescaler set to optain a 1 Hz signal of the 32kHz LSI
|
25 | RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
|
26 |
|
27 | /* Check on RTC init */
|
28 | if (RTC_Init(&RTC_InitStructure) == ERROR)
|
29 | {
|
30 | return;
|
31 | }
|
32 |
|
33 | /* Enable the RTC Clock */
|
34 | RCC_RTCCLKCmd(ENABLE);
|
35 |
|
36 | /* Wait for RTC APB registers synchronisation */
|
37 | RTC_WaitForSynchro();
|
38 |
|
39 | rtc_InitWakeUpInterrupt(16);
|
40 |
|
41 | }
|
42 |
|
43 | void rtc_InitWakeUpInterrupt(uint8_t wakeUpCounter)
|
44 | {
|
45 | NVIC_InitTypeDef NVIC_InitStructure;
|
46 | EXTI_InitTypeDef EXTI_InitStructure;
|
47 |
|
48 | // NVIC init
|
49 | NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
|
50 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
51 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
52 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
53 | NVIC_Init(&NVIC_InitStructure);
|
54 |
|
55 | // ext Interrupt 22 einstellen (fuer WakeUp)
|
56 | EXTI_ClearITPendingBit(EXTI_Line22);
|
57 | EXTI_InitStructure.EXTI_Line = EXTI_Line22;
|
58 | EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
59 | EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
60 | EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
61 | EXTI_Init(&EXTI_InitStructure);
|
62 |
|
63 | // zum einstellen muss Wakup disabled sein
|
64 | RTC_WakeUpCmd(DISABLE);
|
65 |
|
66 | // Teiler 16 => 32,768kHz:16 => 2048 Hz
|
67 | RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
|
68 | // WakeUp Counter einstellen
|
69 | RTC_SetWakeUpCounter(wakeUpCounter); //set to 16 -> 128 interrupts per second (32768Hz / Div16 = 2048; 2048 / 128 = 16
|
70 |
|
71 | // enable Interrupt
|
72 | RTC_ITConfig(RTC_IT_WUT, ENABLE);
|
73 |
|
74 | // enable Wakeup
|
75 | RTC_WakeUpCmd(ENABLE);
|
76 | }
|
77 |
|
78 | void RTC_WKUP_IRQHandler(void)
|
79 | {
|
80 | if(RTC_GetITStatus(RTC_IT_WUT) != RESET)
|
81 | {
|
82 | // Interrupt Flags loeschen
|
83 | EXTI_ClearITPendingBit(EXTI_Line22);
|
84 | RTC_ClearITPendingBit(RTC_IT_WUT);
|
85 |
|
86 |
|
87 | doSomething();
|
88 |
|
89 | }
|
90 | }
|