Part Number: MSP430F5438
According to the User's Guide, when RTC is asynchronous to CPU, majority vote should be taken to determine the correct reading of the counters. However, at least four successive readings are needed to work correctly all the time, as Jens-Michael Gross pointed out here. In addition, one must assume that the CPU frequency is relatively high enough to run all the four readings before the next RTC clock arrives.
An alternative, which is much simpler, is as follows:
u32 RTC_counter(void)
{
volatile u32 v1, v2;
v1 = RTCNT;
v2 = RTCNT;
while (1 < v2 - v1) {
v1 = v2;
v2 = RTCNT;
}
return v2;
}
That is, read successively until one is found to be equal to or one greater than the previous, and return it.
Is that reliable? Or, is there a chance that v1 and v2 hit the same one race condition, resulting in the same v1 and v2 while both are incorrect (which I think is relevant to the majority vote algorithm as well)?
Thanks!