]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/KL25Z/rtc/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / KL25Z / rtc / main.cpp
1 #include "mbed.h"
2
3 DigitalOut status_led(LED_BLUE);
4 DigitalOut error_led(LED_RED);
5
6 extern "C" void RTC_IRQHandler(void) {
7 error_led = 0;
8 }
9
10 extern "C" void RTC_Seconds_IRQHandler(void) {
11 error_led = 0;
12 }
13
14 extern "C" void HardFault_Handler(void) {
15 error_led = 0;
16 }
17
18 extern "C" void NMI_Handler_Handler(void) {
19 error_led = 0;
20 }
21
22 void rtc_init(void) {
23 // enable the clock to SRTC module register space
24 SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
25 SIM->SOPT1 = (SIM->SOPT1 & ~SIM_SOPT1_OSC32KSEL_MASK) | SIM_SOPT1_OSC32KSEL(0);
26
27 // disable interrupts
28 NVIC_DisableIRQ(RTC_Seconds_IRQn);
29 NVIC_DisableIRQ(RTC_IRQn);
30
31 // Reset
32 RTC->CR = RTC_CR_SWR_MASK;
33 RTC->CR &= ~RTC_CR_SWR_MASK;
34
35 // Allow write
36 RTC->CR = RTC_CR_UM_MASK | RTC_CR_SUP_MASK;
37
38 NVIC_EnableIRQ(RTC_Seconds_IRQn);
39 NVIC_EnableIRQ(RTC_Seconds_IRQn);
40
41 printf("LR: 0x%x\n", RTC->LR);
42 printf("CR: 0x%x\n", RTC->CR);
43 wait(1);
44 if (RTC->SR & RTC_SR_TIF_MASK){
45 RTC->TSR = 0;
46 }
47 RTC->TCR = 0;
48
49 // After setting this bit, wait the oscillator startup time before enabling
50 // the time counter to allow the clock time to stabilize
51 RTC->CR |= RTC_CR_OSCE_MASK;
52 for (volatile int i=0; i<0x600000; i++);
53
54 //enable seconds interrupts
55 RTC->IER |= RTC_IER_TSIE_MASK;
56
57 // enable time counter
58 RTC->SR |= RTC_SR_TCE_MASK;
59
60
61 }
62
63 int main() {
64 error_led = 1;
65 rtc_init();
66
67 while (true) {
68 wait(1);
69 status_led = !status_led;
70 printf("%u\n", RTC->TSR);
71 }
72 }
Imprint / Impressum