cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 standard library and thread safety

bruzzim
Associate II
Posted on March 12, 2010 at 10:57

stm32 standard library and thread safety

2 REPLIES 2
Danish1
Lead II
Posted on May 17, 2011 at 13:43

It's worse than you think. Hardware values (like RTC) can change without interrupts happening.

Suppose the RTC happens to be 0x2345 (high) and 0xFFFF (low)

And you read the low value = 0xFFFF

Then the RTC counts once to 0x2346 (high) and 0x0000 (low)

Then you read the high value as 0x2346.

Your return value is 0x2346FFFF - much higher than real-time.

I do code like:

    uint16_t h1, l, h2;

    h2 = RTC_CNTH;

    do {    /* code to catch overflow from low short to high short */

        h1 = h2;

        l = RTC_CNTL;

        h2 = RTC_CNTH;

    } while (h1 != h2);

    return l | (h1 << 16);

Hope this helps,

Danish

bruzzim
Associate II
Posted on May 17, 2011 at 13:43

Very useful comment, indeed.

Thanks a lot.

M