cancel
Showing results for 
Search instead for 
Did you mean: 

USART STM32F4 PROBLEM

haythem
Associate II
Posted on March 26, 2014 at 20:51

Hi, I have an error in my code and I can not seem identifying the problem

The erreur is : ..\main.c(97): error:  #169: expected a declaration

This is my code 

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

 #include ''stm32f4_discovery.h''

 #include <stm32f4xx_usart.h>

  GPIO_InitTypeDef GPIO_InitStructure;

   USART_InitTypeDef USART_InitStructure;

void RCC_Configuration(void);

void GPIO_Configuration(void);

void USART1_Configuration(void);

void RCC_Configuration(void)

{

  /* --------------------------- System Clocks Configuration -----------------*/

  /* USART1 clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

 

  /* GPIOA clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOD, ENABLE);

}

 

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

 

void GPIO_Configuration(void)

{

 

  /*-------------------------- GPIO Configuration ----------------------------*/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

  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(GPIOA, &GPIO_InitStructure);

 

  /* Connect USART pins to AF */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);   // USART1_TX

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);  // USART1_RX

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

  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);

}

 

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

 

void USART1_Configuration(void)

{

 

  /* 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(USART1, &USART_InitStructure);

 

  USART_Cmd(USART1, ENABLE);

}

 

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

 char test;

int main(void)

{

    RCC_Configuration();

 

    GPIO_Configuration();

 

  USART1_Configuration();

 

   while(1)

  {

    uint16_t Data;

 

    while(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET); // Wait for Char

 

    Data = USART_ReceiveData(USART1); // Collect Char

 

    while(USART_GetITStatus(USART1, USART_IT_TXE) == RESET); // Wait for Empty

 

    USART_SendData(USART3, Data); // Echo Char

  }

 

  while(1); // Don't want to exit

}

}

8 REPLIES 8
Posted on March 26, 2014 at 21:01

One too many closing braces? Helps when you paste code with line number references to use the ''Format Code Block'' and check Line Numbers

Random reference to USART3

Also be aware, and I think I've pointed this out before, you cannot use PA9 on the STM32F4-Discovery, read the schematic.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 26, 2014 at 21:08

  1. /**************************************************************************************/
  2.  #include ''stm32f4_discovery.h''
  3.  #include <stm32f4xx_usart.h>
  4.   GPIO_InitTypeDef GPIO_InitStructure;
  5.    USART_InitTypeDef USART_InitStructure;
  6. void RCC_Configuration(void);
  7. void GPIO_Configuration(void);
  8. void USART1_Configuration(void);
  9. int main(void)
  10. {
  11. RCC_Configuration();
  12. GPIO_Configuration();
  13. USART1_Configuration();
  14.  
  15. while(1)
  16.   {
  17.     uint16_t Data;
  18.  
  19.     while(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET); // Wait for Char
  20.  
  21.     Data = USART_ReceiveData(USART1); // Collect Char
  22.  
  23.     while(USART_GetITStatus(USART1, USART_IT_TXE) == RESET); // Wait for Empty
  24.  
  25.     USART_SendData(USART3, Data); // Echo Char
  26.   }
  27. }
  28. void RCC_Configuration(void)
  29. {
  30.   /* --------------------------- System Clocks Configuration -----------------*/
  31.   /* USART1 clock enable */
  32.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  33.   /* GPIOA clock enable */
  34.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  35. }
  36.  
  37. /**************************************************************************************/
  38.  
  39. void GPIO_Configuration(void)
  40. {
  41.  
  42.   /*-------------------------- GPIO Configuration ----------------------------*/
  43.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
  44.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  45.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  46.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  47.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  48.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  49.  
  50.   /* Connect USART pins to AF */
  51.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_USART1);   // USART1_RX
  52.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_USART1);  // USART1_TX
  53. }
  54.  
  55. /**************************************************************************************/
  56.  
  57. void USART1_Configuration(void)
  58. {
  59.  
  60.   /* USARTx configuration ------------------------------------------------------*/
  61.   /* USARTx configured as follow:
  62.         - BaudRate = 9600 baud
  63.         - Word Length = 8 Bits
  64.         - One Stop Bit
  65.         - No parity
  66.         - Hardware flow control disabled (RTS and CTS signals)
  67.         - Receive and transmit enabled
  68.   */
  69.   USART_InitStructure.USART_BaudRate = 9600;
  70.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  71.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  72.   USART_InitStructure.USART_Parity = USART_Parity_No;
  73.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  74.  
  75.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  76.  
  77.   USART_Init(USART1, &USART_InitStructure);
  78.  
  79.   USART_Cmd(USART1, ENABLE);
  80. }
  81.  
  82. }
Posted on March 26, 2014 at 21:15

One too many closing braces?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 26, 2014 at 21:49

plz my program dont work i can't send any data to an other xbee can you help me to solve this problem

Posted on March 26, 2014 at 21:54

86. }  <--- WHAT DOES THIS PAIR WITH?

PA9 / USART1 is NOT usable on the STM32F4-DISCO board.

USART2 PA2/PA3 should be usable.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=9]Example Here
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 27, 2014 at 00:19

I tried but it does not work here is my new code

  1. /**************************************************************************************/
  2.  #include ''stm32f4_discovery.h''
  3.  #include <stm32f4xx_usart.h>
  4.   GPIO_InitTypeDef GPIO_InitStructure;
  5.    USART_InitTypeDef USART_InitStructure;
  6. void RCC_Configuration(void);
  7. void GPIO_Configuration(void);
  8. void USART1_Configuration(void);
  9.  void SysTick_Handler(void);
  10. void RCC_Configuration(void)
  11. {
  12.   /* --------------------------- System Clocks Configuration -----------------*/
  13.   /* USART1 clock enable */
  14.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  15.  
  16.   /* GPIOA clock enable */
  17.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOD, ENABLE);
  18. }
  19.  
  20. /**************************************************************************************/
  21. void GPIO_Configuration(void)
  22. {
  23.  
  24.   /*-------------------------- GPIO Configuration ----------------------------*/
  25.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  26.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  27.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  28.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  29.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  30.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  31.  
  32.   /* Connect USART pins to AF */
  33.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);   // USART1_TX
  34.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);  // USART1_RX
  35. }
  36.  
  37. /**************************************************************************************/
  38.  
  39. void USART1_Configuration(void)
  40. {
  41.  
  42.   /* USARTx configuration ------------------------------------------------------*/
  43.   /* USARTx configured as follow:
  44.         - BaudRate = 9600 baud
  45.         - Word Length = 8 Bits
  46.         - One Stop Bit
  47.         - No parity
  48.         - Hardware flow control disabled (RTS and CTS signals)
  49.         - Receive and transmit enabled
  50.   */
  51.   USART_InitStructure.USART_BaudRate = 9600;
  52.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  53.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  54.   USART_InitStructure.USART_Parity = USART_Parity_No;
  55.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  56.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  57.   USART_Init(USART2, &USART_InitStructure);
  58.   USART_Cmd(USART2, ENABLE);
  59. }
  60.  char Uart2_Data_Ready()
  61. {
  62. if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
  63. {
  64. return(1);
  65. }
  66. return(0);
  67. }
  68. void Uart2_write(char ch)
  69. {
  70. while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
  71. USART_SendData(USART2, ch);
  72. }
  73. char Uart2_read()
  74. {
  75.    return(USART_ReceiveData(USART2));
  76. }
  77. /**************************************************************************************/
  78.  char test;
  79. int main(void)
  80. {
  81. char ch;
  82.     RCC_Configuration();
  83.  
  84.     GPIO_Configuration();
  85.  
  86.   USART1_Configuration();
  87.  
  88.    while(1)
  89.   {
  90.        if(Uart2_Data_Ready()==1)
  91.       {
  92.  ch=Uart2_read();
  93.  Uart2_write(ch);
  94.       }
  95.  return 0;
  96.   }
  97.  
  98.   while(1); // Don't want to exit
  99. }
Posted on March 27, 2014 at 00:49

I tried but it does not work here is my new code

Yeah, I'm not sure what ''not working'' means, and how I would debug it.

The ''return 0'' in the while() loop of main() looks out of place.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 27, 2014 at 03:04

i have a same problem