cancel
Showing results for 
Search instead for 
Did you mean: 

What is the correct way to assing value to array in SPC5 Studio

saranyim
Associate II

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.

0693W00000bhRz0QAE.pngHere Is my code capture I do breakpoint at line rowIdx = (uint16_t)strtoul(hex_str, NULL, 16);

0693W00000bhRqYQAU.pngbut 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]

0693W00000bhRz5QAE.png 

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

5 REPLIES 5
ODOUV.1
ST Employee

Hello ,

I executed your code on SPC560D

and put a break point in C code on strtoul:

0693W00000bhTqfQAE.pngthen I reproduce the same issue:

0693W00000bhTvoQAE.pngbut when looking to disassembly:

0693W00000bhTw8QAE.pngI 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

0693W00000bhTwhQAE.pngand then it is OK:

0693W00000bhTwmQAE.png 

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

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?

ODOUV.1
ST Employee

Hello,

 

on my side on SPC560D, I put the breakpoint after the return of the strtoul function:


_legacyfs_online_stmicro_images_0693W00000bheYRQAY.pngthen I obtain the good return value (0x1C):

0693W00000bheZPQAY_image.png

Would you please try to do the same ?

saranyim
Associate II

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


_legacyfs_online_stmicro_images_0693W00000bhesCQAQ.pngAnyhow 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


_legacyfs_online_stmicro_images_0693W00000bhesDQAQ.pngdataStr from sprintf -- Correct


_legacyfs_online_stmicro_images_0693W00000bhevGQAQ.png 

Concat to uTxStr -- Junk


_legacyfs_online_stmicro_images_0693W00000bhevVQAQ.png 

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

saranyim
Associate II

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