cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L152 EXTI problem

garciavictor
Associate
Posted on November 27, 2011 at 19:47

Hi all.

First of all excuse my english

I am trying to implement the EXTI library but I am doing something wrong.

The program starts and send 0 by the uart (at this point all ok), then whait for a external interrupt with PA0 falling edgue (checked with the oscilloscope and its ok)

I dont know if the flag is reset or not, after applying the external interrupt the program not answered.

This is the code:

#include ''stm32l1xx.h''                 

#include <stdio.h>

GPIO_InitTypeDef  GPIO_InitStructure;

USART_InitTypeDef  USART_InitStructure;

EXTI_InitTypeDef  EXTI_InitStructure;

NVIC_InitTypeDef  NVIC_InitStructure;

uint8_t CONT=0x00;

int main (void){

///UART CONFIGURATION////////////////////////////////////

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

 GPIO_PinAFConfig(GPIOA, 2, GPIO_AF_USART2);

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 USART_InitStructure.USART_BaudRate = 115200;

 USART_InitStructure.USART_WordLength = USART_WordLength_8b;

 USART_InitStructure.USART_StopBits = USART_StopBits_1;

 USART_InitStructure.USART_Parity = USART_Parity_No ;

 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 USART_Init(USART2, &USART_InitStructure);

 USART_Cmd(USART2, ENABLE);

///EXTI CONFIGURATION//////////////////////////////

 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;

 EXTI_InitStructure.EXTI_Line = EXTI_Line0;

 EXTI_InitStructure.EXTI_LineCmd = ENABLE;

 EXTI_Init(&EXTI_InitStructure);

 EXTI_ClearITPendingBit(EXTI_Line0);

 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

 USART_SendData(USART2, 0x30);

 while (1)

  {

  if (EXTI_GetITStatus(EXTI_Line0)== SET){

   EXTI_ClearITPendingBit(EXTI_Line0);

   EXTI_ClearFlag(EXTI_Line0);

   USART_SendData(USART2, CONT);

   CONT++;

   }

  }

}

I hope you can help me.

Thanks in advance and best regards

2 REPLIES 2
Posted on November 27, 2011 at 22:20

If you are generating interrupts, you'd better service them in their interrupt handler, and not in a loop in main().

Failing to clear the interrupt flag within the interrupt service will result in the M3 being stuck endlessly in interrupt context, and no foreground code will execute.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
garciavictor
Associate
Posted on December 01, 2011 at 10:25

Hi clive, thanks a lot for the advice, I used the irqhandler and it is running.

I did use loop to simplify but finally was an error

Regards