_wait.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <ch.h>
  18. /* chThdSleepX of zero maps to infinite - so we map to a tiny delay to still yield */
  19. #define wait_ms(ms) \
  20. do { \
  21. if (ms != 0) { \
  22. chThdSleepMilliseconds(ms); \
  23. } else { \
  24. chThdSleepMicroseconds(1); \
  25. } \
  26. } while (0)
  27. #define wait_us(us) \
  28. do { \
  29. if (us != 0) { \
  30. chThdSleepMicroseconds(us); \
  31. } else { \
  32. chThdSleepMicroseconds(1); \
  33. } \
  34. } while (0)
  35. /* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus
  36. * to which the GPIO is connected.
  37. * The connected buses differ depending on the various series of MCUs.
  38. * And since the instruction execution clock of the CPU and the bus clock of GPIO are different,
  39. * there is a delay of several clocks to read the change of the input signal.
  40. *
  41. * Define this delay with the GPIO_INPUT_PIN_DELAY macro.
  42. * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used.
  43. * (A fairly large value of 0.25 microseconds is set.)
  44. */
  45. #include "wait.c"
  46. #ifndef GPIO_INPUT_PIN_DELAY
  47. # define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK / 1000000L / 4)
  48. #endif
  49. #define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)