cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L communication between 2 USART

kitjazax
Associate II
Posted on March 25, 2015 at 17:39

Hello all,

I 'm novice and

using STM32L152RE to do a communication between 2 USARTs

I use USART2 for recieving GPS Data and I would like to echo all recievied data without parsing on PC Terminal by using USART3 but I  only got a unreadable character return. Can someone please help me with it?

Thank you
12 REPLIES 12
Posted on March 25, 2015 at 17:58

The STM32 USART's don't output at RS232 compatible voltages.

Why don't you start by describing the board you're using, and the connections/pins being used. The GPS receiver used, and the baud rates at play. At least this way someone could mash up a simple example.

Post the code that doesn't work now.

Review other code

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20Problem%20with%20nucleo%20L152&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=123

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kitjazax
Associate II
Posted on March 25, 2015 at 18:58

thanks for your advise I connect the GPS reciever with 5V power supply and use 2 cables for connecting their tx and rx pins to uC(Nucleo l152RE ) here is my code 

GPS reciever is Maestro A-2200 and working as 4800 Buad

#include ''main.h''

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

void RCC_Configuration(void)

{

  /* USART2 and 3 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE);

  /* GPIOA clock enable */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);

}

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

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  /* GPIO Configuration */

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

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* Connect USART pins to AF */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);  // USART2_TX

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,  GPIO_AF_USART2);  // USART2_RX

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8,  GPIO_AF_USART3);  // USART3_TX

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,  GPIO_AF_USART3);  // USART3_RX

}

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

void USART2_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 = 4800;

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

}

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

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 = 4800;

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

}

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

/************************ Function USART2 send String ******************/

void USART_Puts(char *s)

{

  while(*s)//check end of string

  {

    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait until transmission ready

    USART_SendData(USART2, *s++); // Send Char

  }

}

/*********************Function USART2 send Character *********************/

void USART_Putc(char c)

{

     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait until transmission ready

     USART_SendData(USART2,c); // Send Char

}

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

int main(void)

{

  SystemInit();

  RCC_Configuration();

  GPIO_Configuration();

  USART2_Configuration();

  USART3_Configuration();

  char tmp[50];

  int i=0;

  while(1)

  {

    if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET) // Check for Full

      tmp[i++] = USART_ReceiveData(USART3);

        

    USART_Puts(tmp);

      i++;

      if (i >= 50)

        i = 0;

  }

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

}

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

#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

Posted on March 25, 2015 at 20:28

Got some inconsistencies in the GPIO bank, pins and AF settings there. Stuff needs to be coherent.

Quick blind mod

#include ''main.h''
//****************************************************************************
void RCC_Configuration(void)
{
/* USART2 and 3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE);
/* GPIOA and B clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
}
//****************************************************************************
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO Configuration */
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_40MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // USART2_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // USART2_RX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // USART3_TX PB.10
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // USART3_RX PB.11 (in from GPS TX)
}
//****************************************************************************
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 4800 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 = 4800;
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);
}
//****************************************************************************
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 4800 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 = 4800;
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);
}
//****************************************************************************
/************************ Function USART2 send String ******************/
void USART_Puts(char *s)
{
while(*s)//check end of string
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait until transmission ready
USART_SendData(USART2, *s++); // Send Char
}
}
/*********************Function USART2 send Character *********************/
void USART_Putc(char c)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait until transmission ready
USART_SendData(USART2,c); // Send Char
}
/**************************************************************************************/
int main(void)
{
SystemInit();
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
USART3_Configuration();
while(1) // Don't want to exit
{
uint16_t Data;
// Pipe from USART3 to USART2
while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART3); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, Data); // Echo Char
}
}
//****************************************************************************
#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

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Mikk Leini
Senior
Posted on March 25, 2015 at 21:15

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6gs&d=%2Fa%2F0X0000000btJ%2FH3QedEMV_5bq6wm_ybTos_RW64x4TSMzGfw2QlahfkI&asPdf=false
kitjazax
Associate II
Posted on March 25, 2015 at 22:06

thanks for your kind.It 's really a big help for have your  code as reference

kitjazax
Associate II
Posted on March 25, 2015 at 22:08

thank you very much for taking your time

to help

kitjazax
Associate II
Posted on April 01, 2015 at 19:02

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6gO&d=%2Fa%2F0X0000000bt8%2F44QfoR0Fvo6NFasDKog8eum1_ZzXWtI8v8uUkN3uYTc&asPdf=false
Posted on April 01, 2015 at 20:37

You'll need some looping to wait for many characters, rather than just one if it happens to be in the receive buffer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 01, 2015 at 22:16

#include ''main.h''
//****************************************************************************
void RCC_Configuration(void)
{
/* USART2 and 3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
}
//****************************************************************************
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO Configuration */
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_40MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // USART2_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // USART2_RX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // USART3_TX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // USART3_RX
}
//****************************************************************************
void USART2_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 = 4800;
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);
}
//*************************************************************************************
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 = 4800;
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);
}
//**************************************************************************************
// Console USART Routines
//**************************************************************************************
void USART_Putc(char c)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait until transmission ready
USART_SendData(USART2,c); // Send Char
}
//**************************************************************************************
void USART_Puts(char *s)
{
while(*s)//check end of string
USART_Putc(*s++); // Send Char
}
//**************************************************************************************
char USART_Getc(void)
{
while(USART_GetITStatus(USART2, USART_IT_RXNE) == RESET); // Wait until received character
return((char)(USART_ReceiveData(USART2) & 0xFF));
}
//**************************************************************************************
// GPS Routines
//**************************************************************************************
char GPS_Getc(void)
{
while(USART_GetITStatus(USART3, USART_IT_RXNE) == RESET); // Wait until received character
return((char)(USART_ReceiveData(USART3) & 0xFF));
}
//**************************************************************************************
#define BUFFERS 15
#define BUFFER_LEN 80
char rxbuff[BUFFERS][BUFFER_LEN];
char temp[BUFFER_LEN];
//check for first $ and store more chars into rxbuffer until 

 then start new index and do the same ...
void checkNMEAdata2Buffer(void)
{
char ch;
uint8_t nr=0,pos=0;
while(nr < BUFFERS)
{
ch = GPS_Getc();
if (ch=='$') // Resync line
pos = 0;
rxbuff[nr][pos]=ch;
pos++;
if ((ch == '
') || (ch == '
') || (pos == (BUFFER_LEN - 1))
{
rxbuff[nr][pos]=0; // NUL terminate
pos=0;
nr++;
}
} // while
}
//****************************************************************************
//get NMEA sentece form index
void getNMEAdata(uint8_t nr)
{
uint8_t n;
char ch;
for(n=0;n<BUFFER_LEN;n++)
{
ch=rxbuff[nr][n];
temp[n]=ch;
if(ch==0) break;
}
}
//****************************************************************************
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART3_Configuration();
USART2_Configuration();
checkNMEAdata2Buffer();
getNMEAdata(0);
USART_Puts(temp);
while(1)
{
}
}
//****************************************************************************
#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

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
//****************************************************************************

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