Skip to main content
Gigli.Dario
Associate II
February 19, 2013
Question

STM32F4 problem with USART1 INTERRUPT

  • February 19, 2013
  • 3 replies
  • 1097 views
Posted on February 19, 2013 at 21:52

Hi guys,

I have a problem with my new schede STM32F407VG. Until now I have been used the STM32F100B so now I'm adapting the code that I wrote. Part of my code is USART with interrupt, so I have wrote a code that read from USART1 (pin rx P10 and pin tx P9) and when the buffer is full the tx interrupt is ENABLE. I think that the configuration GPIO is wrong because I have modified only this part. If I send a string by Terminal I receive different data for example if I send sasdas  I receive {\xfe}{\xfe}{\xfe}{\xf0}{\xfe}, I checked the baudrate on the Terminal and it is the same of the USART.

I prefix that the code works on the STM32F100B, where the GPIO configuration is:

  

 

 GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure USART Tx as alternate function push-pull */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

 

  /* Configure USART Rx as input floating */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA.10 USART1.RX

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

How is the GPIO configuration for the STM32F4?? I'm very confused.

The code write for the STM32F4 is this, where do I err???

/* Includes ------------------------------------------------------------------*/

#include ''stm32f4xx.h''

#include ''stm324xg_eval.h''

char StringLoop[6];

/**************************************************************************************/

 

void RCC_Configuration(void)

{

  /* Enable GPIO clock */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

 

  /* Enable UART clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

}

 /**************************************************************************************/

 

void GPIO_Configuration(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

 // Pin 10 Rx

 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

 // Pin 9 Tx

 GPIO_PinAFConfig( GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

  /* Configure USART Tx as alternate function  */

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

// 

Is it right??

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//// 

Is it right??  Should it be IN FLOATING????

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_Init(GPIOA, &GPIO_InitStructure);  

}

 

/**************************************************************************************/

 

void USART_Configuration(void)

{

  USART_InitTypeDef USART_InitStructure;

  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 configuration */

  USART_Init(USART1, &USART_InitStructure);

  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

  /* Enable the USART1 */

  USART_Cmd(USART1, ENABLE);

    

}

 

/**************************************************************************************/

 

void NVIC_Configuration(void)

{  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   

  NVIC_InitTypeDef NVIC_InitStructure; 

// Configure the NVIC Preemption Priority Bits

 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  /* Enable the USART1 Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

 

/**************************************************************************************/

int main(void)

{

  RCC_Configuration();

 

  GPIO_Configuration();

 

  NVIC_Configuration();

 

  USART_Configuration();

  while(1){

  }

  

}

/**************************************************************************************/

void USART1_IRQHandler(void)

{

  static int tx_index = 0;

  static int rx_index = 0;

  // Interrupt rx

 if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) 

  {

    StringLoop[rx_index++] = USART_ReceiveData(USART1);

    if (rx_index >= (sizeof(StringLoop) - 1))

    { rx_index = 0;

      USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

    }

  }

 // Interrupt tx

  if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) 

  {

    USART_SendData(USART1, StringLoop[tx_index++]);

 

    if (tx_index >= (sizeof(StringLoop) - 1))

    {tx_index = 0;

          USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

}

  }

}

 

 

 /**************************************************************************************/

#ifdef  USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* User can add his own implementation to report the file name and line number,

     ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

 

  /* Infinite loop */

  while (1)

  {

  }

}

#endif

THANK!!
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    February 20, 2013
    Posted on February 20, 2013 at 01:10

    USART1 PA9/PA10 is problematic on the STM32F4-Discovery due to a large bulk capacitor.

    This should demonstrate the basic pin setup (GPIO and AF pin source mux)

    // STM32 USART IRQ TX/RX Loop (USART3 Tx PD.8, Rx PD.9) STM32F4 Discovery - sourcer32@gmail.com
    #include ''stm32f4_discovery.h''
    volatile char StringLoop[] = ''The quick brown fox jumps over the lazy dog
    
    '';
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    /* --------------------------- System Clocks Configuration -----------------*/
    /* USART3 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    /* GPIOD clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /*-------------------------- GPIO Configuration ----------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    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(GPIOD, &GPIO_InitStructure);
    /* Connect USART pins to AF */
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
    }
    /**************************************************************************************/
    void USART3_Configuration(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_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_RXNE | USART_IT_TXE, ENABLE);
    
     }
    /**************************************************************************************/
    void NVIC_Configuration(void)
    {
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    /* Enable the USART3 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    }
    /**************************************************************************************/
    void USART3_IRQHandler(void)
    {
    static int tx_index = 0;
    static int rx_index = 0;
    if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // Transmit the string in a loop
    {
    USART_SendData(USART3, StringLoop[tx_index++]);
    if (tx_index >= (sizeof(StringLoop) - 1))
    tx_index = 0;
    }
    if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) // Received characters modify string
    {
    StringLoop[rx_index++] = USART_ReceiveData(USART3);
    if (rx_index >= (sizeof(StringLoop) - 1))
    rx_index = 0;
    }
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    NVIC_Configuration();
    USART3_Configuration();
    while(1); // Don't want to exit
    }
    /**************************************************************************************/

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Gigli.Dario
    Associate II
    February 20, 2013
    Posted on February 20, 2013 at 11:03

    Thank you, I have just solved the problem. It isn't only due to ''

    USART1 PA9/PA10 is problematic on the STM32F4-Discovery due to a large bulk capacitor

    '' , in fact I tryed to use another USART but I had the same problem. It was due to HSE_VALUE that in my old schede STM32F100B is 8MHz but in the new STM32F4 is 25MHz , so I changed it in 8 MHz and now the USART1 PA6/PA7 finally works.

    THANK AGAIN

    Tesla DeLorean
    Guru
    February 20, 2013
    Posted on February 20, 2013 at 13:22

    The STM32F4-Discovery uses an 8 MHz crystal, HSE_VALUE always needs to reflect the hardware on your board. For other boards posting to the ''STM32 Discovery'' forum seems a bit odd, in the future posting to the ''STM32'' generic forum would be helpful, along with specific details about the board being used. Thanks.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..