#ifndef DELAY_H
#define DELAY_H

// TODO: define system clock well
#define SYSTEM_CLOCK    ((uint32_t)48000000UL)

#define SYSTICK_CALIB 0x3E8
#define SYSTICK_LOAD (SYSTEM_CLOCK/1000000UL)
#define SYSTICK_DELAY_CALIB (SYSTICK_LOAD >> 1)

#define DELAY_US(us) \
    do { \
         uint32_t start = SysTick->VAL; \
         uint32_t ticks = (us * SYSTICK_LOAD)-SYSTICK_DELAY_CALIB;  \
         while((start - SysTick->VAL) < ticks); \
    } while (0)

#define DELAY_MS(ms) \
    do { \
        for (uint32_t i = 0; i < ms; ++i) { \
            DELAY_US(1000); \
        } \
    } while (0)

void systick_configure();

#endif // DELAY_H
