1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/7/30 - ROBIN KRENS
15 #include <sys/robsys.h>
18 #include <lib/regfunc.h>
19 #include <lib/stdio.h>
22 static void periodic_intr() {
24 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
27 rsetbit(RTC_CRH, 0); // enable periodic (second) interrupt
29 while(!rchkbit(RTC_CRL, 5));
31 rwrite(RTC_PRLL, 0x7FFF); // 1 second
33 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
36 static void calibrate_rtc() {
39 // rsetbit(BKP_RTCCR, 7); // enable CC0,
40 // while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
41 // rsetbit(RTC_CRL, 4);
43 // Set up and check tamper pin
45 // rclrbit(RTC_CRL, 4);
46 // while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
49 void * rtc_handler() {
51 //cputs("TICKING IN REAL TIME\n");
55 static void setup_rtc() {
57 // TODO: long time to get stable?
58 /* Enable PWREN and BKPEN */
59 rsetbit(RCC_APB1ENR, 28);
60 rsetbit(RCC_APB1ENR, 27);
62 /* Enable access to backup registers and RTC */
65 rsetbit(RCC_BDCR, 0); /* LSE enable */
66 while(!rchkbit(RCC_BDCR, 1)); /* wait for LSE to come up */
68 rsetbitsfrom(RCC_BDCR, 8, 0x1); /* use LSE as RTC source */
69 rsetbit(RCC_BDCR, 15); /* enable RTC */
71 ivt_set_gate(19, rtc_handler, 0); /* setup interrupt handler */
73 // calibrate_rtc(); TODO: TAMPER PIN?
75 periodic_intr();// setup periodic interrupt