Timer Commutation Event Callback not firing STM32F429
EDIT* for those seeking a solution, JW in the replies below pointed out that the DIER register for TIM1 never enabled the interrupt bit for the commutation event and the mistake was that I used
HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);Instead of
HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);---------------------------------------------------------------------------------------------------------------------------------------------------------
Dear whomever may read this,
I've been working with PWM on TIM1 with the NUCLEO-F429ZI, I have three channels and their complementary channels active. This is an iterative test project, where I am bring function to life one step at a time. The following is the configuration for TIM1 (Note channel 2, 2N and 3, 3N have the same configuration as 1 and 1N).
TIM1 Channel Modes
TIM1 Parameters
After being happy with the PWM timer, I enabled the commutation interrupt
TIM1 Interrupts
In the beginning I also had the update interrupt and that was working and firing. Then in my loop I was simply trying to fire a commutation generation event about once every second (just rough test). The if statement runs but the "void TIM1_TRG_COM_TIM11_IRQHandler(void)" routine never runs for some reason.
I then took the event generation out of the loop and did it once in the beginning made a test to see if the commutation flag is being set (it is), but it's never serviced and then cleared. So the LED kept toggling. It can be manually reset.
Here is the current version of my main, in the middle of trying out some tests. The code is temporary test code after trying to debug. But from all I can see I have configured the timer to accept the software events, I thought "void TIM1_TRG_COM_TIM11_IRQHandler(void)" would handle that. Perhaps I am wrong there.
int main(void)
{
HAL_Init();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_ADC1_Init();
MX_USART3_UART_Init();
/* Initialize interrupts */
MX_NVIC_Init();
HAL_TIM_Base_MspInit(&htim1);
HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);
// TODO: This is needed for the update event
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);
HAL_ADC_Start(&hadc1);
uint16_t adc_val = 0;
float duty_cycle = 0.5f;
uint16_t period_pwm = __HAL_TIM_GET_AUTORELOAD(&htim1);
uint32_t last_event = HAL_GetTick();
const uint32_t tick_between = 1000;
HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);
while (1)
{
HAL_ADC_PollForConversion(&hadc1, 1000);
adc_val = HAL_ADC_GetValue(&hadc1);
duty_cycle = adc_val / 4095.0f;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, duty_cycle * period_pwm);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0.5 * duty_cycle * period_pwm);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0.25 * duty_cycle * period_pwm);
if ((HAL_GetTick() - last_event) > tick_between)
{
if (__HAL_TIM_GET_FLAG(&htim1, TIM_FLAG_COM))
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
__HAL_TIM_CLEAR_FLAG(&htim1, TIM_FLAG_COM);
}
last_event = HAL_GetTick();
}
}
HAL_ADC_Stop(&hadc1);
}
I can send further information and answer question if anyone has any idea what is happening, I'm no HAL expert and this is a step in my learning journey. I'd appreciate the help.
