2017-10-03 08:54 AM
Hi.
I am currently working with a STM32L476 Discovery board.
I just want to start an interruption handler with a basic timer, but I am facing an issue.
I would like to toggle a LED from the board when the timer is restarting but, for now, the LED doesn't change its state.
It appears that i can go into 'HAL_TIM_PeriodElapsedCallback' but I think this is just for the tick incrementation.
I would like to know why I can't use the handler with my timer.
Thank you for your time.
Here is the code :
uint8_t LCD_String[]= ' STM32L4xx';
GPIO_InitTypeDef s_gpio;TIM_HandleTypeDef s_timer;int main(void)
{HAL_Init();
/* Configure the system clock to 80 MHz */
SystemClock_Config(); InitializeTimer();/* LCD GLASS Initialization */
BSP_LCD_GLASS_Init();Init_GPIO();
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0U); HAL_NVIC_EnableIRQ(TIM2_IRQn); uint_least16_t timer_value; unsigned char show_tab[10]; while(1) { timer_value = __HAL_TIM_GetCounter(&s_timer); sprintf(&show_tab, '%x', timer_value); BSP_LCD_GLASS_DisplayString(show_tab); HAL_Delay(100); }}void Init_GPIO()
{ /* * Initialize GPIO for LED4/LED5 */ LED4_GPIO_CLK_ENABLE(); LED5_GPIO_CLK_ENABLE();s_gpio.Mode = GPIO_MODE_OUTPUT_PP;
s_gpio.Pull = GPIO_PULLUP; s_gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;s_gpio.Pin = LED4_PIN;
HAL_GPIO_Init(LED4_GPIO_PORT, &s_gpio); s_gpio.Pin = LED5_PIN; HAL_GPIO_Init(LED5_GPIO_PORT, &s_gpio);}void InitializeTimer()
{ __HAL_RCC_TIM2_CLK_ENABLE();s_timer.Instance = TIM2_BASE;
s_timer.Channel = HAL_TIM_ACTIVE_CHANNEL_1; s_timer.Init.Prescaler = 40000; s_timer.Init.CounterMode = TIM_COUNTERMODE_UP; s_timer.Init.Period = 500; s_timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; s_timer.Init.RepetitionCounter = 0;HAL_TIM_Base_Init(&s_timer);
/*Starting the timer*/ HAL_TIM_Base_Start(&s_timer);}extern void TIM2_IRQHandler()
{ HAL_TIM_IRQHandler(&s_timer);}void test()
{ HAL_GPIO_TogglePin(LED4_GPIO_PORT, LED4_PIN);}void SystemClock_Config(void)
{ RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitTypeDef RCC_OscInitStruct = {0};/* MSI is enabled after System reset, activate PLL with MSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; RCC_OscInitStruct.PLL.PLLM = 1; RCC_OscInitStruct.PLL.PLLN = 40; RCC_OscInitStruct.PLL.PLLR = 2; RCC_OscInitStruct.PLL.PLLP = 7; RCC_OscInitStruct.PLL.PLLQ = 4; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { /* Initialization Error */ while(1); }/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { /* Initialization Error */ while(1); }}2017-10-03 12:08 PM
The issue was in the NVIC Initialization I the NVIC and Timer into the Initialize_timer :
I think that in the old code I didn't put the 'HAL_TIM_Base_Start_IT(&s_timer);' and maybe the 3 first functions were not in a good order...
void InitializeTimer()
{ HAL_NVIC_SetPriority(TIM2_IRQn, 10, 0U); HAL_NVIC_EnableIRQ(TIM2_IRQn); __HAL_RCC_TIM2_CLK_ENABLE(); s_timer.Instance = TIM2_BASE; s_timer.Channel = HAL_TIM_ACTIVE_CHANNEL_1; s_timer.Init.Prescaler = 0x1FFF; s_timer.Init.CounterMode = TIM_COUNTERMODE_UP; s_timer.Init.Period = 0XFFF; s_timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; s_timer.Init.RepetitionCounter = 0;HAL_TIM_Base_Init(&s_timer);
/*Starting the timer*/ //HAL_TIM_Base_Start(&s_timer); HAL_TIM_Base_Start_IT(&s_timer);}
2017-10-03 12:12 PM
I am not going to muddle through your code, but if the LED changexs between on and off more then ~10 times / second it will appear not to change