2014-11-07 03:41 AM
Hello!
I'vegot an issue with using USART on Nucleo L152LE and need some help. Symptoms are: RX and TX pins are hardware connected, but reading from input register with USART_ReceiveData(USART1) returns zero instead of transmitted value. IDE is Atolic Lite with project settings coresponding to used board. I've tried both USART1 (PIO A, pins 9 and 10) and USART2 (PIO A, PINS 2 and 3) with the same result.Here is initialization code for USART1: /* USART configuration structure for USART1 */ USART_InitTypeDef USART1_init_struct; /* Bit configuration structure for GPIOA PIN9 and PIN10 */ GPIO_InitTypeDef gpioa_init_struct; USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART1, &USART_ClockInitStructure); /* Enable clock for USART1, AFIO and GPIOA */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* GPIOA PIN9 alternative function Tx */ gpioa_init_struct.GPIO_Pin = GPIO_Pin_9; gpioa_init_struct.GPIO_Speed = GPIO_Speed_40MHz; gpioa_init_struct.GPIO_Mode = GPIO_Mode_AF; gpioa_init_struct.GPIO_OType = GPIO_OType_PP; gpioa_init_struct.GPIO_PuPd=GPIO_PuPd_DOWN; GPIO_Init(GPIOA, &gpioa_init_struct); /* GPIOA PIN10 alternative function Rx */ gpioa_init_struct.GPIO_Pin = GPIO_Pin_10; gpioa_init_struct.GPIO_Speed = GPIO_Speed_40MHz; gpioa_init_struct.GPIO_Mode = GPIO_Mode_IN; gpioa_init_struct.GPIO_PuPd=GPIO_PuPd_DOWN; gpioa_init_struct.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOA, &gpioa_init_struct); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9|GPIO_PinSource10, GPIO_AF_USART1); /* Baud rate 9600, 8-bit data, One stop bit * No parity, Do both Rx and Tx, No HW flow control */ USART1_init_struct.USART_BaudRate = 9600; USART1_init_struct.USART_WordLength = USART_WordLength_8b; USART1_init_struct.USART_StopBits = USART_StopBits_1; USART1_init_struct.USART_Parity = USART_Parity_No ; USART1_init_struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART1_init_struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* Configure USART1 */ USART_Init(USART1, &USART1_init_struct); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); /* Enable RXNE interrupt */// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);/* NVIC_EnableIRQ(USART1_IRQn);*/ }And transmittion code:int UartWriteWithEcho(unsigned char *pBuf,unsigned DataLen){ unsigned cnt; uint16_t RetVal; for(cnt=0; cnt<DataLen; cnt++) { USART_SendData(USART1, pBuf[cnt]); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) { //Do nothing. Just wait } RetVal=USART_ReceiveData(USART1); if( (unsigned char)RetVal!= pBuf[cnt]) //Checking echo is correct return 0; } return 1;}Would anyone be so kind to point me what is wrong? #nucleo-stm32l152-uart-txe #nucleo-stm32le-usart-problem2014-11-07 06:50 AM
I've posted SPL examples for the L1 Nucleo
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9|GPIO_PinSource10, GPIO_AF_USART1); // Doesn't work like this, won't OR together
Use TXE to see if the register is empty, NOT TC2014-11-07 07:52 AM
Thank you for response.
Unfortunately these changes didn't help2014-11-07 08:18 AM
// STM32 USART2 (Tx PA.2, Rx PA.3) STM32L152RE NUCLEO - sourcer32@gmail.com
#include ''stm32l1xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // PA.2 USART2_TX, PA.3 USART2_RX
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_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
}
/**************************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 115200 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 = 115200;
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 OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
OutString(''Welcome to Nucleo L152RE
'');
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART2); // 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) */
while (1)
{}
}
#endif
/**************************************************************************************/
2014-11-07 08:23 AM
Quick blind mod, presupposes PA9/PA10 are conflict free, HSE_VALUE is correct, and being connected to a CMOS Serial adapter, the voltages here are not RS232 compatible levels.
// STM32 USART1 (Tx PA.9, Rx PA.10) STM32L152RE NUCLEO - sourcer32@gmail.com
#include ''stm32l1xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; // PA.9 USART1_TX, PA.10 USART1_RX
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_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
}
/**************************************************************************************/
void USART1_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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
/**************************************************************************************/
void OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, *s++); // Send Char
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
OutString(''Welcome to Nucleo L152RE
'');
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART1); // Collect Char
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, 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) */
while (1)
{}
}
#endif
/**************************************************************************************/
2014-11-17 11:58 PM
Thank you a lot! It works, although with one notice - RX pin also need to be set in AF mode. Without this where are no incoming data at all.
Next problem is - if I use TXE flag in transmittion remote end gets wrong data... Looks like a mix of previous and next symbol. I think where are a kind of problem with UART clock settings, but I'm not sure. Can you point me to possible reasons?2014-11-18 05:25 AM
If the receiver has problems with inter-symbol separation you could try 2 stop bits.
TXE flags that the holding register is empty, TC is more useful to know when the last bit of the last byte has left the USART