logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

kint41.c (2184B)


  1. /* Copyright 2020 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 2 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. #include "quantum.h"
  17. #define LED_POWER LINE_PIN13
  18. void matrix_init_kb(void) {
  19. matrix_init_user();
  20. // Turn on the Teensy 4.x Power LED:
  21. gpio_set_pin_output(LED_POWER);
  22. gpio_write_pin_high(LED_POWER);
  23. }
  24. // delay_inline sleeps for |cycles| (e.g. sleeping for F_CPU will sleep 1s).
  25. // delay_inline assumes the cycle counter has already been initialized and
  26. // should not be modified, i.e. it is safe to call during keyboard matrix scan.
  27. //
  28. // ChibiOS enables the cycle counter in chcore_v7m.c:
  29. // https://github.com/ChibiOS/ChibiOS/blob/b63023915c304092acb9f33bbab40f3ec07a7f0e/os/common/ports/ARMCMx/chcore_v7m.c#L263
  30. static void delay_inline(const uint32_t cycles) {
  31. const uint32_t start = DWT->CYCCNT;
  32. while ((DWT->CYCCNT - start) < cycles) {
  33. // busy-loop until time has passed
  34. }
  35. }
  36. void matrix_output_unselect_delay(uint8_t line, bool key_pressed) {
  37. // Use the cycle counter to do precise timing in microseconds. The ChibiOS
  38. // thread sleep functions only allow sleep durations starting at 1 tick, which
  39. // is 100μs in our configuration.
  40. // Empirically: e.g. 5μs is not enough, will result in keys that don’t work
  41. // and ghost key presses. 10μs seems to work well.
  42. // On some variants of the hardware, 20μs seems to be required. This was found
  43. // on a combination of KB600LF+stapelberg v2020-06-30+teensy41.
  44. // 600 cycles at 0.6 cycles/ns == 1μs
  45. const uint32_t cycles_per_us = 600;
  46. delay_inline(20 * cycles_per_us);
  47. }