/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { TIM_IC_InitTypeDef TIMInput_Config; /* Configure the TIM peripheral *********************************************/ /* Set TIMx instance */ Input_Handle.Instance = TIM5; /* TIM5 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM5 TIM_CHANNEL_4. The Rising edge is used as active edge. The TIM5 CCR TIM_CHANNEL_4 is used to compute the frequency value. ------------------------------------------------------------ */ Input_Handle.Init.Prescaler = 0; Input_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Input_Handle.Init.Period = 0xFFFF; Input_Handle.Init.ClockDivision = 0; if(HAL_TIM_IC_Init(&Input_Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Connect internally the TIM5 TIM_CHANNEL_4 Input Capture to the LSI clock output */ __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_TIM5CH4_ENABLE(); /* Configure the Input Capture of TIM_CHANNEL_4 */ TIMInput_Config.ICPolarity = TIM_ICPOLARITY_RISING; TIMInput_Config.ICSelection = TIM_ICSELECTION_DIRECTTI; TIMInput_Config.ICPrescaler = TIM_ICPSC_DIV8; TIMInput_Config.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&Input_Handle, &TIMInput_Config, TIM_CHANNEL_4) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start the TIM Input Capture measurement in interrupt mode */ if(HAL_TIM_IC_Start_IT(&Input_Handle, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } /* Wait until the TIM5 get 2 LSI edges */ while(uwCaptureNumber != 2) { } /* Disable TIM5 CC1 Interrupt Request */ HAL_TIM_IC_Stop_IT(&Input_Handle, TIM_CHANNEL_4); /* Deinitialize the TIM5 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&Input_Handle); return uwLsiFreq; }
/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { uint32_t pclk1 = 0, latency = 0; TIM_IC_InitTypeDef timinputconfig = {0}; RCC_OscInitTypeDef oscinit = {0}; RCC_ClkInitTypeDef clkinit = {0}; /* Enable LSI Oscillator */ oscinit.OscillatorType = RCC_OSCILLATORTYPE_LSI; oscinit.LSIState = RCC_LSI_ON; oscinit.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&oscinit)!= HAL_OK) { Error_Handler(); } /* Configure the TIM peripheral */ /* Set TIMx instance */ TimInputCaptureHandle.Instance = TIMx; /* TIMx configuration: Input Capture mode --------------------- The LSI clock 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, TIMx_REMAP); /* 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) { /* Starting Error */ Error_Handler(); } /* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler() in stm32f4xx_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(); HAL_RCC_GetClockConfig(&clkinit, &latency); /* Get PCLK1 prescaler */ if ((clkinit.APB1CLKDivider) == RCC_HCLK_DIV1) { /* 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) ; } }
/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { uint32_t 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 stm32f4xx_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); } }
/** * @brief Configures TIM14 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { TIM_IC_InitTypeDef TIMInput_Config; /* Configure the TIM peripheral *********************************************/ /* Set TIMx instance */ Input_Handle.Instance = TIM14; /* TIM14 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM14 CH1. The Rising edge is used as active edge. The TIM14 CCR1 is used to compute the frequency value. ------------------------------------------------------------ */ Input_Handle.Init.Prescaler = 0; Input_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Input_Handle.Init.Period = 0xFFFF; Input_Handle.Init.ClockDivision = 0; Input_Handle.Init.RepetitionCounter = 0; if(HAL_TIM_IC_Init(&Input_Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Connect internally the TIM14_CH1 Input Capture to the LSI clock output */ Input_Handle.Instance->OR |= 0x3; // TIM14_OR linked to MCO HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_LSI, RCC_MCODIV_1); /* Configure the Input Capture of channel 1 */ TIMInput_Config.ICPolarity = TIM_ICPOLARITY_RISING; TIMInput_Config.ICSelection = TIM_ICSELECTION_DIRECTTI; TIMInput_Config.ICPrescaler = TIM_ICPSC_DIV8; TIMInput_Config.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&Input_Handle, &TIMInput_Config, TIM_CHANNEL_1) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Reset the flags */ Input_Handle.Instance->SR = 0; /* Start the TIM Input Capture measurement in interrupt mode */ if(HAL_TIM_IC_Start_IT(&Input_Handle, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } /* Wait until the TIM14 get 2 LSI edges (refer to TIM14_IRQHandler() in stm32f3xx_it.c file) ******************************************************/ while(uwCaptureNumber != 2) { } /* Disable TIM14 CC1 Interrupt Request */ HAL_TIM_IC_Stop_IT(&Input_Handle, TIM_CHANNEL_1); /* Deinitialize the TIM14 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&Input_Handle); return uwLsiFreq/*0*/; }