1
0
tmk_keyboard/tool/mbed/mbed-sdk/libraries/tests/KL25Z/rtc/main.cpp
Jun Wako 1fe4406f37 Squashed 'tmk_core/' changes from 7967731..b9e0ea0
b9e0ea0 Merge commit '7fa9d8bdea3773d1195b04d98fcf27cf48ddd81d' as 'tool/mbed/mbed-sdk'
7fa9d8b Squashed 'tool/mbed/mbed-sdk/' content from commit 7c21ce5

git-subtree-dir: tmk_core
git-subtree-split: b9e0ea08cb940de20b3610ecdda18e9d8cd7c552
2015-04-24 16:26:14 +09:00

73 lines
1.5 KiB
C++

#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);
}
}