mbed-os
Fork of mbed-os by
features/unsupported/tests/KL25Z/rtc/main.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "mbed.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | DigitalOut status_led(LED_BLUE); |
elessair | 0:f269e3021894 | 4 | DigitalOut error_led(LED_RED); |
elessair | 0:f269e3021894 | 5 | |
elessair | 0:f269e3021894 | 6 | extern "C" void RTC_IRQHandler(void) { |
elessair | 0:f269e3021894 | 7 | error_led = 0; |
elessair | 0:f269e3021894 | 8 | } |
elessair | 0:f269e3021894 | 9 | |
elessair | 0:f269e3021894 | 10 | extern "C" void RTC_Seconds_IRQHandler(void) { |
elessair | 0:f269e3021894 | 11 | error_led = 0; |
elessair | 0:f269e3021894 | 12 | } |
elessair | 0:f269e3021894 | 13 | |
elessair | 0:f269e3021894 | 14 | extern "C" void HardFault_Handler(void) { |
elessair | 0:f269e3021894 | 15 | error_led = 0; |
elessair | 0:f269e3021894 | 16 | } |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | extern "C" void NMI_Handler_Handler(void) { |
elessair | 0:f269e3021894 | 19 | error_led = 0; |
elessair | 0:f269e3021894 | 20 | } |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | void rtc_init(void) { |
elessair | 0:f269e3021894 | 23 | // enable the clock to SRTC module register space |
elessair | 0:f269e3021894 | 24 | SIM->SCGC6 |= SIM_SCGC6_RTC_MASK; |
elessair | 0:f269e3021894 | 25 | SIM->SOPT1 = (SIM->SOPT1 & ~SIM_SOPT1_OSC32KSEL_MASK) | SIM_SOPT1_OSC32KSEL(0); |
elessair | 0:f269e3021894 | 26 | |
elessair | 0:f269e3021894 | 27 | // disable interrupts |
elessair | 0:f269e3021894 | 28 | NVIC_DisableIRQ(RTC_Seconds_IRQn); |
elessair | 0:f269e3021894 | 29 | NVIC_DisableIRQ(RTC_IRQn); |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | // Reset |
elessair | 0:f269e3021894 | 32 | RTC->CR = RTC_CR_SWR_MASK; |
elessair | 0:f269e3021894 | 33 | RTC->CR &= ~RTC_CR_SWR_MASK; |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | // Allow write |
elessair | 0:f269e3021894 | 36 | RTC->CR = RTC_CR_UM_MASK | RTC_CR_SUP_MASK; |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | NVIC_EnableIRQ(RTC_Seconds_IRQn); |
elessair | 0:f269e3021894 | 39 | NVIC_EnableIRQ(RTC_Seconds_IRQn); |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | printf("LR: 0x%x\n", RTC->LR); |
elessair | 0:f269e3021894 | 42 | printf("CR: 0x%x\n", RTC->CR); |
elessair | 0:f269e3021894 | 43 | wait(1); |
elessair | 0:f269e3021894 | 44 | if (RTC->SR & RTC_SR_TIF_MASK){ |
elessair | 0:f269e3021894 | 45 | RTC->TSR = 0; |
elessair | 0:f269e3021894 | 46 | } |
elessair | 0:f269e3021894 | 47 | RTC->TCR = 0; |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | // After setting this bit, wait the oscillator startup time before enabling |
elessair | 0:f269e3021894 | 50 | // the time counter to allow the clock time to stabilize |
elessair | 0:f269e3021894 | 51 | RTC->CR |= RTC_CR_OSCE_MASK; |
elessair | 0:f269e3021894 | 52 | for (volatile int i=0; i<0x600000; i++); |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | //enable seconds interrupts |
elessair | 0:f269e3021894 | 55 | RTC->IER |= RTC_IER_TSIE_MASK; |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | // enable time counter |
elessair | 0:f269e3021894 | 58 | RTC->SR |= RTC_SR_TCE_MASK; |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | |
elessair | 0:f269e3021894 | 61 | } |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | int main() { |
elessair | 0:f269e3021894 | 64 | error_led = 1; |
elessair | 0:f269e3021894 | 65 | rtc_init(); |
elessair | 0:f269e3021894 | 66 | |
elessair | 0:f269e3021894 | 67 | while (true) { |
elessair | 0:f269e3021894 | 68 | wait(1); |
elessair | 0:f269e3021894 | 69 | status_led = !status_led; |
elessair | 0:f269e3021894 | 70 | printf("%u\n", RTC->TSR); |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | } |