cancel
Showing results for 
Search instead for 
Did you mean: 

Interrupts Management

BILLyTheLiTTle
Associate II

I am trying to understand the priorities, subpriorities and priority grouping in my stm32f407-Discovery.

I would like to have 3 external LEDS (LD_A, LD_B, LD_C) and 3 interrupts (IT_A, IT_B, IT_C) with. IT_A has a highest priority, IT_C has the lowest one.

IT_A turns off LD_B and LD_C but turns on LD_A. The other interrupts work accordingly.

What I want:

When the IT_B happens, the IT_C should not happen but the IT_A should happen.

What I get:

No matter what interrupt happens first it seems that it steals the whole cycles of the CPU and the other interrupts cannot happen.

Here is the source code:

...
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define STATE_SIZE 3
#define COUNTER_SIZE 4
/* USER CODE END PD */
 
/* USER CODE BEGIN PV */
uint8_t state[STATE_SIZE] = {RESET, RESET, RESET};
uint8_t counter[COUNTER_SIZE] = {0, 0, 0, 0};
uint8_t firstRun = 1, red = 0, blue = 0;
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  
  HAL_Init();
SystemClock_Config();
 
  MX_GPIO_Init();
 
  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_1);
  while (1)
  {
	  if (firstRun) {
		  firstRun = 0;
		  HAL_GPIO_WritePin(Red_External_LED_GPIO_Port, Red_External_LED_Pin, RESET);
		  HAL_GPIO_WritePin(Blue_External_LED_GPIO_Port, Blue_External_LED_Pin, RESET);
		  HAL_GPIO_WritePin(Green_External_LED_GPIO_Port, Green_External_LED_Pin, RESET);
		  HAL_Delay(500);
		  HAL_GPIO_WritePin(Red_External_LED_GPIO_Port, Red_External_LED_Pin, SET);
		  HAL_Delay(500);
		  HAL_GPIO_WritePin(Blue_External_LED_GPIO_Port, Blue_External_LED_Pin, SET);
		  HAL_Delay(500);
		  HAL_GPIO_WritePin(Green_External_LED_GPIO_Port, Green_External_LED_Pin, SET);
		  HAL_Delay(500);
		  HAL_GPIO_WritePin(Green_External_LED_GPIO_Port, Green_External_LED_Pin, RESET);
		  HAL_Delay(500);
		  HAL_GPIO_WritePin(Blue_External_LED_GPIO_Port, Blue_External_LED_Pin, RESET);
		  HAL_Delay(500);
		  HAL_GPIO_WritePin(Red_External_LED_GPIO_Port, Red_External_LED_Pin, RESET);
		  HAL_Delay(500);
	  }
  }
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
 
   /*Configure GPIO pins : Green_External_LED_IT_Pin Red_External_LED_IT_Pin Blue_External_LED_IT_Pin */
  GPIO_InitStruct.Pin = Green_External_LED_IT_Pin|Red_External_LED_IT_Pin|Blue_External_LED_IT_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
   
 /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);
 
  HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ(EXTI1_IRQn);
 
  HAL_NVIC_SetPriority(EXTI2_IRQn, 1, 1);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);
 
}
 
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
 
	if (GPIO_Pin == Red_External_LED_IT_Pin) {
		state[0] = HAL_GPIO_ReadPin(Red_External_LED_IT_GPIO_Port, Red_External_LED_IT_Pin);
		counter[0]++;
		blue = 0, red = 1;
		while(red) {
			HAL_GPIO_TogglePin(Red_External_LED_GPIO_Port, Red_External_LED_Pin);
			for(int i = 0; i < 1000000; i++);
		}
	}
	else if (GPIO_Pin == Blue_External_LED_IT_Pin) {
		state[1] = HAL_GPIO_ReadPin(Blue_External_LED_IT_GPIO_Port, Blue_External_LED_IT_Pin);
		counter[1]++;
		red = 0, blue = 1;
		while(blue) {
			HAL_GPIO_TogglePin(Blue_External_LED_GPIO_Port, Blue_External_LED_Pin);
			for(int i = 0; i < 1000000; i++);
		}
	}
	else if (GPIO_Pin == Green_External_LED_IT_Pin) {
		state[2] = HAL_GPIO_ReadPin(Green_External_LED_IT_GPIO_Port, Green_External_LED_IT_Pin);
		HAL_GPIO_WritePin(Green_External_LED_GPIO_Port, Green_External_LED_Pin, state[2]);
		counter[2]++;
	}
	else {
		counter[3]++;
	}
}

2 REPLIES 2
BILLyTheLiTTle
Associate II

Problem solved.

I had not understand well how priorities work. I had to "play" with preemption values instead of subpriorities.

The posts which helped me understand are this and that!

You should've gathered this information from the Programming Manual to the processor core of your STM32 model (i.e. Cortex-M4).

JW