cancel
Showing results for 
Search instead for 
Did you mean: 

Changing EXTINT Trigger Type after Init

PSpei
Associate II

Hi All,

I'm using the following code to set up 4 external triggers and this all works well. What I'd like to do is to be able to change the trigger type of any of the EXTINT at some point in the program. I've tried a couple of things which only resulted in lock ups.

Can someone please point me in the right direction. Would it be better to parametise the below a bit and then just run the entire code block if I wanted to change the trigger type.

Thanks,

Paul

EXTI_InitTypeDef   EXTI_InitStructure;
	GPIO_InitTypeDef   GPIO_InitStructure;
	NVIC_InitTypeDef   NVIC_InitStructure;
 
	/* Configure PA.00 pin as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOD, &GPIO_InitStructure);
 
	/* Enable AFIO clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
 
	/* Connect EXTI0, 1, 2, 3 Lines to PD.xx pins */
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource0);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource1);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource2);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3);
 
 
	// Trigger conditions
	//EXTI_Trigger_Rising = 0x08,
	//EXTI_Trigger_Falling = 0x0C,
	//EXTI_Trigger_Rising_Falling = 0x10
 
 
	/* Configure EXTI0 line */
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
	//EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
 
	EXTI_InitStructure.EXTI_Line = EXTI_Line0;
	EXTI_Init(&EXTI_InitStructure);
 
	EXTI_InitStructure.EXTI_Line = EXTI_Line1;
	EXTI_Init(&EXTI_InitStructure);
 
	EXTI_InitStructure.EXTI_Line = EXTI_Line2;
	EXTI_Init(&EXTI_InitStructure);
 
	EXTI_InitStructure.EXTI_Line = EXTI_Line3;
	EXTI_Init(&EXTI_InitStructure);
 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_Init(&NVIC_InitStructure);
 
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_Init(&NVIC_InitStructure);
 
	NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
	NVIC_Init(&NVIC_InitStructure);
 
	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
	NVIC_Init(&NVIC_InitStructure);

3 REPLIES 3

It's as simple as setting/clearing the respective bit in EXTI_RTSR and EXTI_FTSR. Read the EXTI chapter in RM.

JW

PSpei
Associate II

Hi,

It doesn't appear to be that simple, the following code should set those flags, however as soon as I run the function to choose a different trigger type, I'm no longer catching pulses.

void Set_EXTI_Trigger(uint8_t int_num, EXTITrigger_TypeDef trigger) {
	EXTI_InitTypeDef   EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	// Set trigger type
	EXTI_InitStructure.EXTI_Trigger = trigger;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
 
	switch (int_num) {
		case 0: {
			EXTI_InitStructure.EXTI_Line = EXTI_Line0;
			break;
		}
		case 1: {
			EXTI_InitStructure.EXTI_Line = EXTI_Line1;
			break;
		}
		case 2: {
			EXTI_InitStructure.EXTI_Line = EXTI_Line2;
			break;
		}
		case 3: {
			EXTI_InitStructure.EXTI_Line = EXTI_Line3;
			break;
		}
	}
	// Initialise interrupt
	EXTI_Init(&EXTI_InitStructure);
}

PSpei
Associate II

Update: All good, got it sorted.