STM32L communication between 2 USART
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 9:39 AM
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 9:58 AM
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 codeUp vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 10:58 AM
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 12:28 PM
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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 1:15 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 2:06 PM
thanks for your kind.It 's really a big help for have your code as reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-25 2:08 PM
thank you very much for taking your time
to help- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-04-01 10:02 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-04-01 11:37 AM
You'll need some looping to wait for many characters, rather than just one if it happens to be in the receive buffer.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-04-01 1:16 PM
#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
//****************************************************************************
Up vote any posts that you find helpful, it shows what's working..
