[mw_shl_code=c,true]IWDG_HandleTypeDef IwdgHandle;
TIM_HandleTypeDef TimInputCaptureHandle;
static __IO CPU_INT32U uwLsiFreq = 0;
__IO CPU_INT32U uwCaptureNumber = 0;
__IO CPU_INT32U uwPeriodValue = 0;
__IO CPU_INT32U uwMeasurementDone = 0;
void BSP_IWDG_Init(void)
{
HAL_Init();
if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET)
{
__HAL_RCC_CLEAR_RESET_FLAGS();
}
/*##-2- Get the LSI frequency: TIM5 is used to measure the LSI frequency ###*/
uwLsiFreq = BSP_GetLSIFrequency();
/*##-3- Configure the IWDG peripheral ######################################*/
/* Set counter reload value to obtain 250ms IWDG TimeOut.
IWDG counter clock Frequency = LsiFreq / 32
Counter Reload Value = 250ms / IWDG counter clock period
= 0.25s / (32/LsiFreq)
= LsiFreq / (32 * 4)
= LsiFreq / 128 */
IwdgHandle.Instance = IWDG;
IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32;
IwdgHandle.Init.Reload = uwLsiFreq/128;
if(HAL_IWDG_Init(&IwdgHandle) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
}
/*##-4- Start the IWDG #####################################################*/
if(HAL_IWDG_Start(&IwdgHandle) != HAL_OK)
{
//Error_Handler();
}
/* Infinite loop */
if(HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK)
{
/* Refresh Error */
//Error_Handler();
}
}
/**
* @brief Configures TIM5 to measure the LSI oscillator frequency.
* @param None
* @retval LSI Frequency
*/
CPU_INT32U BSP_GetLSIFrequency(void)
{
CPU_INT32U pclk1 = 0;
TIM_IC_InitTypeDef timinputconfig;
/* Enable the LSI oscillator */
__HAL_RCC_LSI_ENABLE();
/* Wait till LSI is ready */
while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
{
}
/* Configure the TIM peripheral */
/* Set TIMx instance */
TimInputCaptureHandle.Instance = TIM5;
/* TIM5 configuration: Input Capture mode ---------------------
The LSI oscillator is connected to TIM5 CH4.
The Rising edge is used as active edge.
The TIM5 CCR4 is used to compute the frequency value.
------------------------------------------------------------ */
TimInputCaptureHandle.Init.Prescaler = 0;
TimInputCaptureHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimInputCaptureHandle.Init.Period = 0xFFFF;
TimInputCaptureHandle.Init.ClockDivision = 0;
TimInputCaptureHandle.Init.RepetitionCounter = 0;
if(HAL_TIM_IC_Init(&TimInputCaptureHandle) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
}
/* Connect internally the TIM5_CH4 Input Capture to the LSI clock output */
HAL_TIMEx_RemapConfig(&TimInputCaptureHandle, TIM_TIM5_LSI);
/* Configure the Input Capture of channel 4 */
timinputconfig.ICPolarity = TIM_ICPOLARITY_RISING;
timinputconfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
timinputconfig.ICPrescaler = TIM_ICPSC_DIV8;
timinputconfig.ICFilter = 0;
if(HAL_TIM_IC_ConfigChannel(&TimInputCaptureHandle, &timinputconfig, TIM_CHANNEL_4) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
}
/* Reset the flags */
TimInputCaptureHandle.Instance->SR = 0;
/* Start the TIM Input Capture measurement in interrupt mode */
if(HAL_TIM_IC_Start_IT(&TimInputCaptureHandle, TIM_CHANNEL_4) != HAL_OK)
{
//Error_Handler();
}
/* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler() in
stm32f2xx_it.c file) */
while(uwMeasurementDone == 0)
{
}
uwCaptureNumber = 0;
/* Deinitialize the TIM5 peripheral registers to their default reset values */
HAL_TIM_IC_DeInit(&TimInputCaptureHandle);
/* Compute the LSI frequency, depending on TIM5 input clock frequency (PCLK1)*/
/* Get PCLK1 frequency */
pclk1 = HAL_RCC_GetPCLK1Freq();
/* Get PCLK1 prescaler */
if((RCC->CFGR & RCC_CFGR_PPRE1) == 0)
{
/* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */
return ((pclk1 / uwPeriodValue) * 8);
}
else
{ /* PCLK1 prescaler different from 1 => TIMCLK = 2 * PCLK1 */
return (((2 * pclk1) / uwPeriodValue) * 8) ;
}
}[/mw_shl_code]
这个是从STM32的库里找到的例程,稍微修改了下,那么现在问题来了,那个 BSP_IWDG_Init();函数该放到哪里?
有六个任务,放到 AppStart()任务中了,这个任务的大致结构如下:
AppStart()
{
BSP_Init(); //这里是各种驱动的初始化。
BSP_IWDG_Init(); //我放到这里后,屏幕,硬件什么的正常,可是主界面在按下按键后各种没反应
while(1)
{
//这里是主任务做的事情
}
}
该如何处理? |