Since the MSP430 compiler upgrade to 4.3.x I have found a multiplication problem that did not exist in the 4.2.x version.
The code example below will be using the specific compiler versions 4.3.3 and 4.2.1 to illustrate the problem.
Background: We are using the CC1101 with the MSP430. The registers accessors use bit fields to get/set the registers. It makes for readable code.
Here is the function for calculating symbol rate:
/**
* @details
* Rdata = [ (256 + DRATE_M) * 2 ^ DRATE_E / 2^18 ] * FxOsc
*
* @example
* DRATE_E = MDMCFG4.DRATE_E = 0x0B; 11
* DRATE_M = MDMCFG3.DRATE_M = 0x83; 131
*
* Rdata = [ (256 + 131) * 2 ^ 11 / 2 ^ 28 ] * 26MHz = 76.767 K baud
*/
uint32_t cc1101SymbolRate(void)
{
const uint8_t mdmCfg4Val = cc1101Read(CC1101_REGISTER_MDMCFG4);
const uint8_t mdmCfg3Val = cc1101Read(CC1101_REGISTER_MDMCFG3);
const struct CC1101_REG_MDMCFG4 mdmCfg4 = * (const struct CC1101_REG_MDMCFG4 *) &mdmCfg4Val;
const struct CC1101_REG_MDMCFG3 mdmCfg3 = * (const struct CC1101_REG_MDMCFG3 *) &mdmCfg3Val;
uint64_t Rdata = (256 + mdmCfg3.DRATE_M);
Rdata *= CC1101_FxOSC;
Rdata /= iexp2(28 - mdmCfg4.DRATE_E);
return (uint32_t) Rdata;
}
Here are some actual register values:
CC1101 register 0x10: 0x7b 'MDMCFG4'
CC1101 register 0x11: 0x83 'MDMCFG3'
CC1101 register 0x12: 0x13 'MDMCFG2'
CC1101 register 0x13: 0x22 'MDMCFG1'
CC1101 register 0x14: 0xf8 'MDMCFG0'
Here are the bit field data structs and other symbols, functions:
/// The CC1101 Crystal oscillator nominal frequency in Hz.
#define CC1101_FxOSC ((uint32_t) 26UL * 1000UL * 1000UL)
// Calculate 2^power, where power is an unsigned integer.
inline uint32_t iexp2(uint8_t power)
{
uint32_t value = 0x01;
value <<= power;
return value;
}
struct __attribute__(__packed__) CC1101_REG_MDMCFG4 ///< Reg: 0x10
{
unsigned int DRATE_E : 4; ///< [3:0]
unsigned int CHANBW_M : 2; ///< [5:4]
unsigned int CHANBW_E : 2; ///< [7:6]
};
struct __attribute__(__packed__) CC1101_REG_MDMCFG3 ///< Reg: 0x11
{
unsigned int DRATE_M : 8; ///< [7:0]
};
Under version 4.2.1 Compiler, the result (as expected) for our Symbol Rate is
Symbol Rate : 76766 baud
Under the 4.3.3 compiler the unexpected (surprise!) symbol rate is:
Symbol Rate : 75180 baud
Other calculations are also off.
I have tried using intermediate variables that are not bit fields -- but come up with the same results.
Additionally, I tried straight 32 bit to 64 bit multiplies and divides - however they work out properly.
The CPU is MSP430F5528.
Thanks,
Nat