AnsweredAssumed Answered

STM32F4 USART3 IRQ & EXTI IRQ

Question asked by schenk.kyle on Jul 9, 2014
Latest reply on Jul 10, 2014 by schenk.kyle
Hi,

I would like to ask for some guidance with regards to using a USART3 interrupt and a normal external interrupt in the same project. I am using an RTOS and Keil uVision5.

In the below code the interrupt triggers without any problems until I uncomment the NVIC_config(); line. At this point the rest of the code does not run (four test threads). I have searched through countless posts and found many  that seem like they will help me solve my issue and get things running. Unfortunately I have not yet had success. I have also found plenty of examples on how to read the USART data, all of which are straight forward and I for the most part understand, however they do not work in my IRQ handler - I have removed it from the code now. I think the problem stems from the configuration, there must be something I am doing wrong there.

My experience with microcontrollers is limited, especially with USART communication.

If anyone would be so kind as to point me in the right direction / help me get this code running correctly I will be very grateful

Thanking in advance

/*----------------------------------------------------------------------------
 * CMSIS-RTOS 'main' function template
 *---------------------------------------------------------------------------*/
 
#define osObjectsPublic                                // define objects in main module
#include "osObjects.h"                               // RTOS object definitions
#include "stm32f4xx.h"                                  // Device header
#include "stm32f4xx_gpio.h"
#include <stm32f4xx_rcc.h>
#include "cmsis_os.h"
#include "stdio.h"
#include "stm32f4xx_usart.h"
/*
 ------------------Global Variables------------------
*/
int ISR_PE8 = 0;                                                    
int x1, x2, x3;
//Blue RIFD Tag 0C0032E969BE
//Yellow RIFD Tag  0A008CC72160

/*
 ------------------EXTI_config------------------
*/
void EXTI_config(void)
{
  NVIC_InitTypeDef   NVIC_InitStructure;
    EXTI_InitTypeDef   EXTI_InitStructure;
    
  // Configure EXTI Line8 & EXTI Line9  
  EXTI_InitStructure.EXTI_Line = EXTI_Line8 |  EXTI_Line9;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  //Enable and set EXTI Line8 & EXTI Line9 Interrupt to the lowest priority
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
/*
 ------------------NVIC_config------------------
*/
void NVIC_config(void)
{
  NVIC_InitTypeDef   NVIC_InitStructure;
    
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
 
  // Enable the USART3 Interrupt
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
/*
 ------------------RCC_init------------------
*/
void RCC_init(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable PortB clock - AHB1 bus
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //Enable PortD clock - AHB1 bus
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //Enable PortE clock - AHB1 bus
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
}
/*
 ------------------GPIO_init------------------
*/
void GPIO_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;            //Initialize GPIO    
/*
 ------------------Configure Output Pins------------------
*/       
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    
/*
 ------------------Configure USART3 Pins------------------
*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
/*
 ------------------Configure EXTI Pins------------------
*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
    
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource8);
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource9);
}
/*
 ------------------usart3_configure------------------
*/
void usart3_configure(void)
{
    USART_InitTypeDef USART_InitStructure;
  /* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
                - USART TX/RX (USART3 Tx PB.10, Rx PB.11)
  */
  USART_InitStructure.USART_BaudRate = 9600;
  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(USART3, &USART_InitStructure);
 
  USART_Cmd(USART3, ENABLE);

    USART_ITConfig(USART3, USART_IT_TXE, ENABLE);

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);    
}
/*
 ------------------USART3_IRQHandler------------------
*/
void USART3_IRQHandler(void)
{
    //Read RIFD Tag
    /*
            Tag reader info
            part number - RDM630
            TTL Interface RS232 Data output format
            1. 9600bps,N,8,1
            2. CHECKSUM: card 10byte DATA entire do XOR operation
                    
            02 | 10ASCII Data Characters | Checksum | 03

            Example: card number: 62E3086CED
            Output data:36H,32H,45H,33H,30H,38H,36H,43H,45H,44H
            CHECKSUM: (62H) XOR (E3H) XOR (08H) XOR (6CH) XOR (EDH)=08H
    */
}

void EXTI9_5_IRQHandler(void)                    
{
    osDelay(500);
  if(EXTI_GetITStatus(EXTI_Line8) != RESET)            
  {
        ISR_PE8 = 1;                                                                 
        x1 = 1;
        x2 = 2;
        x3 = 3;
    EXTI_ClearITPendingBit(EXTI_Line8);                    //Clear the interrupt flag
  }
}
/*
 ------------------Testing------------------
*/
void One (void const *argument)
{
    while(1)
    {
        if(ISR_PE8 !=0)                                                        
        {
            printf("One\n");
            GPIO_SetBits(GPIOD,GPIO_Pin_14);
            GPIO_SetBits(GPIOD,GPIO_Pin_12);
            osDelay(500);
            GPIO_ResetBits(GPIOD,GPIO_Pin_14);
            GPIO_ResetBits(GPIOD,GPIO_Pin_12);
            ISR_PE8 = 0;        
        }
    }
}
/*
 ------------------Testing------------------
*/
void Two (void const *argument)
{    
    while(1)
    {
        if(x1 == 1)
        {
            printf("Two\n");
            osDelay(500);
            x1 = 0;
        }
    }
}
/*
 ------------------Testing------------------
*/
void Three (void const *argument)
{
    while(1)
    {
        if(x2== 2)
        {
            printf("Three\n");
            osDelay(500);
            x2 = 0;
        }
    }
}
/*
 ------------------Testing------------------
*/
void Four (void const *argument)
{
    while(1)
    {
        if(x3== 3)
        {
            printf("Four\n");
            osDelay(500);
            x3 = 0;
        }
    }
}
/*
 ------------------Thread defiine and priority set------------------
*/
osThreadDef(One, osPriorityNormal, 1, 0);
osThreadDef(Two, osPriorityNormal, 1, 0);
osThreadDef(Three, osPriorityNormal, 1, 0);
osThreadDef(Four, osPriorityNormal, 1, 0);
/*
 ------------------main: initialize and start the system------------------
*/
int main (void)     
{    
  osKernelInitialize ();                    // initialize CMSIS-RTOS
/*
 ------------------Create Threads------------------
*/    
    osThreadCreate(osThread (One), NULL);
    osThreadCreate(osThread (Two), NULL);    
    osThreadCreate(osThread (Three), NULL);    
    osThreadCreate(osThread (Four), NULL);    
/*
 ------------------Init & config(s)------------------
*/    
    RCC_init();
    GPIO_init();
    usart3_configure();
    EXTI_config();    
    //NVIC_config();
    
    osKernelStart ();                         // start thread execution
}

Outcomes