blob: 4159f4429755ca7110fe129a6f596a4745c6f055 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "time.h"
#include "stm32f0xx_conf.h"
/* Uncomment the corresponding line to select the RTC Clock source */
#define RTC_CLOCK_SOURCE_LSE /* LSE used as RTC source clock */
//#define RTC_CLOCK_SOURCE_LSI // LSI used as RTC source clock. The RTC Clock
// may varies due to LSI frequency dispersion
#define BKP_VALUE 0x32F0
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
/* Private function prototypes -----------------------------------------------*/
static void RTC_Config(void);
static void RTC_TimeRegulate(void);
// - êàëèáðîâêó RTC êàê äåëàòü?
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void TimeInit (void)
{
RTC_InitTypeDef RTC_InitStructure;
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE)
//if (SynchPrediv == 0)
{
/* RTC configuration */
RTC_Config();
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
/* Check on RTC init */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
// printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
}
/* Configure the time register */
RTC_TimeRegulate();
}
else
{
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
//printf("\r\n Power On Reset occurred....\n\r");
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
// printf("\r\n External Reset occurred....\n\r");
}
//printf("\n\r No need to configure RTC....\n\r");
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#ifdef RTC_CLOCK_SOURCE_LSI
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
#endif /* RTC_CLOCK_SOURCE_LSI */
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Clear the RTC Alarm Flag */
RTC_ClearFlag(RTC_FLAG_ALRAF);
/* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
//EXTI_ClearITPendingBit(EXTI_Line17);
/* Display the RTC Time and Alarm */
//RTC_TimeShow();
//RTC_AlarmShow();
}
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
static void RTC_Config(void)
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/
/* The RTC Clock may varies due to LSI frequency dispersion. */
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
SynchPrediv = 0x18F;
AsynchPrediv = 0x63;
#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
}
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
static void RTC_TimeRegulate ( void )
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_TimeStructure.RTC_Hours = 14;
RTC_TimeStructure.RTC_Minutes = 01;
RTC_TimeStructure.RTC_Seconds = 00;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister(RTC_BKP_DR0, BKP_VALUE);
}
|