STM32F0, Hall sensor TIM3 interrupt not triggering
My device: STM32F051R8T6
Dev board, F0 disco.
My end goal is to make a BLDC control system with Hall Sensors and utilize the ability of general purpose timer to trigger the interrupt on hall sensor line edge change.
I am using TIM3 as an interface timer with PC6,7,8 as Ch1,2,3 respectively.
For now, just as a test I am planning to feed only PC6, or ch1 with signal generator square wave with 2ms period (1ms on, 1 ms off). I think that in order to just test the ability to receive edge signal and trigger on it is enough, since according to ref. manual all 3 inputs are XOR'ed, and if 2 inputs kept low, while one is toggling, it should still make TIM3 to trigger.
Below is a code i use for initialization:
/*************************************************************************/
// PWM configuration
/*************************************************************************/
/* Compute the prescaler value to have TIM1 counter clock equal to 12MHz */
uwPrescalerValue = (uint32_t)((SystemCoreClock / 12000000) - 1);
//--------------------------------------------------------------------------------
// configure MAIN timer: TIM1
//--------------------------------------------------------------------------------
/* Select the Timer instance */
TimHandle.Instance = TIM1;
TimHandle.Init.Prescaler = uwPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
TimHandle.State = HAL_TIM_STATE_RESET;
if (HAL_TIM_OC_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the output channels ######################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_TIMING;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCIdleState = TIM_OCIDLESTATE_SET;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_SET;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
/* Set the pulse value for channel 1 */
sConfig.Pulse = PULSE1_VALUE;
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = PULSE2_VALUE;
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 3 */
sConfig.Pulse = PULSE3_VALUE;
if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Configure the Break stage ##########################################*/
sConfigBK.OffStateRunMode = TIM_OSSR_ENABLE;
sConfigBK.OffStateIDLEMode = TIM_OSSI_ENABLE;
sConfigBK.LockLevel = TIM_LOCKLEVEL_OFF;
sConfigBK.BreakState = TIM_BREAK_ENABLE;
sConfigBK.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sConfigBK.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;
sConfigBK.DeadTime = 100;
if (HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sConfigBK) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
// at this point all the TIM1 config is done
//--------------------------------------------------------------------------------
// configure interface timers (TIM3)
//--------------------------------------------------------------------------------
/* Set TIM3 instance */
TimHall.Instance = TIM3;
TimHall.Channel = HAL_TIM_ACTIVE_CHANNEL_1;
TimHall.Init.Period = 65535;
TimHall.Init.Prescaler = 126;
TimHall.Init.ClockDivision = 0;
TimHall.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHall.Init.RepetitionCounter = 0;
TimHall.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
TimHall.State = HAL_TIM_STATE_RESET;
TimHallConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
TimHallConfig.IC1Filter = 0;
TimHallConfig.IC1Prescaler = TIM_ICPSC_DIV1;
TimHallConfig.Commutation_Delay = 0;
// initialize the interface timer
HAL_TIMEx_HallSensor_Init(&TimHall, &TimHallConfig);
HAL_TIMEx_ConfigCommutationEvent_IT(&TimHandle, TIM_TS_ITR2, TIM_COMMUTATION_TRGI);
// HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &TimMaster);
//HAL_TIM_SlaveConfigSynchronization_IT(&TimHall, &TimSlave);
// enable and configure TIM3 related interrupt
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
// start the interface timer
HAL_TIMEx_HallSensor_Start_IT(&TimHall);
/*- Start signals generation ###########################################*/
/*--------------------------------------------------------------------------*/
/* Start channel 1 */
if (HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 1N */
if (HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* Start channel 2 */
if (HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 2N */
if (HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* Start channel 3 */
if (HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 3N */
if (HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/*--------------------------------------------------------------------------*/
GPIO_PinState pa0;
/*
do
{
pa0 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
} while (pa0 == GPIO_PIN_RESET);
*/
uint32_t clock_val = HAL_RCC_GetSysClockFreq(); // what is system clock?
printf("its semi testing!\n");
for (;;)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
HAL_Delay(250);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
HAL_Delay(250);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
{
__HAL_TIM_SET_AUTORELOAD(&TimHandle, 599);
}
}
}This is not a complete code yet, since there must be additional code for handling each of 6 "steps" and changing the OCx. however, at this point it is not necessary since I only test for now the Interface timer triggering.
So, the thing is, after i start it all up and feed PC6 with test signal, the interrupt on TIM3 not happening, any idea?