/*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
}
/*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);
}
示例#3
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)));
        }