# STM32 HAL UART transmission

I'm trying to send data by STM32f103 to an Arduino board using UART. Data isn't received properly. The code is generated using STM32CUBEMX and here is the part I added:

STM32 code (Transmit):

uint8_t Test[] = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 \n"; //Data to send

HAL_UART_Transmit(&huart1,Test,sizeof(Test),10);// Sending in normal mode
HAL_Delay(1000);
HAL_UART_Transmit_DMA(&huart1,Test,sizeof(Test));// Sending in DMA mode
HAL_Delay(1000);

/* USART1 init function */
static void MX_USART1_UART_Init(void)
{

huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}

}


  {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1}
enter code here


in both DMA and normal modes the received data is pretty similar. The UART baud rate is 115200.

Why is my data being truncated? Is it an array bounds problem? Or am I hitting the limit of my buffer?

Edit:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 9); // RX, TX ports

void setup() {
// set the data baud rate
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
mySerial.begin(115200);
}

void loop() {
if (mySerial.available()) {
}
}


Data transmission works good between two Arduino boards.

• What baudrate are you using ? – Stefan Merfu Dec 7 '16 at 12:34
• What does debugger say? Are you sure it is STM32 side problem, what about the Arduino? What about the UART initialization? – Bence Kaulics Dec 8 '16 at 8:44
• @BenceKaulics Debugger doesn't work (Kiel version 5)!! My code works good between two Arduino boards. UART initialization added. – Mehran Dec 8 '16 at 19:44
• Could you share the code for the pin configuration/initialization? I can't seem to figure it out – VanGo Dec 5 '17 at 15:04
• @VanGo Unfortunately I don't have the code. I just selected the board and enabled the USART1 in STM32CubeMX and the code was generated by STM32CubeMX. I started with this video on youtube, youtube.com/watch?v=ZYxNote8JQ0&t=9s – Mehran Dec 6 '17 at 16:05

The higher a baud rate goes, the faster data is sent/received, but there are limits to how fast data can be transferred. You usually won’t see speeds exceeding 115200 - that’s fast for most microcontrollers. Get too high, and you’ll begin to see errors on the receiving end, as clocks and sampling periods just can’t keep up.

Test lower baud rate.

You receive ~67 characters, which at 10 bits/character, is 670 bits. Given that your timeout (parameter 4) is set to 10ms or 0.01 s, the average bit rate seems to be around 67000 bit/s. My guess is that you are transmitting at 115200 baud with some inter-character delay giving an effective bit rate of 67000 bit/s.

Therefore, after 67 characters, 10ms expires and the transmit routine returns.

To fix, increase the value of parameter 4 - the timeout value, to at least 20.

Also check that your CPU clock frequency is correctly defined in your project setup code and measure it to ensure it's within 2.5% of the intended value.

• I'm pretty sure the timeout is good if the code is generated by CubeMX - it generates a line like this configuring SysTick to 1ms: HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); and SysTick is what's used for those delays in ST's HAL. – Jan Dorniak Dec 7 '16 at 14:42
• The CPU frequency is defined as @JanDorniak said. I changed the 4th parameter and still the same problem. Baud rate is 115200. – Mehran Dec 8 '16 at 7:47
• @JanDorniak First what STM32CubeMX generates is not always good. Also Cube does not generate calls like this HAL_UART_Transmit(&huart1,Test,sizeof(Test),10); so the last param was user definied. Slightly bigger timeout never bad. – Bence Kaulics Dec 8 '16 at 8:40
• @BenceKaulics I had problems with other parts of code generated by CubeMX or with HAL itself but never with clock configuration. As for the timeout - yes it is too short. – Jan Dorniak Dec 8 '16 at 9:58
• @Mehran can you confirm on an oscilloscope that only 67 characters are being sent? It might help to send something like uint8_t Test[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF...". That way every 10th character is easily visible. – neonzeon Dec 8 '16 at 20:42

Arduino's SoftwareSerial library cannot operate with high UART rates. Never use it with speeds higher than 38400 on Uno/Nano boards for example. If you still want 115200 try HardwareSerial instead.

• I wish I could mark this as the best answer. I missed the SOFTWAREserial clue. Thanks @Almaz. – neonzeon Mar 20 at 16:19