Author Topic: STM32 HAL UART data doesn't change  (Read 1413 times)

0 Members and 1 Guest are viewing this topic.

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
STM32 HAL UART data doesn't change
« on: March 31, 2021, 08:38:07 pm »
I'm working with a STM32F429zi board. I'm trying to communicate over UART with an other device.
My I/O is initialized via CubeMX, baud rate is set at 115200. Because i didn't acheived the correct result, i've hooked up a logic analyzer to see whats happening.
No matter what i do, the data sent from the microcontroller over uart is always 0x30. It doesn't matter what data i put into the send buffer, still the same result.
I have no clue what's happening here. Does anybody encountered a similar issue? Baud rate on logic analyzer is correct
This is the code i'm using:

Code: [Select]
  char resetBuffer[100] = "0xAA300026";
  HAL_UART_Transmit(&huart4, resetBuffer ,sizeof(resetBuffer), 1000);

Could anybody help me solve this issue?
 

Offline Stuart Coyle

  • Contributor
  • Posts: 40
  • Country: au
Re: STM32 HAL UART data doesn't change
« Reply #1 on: March 31, 2021, 09:19:57 pm »
I take it that you want to send the Hex value 0xAA300026. You are sending the string "0xAA300026". Which starts with the character "0" which has an ASCII hex value of 0x30. So I guess that you are seeing just the first byte of the data.
To send the hex value you want to do something like this (but beware of endianness issues):
Code: [Select]
int resetBuffer = 0xAA300026;
HAL_UART_Transmit(&huart4, &resetBuffer, sizeof(resetBuffer) , 1000);         
 

You will have issues trying to convert this hex value into characters since ASCII is 7bit and 0xAA uses the 8th bit.
I hope this helps.
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #2 on: March 31, 2021, 09:29:59 pm »
Oh damn, stupid. I didn't realized 0 was equal to 0x30.
I don't need to send ASCII chars. The device i'm trying to communicate with needs hex values to access registers.
No matter what i do i still receive a compilation warning:
passing argument 2 of 'HAL_UART_Transmit' from incompatible pointer type [-Wincompatible-pointer-types]

 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 7318
  • Country: fr
Re: STM32 HAL UART data doesn't change
« Reply #3 on: March 31, 2021, 09:38:55 pm »
Oh damn, stupid. I didn't realized 0 was equal to 0x30.
I don't need to send ASCII chars. The device i'm trying to communicate with needs hex values to access registers.
No matter what i do i still receive a compilation warning:
passing argument 2 of 'HAL_UART_Transmit' from incompatible pointer type [-Wincompatible-pointer-types]

Post your code, but the warning is self-explanatory really. It's basic C stuff. Take a look at the prototype of the HAL_UART_Transmit() function.

If you want to send a 32-bit integer, for instance, you can do this:
Code: [Select]
uint32_t n = 0xAA300026;
HAL_UART_Transmit(&huart4, (uint8_t *) &n, sizeof(n), 1000);

Now you need to consider endianness when doing this, of course. On a typical little-endian CPU (STM32 MCUs are), the least significant byte will be sent first. (So here, 0x26.)



« Last Edit: March 31, 2021, 09:40:37 pm by SiliconWizard »
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #4 on: March 31, 2021, 09:44:47 pm »
Thank you for your feedback.
I've found a thread online about sending hex over UART.
Now i've changed my code to:
Code: [Select]
  uint8_t  resetBuffer[100] = "0xAA300026";
  HAL_UART_Transmit(&huart4, resetBuffer ,sizeof(resetBuffer), 1000);

Attached you can find a print screen from the datasheet of the device i'm trying to communicate with.
So with STM32 i would always have to send the least significant bit first?

 

Offline Stuart Coyle

  • Contributor
  • Posts: 40
  • Country: au
Re: STM32 HAL UART data doesn't change
« Reply #5 on: March 31, 2021, 10:06:24 pm »
I think that you need this:
Code: [Select]
uint8_t resetBuffer[4] = {0xAA, 0x30, 0x00, 0x26};
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 7318
  • Country: fr
Re: STM32 HAL UART data doesn't change
« Reply #6 on: March 31, 2021, 10:38:01 pm »
Thank you for your feedback.
I've found a thread online about sending hex over UART.
Now i've changed my code to:
Code: [Select]
  uint8_t  resetBuffer[100] = "0xAA300026";
  HAL_UART_Transmit(&huart4, resetBuffer ,sizeof(resetBuffer), 1000);

And... you're back to square one. You're trying to send a sequence of bytes, but using a string. This isn't gonna work - it's going to send the ASCII-encoded characters of the string, not the values in hex as you would need.

You just need to do as Stuart Coyle showed. I think you're confused with how C deals with arrays, and you're mixing strings with integer values.
Another thing wrong with your code is that you're declaring an array of 100 bytes and are passing this size to the HAL_UART_Transmit() function; thus it will not only send the string as ASCII characters (there are ten here), but it's going to send 90 zeroes after that. Definitely all wrong.

The correct and more maintainable way of doing it is like so:
Code: [Select]
uint8_t resetBuffer[] = {0xAA, 0x30, 0x00, 0x26};
HAL_UART_Transmit(&huart4, resetBuffer, sizeof(resetBuffer), 1000);

In C, when you declare an array without a specified size and use an intializer, the resulting size will be the size of the initializer. So that makes it convenient to write code like this without having to actually bother with the number of items, the compiler will do this for you.

 

Offline Stuart Coyle

  • Contributor
  • Posts: 40
  • Country: au
Re: STM32 HAL UART data doesn't change
« Reply #7 on: March 31, 2021, 10:44:08 pm »
+1 to SiliconWizard's suggestion of not specifying the array size in the declaration.
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #8 on: April 01, 2021, 09:08:50 pm »
Thank you for your feedback.
I've found a thread online about sending hex over UART.
Now i've changed my code to:
Code: [Select]
  uint8_t  resetBuffer[100] = "0xAA300026";
  HAL_UART_Transmit(&huart4, resetBuffer ,sizeof(resetBuffer), 1000);

And... you're back to square one. You're trying to send a sequence of bytes, but using a string. This isn't gonna work - it's going to send the ASCII-encoded characters of the string, not the values in hex as you would need.

You just need to do as Stuart Coyle showed. I think you're confused with how C deals with arrays, and you're mixing strings with integer values.
Another thing wrong with your code is that you're declaring an array of 100 bytes and are passing this size to the HAL_UART_Transmit() function; thus it will not only send the string as ASCII characters (there are ten here), but it's going to send 90 zeroes after that. Definitely all wrong.

The correct and more maintainable way of doing it is like so:
Code: [Select]
uint8_t resetBuffer[] = {0xAA, 0x30, 0x00, 0x26};
HAL_UART_Transmit(&huart4, resetBuffer, sizeof(resetBuffer), 1000);

In C, when you declare an array without a specified size and use an intializer, the resulting size will be the size of the initializer. So that makes it convenient to write code like this without having to actually bother with the number of items, the compiler will do this for you.

I've tried the suggested code and it works like a charm. Thank you for this!
Now i'm struggling to receive some data and print the hex code to the terminal using a printf function.
The printf function is working and i'm receiving the data with the UART_HAL_receive function. But it's not printed as hex code to the terminal but as ASCII code.
Any suggestions on this point? 
 

Offline Stuart Coyle

  • Contributor
  • Posts: 40
  • Country: au
Re: STM32 HAL UART data doesn't change
« Reply #9 on: April 02, 2021, 03:30:47 am »
You want the %X printf format specifier. Or %x for lower case.
It can take an optional number of bytes.
For example:
Code: [Select]
printf("%04X", data)will print a 32bit (4 byte) integer in hex.

Take some time to read the printf documentation, there are lots of
format specifiers with lots of options. On Unixy systems:
Code: [Select]
man 3 printf is your best friend.
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1675
  • Country: es
Re: STM32 HAL UART data doesn't change
« Reply #10 on: April 02, 2021, 09:30:10 am »
Get a proper C book... It's the best. So you can quickly go to the printf chapter and hopefully find what you were searching.
Anyways, you never have all learned.
Recently I run through a similar problem, I wanted to have a variable padding size, but if you don't know how it's called, you are going to have a hard time trying to find anything.
Until I found the "*" option. But it took me a while!
« Last Edit: April 02, 2021, 09:33:24 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #11 on: April 02, 2021, 05:26:15 pm »
You want the %X printf format specifier. Or %x for lower case.
It can take an optional number of bytes.
For example:
Code: [Select]
printf("%04X", data)will print a 32bit (4 byte) integer in hex.

Take some time to read the printf documentation, there are lots of
format specifiers with lots of options. On Unixy systems:
Code: [Select]
man 3 printf is your best friend.

I'm aware of the X format specifier.
When i print a buffer with hex character i've made, this works as it should.
Code: [Select]
uint8_t resetBuffer[] = {0xAA, 0x30, 0x00, 0x26};
  printf("%02X %02X %02X %02X\n\r",resetBuffer[0],resetBuffer[1],resetBuffer[2],resetBuffer[3] );

But when i try to print the buffer with the data received over UART, i get some strange values.
I've checked this with a logic analyzer and it should receive a different value than the value that gets printed on the terminal
Code: [Select]
uint8_t receiveBuffer [4];
  HAL_UART_Receive(&huart4, receiveBuffer, 4, 1);
  printf("%02X %02X %02X %02X\n\r",receiveBuffer[0],receiveBuffer[1],receiveBuffer[2],receiveBuffer[3] );

This results in D5 01 00 08 while it should be AA B0 00 A6.
Any ideas?
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1675
  • Country: es
Re: STM32 HAL UART data doesn't change
« Reply #12 on: April 02, 2021, 07:38:52 pm »
Did you check that the received data was correct by debugging it?
First problem, you set only 1mS timeout.
Try a higher value, like 3000.
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #13 on: April 03, 2021, 02:48:51 pm »
Did you check that the received data was correct by debugging it?
First problem, you set only 1mS timeout.
Try a higher value, like 3000.

I have checked the data with a logic analyzer and it should be AA B0 00 A6(logic analyzer agrees with this).
For some reason when i try to print this received value to the terminal it's always D5 01 00 08.
Changing the timeout didn't solved the issue...
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1675
  • Country: es
Re: STM32 HAL UART data doesn't change
« Reply #14 on: April 03, 2021, 09:24:13 pm »
The logic analyzer won't tell you what the stm32 understood.

You're poking around in fog, you don't know what the problem comes from as you're sending what you received.
But was the received data wrong? Or the sending?

Set a breakpoint once you received the 4 bytes, and check the array contents.
If they are wrong, then find the issue with the receiving part
If they're correct, then fix the transmit function.
You will lose a lot of time otherwise.
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #15 on: April 19, 2021, 03:01:50 pm »
So i found out i had a damaged UART pin.
I'm now able to receive the UART message but only the first byte.
The next 3 bytes don't match with the data i see on my logic analyzer.
When i remove the data lines the code gives the same output. So it shows a random value.
What could be the reason for only receiving the first byte in the micro controller?
I have already tried to change the timeout value but this doesn't make any difference.

Code: [Select]
uint8_t receiveBuffer [4];
  HAL_UART_Receive(&huart5, receiveBuffer, 4, 10);
  printf("%02X %02X %02X %02X\n\r",receiveBuffer[0],receiveBuffer[1],receiveBuffer[2],receiveBuffer[3] );
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1675
  • Country: es
Re: STM32 HAL UART data doesn't change
« Reply #16 on: April 19, 2021, 03:20:50 pm »
Only 10mS timeout? Are you sure you have enough time before you init HAL_receive and finish sending the data?
Give It more time! 60000! You'll adjust the timeouts later.
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate
 

Offline reyntjensm

  • Regular Contributor
  • *
  • Posts: 97
  • Country: be
Re: STM32 HAL UART data doesn't change
« Reply #17 on: April 19, 2021, 04:14:27 pm »
Changing the timeout doesn't make any difference...

 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1675
  • Country: es
Re: STM32 HAL UART data doesn't change
« Reply #18 on: April 19, 2021, 05:04:09 pm »
Code: [Select]
uint8_t receiveBuffer [4];

That is not initialized. It will contain random data.
My guess is that your uart is only receiving 1 byte, clean the array to ensure.
Also, check the return value. Should be HAL_OK.

Code: [Select]
uint8_t receiveBuffer [4]  = {0,0,0,0};
uint8_t res = HAL_OK;

res = HAL_UART_Receive(...)
« Last Edit: April 19, 2021, 05:06:52 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf