X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Lib%2Fdelay.h;fp=Lib%2Fdelay.h;h=58746efcfcccb533940db3802f9d630d513c1797;hb=c8b4baf652ed7f3ea7b569e50db70aa718b0fdff;hp=0000000000000000000000000000000000000000;hpb=6da1558b78247ed2aabc1c9ef48b03f1ff63b9b1;p=kiibohd-controller.git diff --git a/Lib/delay.h b/Lib/delay.h new file mode 100644 index 0000000..58746ef --- /dev/null +++ b/Lib/delay.h @@ -0,0 +1,48 @@ + +#ifndef __DELAY_H +#define __DELAY_H + +#include + +// Convenience Macros, for delay compatibility with AVR-GCC +#define _delay_ms(val) delay( val ) +#define _delay_us(val) delayMicroseconds( val ) + + +// the systick interrupt is supposed to increment this at 1 kHz rate +extern volatile uint32_t systick_millis_count; + +static inline uint32_t millis(void) __attribute__((always_inline, unused)); +static inline uint32_t millis(void) +{ + return systick_millis_count; // single aligned 32 bit is atomic; +} + + +static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused)); +static inline void delayMicroseconds(uint32_t usec) +{ +#if F_CPU == 96000000 + uint32_t n = usec << 5; +#elif F_CPU == 48000000 + uint32_t n = usec << 4; +#elif F_CPU == 24000000 + uint32_t n = usec << 3; +#endif + asm volatile( + "L_%=_delayMicroseconds:" "\n\t" + "subs %0, #1" "\n\t" + "bne L_%=_delayMicroseconds" "\n" + : "+r" (n) : + ); +} + + +void yield(void) __attribute__ ((weak)); + +uint32_t micros(void); + +void delay(uint32_t ms); + +#endif +