#ifndef delay_h_included
#define delay_h_included

#include <stdint.h>

//one step takes 3 clock cycles (when running from flash), which is 17.857ns at 168mhz
static inline void Delay(uint32_t n_clock3)
{
        asm volatile (
        "\n"
        "Lab%=:" "\n\t"
        "subs %0, %0, 1" "\n\t"
        "bne.n Lab%=" "\n\t"
	: "=r" (n_clock3) //let gcc know the input register will be modified and its value cannot be reused 
	: "0" (n_clock3) 
        );
        return;
}


#define delay_us(s) Delay((s)*SYSTEM_CLOCK/3/1000000)

#define delay_ms(s) Delay((s)*SYSTEM_CLOCK/3/1000)

#define delay_xs(s) Delay((s)*SYSTEM_CLOCK/3/2000000)


#endif
