Looking for some simple timer sample code (mine seems to cause a Hard Fault)
I'm using STM32CubeMX to develop an app for the STM32F030. I've done this many times and thought I understood the various issues - but I'm having a hell of a time getting a basic timer (TIM6) to run (interrupt-driven).
The generated init code is:
htim6.Instance = TIM6;
htim6.Init.Prescaler = 0;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 816;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}Then I start it with this: (The GPIO is for debugging - see below).
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_SET); //!!!
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_RESET); //!!!
// Start the system timer
HAL_TIM_Base_Start_IT(&htim6);
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_SET); //!!!
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_RESET); //!!!Finally, my interrupt handler simply asserts a GPIO test point and increments a global variable:
/**
* @brief This function handles TIM6 global interrupt.
*/
void TIM6_IRQHandler(void)
{
/* USER CODE BEGIN TIM6_IRQn 0 */
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_SET);
swTimerTick++;
/* USER CODE END TIM6_IRQn 0 */
HAL_TIM_IRQHandler(&htim6);
/* USER CODE BEGIN TIM6_IRQn 1 */
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_RESET);
/* USER CODE END TIM6_IRQn 1 */
}When I run this, it goes into the ditch. On the oscilloscope, I see only the first test point pulse (before the call to HAL_TIM_Base_Start_IT(). If I run it in the debugger, I can step through that _Start call seemingly without issues, but when I tell it to run it hangs. If I stop the debugger, it's looping in the HardFault_Handler.
I feel like I must be missing something obvious.... Any thoughts?