logo

qmk_firmware

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

matrix.c (1984B)


  1. // Copyright 2024 splitkb.com (support@splitkb.com)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "matrix.h"
  4. #include "spi_master.h"
  5. // The matrix is hooked up to a chain of 74xx165 shift registers.
  6. // Pin F0 acts as Chip Select (active-low)
  7. // The signal goes to a NOT gate, whose output is wired to
  8. // a) the latch pin of the shift registers
  9. // b) the "enable" pin of a tri-state buffer,
  10. // attached between the shift registers and MISO
  11. // F0 has an external pull-up.
  12. // SCK works as usual.
  13. //
  14. // Note that the matrix contains a variety of data.
  15. // In addition to the keys, it also reads the rotary encoders
  16. // and whether the board is the left/right half.
  17. void matrix_init_custom(void) {
  18. // Note: `spi_init` has already been called
  19. // in `keyboard_pre_init_kb()`, so nothing to do here
  20. }
  21. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  22. // Enough to hold the shift registers
  23. uint16_t length = 5;
  24. uint8_t data[length];
  25. // Matrix SPI config
  26. // 1) Pin
  27. // 2) Mode: Register shifts on rising clock, and clock idles low
  28. // pol = 0 & pha = 0 => mode 0
  29. // 3) LSB first: Register outputs H first, and we want H as MSB,
  30. // as this result in a neat A-H order in the layout macro.
  31. // 4) Divisor: 2 is the fastest possible, at Fclk / 2.
  32. // range is 2-128
  33. spi_start(GP13, false, 0, 128);
  34. spi_receive(data, length);
  35. spi_stop();
  36. bool matrix_has_changed = false;
  37. for (uint8_t i = 0; i < length; i++) {
  38. // Bitwise NOT because we use pull-ups,
  39. // and switches short the pin to ground,
  40. // but the matrix uses 1 to indicate a pressed switch
  41. uint8_t word = ~data[i];
  42. matrix_has_changed |= current_matrix[i] ^ word;
  43. current_matrix[i] = word;
  44. }
  45. #ifdef MYRIAD_ENABLE
  46. bool myriad_hook_matrix(matrix_row_t current_matrix[]);
  47. return matrix_has_changed || myriad_hook_matrix(current_matrix);
  48. #else
  49. return matrix_has_changed;
  50. #endif
  51. }