Greetings all.
I am having an issue with getting the timer interrupts to trigger, as in, they do not enter the if-statements from user code 4. I have debugged it and cycled through several times with breakpoints at the if-statements but they will not enter. The file in question is the main.c file. I have copied in what I think is relevant, but I might be wrong about that. The setup for the timers are set in Cube IDE Version 1.9.0. I am using a STM32F407VGTxLQFP100.
TLDR: The if-statements arent being entered
I hope this wasn't too confusing.
From int main(void)
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_Base_Start_IT(&htim13);
TIM3->CCR1 = 950;
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
static void MX_TIM6_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 16-1;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 50000-1;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 16-1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 1000-1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
static void MX_TIM13_Init(void)
{
/* USER CODE BEGIN TIM13_Init 0 */
/* USER CODE END TIM13_Init 0 */
/* USER CODE BEGIN TIM13_Init 1 */
/* USER CODE END TIM13_Init 1 */
htim13.Instance = TIM13;
htim13.Init.Prescaler = 16000-1;
htim13.Init.CounterMode = TIM_COUNTERMODE_UP;
htim13.Init.Period = 100-1;
htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim13.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;From user code 4
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// Timer for å ta inn ADC målinger og sende dem på CAN
if(htim->Instance==&htim6)
{
HAL_GPIO_TogglePin(GPIOE, EN_sikring_1300W_Pin);
}
// Timer for så resette sikringen, KANSKJE man skal vurdere å bruke to timere, en for 1300W sikring reset, og en for 240W sikring reset?
if(htim->Instance==&htim13)
{
if (counter > 100)
{
HAL_TIM_Base_Stop_IT(&htim13);
}
else
{
HAL_GPIO_TogglePin(GPIOE,EN_sikring_240W_Pin);
}
counter++;
}
if(htim->Instance==&htim3)
{
counter++;
TIM3->CCR1 = counter;
}
}
/* USER CODE END 4 */Timer isn't accurate
Timer interrupts and HAL_TIM_PeriodElapsedCallback
Videos
You don't show the full code, but my guess is, you set and enable the callback after the timer has already elapsed once or many times, so there is a pending elapsed event which triggers immediately when you try to enable the functionality. It may be due to you using debugger and the timer runs even if code is stopped. It may be that you enable the callback after timer has alredy been running due to spending time running any other code before enabling the callback.
And why the HAL seems to wait one extra millisecod; it can be simply explained by looking at the source code of HAL_Delay - surprised that your source of information did not bother to check it and left it as mystery.
First of all, the HAL code adds 1 timer tick, and as they happen at 1ms in your system, asking for a 50ms delay will guarantee you get a delay of 51 ticks of the timer that increases the time variable.
The comment also explains why it adds the 1 timer tick. As when you call the HAL_Delay with some parameter like 50, you likely call it at a random time compared to the system timer ticks. So there can be a time range between 0 and 1 milliseconds before the next tick happens. If you want a 50ms delay, the HAL is guaranteed to give you a 50ms delay, in the range of 50 to 51 milliseconds, by adding one more tick to wait. If it did not do this, then you would be asking why you get time delays between 49 and 50 milliseconds even if you asked for 50 because you thought it will be enough and you get exactly 50ms but no less. It is hard to please everyone and with 1ms timer ticks, you only get 1ms precision or granularity in the times you need to delay.
A couple of gurus at community.st.com answered this for me. "Cube/HAL functions generate an Update during setup, so the UIF flag is already set."
I had to reset the UIF flag before calling HAL_TIM_Base_Start().
TIM17->SR &= ~TIM_SR_UIF;
HAL_TIM_Base_Start_IT(&htim17); //start 30 msec timeout timer
while waiting for MM to assert our NSS pin
I don't know why STM didn't reset the flag as the first instruction in HAL_TIM_Base_Start(), but there it is.