PER Firmware
Loading...
Searching...
No Matches
rtc.h
Go to the documentation of this file.
1
12#ifndef _PHAL_RTC_H_
13#define _PHAL_RTC_H_
14
15#include "common/phal_F4_F7/phal_F4_F7.h"
16
17// Clock source: currently only supporting LSE
18
19/*
20 When both prescalers are used, it is recommended to configure the asynchronous
21 prescaler to a high value to minimize consumption. (RM0090 26.3.1)
22
23 Default Configuration STM32F4
24 =======================================================
25 The asynchronous prescaler division factor is set to 128, and the synchronous division factor to 256,
26 to obtain an internal clock frequency of 1 Hz (ck_spre) with an LSE frequency of 32.768 kHz.
27*/
28
29// Maximum input frequency is 4MHz, reccomended to use LSE at 32.768 kHz
30// Minimum division factor: 1
31// Maximum division factor: 2^22
32
33// RTC Clock = source / ((ASCNC + 1) * (SYNC + 1))
34// To target 1Hz on LSI, we are doing 32000 / ((255 + 1) * (124 + 1))
35#define RTC_ASYNC_PRESCAL (255U)
36#define RTC_SYNC_PRESCAL (124U)
37
38// STM32 RTC uses lovely BCD
39// https://embedded.fm/blog/2018/6/5/an-introduction-to-bcd
40
41typedef enum {
42 RTC_MONTH_UNKNOWN = 0x0,
43 RTC_MONTH_JANUARY = 0x1,
44 RTC_MONTH_FEBRUARY = 0x2,
45 RTC_MONTH_MARCH = 0x3,
46 RTC_MONTH_APRIL = 0x4,
47 RTC_MONTH_MAY = 0x5,
48 RTC_MONTH_JUNE = 0x6,
49 RTC_MONTH_JULY = 0x7,
50 RTC_MONTH_AUGUST = 0x8,
51 RTC_MONTH_SEPTEMBER = 0x9,
52 RTC_MONTH_OCTOBER = 0x10,
53 RTC_MONTH_NOVEMBER = 0x11,
54 RTC_MONTH_DECEMBER = 0x12,
55} RTC_MONTH_t;
56
57typedef enum {
58 RTC_WEEKDAY_UNKNOWN = 0x0,
59 RTC_WEEKDAY_MONDAY = 0x1,
60 RTC_WEEKDAY_TUESDAY = 0x2,
61 RTC_WEEKDAY_WEDNESDAY = 0x3,
62 RTC_WEEKDAY_THURSDAY = 0x4,
63 RTC_WEEKDAY_FRIDAY = 0x5,
64 RTC_WEEKDAY_SATURDAY = 0x6,
65 RTC_WEEKDAY_SUNDAY = 0x7,
66} RTC_WEEKDAY_t;
67
68typedef enum {
69 RTC_FORMAT_24_HOUR = 0U,
70 RTC_FORMAT_AM_PM,
71} RTC_TIME_FORMAT_t;
72
73typedef struct
74{
75 RTC_MONTH_t month_bcd;
76 RTC_WEEKDAY_t weekday;
77 uint8_t day_bcd;
78 uint8_t year_bcd;
79
81
82typedef struct
83{
84 // Must be a number between 0x00 and 0x12 if RTC_FORMAT_AM_PM
85 // Must be a number between 0x00 and 0x23 if RTC_FORMAT_24_HOUR
86 uint8_t hours_bcd;
87
88 // Must be a number between 0x0 and 0x59
89 uint8_t minutes_bcd;
90 uint8_t seconds_bcd;
91
92 RTC_TIME_FORMAT_t time_format;
94
95typedef struct
96{
97 RTC_date_t date;
98 RTC_time_t time;
100
101#define RTC_CONV_TO_BCD(v) (((v / 10) << 4) | (v % 10))
102
103bool PHAL_getTimeRTC(RTC_timestamp_t* currentTimestamp);
104uint8_t PHAL_configureRTC(RTC_timestamp_t* initial_time, bool force_time);
105
106#endif // _PHAL_RTC_H_
Definition rtc.h:74
Definition rtc.h:83
Definition rtc.h:96