cancel
Showing results for 
Search instead for 
Did you mean: 

Having difficulty getting a timer interrupt to fire- Accidentally deleted the source so here it is......

SScot.3
Associate II
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
TIM_HandleTypeDef htim17;
UART_HandleTypeDef huart1;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM17_Init(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
	uint8_t Snd_Tx_Data[5];												//SPI data
	uint8_t	Audio;														//var to hold which audio playback file
	uint8_t Cntrl_Rx_Data[4];											//RX Data from Nextion - 4 bytes; start-page#-data-end char
	int Cntrl;															//used for debug
	uint8_t Timer;														//Var for amount of time to play the audio file selected
	uint8_t timer_val;
 
	void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
	{
		Cntrl=Cntrl_Rx_Data[2];										//used for double-check pulling 3rd byte works - Live Expression in debug
		 switch (Cntrl_Rx_Data[2])										//use the 3rd byte in the packet to decide what we send out SPI
		 {
		 case 'A':
		 	 HAL_GPIO_WritePin (GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);		//turn ON the BLUE and turn OFF the RED
		 	 HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
		 	 break;
		 case 'B':
		 	 HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);		//turn ON the RED and turn OFF the BLUE
		 	 HAL_GPIO_WritePin (GPIOA, GPIO_PIN_2, GPIO_PIN_SET);
		 	 break;
		 case 'C':
		 {
 
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);		//Pull Sound Chip CS LOW
			 uint8_t SPI_TX_BUF[] = {0xB8,0x03,0x00};
			 HAL_SPI_Transmit (&hspi1, SPI_TX_BUF,3,100);				//send HEX B8-03-00 out SPI - high volume
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);		//Pull Sound Chip CS HIGH
		 	 break;
		 }
		 case 'D':
		 {
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);		//Pull Sound Chip CS LOW
			 uint8_t SPI_TX_BUF[] = {0xB8,0x03,0x80};
			 HAL_SPI_Transmit (&hspi1, SPI_TX_BUF,3,100);				//send HEX B8-03-80 out SPI - med volume
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);		//Pull Sound Chip CS HIGH
		 	 break;
		 }
		 case 'E':
		 {
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);		//Pull Sound Chip CS LOW
			 uint8_t SPI_TX_BUF[] = {0xB8,0x03,0x40};
			 HAL_SPI_Transmit (&hspi1, SPI_TX_BUF,3,100);				//send HEX B8-03-40 out SPI - low volume
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);		//Pull Sound Chip CS HIGH
		 	 break;
		 }
		 case 'F':
		 {
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);		//Pull Sound Chip CS LOW
			 uint8_t SPI_TX_BUF[] = {0xA4,0x00,0x03,0xFF,0xF2};
			 HAL_SPI_Transmit (&hspi1, SPI_TX_BUF,5,100);				//send HEX A4-00-03-FF-F2 out SPI - loop Audio1
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);		//Pull Sound Chip CS HIGH
		 	 break;
		 }										//need to figure out how to select Audio1-4 1st......
		 case 'G':
		 {
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);		//Pull Sound Chip CS LOW
			 uint8_t SPI_TX_BUF[] = {0xA6,0x00,Audio};
			 HAL_SPI_Transmit (&hspi1, SPI_TX_BUF,3,100);   			//send HEX B8-03-00 out SPI - PLAYs whatever is selected
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);		//Pull Sound Chip CS HIGH
		 	 break;
		 }										//need to figure out how to select Audio1-4 1st.......
		 case 'H':
		 {
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);		//Pull Sound Chip CS LOW
			 uint8_t SPI_TX_BUF[] = {0x2A};
			 HAL_SPI_Transmit (&hspi1, SPI_TX_BUF,1,100);   			//send HEX 2A out SPI - STOP
			 HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);		//Pull Sound Chip CS HIGH
		 	 break;
		 }
		HAL_UART_Receive_IT (&huart1, Cntrl_Rx_Data, 4);				//restart the interrupt reception mode
		}
	//Callback: timer has reset
		void HAL_TIM_PeriodElaspedCallback(TIM_HandleTypeDef *htim)		//original
		{
		//Check which version of the timer triggered this callback and toggle the LED
			if (htim == &htim17)
			{
			HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);						//visual to use for debug
			}
		HAL_TIM_Base_Start_IT(&htim17);									//restart the Timer17 interrupt
		}
/* USER CODE END 0 */
int main(void)
{
  /* USER CODE BEGIN 1 */
	HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_SET);				//turn OFF LEDs
	HAL_GPIO_WritePin (GPIOA, GPIO_PIN_2, GPIO_PIN_SET);				//turn OFF LEDs
  /* USER CODE END 1 */
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  MX_TIM17_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT (&huart1, Cntrl_Rx_Data, 4);					//Rx data from keypad interrupt style
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
//	  HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);								//visual to use for debug when needed
//	  HAL_Delay(750);
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL5;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
/**
  * @brief SPI1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI1_Init(void)
{
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}
/**
  * @brief TIM17 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM17_Init(void)
{
  htim17.Instance = TIM17;
  htim17.Init.Prescaler = 4000-1;
  htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim17.Init.Period = 10000-1;
  htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim17.Init.RepetitionCounter = 0;
  htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim17) != HAL_OK)
  {
    Error_Handler();
  }
}

While I am learning each day and have made a great deal of self-progress, I apparently do not yet posses enough background to spot what I am doing wrong.

This should be a simple timer interrupt and I will eventually use it to cancel another function but all I am trying to di is fire a simple LED. Good Grief.

I sincerely want to 'learn how to fish' here so any help pointing me in the right direction will yield an Adult Beverage being had in your honor later this evening......

7 REPLIES 7
LCE
Principal

More info please! With that info, we can't even start guessing, and for a start, we shouldn't be guessing at all...

STM32 device?

LED connection?

Source code of timer init, IRQ, LED-GPIO?

SScot.3
Associate II

Yup - I blew the post by accidentally deleting my source as I was posting - need coffee.......

SScot.3
Associate II

I have tried several from the 'net - they all do not blink the LED.

I can see the timer_val changing using 'Live Expression' but the LED comes on and stays on.......

Latest code - without all the extra 'stuff':

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_TIM3_Init(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
	void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
	{
	    if (htim->Instance==TIM3) 										//check if the interrupt comes from TIM3
	        {
	    	HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);						//visual to use for debug
	        }
	}
/* USER CODE END 0 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
   /* Initialize all configured peripherals */
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
  	HAL_TIM_Base_Start_IT(&htim3);
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  timer_val = __HAL_TIM_GetCounter(&htim3);    					//read TIM3 counter value for debug
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL5;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{
  /* USER CODE BEGIN TIM3_Init 0 */
 
  /* USER CODE END TIM3_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM3_Init 1 */
 
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 40000-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 65535;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */
 
  /* USER CODE END TIM3_Init 2 */
 
}
 
 
 

TDK
Guru

You need to initialize PA4 as a GPIO output if you want to use it to drive an LED. Your code does not initialize this pin. Perhaps you deleted it.

Attaching the main.c file would be helpful. It doesn't appear that the problem is in the code you posted.

You need to implement the IRQ handler somewhere and that handler needs to call HAL_TIM_IRQHandler. Typically this is in stm32f4xx_it.c, or similar.

You didn't answer what chip you're using.

You don't show what your system clock is, but assuming it's around 100 MHz, your timer settings are such that the update happens at 100 MHz/40000/65536 = 0.038 Hz. So you'll need to wait 26 seconds to see the LED change. Are you waiting that long?

> Latest code - without all the extra 'stuff':

When getting a program to work, everything has to be right. In this case, many of the details are in the extra "stuff" that you omitted.

If you feel a post has answered your question, please click "Accept as Solution".
LCE
Principal

As TDK said, GPIO configured?

Peripheral clock enabled?

__HAL_RCC_TIM3_CLK_ENABLE();

Interrupts enabled?

   HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);

   HAL_NVIC_EnableIRQ(TIM3_IRQn);

Check the timer registers, CubeMX does not set all the necessary bits I have found out,

especially the timer enable bit in CR1 and the interrupt enable in DIER.

TIM3->CR1 ?

TIM3->DIER ?

TIM3->ARR ?

TDK-

Thanks for your input. PA4 is set as an Output and the chip is a STM32F03F4. The system clock is set to 40MHz as well as APB1 clocks which are used by the peripherals.

The UART interrupt is working fine and I have followed several different interrupt-driven tutorials from the Web but none seem to work which is crazy as it seems so incredible easy to do this - far easier than what I had to do in Microchip PIC parts!

I have tried all the timers available to me on the chip; TIM1, 3, 14, 16, and 17 - all with the same results.

Currently using TIM3 and Prescale is 40000-1 and Period is set 500.

I did not include everything as it was too long to list - I am still learning what is important in order to help those who wish to help me.

I will still be learning and looking on the web for an answer or answers. I feel it is quite simple and may have to do with this 'simple' proc.

Not sure where to look for these answers so I have included the TIM3 info:

/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{
 
  /* USER CODE BEGIN TIM3_Init 0 */
 
  /* USER CODE END TIM3_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM3_Init 1 */
 
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 40000-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 500-1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */
 
  /* USER CODE END TIM3_Init 2 */
 
}