void _rtc_handler(int argc, char **argv) { if (argc < 2) { _rtc_usage(); } else if (strncmp(argv[1], "init", 4) == 0) { rtc_init(); } else if (strncmp(argv[1], "poweron", 7) == 0) { rtc_poweron(); } else if (strncmp(argv[1], "poweroff", 8) == 0) { rtc_poweroff(); } else if (strncmp(argv[1], "clearalarm", 8) == 0) { rtc_clear_alarm(); } else if (strncmp(argv[1], "getalarm", 8) == 0) { _rtc_getalarm(); } else if (strncmp(argv[1], "setalarm", 8) == 0) { _rtc_setalarm(argv + 2); } else if (strncmp(argv[1], "gettime", 7) == 0) { _rtc_gettime(); } else if (strncmp(argv[1], "settime", 7) == 0) { _rtc_settime(argv + 2); } else { printf("unknown command: %s\n", argv[1]); } }
void rtc_init(void) { RtcMode2 *rtcMode2 = &(RTC_DEV); /* Turn on power manager for RTC */ PM->APBAMASK.reg |= PM_APBAMASK_RTC; /* RTC uses External 32,768KHz Oscillator (OSC32K isn't accurate enough p1075/1138)*/ SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_ONDEMAND | SYSCTRL_XOSC32K_EN32K | SYSCTRL_XOSC32K_XTALEN | SYSCTRL_XOSC32K_STARTUP(6) | SYSCTRL_XOSC32K_ENABLE; /* Setup clock GCLK2 with OSC32K divided by 32 */ GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4); while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY); GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL ); while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY); GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos))); while (GCLK->STATUS.bit.SYNCBUSY); /* DISABLE RTC MASTER */ while (rtcMode2->STATUS.reg & RTC_STATUS_SYNCBUSY); rtc_poweroff(); /* Reset RTC */ while (rtcMode2->STATUS.bit.SYNCBUSY); rtcMode2->CTRL.reg= RTC_MODE2_CTRL_SWRST; while (rtcMode2->STATUS.bit.SYNCBUSY); /* RTC config with RTC_MODE2_CTRL_CLKREP = 0 (24h) */ rtcMode2->CTRL.reg = RTC_MODE2_CTRL_PRESCALER_DIV1024|RTC_MODE2_CTRL_MODE_CLOCK; while (rtcMode2->STATUS.bit.SYNCBUSY); rtcMode2->INTENSET.reg = RTC_MODE2_INTENSET_OVF; while (rtcMode2->STATUS.bit.SYNCBUSY); rtc_poweron(); }
void rtc_init(void) { RtcMode2 *rtcMode2 = &(RTC_DEV); /* Turn on power manager for RTC */ PM->APBAMASK.reg |= PM_APBAMASK_RTC; SYSCTRL->OSC32K.bit.ENABLE = 0; SYSCTRL->OSC32K.bit.ONDEMAND = 1; SYSCTRL->OSC32K.bit.RUNSTDBY = 0; SYSCTRL->OSC32K.bit.EN1K = 1; SYSCTRL->OSC32K.bit.EN32K = 1; SYSCTRL->OSC32K.bit.ENABLE = 1; /* Setup clock GCLK2 with OSC32K divided by 32 */ GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4); while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY); GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL ); while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY); GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos))); while (GCLK->STATUS.bit.SYNCBUSY); /* DISABLE RTC MASTER */ while (rtcMode2->STATUS.reg & RTC_STATUS_SYNCBUSY); rtc_poweroff(); /* Reset RTC */ while (rtcMode2->STATUS.bit.SYNCBUSY); rtcMode2->CTRL.reg= RTC_MODE2_CTRL_SWRST; while (rtcMode2->STATUS.bit.SYNCBUSY); /* RTC config with RTC_MODE2_CTRL_CLKREP = 0 (24h) */ rtcMode2->CTRL.reg = RTC_MODE2_CTRL_PRESCALER_DIV1024|RTC_MODE2_CTRL_MODE_CLOCK; while (rtcMode2->STATUS.bit.SYNCBUSY); rtcMode2->INTENSET.reg = RTC_MODE2_INTENSET_OVF; while (rtcMode2->STATUS.bit.SYNCBUSY); rtc_poweron(); }
void rtc_init(void) { /* Turn on power manager for RTC */ /* Already done in cpu_init() */ /* MCLK->APBAMASK.reg |= MCLK_APBAMASK_RTC; */ /* DISABLE RTC MASTER */ rtc_poweroff(); #if EXTERNAL_OSC32_SOURCE /* RTC uses External 32,768KHz Oscillator */ OSC32KCTRL->XOSC32K.reg = OSC32KCTRL_XOSC32K_XTALEN | OSC32KCTRL_XOSC32K_EN1K | OSC32KCTRL_XOSC32K_RUNSTDBY | OSC32KCTRL_OSC32K_ENABLE; /* Wait XOSC32K Ready */ while (OSC32KCTRL->STATUS.bit.XOSC32KRDY==0); /* RTC source clock is external oscillator at 1kHz */ OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K; #endif /* EXTERNAL_OSC32_SOURCE */ #if INTERNAL_OSC32_SOURCE uint32_t * pCalibrationArea; uint32_t osc32kcal; /* Read OSC32KCAL, calibration data for OSC32 !!! */ pCalibrationArea = (uint32_t*) NVMCTRL_OTP5; osc32kcal = ( (*pCalibrationArea) & 0x1FC0 ) >> 6; /* RTC use Low Power Internal Oscillator at 1kHz */ OSC32KCTRL->OSC32K.reg = OSC32KCTRL_OSC32K_RUNSTDBY | OSC32KCTRL_OSC32K_EN1K | OSC32KCTRL_OSC32K_CALIB(osc32kcal) | OSC32KCTRL_OSC32K_ENABLE; /* Wait OSC32K Ready */ while (OSC32KCTRL->STATUS.bit.OSC32KRDY==0); /* RTC uses internal 32,768KHz Oscillator */ OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_OSC1K; #endif /* INTERNAL_OSC32_SOURCE */ #if ULTRA_LOW_POWER_INTERNAL_OSC_SOURCE /* RTC uses Ultra Low Power internal 32,768KHz Oscillator */ OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K; #endif /* ULTRA_LOW_POWER_INTERNAL_OSC_SOURCE */ /* Software Reset the RTC */ RTC->MODE2.CTRLA.bit.SWRST = 1; /* Wait end of reset */ while (RTC->MODE2.CTRLA.bit.SWRST); /* RTC config with RTC_MODE2_CTRL_CLKREP = 0 (24h) */ RTC->MODE2.CTRLA.reg = RTC_MODE2_CTRLA_PRESCALER_DIV1024 | /* CLK_RTC_CNT = 1KHz / 1024 -> 1Hz */ #if (SAML21XXXB) || (SAMR30) RTC_MODE2_CTRLA_CLOCKSYNC | /* Clock Read Synchronization Enable */ #endif RTC_MODE2_CTRLA_MODE_CLOCK; /* Mode 2: Clock/Calendar */ /* Clear interrupt flags */ RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_OVF; RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_ALARM0; rtc_poweron(); }