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.

MSP430F2274: Issues with unsigned long values not being correct using Code Composer Studio

Part Number: MSP430F2274


Tool/software:

I have been chasing a calculation error and have narrowed it down to an issue with Unsigned Long (int32_t).

Here is the simple code and an image from the debugger watch window.

long temp32s;

temp32s = (long)1;
temp32s = (long)temp32s * (long)256;

I'm expecting the result to be 256 and not 4194304

  • I don't see this behavior on a G2553 (F2 series) with CCS 12.6.0. I tried both -O0 and -O4.

    I suspect an optimizer/debugger illusion. What does the surrounding code look like? Are you using the result anywhere?

  • In the code set I provide there was an ADC sample above but never used in the example.

    In order to get the compiler to actually compile "temp32s" value I had to use return temp32s.

    int32_t temp32s;

    int16_t Raw1;

    Raw1 = (int16_t)(GetADC10Sample(ADC_TEMPERATURE)& 0x03FF);

    temp32s = (long)1;
    temp32s = (long)temp32s * (long)256;

    return temp32s;

    I did some internet research after posting my question and found this:

    (+) MSP430FR6007: C problem with unsigned long and unsigned long long with msp430 - MSP low-power microcontroller forum - MSP low-power microcontrollers - TI E2E support forums

    Which supports your comment about "optimizer". I also figured out that CCS  Version: 12.2.0.00009 defaults to some optimization when a new project is created. 

    Optimization and Compression (Properties > CCS Build > Optimization)
    By default Code Composer Studio sets the:

    • Optimization Level (--opt_level, -O): 0-Register Optimizations          ====> Set to OFF
    • Speed vs. size trade-offs (--opt_for_speed, -mf): 1
    • Align functions and loops for power (--align_for_power): uncheck
    • Inline hardware multiply version of RTS (--use_hw_mpy) : none

    After changing Optimization Level (--opt_level, -O) to OFF the issue went away.

    So not the quest is why did the compiler create this issue?

  • Since the arithmetic is constant, I expect the compiler computed the result at compile time, and it became "return((long)256)". If so it probably never actually populated the variable. I seem to recall the debugger saying "optimized out" for some variables; that may have been the "Variables" (vs "Expressions") view.

    You may get the result you want by declaring the variable "volatile".

**Attention** This is a public forum