Hi,
I have an STTM32F103C8 MCU ("Blue pill") board. What I write to UART1 goes well to he PC. But if I try send data over UART2 only some of the sent characers appear on the PC.
You can see he code below. Is there anyone who knows, what is wrong?
Thanks!
#include "stm32f10x.h" /* function prototypes */ void RCC_Configuration(void); void GPIO_Configuration(void); void USART_Configuration(USART_TypeDef* USARTx); void SysTick_Handler(void); void USART_Send_Char(USART_TypeDef* USARTx, char cData); void USART_Send_CharArray(USART_TypeDef* USARTx, char* Buffer); void USART_Send_Number(USART_TypeDef* USARTx, uint32_t x); int iTick = 0; void delay_us(long usecs); int main(void) { char data; int j=0; char* RXBUFF="testmessage\0"; char* TXBUFF; SystemInit(); RCC_Configuration(); //NVIC_Configuration(); GPIO_Configuration(); USART_Configuration(USART2); USART_Configuration(USART1); //TIM2_Configuration(); // Init SysTick Timer SysTick_Config(1000000); while(1) { delay_us(5000); USART_Send_CharArray(USART1,RXBUFF); USART_Send_Number(USART1,123); delay_us(5000); USART_Send_CharArray(USART2,RXBUFF); USART_Send_Number(USART2,123456789); } } void RCC_Configuration(void) { // Enable GPIOA, GPIOB clock RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE); // Enable USARTx clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } void SysTick_Handler(void) { iTick = 1; } void GPIO_Configuration(void) { // Initialize Leds mounted on STM32 board GPIO_InitTypeDef GPIO_InitStructure; // Configure the GPIO_LED pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); // Configure USART1 Rx (PA10) as input GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART1 Tx (PA9) as alternate function GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); } void USART_Configuration(USART_TypeDef* USARTx) { USART_InitTypeDef USART_InitStructure; /* USART1 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(USARTx, &USART_InitStructure); USART_Cmd(USARTx, ENABLE); } void USART_Send_Char(USART_TypeDef* USARTx, char cData) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USARTx, cData); } void USART_Send_CharArray(USART_TypeDef* USARTx, char Buffer[]) { int i=0; while (Buffer[i]) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USARTx,Buffer[i++]); } } void USART_Send_String(USART_TypeDef* USARTx, const char *str) { while (*str) { while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); USART_SendData(USARTx, *str++); } } void USART_Send_Number(USART_TypeDef* USARTx, uint32_t x) { char value[10]; //a temp array to hold results of conversion int i = 0; //loop index do { value[i++] = (char)(x % 10) + '0'; //convert integer to character x /= 10; } while(x); while(i) //send data { USART_Send_Char(USARTx, value[--i]); } } void delay_us(long usecs) { long i,j; for (i=0;i++ < usecs;) { for (j=0;j++ < 0x12;) { ;} } }
Your title says, "Wrong data on UART2";
But your description says, "only some of the sent characters appear on the PC".
So please clarify: is the problem that characters are corrupted ("wrong") or that they are missing? Or what?
Wait on USARTx not USART1 then... while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
You are right. So if I try to send these characters:
abcdefghijkl
only some of the characters get to the PC like abdegi or acdfgi
So the exact phrase to this phenomena is missing data, I think.
I inserted the lines you suggested like this:
USART_Send_CharArray(USART1,RXBUFF); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_Send_CharArray(USART2,RXBUFF); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
but there was no change
Your code is broken. You send to two uart but only test the flags for one uart.
I can't really understand: here I send data and check the port, don't I?
//send data to USART1 USART_Send_CharArray(USART1,RXBUFF); //test for USART1 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //send to USART2 USART_Send_CharArray(USART2,RXBUFF); //test for USART2 while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
Or which code is broken? Can you explain a bit?
If I insert a 50 usec delay after USART_SendData then each character get to the PC. Is there anyone who knows why works the USART1 without delay?
Repeat very slowly to yourself USARTx not USART1 while looking at this code, keep doing so until you see the issue.
void USART_Send_Char(USART_TypeDef* USARTx, char cData) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USARTx, cData); } void USART_Send_CharArray(USART_TypeDef* USARTx, char Buffer[]) { int i=0; while (Buffer[i]) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USARTx,Buffer[i++]); } }
"Is there anyone who knows why works the USART1 without delay?"
Most definitely.
And you have been told in this thread exactly why it works for USART1 and not for USART2.
OK, now I see... Excuse me for my recklessness, and thank you very much for your paient and help!