AnsweredAssumed Answered

Can not setup NVIC for usart3 receive interrupt

Question asked by doan_trung.nghia on Jun 9, 2014
Latest reply on Jun 15, 2014 by doan_trung.nghia
Hi everybody,
I programmed some code for the usart 1,2,3 and 6 of the STM32F405rg, the four mentioned usarts work fine but there is only problem of the nvic_init function of the usart3. When i have this command, my program no longer works, whereas when i disable this command it runs correctly. The usart1,2 and 6 work fine with the received interrupt service program. Please point out which is incorrect of my program. 

I just want to make the usart3 can do the same things as the other usart 1,2, and 6 when i send data from my computer. Are there any differences between the usart3 and the others ???

Thanks.

#include "my_usart.h"


#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
     
//----- variables -----     
uint8_t usart_port;
//---------------------     


void MY_USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{    
  /* Transmit Data */
  USARTx->DR = (Data & (uint16_t)0x01FF);
}


#ifdef usart_1
void USART1_Config(void)
{
     //---type define -------
     GPIO_InitTypeDef GPIOA_InitStructure;
     USART_InitTypeDef USART1_InitStructure;
     NVIC_InitTypeDef  USART1_NVIC_InitStructure;
     //----------------------
     
     //---- enable clock for all relative components -----------
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
     //---------------------------------------------------------
     
     //--- config pa10(rx) as input_pp_pin, pa9(tx) as output_pp_pin----
     GPIOA_InitStructure.GPIO_Mode=GPIO_Mode_AF;
     GPIOA_InitStructure.GPIO_OType=GPIO_OType_PP;
     GPIOA_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
     GPIOA_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
     GPIOA_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
     GPIO_Init(GPIOA,&GPIOA_InitStructure);     
          
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
     //---------------------------------------------------------
     
     //---- config the specific operation of the usart1 --------
     USART1_InitStructure.USART_BaudRate=115200;
     USART1_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
     USART1_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
     USART1_InitStructure.USART_Parity=USART_Parity_No;
     USART1_InitStructure.USART_StopBits=USART_StopBits_1;
     USART_Init(USART1,&USART1_InitStructure);
     USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//enable received complet interrupt
     USART_ClearITPendingBit(USART1,USART_IT_RXNE);//clear received complete intr flag for the first detection
     //---------------------------------------------------------
     
     //---- config the NVIC for received interrupt -------------
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
     USART1_NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
     USART1_NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
     USART1_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
     USART1_NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
     NVIC_Init(&USART1_NVIC_InitStructure);
     //---------------------------------------------------------
          
     //------ enable USART1--------
     USART_Cmd(USART1, ENABLE);
     //----------------------------
}


void USART1_IRQHandler(void)
{
     if(USART_GetITStatus(USART1,USART_IT_RXNE))//receive complete
     {
          char rx = USART1->DR;
          rx-=48;
          usart_port=1;
          if(rx)
          {
               printf("aaaa\r");
          }
          else
          {
               printf("bbbb\r");
          }
          USART_ClearITPendingBit(USART1,USART_IT_RXNE);
     }
}
#endif


#ifdef usart_2
void USART2_Config(void)
{
     //---type define -------
     GPIO_InitTypeDef GPIOA_InitStructure;
     USART_InitTypeDef USART2_InitStructure;
     NVIC_InitTypeDef  USART2_NVIC_InitStructure;
     //----------------------
     
     //---- enable clock for all relative components -----------
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
     //---------------------------------------------------------
     
     //--- config pa3(rx) as input_pp_pin, pa2(tx) as output_pp_pin----
     GPIOA_InitStructure.GPIO_Mode=GPIO_Mode_AF;
     GPIOA_InitStructure.GPIO_OType=GPIO_OType_PP;
     GPIOA_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;
     GPIOA_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
     GPIOA_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
     GPIO_Init(GPIOA,&GPIOA_InitStructure);     
          
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
     //---------------------------------------------------------
     
     //---- config the specific operation of the usart1 --------
     USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//enable received complet interrupt
     USART_ClearITPendingBit(USART2,USART_IT_RXNE);
     USART2_InitStructure.USART_BaudRate=115200;
     USART2_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
     USART2_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
     USART2_InitStructure.USART_Parity=USART_Parity_No;
     USART2_InitStructure.USART_StopBits=USART_StopBits_1;
     USART_Init(USART2,&USART2_InitStructure);
     //---------------------------------------------------------
     
     //---- config the NVIC for received interrupt -------------
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
     USART2_NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
     USART2_NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
     USART2_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
     USART2_NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
     NVIC_Init(&USART2_NVIC_InitStructure);
     //---------------------------------------------------------
     
     //------ enable USART2--------
     USART_Cmd(USART2, ENABLE);
     //----------------------------
}


void USART2_IRQHandler(void)
{
     if(USART_GetITStatus(USART2,USART_IT_RXNE))//receive complete
     {
          char rx = USART2->DR;
          rx-=48;
          usart_port=2;
          if(rx)
          {
               printf("cccc\r");
          }
          else
          {
               printf("eeee\r");
          }
          USART_ClearITPendingBit(USART2,USART_IT_RXNE);
     }
}
#endif


#ifdef usart_3
void USART3_Config(void)
{
     //---type define -------
     GPIO_InitTypeDef GPIOB_InitStructure;
     USART_InitTypeDef USART3_InitStructure;
     NVIC_InitTypeDef  USART3_NVIC_InitStructure;
     //----------------------
     
     //---- enable clock for all relative components -----------
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
     //---------------------------------------------------------
     
     //--- config pb10(tx) as input_pp_pin, pb11(rx) as output_pp_pin----
     GPIOB_InitStructure.GPIO_Mode=GPIO_Mode_AF;
     GPIOB_InitStructure.GPIO_OType=GPIO_OType_PP;
     GPIOB_InitStructure.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_11;
     GPIOB_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
     GPIOB_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
     GPIO_Init(GPIOB,&GPIOB_InitStructure);     
          
     GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
     GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
     //---------------------------------------------------------
     
     //---- config the specific operation of the usart1 --------
     USART3_InitStructure.USART_BaudRate=115200;
     USART3_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
     USART3_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
     USART3_InitStructure.USART_Parity=USART_Parity_No;
     USART3_InitStructure.USART_StopBits=USART_StopBits_1;
     USART_Init(USART3,&USART3_InitStructure);     
     USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//enable received complet interrupt
     USART_ClearITPendingBit(USART3,USART_IT_RXNE);
     //---------------------------------------------------------
     
     //---- config the NVIC for received interrupt -------------
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
     USART3_NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
     USART3_NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
     USART3_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
     USART3_NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
     //NVIC_Init(&USART3_NVIC_InitStructure);
     //---------------------------------------------------------
     
     //------enable USART3--------
     USART_Cmd(USART3,ENABLE);
     //----------------------------
}


void USART3_IRQHandler(void)
{
     if(USART_GetITStatus(USART3,USART_IT_RXNE))//receive complete
     {
          char rx = USART3->DR;
          rx-=48;
          usart_port=3;
          if(rx)
          {
               printf("tttt\r");
          }
          else
          {
               printf("yyyy\r");
          }
          USART_ClearITPendingBit(USART3,USART_IT_RXNE);
     }
}


#endif




#ifdef usart_6


void USART6_Config(void)
{
     //---type define -------
     GPIO_InitTypeDef GPIOC_InitStructure;
     USART_InitTypeDef USART6_InitStructure;
     NVIC_InitTypeDef  USART6_NVIC_InitStructure;
     //----------------------
     
     //---- enable clock for all relative components -----------
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);
     //---------------------------------------------------------
     
     //--- config pc6(tx) as input_pp_pin, pc7(rx) as output_pp_pin----
     GPIOC_InitStructure.GPIO_Mode=GPIO_Mode_AF;
     GPIOC_InitStructure.GPIO_OType=GPIO_OType_PP;
     GPIOC_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
     GPIOC_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
     GPIOC_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
     GPIO_Init(GPIOC,&GPIOC_InitStructure);     
          
     GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6);
     GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6);
     //---------------------------------------------------------
     
     //---- config the specific operation of the usart1 --------
     USART6_InitStructure.USART_BaudRate=115200;
     USART6_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
     USART6_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
     USART6_InitStructure.USART_Parity=USART_Parity_No;
     USART6_InitStructure.USART_StopBits=USART_StopBits_1;
     USART_Init(USART6,&USART6_InitStructure);
     USART_ITConfig(USART6,USART_IT_RXNE,ENABLE);
     USART_ClearFlag(USART6,USART_FLAG_RXNE);
     //---------------------------------------------------------
     
     //---- config the NVIC for received interrupt -------------
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
     USART6_NVIC_InitStructure.NVIC_IRQChannel=USART6_IRQn;
     USART6_NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
     USART6_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
     USART6_NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
     NVIC_Init(&USART6_NVIC_InitStructure);
     //---------------------------------------------------------
     
     //------ enable USART6--------
     USART_Cmd(USART6, ENABLE);
     //----------------------------
}


     void USART6_IRQHandler(void)
{
     if(USART_GetITStatus(USART6,USART_IT_RXNE))//receive complete
     {
          char rx = USART6->DR;
          rx-=48;
          usart_port=6;
          if(rx)
          {
               printf("hhhh\r");
          }
          else
          {
               printf("jjjj\r");
          }
          USART_ClearITPendingBit(USART6,USART_IT_RXNE);
     }
}
#endif


PUTCHAR_PROTOTYPE
{
     switch(usart_port)
     {
          case 1:
                    USART1->DR = ((uint16_t)ch & (uint16_t)0x01FF);
                    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
                    break;
          case 2:
                    USART2->DR = ((uint16_t)ch & (uint16_t)0x01FF);
                    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET){}          
                    break;
          case 3:
                    USART3->DR = ((uint16_t)ch & (uint16_t)0x01FF);
                    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET){}          
                    break;
          case 6:
                    USART6->DR = ((uint16_t)ch & (uint16_t)0x01FF);
                    while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET){}          
                    break;
     }
     return ch;     



//--------- my_usart.h contents ------------
#ifndef MY_USART_H
#define MY_USART_H
#endif


#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"
#include "misc.h"
#include <stdio.h>


#define usart_1
#define usart_2
#define usart_3
#define usart_6


//-------- list of variables ----------
extern uint8_t usart_port;
//-------------------------------------


//-------- list of functions ----------
#ifdef usart_1
void USART1_Config(void);
void USART1_IRQHandler(void);
#endif


#ifdef usart_2
void USART2_Config(void);
void USART2_IRQHandler(void);
#endif


#ifdef usart_3
void USART3_Config(void);
void USART3_IRQHandler(void);
#endif


#ifdef usart_6
void USART6_Config(void);
void USART6_IRQHandler(void);
#endif


//-------------------------------------


Outcomes