LoRaMac-node icon indicating copy to clipboard operation
LoRaMac-node copied to clipboard

Race condition in RtcGetCalendarValue on NucleoL476 board

Open ndiddy99 opened this issue 7 months ago • 0 comments

The RtcGetCalendarValue function checks the RTC subsecond register for consistency but does not check the date or time registers. Because the code doesn't use the RTC shadow registers (see here), the code needs to check the subsecond, time, and date registers to make sure none of them have changed. Our internal fix looks like this:


    uint64_t calendarValue = 0;
    volatile uint32_t firstSSR;
    volatile uint32_t firstTR;
    volatile uint32_t firstDR;
    uint32_t correction;
    uint32_t seconds;

    // The LoRaWAN stack bypasses the RTC's shadow registers, so we need to make
    // sure the RTC isn't in the process of updating any of its registers while
    // we get the date & time.
    do
    {
        firstSSR = RTC->SSR;
        firstTR = RTC->TR;
        firstDR = RTC->DR;
        HAL_RTC_GetTime( &RtcHandle, time, RTC_FORMAT_BIN );
        HAL_RTC_GetDate( &RtcHandle, date, RTC_FORMAT_BIN );
    } while ((firstSSR != RTC->SSR) || (firstTR != RTC->TR) || (firstDR != RTC->DR));

I haven't checked to see if the board files for the other STM32 processors have the same issue, but it's probably something to look at.

ndiddy99 avatar Dec 08 '23 18:12 ndiddy99