cancel
Showing results for 
Search instead for 
Did you mean: 

Switch GPIO interruption

Vertinhol
Associate III

Hi, I have 3 interruption pins PA13, PA14 and PA15, I would like to enable one pin for interruption at a time and changing the interruption mode, I made this function, but I'm not sure if it's the right way to achieve that.

void Switch(uint16_t PIN, uint32_t MODE)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	switch(PIN)
	{
		case GPIO_PIN_13:
			GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
			GPIO_InitStruct.Mode = 0;
			GPIO_InitStruct.Pull = GPIO_NOPULL;
			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
			GPIO_InitStruct.Pin = PIN;
			GPIO_InitStruct.Mode = MODE;
			GPIO_InitStruct.Pull = GPIO_NOPULL;
			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
			break;
		case GPIO_PIN_14:
			GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
			GPIO_InitStruct.Mode = 0;
			GPIO_InitStruct.Pull = GPIO_NOPULL;
			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
			GPIO_InitStruct.Pin = PIN;
			GPIO_InitStruct.Mode = MODE;
			GPIO_InitStruct.Pull = GPIO_NOPULL;
			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
			break;
		case GPIO_PIN_15:
			GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
			GPIO_InitStruct.Mode = 0;
			GPIO_InitStruct.Pull = GPIO_NOPULL;
			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
			GPIO_InitStruct.Pin = PIN;
			GPIO_InitStruct.Mode = MODE;
			GPIO_InitStruct.Pull = GPIO_NOPULL;
			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
			break;
	}
}

I'm using a NUCLEO-L476.

2 REPLIES 2
TDK
Guru

Seems like it'll work if you call it with the right arguments. You should use "GPIO_InitStruct.Mode = GPIO_MODE_INPUT;" instead of 0 for clarity. You also need to enable the NVIC interrupt somewhere. Maybe you're doing that somewhere else.

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

It didn't work, choosing the pin any of the three pins didn't disable the interruption on other, I don't know if I'm doing something wrong but maybe I'm not calling correctly the interruption function:

Switch(GPIO_PIN_13, GPIO_MODE_IT_RISING);
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_8); // Diode
}