3
\$\begingroup\$

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

}

the received data is:

  {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:

Arduino Code (Receive):

#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()) {
    Serial.write(mySerial.read());
  }
}

Data transmission works good between two Arduino boards.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ What baudrate are you using ? \$\endgroup\$ Commented Dec 7, 2016 at 12:34
  • \$\begingroup\$ What does debugger say? Are you sure it is STM32 side problem, what about the Arduino? What about the UART initialization? \$\endgroup\$ Commented Dec 8, 2016 at 8:44
  • \$\begingroup\$ @BenceKaulics Debugger doesn't work (Kiel version 5)!! My code works good between two Arduino boards. UART initialization added. \$\endgroup\$
    – Mehran
    Commented Dec 8, 2016 at 19:44
  • \$\begingroup\$ Could you share the code for the pin configuration/initialization? I can't seem to figure it out \$\endgroup\$
    – VanGo
    Commented Dec 5, 2017 at 15:04
  • \$\begingroup\$ @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 \$\endgroup\$
    – Mehran
    Commented Dec 6, 2017 at 16:05

3 Answers 3

1
\$\begingroup\$

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.

\$\endgroup\$
0
6
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$
    – jaskij
    Commented Dec 7, 2016 at 14:42
  • \$\begingroup\$ The CPU frequency is defined as @JanDorniak said. I changed the 4th parameter and still the same problem. Baud rate is 115200. \$\endgroup\$
    – Mehran
    Commented Dec 8, 2016 at 7:47
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Dec 8, 2016 at 8:40
  • \$\begingroup\$ @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. \$\endgroup\$
    – jaskij
    Commented Dec 8, 2016 at 9:58
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – neonzeon
    Commented Dec 8, 2016 at 20:42
4
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ I wish I could mark this as the best answer. I missed the SOFTWAREserial clue. Thanks @Almaz. \$\endgroup\$
    – neonzeon
    Commented Mar 20, 2019 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.