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.

MSP430F6726: MSP Issue with generated code

Part Number: MSP430F6726

Hello,

I think I have found a bug in the code generation of the MSP TI Compiler v18.12.8.LTS.

I use a c++ clase for portpins and one for ports to access the hardware registers.

In this special case the SCL and SDA pins of the I2C bus.

scl_.selectIoFunction();
sda_.selectIoFunction();

Those two lines program the IOs as normal IOs, clearing one bit in the  PnSEL register.

The compiler generates this code:

124 void clrPortFunktion(uint16_t selectedPins) {HWREG16(baseAddress_ + OFS_PASEL) &= ~selectedPins;}
017eb2: MOVA 0x0004(R12),R14
017eb6: MOVA @R14,R13
017eb8: MOV.W #0x000a,R15
017ebc: ADD.W 0x0004(R13),R15
017ec0: MOV.W R15,0x0000(SP)
017ec4: MOV.W 0x0004(R14),R13
017ec8: INV.W R13
017eca: MOVA 0x0000(SP),R15
017ece: AND.W R13,0x0000(R15)

124 void clrPortFunktion(uint16_t selectedPins) {HWREG16(baseAddress_ + OFS_PASEL) &= ~selectedPins;}
017ef0: MOVA 0x0008(R12),R15
017ef4: MOVA @R15,R14
017ef6: MOV.W #0x000a,R10
017efa: ADD.W 0x0004(R14),R10
017efe: BIC.W 0x0004(R15),0x0000(R10)

The strange thing is that the compiler generates totally different code for this two c++ lines, which are similar.

Both are instances of the same clase.

And the code generated for the first line is not working correctly. See the two red lines, the compiler stores the calculated address with 16 bit on the stack

and reloads it into a register with 20 bits, which leads to a random behavior, dependig on the old values on the stack.

I did not find an explanation for this in the errata sheets for the compiler. Is this a known problem ?

How can I avoid it ?

Thanks for your help

  • Hi,

    Could you try with the latest complier version and see if you still get this issue?

    Best regards,

    Cash Hao

  • Hello,

    I tried the v21.6.0.LTS compiler, the code is a bit different, but the error is the same.

    void clrPortFunktion(uint16_t selectedPins) {HWREG16(baseAddress_ + OFS_PASEL) &= ~selectedPins;}
    018976: 0C3E 0004 MOVA 0x0004(R12),R14
    01897a: 0E0D MOVA @R14,R13
    01897c: 403F 000A MOV.W #0x000a,R15
    018980: 5D1F 0004 ADD.W 0x0004(R13),R15
    018984: 4F81 0000 MOV.W R15,0x0000(SP)
    018988: 013F 0000 MOVA 0x0000(SP),R15
    01898c: CE9F 0004 0000 BIC.W 0x0004(R14),0x0000(R15)

    I played a bit and found one solution:

    baseAddress_ is an uint16_t because the addresses are declared like:

    #define __MSP430_BASEADDRESS_PORTA_R__ 0x0200 in the header files.

    if I change baseAddress_ to uint8_t* the produced code is a bit complicated, but correct.

    Thanks for your help

**Attention** This is a public forum