This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/CC2640R2F: Program not executing when sprintf is used.

Part Number: CC2640R2F

Tool/software: Code Composer Studio

Hi Everyone,

I'm facing some problems while executing the below code.

1.Shifting is not working

2.when i use sprintf my program is not running.

Please let  me know the reason why it's not working. snippet of code is written below.

Thanking You,

Vijay Rakesh

Code..

char test[20];

uint32_t bleAddressl = *((uint32_t *)(FCFG1_BASE + FCFG1_O_MAC_BLE_0)) & 0xFFFFFFFF;

uint32_t bleAddressm = *((uint32_t *)(FCFG1_BASE + FCFG1_O_MAC_BLE_1)) & 0x0000FFFF;

uint64_t bleAddress = (bleAddressl<<32)+bleAddressm;      // WARNING : #64-D shift count is too large

Log_info1("MAC ID : %x",bleAddress);

Log_info1(" After Alogorithm MAC ID : %x",bleAddress);

sprintf(test,"%d",bleAddress);                                              // WARNING : #183-D argument is incompatible with corresponding format string conversion

Log_info1("TEST : %s",test);                                                 

  • 1.   left shifting is probably working fine because left-shifting a 32 bit quantity by 32 bits always produces a result of zero.    btw, back in the PDP-8 days we used to rotate the bits instead of shifting, and we had a bonus single-bit "link register", through which we could shift/rotate or for arithmetic overflow.   www.grc.com/.../pdp-8.htm

    2.  possibly try significantly increasing stack size in your linker options so as to use available memory in your target platform?  

    best regards...  

  • Hi Steve Eli,

    Thanks for the reply.

    1.I changed the variable size to 64bit, even though i am missing the msb bytes as shown below.

    2. After increasing the stack size, The program is running but im not getting expected output. Previously my stack is 800 and changed to 1000 and then to 1250 but both doesn't work.

    Output :

    1.#000018 [ 0.015 ] [32;1mINFO: [30;1m(project_zero.c:589) [0m MAC ID0 : ea04950  //LSB

      #000019 [ 0.015 ] [32;1mINFO: [30;1m(project_zero.c:591) [0m MAC ID1 : 546c         //MSB
      #000020 [ 0.015 ] [32;1mINFO: [30;1m(project_zero.c:593) [0m MAC ID : cea04950   // Total Address

    2. #000022 [ 0.015 ] [32;1mINFO: [30;1m(project_zero.c:597) [0mTEST : ⸮⸮

        

  • you are most welcome Vijay Rakesh.   

    An additional issue appears to be that the format strings are both specifying/printing 32 bit quantities while being provided 64 bit parameters.  

  • Vijay Rakesh Munganda said:

    2. After increasing the stack size, The program is running but im not getting expected output. Previously my stack is 800 and changed to 1000 and then to 1250 but both doesn't work.

    Since you are trying to print a unsigned long long (64-bit unsigned integer) the format string should be %llu. Reference: https://stackoverflow.com/questions/5140871/sprintf-for-unsigned-int64

    Also I recommend (if supported by your compiler and C library) to use snprintf() instead of sprintf(). The difference is that with snprintf() you supply the size of the buffer into which you'll receive your string and snprintf() will not overflow that buffer. With the older sprintf() if the resulting string is too long, it may overflow your buffer and possibly clobber other state information of your program, resulting in undefined outcome.

  • Yeah, I agree with you but I used my buffer size only 10 Bytes and tried to execute this "sprintf(variable, "%x",32Bit_hexdata);"  and the output is like " ???? " (Garbage Value). I don't understand why such kind of behaviour. I more thing I would like to know is how can we read secondary Mac address? I changed the secondary Mac I'd using smart RF flash programmer.

  • I apologize for the delay.

    Vijay Rakesh Munganda said:
    Yeah, I agree with you but I used my buffer size only 10 Bytes and tried to execute this "sprintf(variable, "%x",32Bit_hexdata);"  and the output is like " ???? " (Garbage Value). I don't understand why such kind of behaviour.

    Please be sure you #include <stdio.h> .  If that doesn't help, then look through the suggestions in the article Tips on using printf.

    Vijay Rakesh Munganda said:
    I more thing I would like to know is how can we read secondary Mac address? I changed the secondary Mac I'd using smart RF flash programmer.

    That's not a question we can answer in this forum.  I recommend you start a new thread in the Bluetooth Low Energy forum.

    Thanks and regards,

    -George

  • Also make sure you are using the linker option --printf_support=full
  • While your buffer may be only 10 bytes, sprintf might still need a *lot* of stack. Try increasing it.
  • Since it has been a while, I presume one of the suggestions made has resolved the issue.  I'd appreciate hearing how you resolved it.

    Thanks and regards,

    -George