cancel
Showing results for 
Search instead for 
Did you mean: 

timer 3 CNT not resetting upon elapsed period with an ARR of 4

JustSomeGuy
Senior

EDIT: My apologies, it turns out that since i had auto reload preload enabled and was not setting the CNT back to 0, the timer would keep counting up until a rollover. 

I wrote a test program to test a 5 ms  PWM single pulse output with a 3-timer master/slave configuration with an automatic period scaling feature that I wrote.

you press the onboard user pushbutton to cycle through a couple of presets.

TIM22 is used as a prescalar for TIM3 by setting its slave mode as external clock mode 1. then TIM3's TRGO is mapped to TIM'2 trigger source. TIM2 is configured in slave mode reset mode. As far as I'm aware, when TIM3's CNT reaches the ARR, the program should call HAL_TIM_PeriodElapsedCallback() and reset the CNT register, no? However when i'm on preset where TIM3's ARR is set to 4, the controller will seem to 'skip' the entire UE and not trigger an interrupt nor reset TIM2 like it is supposed to. TIM3 will just keep count up. Why is this happening? Attached is my .ioc file.

inside main() after the cubeMX initialization functions, i have this:

 

 

 

  calculate_timer_chain_period(&htim22, &htim3, pulsePresets[currentPulsePreset]);
  HAL_TIM_Base_Start(&htim22);
  __HAL_TIM_DISABLE(&htim22);
  HAL_TIM_Base_Start_IT(&htim3);
  __HAL_TIM_DISABLE(&htim3);
  HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_1);

 pulseCount = pulsePresets[currentPulsePreset];
  //HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_2);

  while (1)
  {
	  if(presetChanged > 0)
	  {
		  //if there is only 1 pulse to send, send it right away, otherwise scale the period to the number of pulses
		  if(pulseCount == 1)
			  __HAL_TIM_ENABLE(&htim2);
		  else
			  //calculate the new period
			  calculate_timer_chain_period(&htim22, &htim3, pulseCount);

		  presetChanged = false;
	  }
  }

 

 

 

and my functions look like this

 

 

 

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM2)
	{
		pulseCount--;
		HAL_GPIO_TogglePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin);
	}

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{
		HAL_GPIO_TogglePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
		if(!(htim2.Instance->CR1 & TIM_CR1_CEN))
			__HAL_TIM_ENABLE(&htim2);
		if(pulseCount <= 1)
		{
			__HAL_TIM_DISABLE(&htim22);
			__HAL_TIM_DISABLE(&htim3);
		}
	}
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == USER_PB_Pin)
	{
		(currentPulsePreset < (numberOfPresets - 1)) ? (currentPulsePreset++) : (currentPulsePreset = 0);
		pulseCount = pulsePresets[currentPulsePreset];
		presetChanged = true;
	}
}

 

 

and here are my global variables

 

 

bool startPWM;
bool presetChanged = true;
uint8_t currentPulsePreset = 0;
uint32_t blueLedTimestamp = 0;
uint32_t redLedTimestamp = 0;
uint32_t greenLedTimestamp = 0;
uint32_t pulsePresets[] = {1, 450, 900, 1800, 3600, 10000, 100000};
uint8_t numberOfPresets = (sizeof(pulsePresets) / sizeof(uint32_t));
uint16_t pulseCount = 0;

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Mike_ST
ST Employee

Hello,

Thank you for your question,

 


@JustSomeGuy wrote:

EDIT: My apologies, it turns out that since i had auto reload preload enabled and was not setting the CNT back to 0, the timer would keep counting up until a rollover. 

Can we consider your problem as solved ?

View solution in original post

2 REPLIES 2
Mike_ST
ST Employee

Hello,

Thank you for your question,

 


@JustSomeGuy wrote:

EDIT: My apologies, it turns out that since i had auto reload preload enabled and was not setting the CNT back to 0, the timer would keep counting up until a rollover. 

Can we consider your problem as solved ?

Indeed