STM32 HAL UART data doesn't change - Page 1
Forum section: electronics / general

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 31, 2021, 08:38:07 pm
content: 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 correctThis is the code i'm using:Code:char resetBuffer = "0xAA300026";HAL_UART_Transmit(&huart4, resetBuffer ,sizeof(resetBuffer), 1000);Could anybody help me solve this issue?

author: Stuart Coyle
author_rank: Contributor
timestamp: March 31, 2021, 09:19:57 pm
content: 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: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.

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 31, 2021, 09:29:59 pm
content: 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]

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 31, 2021, 09:38:55 pm
content: Quote from: reyntjensm on March 31, 2021, 09:29:59 pmPost 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: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.)

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 31, 2021, 09:44:47 pm
content: Thank you for your feedback.I've found a thread online about sending hex over UART.Now i've changed my code to:Code:uint8_t resetBuffer = "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?" alt="" class="bbc_img" />

author: Stuart Coyle
author_rank: Contributor
timestamp: March 31, 2021, 10:06:24 pm
content: I think that you need this:Code:uint8_t resetBuffer = {0xAA, 0x30, 0x00, 0x26};

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 31, 2021, 10:38:01 pm
content: Quote from: reyntjensm on March 31, 2021, 09:44:47 pmAnd... 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: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.

author: Stuart Coyle
author_rank: Contributor
timestamp: March 31, 2021, 10:44:08 pm
content: +1 to SiliconWizard's suggestion of not specifying the array size in the declaration.

author: reyntjensm
author_rank: Regular Contributor
timestamp: April 01, 2021, 09:08:50 pm
content: Quote from: SiliconWizard on March 31, 2021, 10:38:01 pmI'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?

author: Stuart Coyle
author_rank: Contributor
timestamp: April 02, 2021, 03:30:47 am
content: You want the%Xprintf format specifier. Or%xfor lower case.It can take an optional number of bytes.For example:Code:printf("%04X", data)will print a 32bit (4 byte) integer in hex.Take some time to read the printf documentation, there are lots offormat specifiers with lots of options. On Unixy systems:Code:man 3 printfis your best friend.

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 02, 2021, 09:30:10 am
content: 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!

author: reyntjensm
author_rank: Regular Contributor
timestamp: April 02, 2021, 05:26:15 pm
content: Quote from: Stuart Coyle on April 02, 2021, 03:30:47 amI'm aware of the X format specifier.When i print a buffer with hex character i've made, this works as it should.Code:uint8_t resetBuffer[] = {0xAA, 0x30, 0x00, 0x26};printf("%02X %02X %02X %02X\n\r",resetBuffer,resetBuffer,resetBuffer,resetBuffer );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 terminalCode:uint8_t receiveBuffer ;HAL_UART_Receive(&huart4, receiveBuffer, 4, 1);printf("%02X %02X %02X %02X\n\r",receiveBuffer,receiveBuffer,receiveBuffer,receiveBuffer );This results in D5 01 00 08 while it should be AA B0 00 A6.Any ideas?

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 02, 2021, 07:38:52 pm
content: 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.

author: reyntjensm
author_rank: Regular Contributor
timestamp: April 03, 2021, 02:48:51 pm
content: Quote from: DavidAlfa on April 02, 2021, 07:38:52 pmI 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...

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 03, 2021, 09:24:13 pm
content: 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 partIf they're correct, then fix the transmit function.You will lose a lot of time otherwise.

author: reyntjensm
author_rank: Regular Contributor
timestamp: April 19, 2021, 03:01:50 pm
content: 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:uint8_t receiveBuffer ;HAL_UART_Receive(&huart5, receiveBuffer, 4, 10);printf("%02X %02X %02X %02X\n\r",receiveBuffer,receiveBuffer,receiveBuffer,receiveBuffer );

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 19, 2021, 03:20:50 pm
content: 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.

author: reyntjensm
author_rank: Regular Contributor
timestamp: April 19, 2021, 04:14:27 pm
content: Changing the timeout doesn't make any difference...

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 19, 2021, 05:04:09 pm
content: Code:uint8_t receiveBuffer ;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:uint8_t receiveBuffer  = {0,0,0,0};uint8_t res = HAL_OK;res = HAL_UART_Receive(...)