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>
21 #include <drivers/led.h>
24 static void periodic_intr() {
26 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
27 rsetbit(RTC_CRL, 4); // start configure
29 rsetbit(RTC_CRH, 0); // enable periodic (second) interrupt
31 while(!rchkbit(RTC_CRL, 5));
33 rwrite(RTC_PRLL, 0x7FFF); // 1 second
34 rclrbit(RTC_CRL, 4); // stop configure
35 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
36 rsetbit(NVIC_ISER0, 3); // enable in register vector
39 static void calibrate_rtc() {
42 // rsetbit(BKP_RTCCR, 7); // enable CC0,
43 // while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
44 // rsetbit(RTC_CRL, 4);
46 // Set up and check tamper pin
48 // rclrbit(RTC_CRL, 4);
49 // while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
52 void * rtc_handler() {
54 //cputs("TICKING IN REAL TIME\n");
55 //uint32_t curr = *RTC_CNTL;
56 int even = *RTC_CNTL % 2;
57 (!even) ? led_off() : led_on();
62 static void setup_rtc() {
64 // TODO: long time to get stable?
65 /* Enable PWREN and BKPEN */
66 rsetbit(RCC_APB1ENR, 28);
67 rsetbit(RCC_APB1ENR, 27);
69 /* Enable access to backup registers and RTC */
72 rsetbit(RCC_BDCR, 0); /* LSE enable */
73 while(!rchkbit(RCC_BDCR, 1)); /* wait for LSE to come up */
75 rsetbitsfrom(RCC_BDCR, 8, 0x1); /* use LSE as RTC source */
76 rsetbit(RCC_BDCR, 15); /* enable RTC */
78 ivt_set_gate(19, rtc_handler, 0); /* setup interrupt handler */
80 // calibrate_rtc(); TODO: TAMPER PIN?
82 periodic_intr();// setup periodic interrupt