Beispiel #1
0
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);
}
/*FUNCTION**********************************************************************
 *
 * Function Name : OSA_TimeInit
 * Description   : This function initializes the timer used in BM OSA, the
 * functions such as OSA_TimeDelay, OSA_TimeGetMsec, and the timeout are all
 * based on this timer.
 *
 *END**************************************************************************/
__WEAK_FUNC void OSA_TimeInit(void)
{
#if (FSL_OSA_BM_TIMER_CONFIG == FSL_OSA_BM_TIMER_LPTMER)
    lptmr_prescaler_user_config_t prescaler_config;
    lptmr_working_mode_user_config_t lptmr_config;

    /*
     * Setup LP Timer for timeout and delay.
     * Use 1kHz LPO as clock source, disable prescaler, freerun mode.
     */
    CLOCK_SYS_EnableLptmrClock(BM_LPTMR_INSTANCE);

    LPTMR_HAL_Disable(BM_LPTMR_BASE);

    prescaler_config.prescalerBypass = true;
    prescaler_config.prescalerClockSelect = (lptmr_prescaler_clock_select_t)kClockLptmrSrcLpoClk;
    LPTMR_HAL_SetPrescalerMode(BM_LPTMR_BASE, prescaler_config);

    lptmr_config.freeRunningEnable = true;
    lptmr_config.timerModeSelect = kLptmrTimerModeTimeCounter;
    LPTMR_HAL_SetTimerWorkingMode(BM_LPTMR_BASE, lptmr_config);

    LPTMR_HAL_SetIntCmd(BM_LPTMR_BASE,false);

    LPTMR_HAL_Enable(BM_LPTMR_BASE);
#endif
}
Beispiel #3
0
static void lp_ticker_schedule_lptmr(void)
{
    // schedule LPTMR, restart counter and set compare
    LPTMR_HAL_Disable(LPTMR0_BASE);
    LPTMR_HAL_SetCompareValue(LPTMR0_BASE, lp_lptmr_schedule_ticks);
    LPTMR_HAL_Enable(LPTMR0_BASE);
    lp_lptmr_schedule_ticks = 0;
}
/*FUNCTION**********************************************************************
 *
 * Function Name : LPTMR_HAL_Init
 * Description   : Initialize LPTMR module to reset state.
 *
 *END**************************************************************************/
void LPTMR_HAL_Init(LPTMR_Type * base)
{
    lptmr_working_mode_user_config_t working_mode_config;
    lptmr_prescaler_user_config_t prescaler_config;
    
    LPTMR_HAL_Disable(base);
    LPTMR_HAL_ClearIntFlag(base);

    working_mode_config.timerModeSelect = kLptmrTimerModeTimeCounter;
    working_mode_config.freeRunningEnable = false;
    working_mode_config.pinPolarity = kLptmrPinPolarityActiveHigh;
    working_mode_config.pinSelect = kLptmrPinSelectInput0;
    LPTMR_HAL_SetTimerWorkingMode(base, working_mode_config);
    
    prescaler_config.prescalerValue = kLptmrPrescalerDivide2;
    prescaler_config.prescalerBypass = true;
    prescaler_config.prescalerClockSelect = kLptmrPrescalerClock0;
    LPTMR_HAL_SetPrescalerMode(base, prescaler_config);
    
    LPTMR_HAL_SetCompareValue(base, 0U);
    LPTMR_HAL_SetIntCmd(base, false);
}
Beispiel #5
0
/*FUNCTION**********************************************************************
 *
 * Function Name : LPTMR_DRV_Init
 * Description   : initializes the LPTMR driver.
 * This function will initialize the LPTMR driver according to user configure
 * strcuture.
 *
 *END*/
lptmr_status_t LPTMR_DRV_Init(uint32_t instance, lptmr_state_t *userStatePtr, const lptmr_user_config_t* userConfigPtr)
{
    assert(instance < LPTMR_INSTANCE_COUNT);

    LPTMR_Type * base = g_lptmrBase[instance];
    lptmr_prescaler_user_config_t prescalerUserConfig;
    lptmr_working_mode_user_config_t workingModeUserConfig;

    if ((!userConfigPtr) || (!userStatePtr))
    {
        return kStatus_LPTMR_NullArgument;
    }

     /** prescaler value 0 is invalid while working as pulse counter */
    if ((kLptmrTimerModePulseCounter == userConfigPtr->timerMode) &&
         (true == userConfigPtr->prescalerEnable) &&
         (kLptmrPrescalerDivide2 == userConfigPtr->prescalerValue))
    {
        return kStatus_LPTMR_InvalidPrescalerValue;
    }

     /** Enable clock for lptmr */
    CLOCK_SYS_EnableLptmrClock(instance);

     /** Disable lptmr and reset lptmr logic */
    LPTMR_HAL_Disable(base);

     /** LPTMR prescaler configure */
    prescalerUserConfig.prescalerClockSelect = (lptmr_prescaler_clock_select_t)userConfigPtr->prescalerClockSource;
    prescalerUserConfig.prescalerBypass = (uint8_t)(userConfigPtr->prescalerEnable == false);
    prescalerUserConfig.prescalerValue = userConfigPtr->prescalerValue;
    LPTMR_HAL_SetPrescalerMode(base, prescalerUserConfig);

     /** Working Mode configure */
    workingModeUserConfig.timerModeSelect = userConfigPtr->timerMode;
    workingModeUserConfig.freeRunningEnable = userConfigPtr->freeRunningEnable;
    workingModeUserConfig.pinPolarity = userConfigPtr->pinPolarity;
    workingModeUserConfig.pinSelect = userConfigPtr->pinSelect;
    LPTMR_HAL_SetTimerWorkingMode(base,workingModeUserConfig);

     /** Internal context */
    lptmr_state_ptrs[instance] = userStatePtr;

    userStatePtr->userCallbackFunc = NULL;

     /** LPTMR interrupt */
    if (userConfigPtr->isInterruptEnabled)
    {
        LPTMR_HAL_SetIntCmd(base,true);
        INT_SYS_EnableIRQ(g_lptmrIrqId[instance]);
    }
    else
    {
        LPTMR_HAL_SetIntCmd(base,false);
        INT_SYS_DisableIRQ(g_lptmrIrqId[instance]);
    }

     /** Caculate prescaler clock frequency */
    if ( kLptmrTimerModeTimeCounter == userConfigPtr->timerMode)
    {
        userStatePtr->prescalerClockHz = CLOCK_SYS_GetLptmrFreq(instance,
                userConfigPtr->prescalerClockSource);

        if (userConfigPtr->prescalerEnable)
        {
            userStatePtr->prescalerClockHz = (userStatePtr->prescalerClockHz >> ((uint32_t)(userConfigPtr->prescalerValue+1)));
        }
Beispiel #6
0
static void lptmr_isr(void)
{
    // TODO 0xc0170: looks like this gets fired even before set_interrupt
    LPTMR_HAL_ClearIntFlag(LPTMR0_BASE);
    LPTMR_HAL_Disable(LPTMR0_BASE);
}