Skip to main content
bruzzim
Associate III
Mar 12, 2010 9:57 AM UTC
Question

stm32 standard library and thread safety

Posted on March 12, 2010 at 10:57

stm32 standard library and thread safety

    This topic has been closed for replies.

    2 replies

    Danish1
    Lead III
    May 17, 2011 11:43 AM UTC
    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
    bruzzimAuthor
    Associate III
    May 17, 2011 11:43 AM UTC
    Posted on May 17, 2011 at 13:43

    Very useful comment, indeed.

    Thanks a lot.

    M