Hello,
recently I ran into a problem while working with 64-bit integers on MSP430G2553. The code listed below does some manipulations with 64-bit unsigned integers. The problem only occurs on "cold start", i. e. after powering up the board, flashing the firmware and running it from CCS. However, this is a result of my attempt of minimizing the amount of code required to reproduce the problem; in my current project this problem happens every time the partucular piece of code is executed. Anyway, here is the code:
main.c:
#include <msp430.h> #include <stdint.h> #include "test-file.h" static test_struct test_struct_array[] = { { .data = 0xDEADBEEFDEADBEEF }, { .data = 0xDEADBEEFDEADBEEF } }; /* * main.c */ void main(void) { WDTCTL = WDTPW | WDTHOLD; f_init( (test_struct *) &test_struct_array, ARRAY_SIZE(test_struct_array) ); LPM3; }
test-file.h:
#ifndef TEST_FILE_H #define TEST_FILE_H #include <msp430.h> #include <stdint.h> #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) typedef struct { uint64_t data; } test_struct; void f_init(test_struct * d, uint8_t s); #endif
test-file.c:
#include "test-file.h" test_struct * t; uint8_t c; void f_b(uint8_t a) { volatile int b = a; } void f_a() { uint64_t a = t[c - 1].data; uint64_t b = t[c - 1].data; // uint8_t i = 63; // do { f_b(1 && (a & ((uint64_t) 1 << i))); f_b(1 && (b & ((uint64_t) 1 << i))); } while (i--); } void f_init(test_struct * d, uint8_t s) { t = d; c = s; f_a(); }
Expected result and the problem
There is a breakpoint in the screenshot below. At this point, both a
and b
should be equal 0xDEADBEEFDEADBEEF
. However, b
is 0xFFFF847DDEADBEEF
(upper 32 bits differ from time to time, so I guess these bits are kind of random). Screenshot:
When volatile
keyword is added to the variable declarations, everything works as excepted.
Is this a compiler or code problem?
Optimization settings: 0 (Register optimizations)
Compiler: MSP430 Compiler Tools 4.3.5
CCS: 6.0.1.00040
Regards,
-- Vlad