_wait.h 2.5 KB

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