_wait.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Copyright 2021 QMK
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <util/delay.h>
  18. #define wait_ms(ms) \
  19. do { \
  20. if (__builtin_constant_p(ms)) { \
  21. _delay_ms(ms); \
  22. } else { \
  23. for (uint16_t i = ms; i > 0; i--) { \
  24. _delay_ms(1); \
  25. } \
  26. } \
  27. } while (0)
  28. #define wait_us(us) \
  29. do { \
  30. if (__builtin_constant_p(us)) { \
  31. _delay_us(us); \
  32. } else { \
  33. for (uint16_t i = us; i > 0; i--) { \
  34. _delay_us(1); \
  35. } \
  36. } \
  37. } while (0)
  38. #define wait_cpuclock(n) __builtin_avr_delay_cycles(n)
  39. #define CPU_CLOCK F_CPU
  40. /* The AVR series GPIOs have a one clock read delay for changes in the digital input signal.
  41. * But here's more margin to make it two clocks. */
  42. #ifndef GPIO_INPUT_PIN_DELAY
  43. # define GPIO_INPUT_PIN_DELAY 2
  44. #endif
  45. #define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)