1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/8/14 - ROBIN KRENS
8 * Real Time Clock configuration. A Real Time Clock is configured to use
9 * the more accurate external chrystal (LSE) and ticks at exactly
10 * one second. (Can be further calibrated using a tamper pin)
11 * Each clock tick triggers an interrupt and invokes the real time clock
14 * Note: this is not the Cortex M3/M4 SysTick!
22 #include <sys/robsys.h>
25 #include <lib/regfunc.h>
26 #include <lib/tinyprintf.h>
28 #include <drivers/led.h>
29 #include <drivers/tm1637.h>
30 #include <drivers/mk450_joystick.h>
33 /* This handler is invoked each clock tick. I've included two examples
34 * one is writing the clock count to a 4 grid led display. Another is a simple LED blink */
35 void * rtc_handler() {
37 // Real time clock output on a LED display
38 /* int cntvalue = *RTC_CNTL;
39 if (cntvalue < 9999) {
41 unsigned char dn[10] = {0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6};
43 int grid0, grid1, grid2, grid3;
45 grid0 = cntvalue % 10;
46 grid1 = ((cntvalue % 100) - grid0) / 10;
47 grid2 = ((cntvalue % 1000) - grid0 - grid1) / 100;
48 grid3 = ((cntvalue % 10000) - grid0 - grid1 - grid2) / 1000;
50 //printf("%d, %d, %d, %d\n", grid0, grid1, grid2, grid3);
52 char current[4] = { dn[grid3], dn[grid2], dn[grid1], dn[grid0] };
54 for (int i = 0; i < 4; i++) {
55 set_grid(i, current[i], false);
62 uint32_t curr = *RTC_CNTL;
63 int even = *RTC_CNTL % 2;
64 (!even) ? led_off() : led_on();
66 // Double Axis Joystick
67 //printf("X: %x\n", mk450_getx());
68 //printf("Y: %x\n", mk450_gety());
70 rclrbit(RTC_CRL, 0); /* clear interrupt flag */
73 /* Enable a periodic interrupt. At each clock count a interrupt is triggered
74 * and consequently the handler is invoked. */
75 static void periodic_intr() {
77 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
78 rsetbit(RTC_CRL, 4); // start configure
79 rsetbit(RTC_CRH, 0); // enable periodic (second) interrupt
81 while(!rchkbit(RTC_CRL, 5));
82 rwrite(RTC_PRLL, 0x7FFF); // 0x7FFF equals 1 second clock configuration
83 rclrbit(RTC_CRL, 4); // stop configure
84 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
85 rsetbit(NVIC_ISER0, 3); // enable in register vector
88 /* Setup the Real time clock to work with the Low speed external
89 * chrystal (32.768 kHz) */
90 static void setup_rtc() {
92 /* Enable PWREN and BKPEN */
93 rsetbit(RCC_APB1ENR, 28);
94 rsetbit(RCC_APB1ENR, 27);
96 /* Enable access to backup registers and RTC */
99 rsetbit(RCC_BDCR, 0); /* LSE enable */
100 while(!rchkbit(RCC_BDCR, 1)); /* wait for LSE to come up */
102 rsetbitsfrom(RCC_BDCR, 8, 0x1); /* use LSE as RTC source */
103 rsetbit(RCC_BDCR, 15); /* enable RTC */
105 ivt_set_gate(19, rtc_handler, 0); /* setup interrupt handler */
107 //calibrate_rtc(); // uncomment if need, needs TAMPER PIN
109 periodic_intr();// setup periodic interrupt
113 /* Function to calibrate the RTC even more accurate.
114 * Output to a so-called tamper pin. Uncomment if needed */
115 static void calibrate_rtc() {
116 /* rsetbit(BKP_RTCCR, 7); // enable CC0,
117 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated
119 // Set up and check tamper pin
121 while(!rchkbit(RTC_CRL, 5)); // Check last write is terminated */