-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensClock.cpp
executable file
·375 lines (305 loc) · 9.31 KB
/
sensClock.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "sensClock.h"
#include "stm32f10x_rtc.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_bkp.h"
#include "stm32f10x_gpio.h"
#include "misc.h"
#include "listener.h"
#include "debug.h"
#include <string.h>
#include <time.h>
#include <stdio.h>
static void RTC_Configuration(void);
struct tm Time_GetCalendarTime(void);
time_t Time_GetUnixTime(void);
void RTC_Init(void);
sensClock& sensClock::getInstance()
{
static sensClock instance(SENSOR_ID_CLOCK);
return instance;
}
void sensClock::run()
{
DEBUG.print("sensClock thread started!\r\n");
while(1)
{
struct tm time;
_timeUnix = Time_GetUnixTime();
time = Time_GetCalendarTime();
sprintf(_timeDate,"%d.%d.%d. %02d:%02d:%02d", time.tm_mday, \
time.tm_mon+1, time.tm_year,\
time.tm_hour, time.tm_min, time.tm_sec);
for(int i = 0; i < _numListeners; i++)
{
_listeners[i]->update(this, 0, DATA_TYPE_INVALID);
_listeners[i]->update(this, _timeDate, DATA_TYPE_STRING);
_listeners[i]->update(this,(void*)(&_timeUnix),DATA_TYPE_INT32);
}
CoTimeDelay(0,0,1,0);
}
}
sensClock::sensClock(SENSOR_ID ID) : Sensor(ID)
{
_timeDate[0]=0;
// static bool setClock = true;
// if(setClock)
// {
// struct tm t;
// t.tm_hour = 22;
// t.tm_min = 5;
// t.tm_sec = 0;
// t.tm_isdst = 0;
// t.tm_mday = 22;
// t.tm_mon = 0;
// t.tm_year = 2016;
// RTC_Configuration();
// DEBUG.print("Setting the clock!\r\n");
// CoSchedLock();
// sensClock::Time_SetCalendarTime(t);
// BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
// CoSchedUnlock();
// DEBUG.print("The clock is set!\r\n");
// setClock = false;
// }
RTC_Init();
}
struct tm sensClock::Time_ConvUnixToCalendar(time_t t)
{
struct tm *t_tm;
t_tm = localtime(&t);
t_tm->tm_year += 1900;
return *t_tm;
}
struct tm Time_ConvUnixToCalendar(time_t t)
{
struct tm *t_tm;
t_tm = localtime(&t);
t_tm->tm_year += 1900;
return *t_tm;
}
time_t Time_ConvCalendarToUnix(struct tm t)
{
t.tm_year -= 1900; /* Íⲿtmœá¹¹ÌåŽæŽ¢µÄÄê·ÝΪ2008žñÊœ */
/* ¶øtime.hÖжšÒåµÄÄê·ÝžñʜΪ1900Ä꿪ʌµÄÄê·Ý */
/* ËùÒÔ£¬ÔÚÈÕÆÚת»»Ê±Òª¿ŒÂǵœÕâžöÒòËØ¡£*/
return mktime(&t);
}
time_t Time_GetUnixTime(void)
{
return (time_t)RTC_GetCounter();
}
struct tm Time_GetCalendarTime(void)
{
time_t t_t;
struct tm t_tm;
t_t = (time_t)RTC_GetCounter();
t_tm = Time_ConvUnixToCalendar(t_t);
return t_tm;
}
void Time_SetUnixTime(time_t t)
{
RTC_WaitForLastTask();
RTC_SetCounter((u32)t);
RTC_WaitForLastTask();
return;
}
void sensClock::Time_SetCalendarTime(struct tm t)
{
Time_SetUnixTime(Time_ConvCalendarToUnix(t));
return;
}
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);
/**
* LED1 -> PB0 LED2 -> PB1
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
static void RTC_Configuration(void)
{
GPIO_Configuration();
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
/* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
//static uint16_t USART_Scanf(uint32_t min_value,uint32_t max_value,uint8_t lenght)
//{
// uint16_t index = 0;
// uint32_t tmp[4] = {0, 0, 0, 0};
// while (index < lenght)
// {
// /* Loop until RXNE = 1 */
// while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
// {}
// tmp[index++] = (USART_ReceiveData(USART1));
// if( tmp[index - 1] == 0x0D ) { index--; continue; }
// if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
// {
// DEBUG.print("Please enter valid number between 0 and 9\r\n");
// index--;
// }
// }
// /* Calculate the Corresponding value */
// if( lenght ==2 )
// index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10 );
// else /* lenght ==4 */
// index = (tmp[3] - 0x30) + ((tmp[2] - 0x30) * 10 ) + ((tmp[1] - 0x30) * 100 ) + ((tmp[0] - 0x30) * 1000 );
// /* Checks */
// if (index > max_value || index < min_value)
// {
// DEBUG.print("Please enter valid number between %d and %d\r\n", min_value, max_value);
// return 0;
// }
// return index;
//}
//void Time_Regulate(void)
//{
// struct tm time;
// memset(&time, 0 , sizeof(time) ); /* Çå¿Õœá¹¹Ìå */
// DEBUG.print("=======================Time Settings==========================\r\n");
// DEBUG.print("Please Set Years between 1970 to 2037\r\n");
// while ( time.tm_year>2037 || time.tm_year<1970)
// {
// time.tm_year =1970;
// }
// DEBUG.print("Set Years: %d\r\n", time.tm_year);
// DEBUG.print("Please Set Months between 01 to 12\r\n");
// while (time.tm_mon >12 || time.tm_mon < 1 )
// {
// time.tm_mon= 1;
// }
// DEBUG.print("Set Months: %d\r\n", time.tm_mon);
// DEBUG.print("Please Set Days between 01 to 31\r\n");
// while (time.tm_mday >31 ||time.tm_mday <1 )
// {
// time.tm_mday =1;
// }
// DEBUG.print("Set Days: %d\r\n", time.tm_mday);
// DEBUG.print("Please Set Hours between 01 to 23\r\n");
// while (time.tm_hour >23 ||time.tm_hour <1 )
// {
// time.tm_hour = 12;
// }
// DEBUG.print("Set Hours: %d\r\n", time.tm_hour);
// DEBUG.print("Please Set Minutes between 01 to 59\r\n");
// while (time.tm_min >59 || time.tm_min <1 )
// {
// time.tm_min = 1;
// }
// DEBUG.print("Set Minutes: %d\r\n", time.tm_min);
// DEBUG.print("Please Set Seconds between 01 to 59\r\n");
// while (time.tm_sec >59 || time.tm_sec <1 )
// {
// time.tm_sec = 1;
// }
// DEBUG.print("Set Seconds: %d\r\n", time.tm_sec);
// /* Return the value to store in RTC counter register */
// Time_SetCalendarTime(time);
//}
void RTC_Init(void)
{
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
/* Backup data register value is not correct or not yet programmed (when
the first time the program is executed) */
DEBUG.print("RTC not yet configured....\r\n");
/* RTC Configuration */
RTC_Configuration();
//Time_Regulate();
/* Adjust time by values entred by the user on the hyperterminal */
DEBUG.print("RTC configured....\r\n");
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
DEBUG.print("Power On Reset occurred....\r\n");
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
DEBUG.print("External Reset occurred....\r\n");
}
DEBUG.print("No need to configure RTC....\r\n");
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
DEBUG.print("Synced RTC....\r\n");
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/* NVIC configuration */
NVIC_Configuration();
#ifdef RTCClockOutput_Enable
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Disable the Tamper Pin */
BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
functionality must be disabled */
/* Enable RTC Clock Output on Tamper Pin */
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
/* Clear reset flags */
RCC_ClearFlag();
DEBUG.print("Configured RTC!\r\n");
return;
}
extern "C"
{
void RTC_IRQHandler(void)
{
static uint8_t Display;
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
/* Clear the RTC Second interrupt */
RTC_ClearITPendingBit(RTC_IT_SEC);
}
}
}