在线时间269 小时
UID2056636
注册时间2013-11-13
NXP金币0
TA的每日心情 | 慵懒 2016-11-24 10:19 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
金牌会员
 
- 积分
- 2870
- 最后登录
- 1970-1-1
|
学习 LPTMR 的驱动,在函数中有段指针的操作把我给搞晕了,求指教
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)));
}
}
return kStatus_LPTMR_Success;
}
lptmr_state_ptrs[instance] = userStatePtr;
userStatePtr->userCallbackFunc = NULL;
红色部分代码是关于指针的,目的是实现 LPTMR 中断中调用的回调函数
假设调用该函数的用户定义了一个全家变量 UserState,
那么通过执行第一句红色代码, lptmr_state_ptrs[instance] 和 userStatePtr 都指向的是 UserState
再执行第二句红色代码,全局变量 UserState 中的回调函数岂不是变成空指针了?不解,求解惑
|
|