Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

delay.c 748B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "delay.h"
  2. #include "mk20dx.h"
  3. // the systick interrupt is supposed to increment this at 1 kHz rate
  4. volatile uint32_t systick_millis_count = 0;
  5. void yield(void) {};
  6. uint32_t micros(void)
  7. {
  8. uint32_t count, current, istatus;
  9. __disable_irq();
  10. current = SYST_CVR;
  11. count = systick_millis_count;
  12. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  13. __enable_irq();
  14. if ((istatus & SCB_ICSR_PENDSTSET) && current > ((F_CPU / 1000) - 50)) count++;
  15. current = ((F_CPU / 1000) - 1) - current;
  16. return count * 1000 + current / (F_CPU / 1000000);
  17. }
  18. void delay(uint32_t ms)
  19. {
  20. uint32_t start = micros();
  21. while (1) {
  22. if ((micros() - start) >= 1000) {
  23. ms--;
  24. if (ms == 0) break;
  25. start += 1000;
  26. }
  27. yield();
  28. }
  29. }