1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
36
37
38
39
40
41
42
43
46
47
48
49
50
59
60
61
66
67
68
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
112
113
114
115
116
117
118
119
124
125
126
127
128
129
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
167
168
169
170
171
172
173
174
175
176
177
178
182
183
184
189
190
192
193
194
195
196
197
201
202
203
208
216
217
222
223
224
225
226
227
228
229
230
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
264
265
266
267
268
269
270
271
272
273
274
279
280
281
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
310
311
312
313
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
397
398
399
400
401
402
403
404
405
406
407
408
418
419
420
423
424
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
typedef struct {
uint8_t tab[9];
...} Table_TypeDef;
Private typedef
RTC_HandleTypeDef RtcHandle;
#if !defined USE_LCD
uint8_t aShowTime[50] = {0}, aShowTimeStamp[50] = {0};
uint8_t aShowDate[50] = {0}, aShowDateStamp[50] = {0};/* ... */
#endif
FlagStatus TamperStatus = RESET;
Private variables
static void SystemClock_Config(void);
static void RTC_TimeStampConfig(void);
static void RTC_CalendarShow(void);
#ifdef USE_LCD
static Table_TypeDef RTC_Get_Time(RTC_TimeTypeDef* RTC_TimeStructure);
static void RTC_Time_display(uint8_t Line, uint32_t Color_x, Table_TypeDef table);/* ... */
#endif
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
SystemClock_Config();
BSP_LED_On(LED1);
#ifdef USE_LCD
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS);
BSP_LCD_SelectLayer(1);
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_ClearStringLine(0);
BSP_LCD_DisplayStringAtLine(0, (uint8_t*)" TimeStamp Example ");/* ... */
#endif
RtcHandle.Instance = RTC;
/* ... */
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_12;
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
__HAL_RTC_RESET_HANDLE_STATE(&RtcHandle);
if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
{
Error_Handler();
}if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { ... }
RTC_TimeStampConfig();
while (1)
{
RTC_CalendarShow();
}while (1) { ... }
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... }
if(HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}if (HAL_PWREx_EnableOverDrive() != HAL_OK) { ... }
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { ... }
}{ ... }
/* ... */
void Error_Handler(void)
{
BSP_LED_On(LED3);
while(1)
{
}while (1) { ... }
}{ ... }
/* ... */
static void RTC_TimeStampConfig(void)
{
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
RTC_TamperTypeDef stamperstructure;
/* ... */
stamperstructure.Filter = RTC_TAMPERFILTER_DISABLE;
stamperstructure.Tamper = RTC_TAMPER_1;
stamperstructure.PinSelection = RTC_TAMPERPIN_PC13;
stamperstructure.Trigger = RTC_TAMPERTRIGGER_FALLINGEDGE;
stamperstructure.SamplingFrequency = RTC_TAMPERSAMPLINGFREQ_RTCCLK_DIV4096;
stamperstructure.PrechargeDuration = RTC_TAMPERPRECHARGEDURATION_1RTCCLK;
stamperstructure.TamperPullUp = RTC_TAMPER_PULLUP_DISABLE;
stamperstructure.TimeStampOnTamperDetection = RTC_TIMESTAMPONTAMPERDETECTION_ENABLE;
HAL_RTCEx_SetTamper(&RtcHandle, &stamperstructure);
HAL_RTCEx_SetTimeStamp_IT(&RtcHandle, RTC_TIMESTAMPEDGE_RISING, RTC_TIMESTAMPPIN_PC13);
__HAL_RTC_TIMESTAMP_CLEAR_FLAG(&RtcHandle,RTC_FLAG_TSF);
sdatestructure.Year = 0x14;
sdatestructure.Month = RTC_MONTH_FEBRUARY;
sdatestructure.Date = 0x18;
sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}if (HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { ... }
stimestructure.Hours = 0x08;
stimestructure.Minutes = 0x10;
stimestructure.Seconds = 0x00;
stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}if (HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK) { ... }
}{ ... }
/* ... */
void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
{
RTC_DateTypeDef sTimeStampDateget;
RTC_TimeTypeDef sTimeStampget;
BSP_LED_Toggle(LED4);
#ifdef USE_LCD
HAL_RTCEx_GetTimeStamp(&RtcHandle, &sTimeStampget, &sTimeStampDateget, RTC_FORMAT_BCD);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAtLine(8, (uint8_t *)"TimeStamp Display");
RTC_Time_display(9, LCD_COLOR_BLACK , RTC_Get_Time(&sTimeStampget));/* ... */
#else
HAL_RTCEx_GetTimeStamp(&RtcHandle, &sTimeStampget, &sTimeStampDateget, RTC_FORMAT_BIN);
sprintf((char*)aShowTimeStamp,"%0.2d:%0.2d:%0.2d", sTimeStampget.Hours, sTimeStampget.Minutes, sTimeStampget.Seconds);
sprintf((char*)aShowDateStamp,"%0.2d-%0.2d-%0.2d", sTimeStampDateget.Month, sTimeStampDateget.Date, 2013); /* ... */
#endif
}{ ... }
/* ... */
static void RTC_CalendarShow(void)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
#ifdef USE_LCD
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BCD);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAtLine(6, (uint8_t *)"Current Time Display");
RTC_Time_display(7, LCD_COLOR_BLACK , RTC_Get_Time(&stimestructureget));/* ... */
#else
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN);
sprintf((char*)aShowTime,"%0.2d:%0.2d:%0.2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
sprintf((char*)aShowDate,"%0.2d-%0.2d-%0.2d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year); /* ... */
#endif
}{ ... }
#ifdef USE_LCD
/* ... */
static Table_TypeDef RTC_Get_Time(RTC_TimeTypeDef* RTC_TimeStructure)
{
Table_TypeDef table2;
table2.tab[0] = (((uint8_t)(RTC_TimeStructure->Hours & 0xF0) >> 0x04) + 0x30);
table2.tab[1] = (((uint8_t)(RTC_TimeStructure->Hours & 0x0F))+ 0x30);
table2.tab[2] = 0x3A;
table2.tab[3] = (((uint8_t)(RTC_TimeStructure->Minutes & 0xF0) >> 0x04) + 0x30);
table2.tab[4] = (((uint8_t)(RTC_TimeStructure->Minutes & 0x0F))+ (uint8_t)0x30);
table2.tab[5] = 0x3A;
table2.tab[6] = (((uint8_t)(RTC_TimeStructure->Seconds & 0xF0) >> 0x04)+ 0x30);
table2.tab[7] = (((uint8_t)(RTC_TimeStructure->Seconds & 0x0F)) + 0x30);
table2.tab[8] = 0xA0;
return table2;
}{ ... }
/* ... */
static void RTC_Time_display(uint8_t Line, uint32_t Color_x, Table_TypeDef table )
{
uint8_t i = 0;
BSP_LCD_SetTextColor(Color_x);
for(i=0; i<8; i++)
{
BSP_LCD_DisplayChar((150 + (20 *i)),LINE(Line), table.tab[i]);
}for (i=0; i<8; i++) { ... }
}{ ... }
#endif/* ... */
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t* file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t* file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */