2023-04-03 07:12 PM
Hi I've declare a 3 byte array uint8_t hex_str[3] and assign a value to convert Hex string to int using strtoul function the point is I set hex_str[2] = 0 but cannot.
Here Is my code capture I do breakpoint at line rowIdx = (uint16_t)strtoul(hex_str, NULL, 16);
but hex_str[2] is not 0 as I just assigned
hex_str[0] and hex_str[1] is correct assigned by input array uRxBuf [12] and uRxBuf[13]
That cause strtoul give out a wrong output
please help correct me what I did wrong
Edit 1:
I try change type of hex_str to uint8_t, int8_t, char, unsigned char it give the same output
2023-04-04 02:27 AM
Hello ,
I executed your code on SPC560D
and put a break point in C code on strtoul:
then I reproduce the same issue:
but when looking to disassembly:
I saw the break point is at the beginning of preparation to call strtoul, but before to set the hex_str array
so i moved the breakpoint to the branch to strtoul
and then it is OK:
It is a false issue, due to assembly optimization.
setting breakpoint in C code is not optimal
I recommend you to set breakpoint in assembly code, it is safer
2023-04-05 08:31 PM
Hello, I try putting break in Assembly. And yes, you are correct with array value, but it does not make the strtoul return the correct calculation. It still returns junk that I still cannot figure it out.
I'm not sure that SPC5 can work with the standard C library, As the problem with sbrk function that it should be in standard library but we need to implement in main.c that cause me getting doubt with the standard C compatabilty. Any suggestion on this?
2023-04-06
02:19 AM
- last edited on
2023-12-06
04:41 AM
by
Laurids_PETERSE
Hello,
on my side on SPC560D, I put the breakpoint after the return of the strtoul function:
then I obtain the good return value (0x1C):
Would you please try to do the same ?
2023-04-06 03:21 AM - edited 2023-11-20 09:00 AM
I think issue is come from optimizer it happen very random with same code no change just upload and debug it happen at random place in my code
Anyhow I manage to fix the issue for strtoul in the top part of function, Don't ask me for a reason why. I'm just play around to try to make it work
now problem is move to lower line with strcat function so the history is I create a cuntion hex_array_to_string by passing pointer of source and destination array. and it not work it return junk to destination array
so I create a loop with sprintf direct to uTxBuf. still got junk
Next I declare another global array dataStr[128] for output of sprintf -- OK I got the correct data
Now I have to concat to uTxBuf using strcat -- Junk
Change to use sprintf with %S -- Junk
and it show junk on debugger and the real data from serial port
this is Hex data
dataStr from sprintf -- Correct
Concat to uTxStr -- Junk
So I was working with ST MCU over 10 years start with STM8 then STM32 and now I'm move to SPC56, I've finish some project with SPC560 using CAN bus has no issue. This is the 1st time with SPC560 using ascii to Serial port and I have a lot of issue on this most of the issue is converting between Hex array and Char (ASCII) array with function strcat, memcpy, strcpy this 3 function is the most frequence problem other string related function happen sometime
The most worry point for me is the same code different upload for debug, problem is move to another line of code. I'm spend almost a weeks with this issue
Any one got the same issue and any suggestion are welcome
Regards,
Saran
2023-04-06 08:56 PM
Now I found the solution. it start with sbrk problem, in SPC5 V6.0.0 just add SPC5 Runtime-IO Component RLA to project and all problem solve
Some one has to edit the answer in these 2 posts
https://community.st.com/s/question/0D50X00009XkYK5SAN/undefined-reference-to-sbrk
https://community.st.com/s/question/0D50X00009XkgnUSAR/problem-using-sprintf
the old without update post take me a week to figure it out