-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrtc.ino
61 lines (50 loc) · 1.51 KB
/
rtc.ino
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
// T4 RTC 32khz crystal LPSRTC start and sync HPRTC too ?
#include <time.h>
//extern void *__rtc_localtime; // Arduino build process sets this, boards.txt
// latest: T4 bootloader sets RTC time
// SNVS_LPSRTCLR gives subsecond values, counts 32768 ticks/sec
#define SNVS_DEFAULT_PGD_VALUE (0x41736166U)
#define SNVS_LPSR_PGD_MASK (0x8U)
#define SNVS_LPSRTCMR (IMXRT_SNVS.offset050)
#define SNVS_LPSRTCLR (IMXRT_SNVS.offset054)
void rtc_init() {
CCM_CCGR2 |= CCM_CCGR2_IOMUXC_SNVS(CCM_CCGR_ON);
SNVS_LPGPR = SNVS_DEFAULT_PGD_VALUE;
SNVS_LPSR = SNVS_LPSR_PGD_MASK;
// ? calibration
// ? tamper pins
SNVS_LPCR |= 1; // start RTC
while (!(SNVS_LPCR & 1));
}
void rtc_set_time(uint32_t secs) {
SNVS_LPCR &= ~1; // stop RTC
while (SNVS_LPCR & 1);
SNVS_LPSRTCMR = (uint32_t)(secs >> 17U);
SNVS_LPSRTCLR = (uint32_t)(secs << 15U);
SNVS_LPCR |= 1; // start RTC
while (!(SNVS_LPCR & 1));
}
uint32_t rtc_secs() {
uint32_t seconds = 0;
uint32_t tmp = 0;
/* Do consecutive reads until value is correct */
do
{
seconds = tmp;
tmp = (SNVS_LPSRTCMR << 17U) | (SNVS_LPSRTCLR >> 15U);
} while (tmp != seconds);
return seconds;
}
void setup() {
Serial.begin(9600);
while (!Serial);
delay(2000);
rtc_init();
// rtc_set_time((uint32_t)&__rtc_localtime); //LPSRTC will start at 0 otherwise 1547051415;
time_t tt = rtc_secs();
Serial.printf("time set to %s\n", ctime(&tt));
}
void loop() {
Serial.println(rtc_secs());
delay(1000);
}