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