From 40c7ecfdeaf50ab76e10854a84aebfcb82ddb092 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Wed, 10 Mar 2021 22:47:36 +0000 Subject: Move gpio wait logic to wait.h (#12067) --- tmk_core/common/chibios/_wait.h | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tmk_core/common/chibios/_wait.h (limited to 'tmk_core/common/chibios/_wait.h') diff --git a/tmk_core/common/chibios/_wait.h b/tmk_core/common/chibios/_wait.h new file mode 100644 index 0000000000..5bface53e1 --- /dev/null +++ b/tmk_core/common/chibios/_wait.h @@ -0,0 +1,55 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +/* chThdSleepX of zero maps to infinite - so we map to a tiny delay to still yield */ +#define wait_ms(ms) \ + do { \ + if (ms != 0) { \ + chThdSleepMilliseconds(ms); \ + } else { \ + chThdSleepMicroseconds(1); \ + } \ + } while (0) +#define wait_us(us) \ + do { \ + if (us != 0) { \ + chThdSleepMicroseconds(us); \ + } else { \ + chThdSleepMicroseconds(1); \ + } \ + } while (0) + +/* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus + * to which the GPIO is connected. + * The connected buses differ depending on the various series of MCUs. + * And since the instruction execution clock of the CPU and the bus clock of GPIO are different, + * there is a delay of several clocks to read the change of the input signal. + * + * Define this delay with the GPIO_INPUT_PIN_DELAY macro. + * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used. + * (A fairly large value of 0.25 microseconds is set.) + */ + +#include "wait.c" + +#ifndef GPIO_INPUT_PIN_DELAY +# define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK / 1000000L / 4) +#endif + +#define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) -- cgit v1.2.3 From 0a1bf7f6aa6e44557041e03f6e58df5a180c6d79 Mon Sep 17 00:00:00 2001 From: Simon Arlott <70171+nomis@users.noreply.github.com> Date: Fri, 20 Aug 2021 00:31:23 +0100 Subject: Support using a timer for wait_us() on ChibiOS-based boards (#12211) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support using a timer for wait_us() on ChibiOS-based boards (#12198) There are spare GPT timers that can be used to get a more accurate wait_ms() time. This is required for the matrix scan unselect delay (30µs) to be shorter than the system tick rate of 100µs. This is limited to the maximum GPT duration of 65535 so values above that will automatically use the previous implementation based on the system tick. Using a specific timer means it can't be shared by another thread at the same time so when wait_us() is called from anything other than the main thread it will use the system tick implementation too. * Update tmk_core/common/chibios/wait.c * Update tmk_core/common/chibios/wait.c Co-authored-by: Joel Challis --- tmk_core/common/chibios/_wait.h | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'tmk_core/common/chibios/_wait.h') diff --git a/tmk_core/common/chibios/_wait.h b/tmk_core/common/chibios/_wait.h index 5bface53e1..4a5172536b 100644 --- a/tmk_core/common/chibios/_wait.h +++ b/tmk_core/common/chibios/_wait.h @@ -16,6 +16,7 @@ #pragma once #include +#include /* chThdSleepX of zero maps to infinite - so we map to a tiny delay to still yield */ #define wait_ms(ms) \ @@ -26,14 +27,19 @@ chThdSleepMicroseconds(1); \ } \ } while (0) -#define wait_us(us) \ - do { \ - if (us != 0) { \ - chThdSleepMicroseconds(us); \ - } else { \ - chThdSleepMicroseconds(1); \ - } \ - } while (0) + +#ifdef WAIT_US_TIMER +void wait_us(uint16_t duration); +#else +# define wait_us(us) \ + do { \ + if (us != 0) { \ + chThdSleepMicroseconds(us); \ + } else { \ + chThdSleepMicroseconds(1); \ + } \ + } while (0) +#endif /* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus * to which the GPIO is connected. @@ -46,7 +52,7 @@ * (A fairly large value of 0.25 microseconds is set.) */ -#include "wait.c" +#include "_wait.c" #ifndef GPIO_INPUT_PIN_DELAY # define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK / 1000000L / 4) -- cgit v1.2.3 From c9c60d227e38abdf08ca86bcdad3a0cac2579e3f Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Tue, 24 Aug 2021 22:34:24 +0900 Subject: add wait_cpuclock() macro for AVR and CPU_CLOCK macro (#12755) --- tmk_core/common/chibios/_wait.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tmk_core/common/chibios/_wait.h') diff --git a/tmk_core/common/chibios/_wait.h b/tmk_core/common/chibios/_wait.h index 4a5172536b..b740afbd24 100644 --- a/tmk_core/common/chibios/_wait.h +++ b/tmk_core/common/chibios/_wait.h @@ -41,6 +41,10 @@ void wait_us(uint16_t duration); } while (0) #endif +#include "_wait.c" + +#define CPU_CLOCK STM32_SYSCLK + /* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus * to which the GPIO is connected. * The connected buses differ depending on the various series of MCUs. @@ -51,11 +55,8 @@ void wait_us(uint16_t duration); * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used. * (A fairly large value of 0.25 microseconds is set.) */ - -#include "_wait.c" - #ifndef GPIO_INPUT_PIN_DELAY -# define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK / 1000000L / 4) +# define GPIO_INPUT_PIN_DELAY (CPU_CLOCK / 1000000L / 4) #endif #define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) -- cgit v1.2.3