xtimer race condition

Hello all,

I have had some odd problems with a PR I am attempting to finalize:

cpu/atmega_common: RTT and RTC support

When working with the real-time timers, there are a number of times that you have to wait a few of the slow clock cycles to go by. I am using xtimer_usleep to sleep the thread temporarily. But! xtimer causes the code I am using to hang. Debugging it is difficult, because anything sending debug messages over UART causes the problems to disappear!

I have been editing and running various numbers: xtimer_usleep(8); 8 and bigger fail xtimer_usleep(7); 7 and smaller work

Bigger numbers make them fail!

Here is a snippet of the offending code:

void __asynch_wait(uint8_t num_cycles) {     /* Wait until all busy flags clear */     while( ASSR & ((1 << TCN2UB) | (1 << OCR2AUB) | (1 << OCR2BUB)                  > (1 << TCR2AUB) | (1 << TCR2BUB)) ) { #ifdef MODULE_XTIMER         /* Sleep for num_cycles RTC cycles */         xtimer_usleep(num_cycles * 35 * US_PER_MS); #else         /* Suppress unused parameter warning */         (void)num_cycles;

        thread_yield(); #endif     } }

Anyone have any ideas on what is going on?

Sincerely, Matthew

Hi Matthew, My guess is that you are running that function with interrupts disabled, which blocks the xtimer timeouts. The reason that short sleeps work in xtimer is that internally that is handled as a spin, while longer timeouts are handled by setting a hardware timer target. You can try using xtimer_spin instead of usleep, but chances are your PR will not be merged if you are using xtimer_spin with long timeouts. xtimer_spin is a busy wait which should be used only for very short periods, since it will be obstructing other parts of the system from running while you are spinning.

Best regards, Joakim