uint32_t lp_ticker_read() {
    if (!lp_ticker_inited) {
        lp_ticker_init();
    }
    uint32_t temp_pre = RTC_HAL_GetPrescaler(RTC_BASE);
    // RTC sec might be GT 4095, therfore use modulo the max allowance
    uint32_t temp_sec = RTC_HAL_GetSecsReg(RTC_BASE) % (1 << RTC_TIMER_BITS);
    while ((temp_pre != RTC_HAL_GetPrescaler(RTC_BASE)) || (temp_sec != (RTC_HAL_GetSecsReg(RTC_BASE) & RTC_TIMER_MASK))) {
        temp_pre = RTC_HAL_GetPrescaler(RTC_BASE);
        temp_sec = RTC_HAL_GetSecsReg(RTC_BASE) & RTC_TIMER_MASK;
    }
    return (temp_sec << 15 | temp_pre);
}
void lp_ticker_set_interrupt(uint32_t now, uint32_t time) {
    // On K64F, the compare value can be only set when the LPTMR is disabled
    // However, disabling the LPTMR will automatically clear the LPTMR counter
    // So we need to compensate for the time that already passed
    lp_compare_value = time;
    uint32_t ticks = time > now ? time - now : (uint32_t)((uint64_t)time + 0xFFFFFFFFu - now);
    lp_lptmr_schedule_ticks = 0;
    lp_ticker_lptmr_scheduled_flag = 0;

    RTC_HAL_EnableCounter(RTC_BASE, false);
    if (ticks > LPTMR_TIMER_MAX_VALUE) {
        ticks -= RTC_PRESCALER_MAX_VALUE + 1 - RTC_HAL_GetPrescaler(RTC_BASE);
        uint32_t seconds = RTC_HAL_GetSecsReg(RTC_BASE);
        while (ticks > LPTMR_TIMER_MAX_VALUE) {
            ticks -= RTC_PRESCALER_MAX_VALUE + 1;
            seconds++;
        }
        RTC_HAL_SetAlarmReg(RTC_BASE, seconds);
        RTC_HAL_SetAlarmIntCmd(RTC_BASE, true);
        // the lp timer will be triggered once RTC alarm is set
        lp_lptmr_schedule_ticks = ticks;
    } else {
        // restart counter, set compare
        LPTMR_HAL_Disable(LPTMR0_BASE);
        LPTMR_HAL_SetCompareValue(LPTMR0_BASE, ticks);
        LPTMR_HAL_Enable(LPTMR0_BASE);
    }
    RTC_HAL_EnableCounter(RTC_BASE, true);
}
Exemple #3
0
/*FUNCTION**********************************************************************
 *
 * Function Name : RTC_DRV_GetDatetime
 * Description   : gets the actual RTC time and stores it in the given time struct.
 * This function will get the actual RTC time and stores it in the given time
 * struct.
 *
 *END**************************************************************************/
void RTC_DRV_GetDatetime(uint32_t instance, rtc_datetime_t *datetime)
{
    assert(datetime);

    uint32_t rtcBaseAddr = g_rtcBaseAddr[instance];
    uint32_t seconds = 0;
    uint32_t srcClock = 0;

    RTC_HAL_GetDatetimeInSecs(rtcBaseAddr, &seconds);

    if ((srcClock = CLOCK_SYS_GetExternalRefClock32kFreq()) != 32768U)
    {
        /* In case the input clock to the RTC counter is not 32KHz, the seconds register will not
         * increment every second, therefore the seconds register value needs to be adjusted.
         * to get actual seconds. We then add the prescaler register value to the seconds.
         */
        seconds = (seconds * (32768U / srcClock)) + (RTC_HAL_GetPrescaler(rtcBaseAddr) / srcClock);
    }
    RTC_HAL_ConvertSecsToDatetime(&seconds, datetime);
}