summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/tests/KL25Z/rtc/main.cpp
blob: 31d7e64d262e4899e9c6ff64acc474f0d42fc0ca (plain)
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
62
63
64
65
66
67
68
69
70
71
72
#include "mbed.h"

DigitalOut status_led(LED_BLUE);
DigitalOut error_led(LED_RED);

extern "C" void RTC_IRQHandler(void) {
    error_led = 0;
}

extern "C" void RTC_Seconds_IRQHandler(void) {
    error_led = 0;
}

extern "C" void HardFault_Handler(void) {
    error_led = 0;
}

extern "C" void NMI_Handler_Handler(void) {
    error_led = 0;
}

void rtc_init(void) {
    // enable the clock to SRTC module register space
    SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
    SIM->SOPT1 = (SIM->SOPT1 & ~SIM_SOPT1_OSC32KSEL_MASK) | SIM_SOPT1_OSC32KSEL(0);

    // disable interrupts
    NVIC_DisableIRQ(RTC_Seconds_IRQn);
    NVIC_DisableIRQ(RTC_IRQn);

    // Reset
    RTC->CR = RTC_CR_SWR_MASK;
    RTC->CR &= ~RTC_CR_SWR_MASK;

    // Allow write
    RTC->CR = RTC_CR_UM_MASK | RTC_CR_SUP_MASK;

    NVIC_EnableIRQ(RTC_Seconds_IRQn);
    NVIC_EnableIRQ(RTC_Seconds_IRQn);

    printf("LR: 0x%x\n", RTC->LR);
    printf("CR: 0x%x\n", RTC->CR);
    wait(1);
    if (RTC->SR & RTC_SR_TIF_MASK){
        RTC->TSR = 0;
    }
    RTC->TCR = 0;

    // After setting this bit, wait the oscillator startup time before enabling
    // the time counter to allow the clock time to stabilize
    RTC->CR |= RTC_CR_OSCE_MASK;
    for (volatile int i=0; i<0x600000; i++);

    //enable seconds interrupts
    RTC->IER |= RTC_IER_TSIE_MASK;

    // enable time counter
    RTC->SR |= RTC_SR_TCE_MASK;


}

int main() {
    error_led = 1;
    rtc_init();

    while (true) {
        wait(1);
        status_led = !status_led;
        printf("%u\n", RTC->TSR);
    }
}