logo

qmk_firmware

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

matrix.c (3280B)


  1. /*
  2. * Copyright 2020 Michael Schwingen
  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 "util.h"
  17. #include "matrix.h"
  18. #include "debounce.h"
  19. #include "spi_master.h"
  20. #include "print.h"
  21. #include "mschwingen.h"
  22. #define SPI_TIMEOUT 100
  23. /* Keyboard Matrix Assignments */
  24. static uint16_t row_bits[MATRIX_ROWS] = {
  25. 0x4000, 0x8000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0100, 0x0200,
  26. 0x0040, 0x0080, 0x0020, 0x0010, 0x0008, 0x0004, 0x0001, 0x0002};
  27. static const pin_t col_pins[MATRIX_COLS] = {D1, D4, D7, B4, F7, F6, F5, F4};
  28. static void select_col(uint8_t col) {
  29. gpio_set_pin_output(col_pins[col]);
  30. gpio_write_pin_low(col_pins[col]);
  31. }
  32. static void unselect_col(uint8_t col) { gpio_set_pin_input_high(col_pins[col]); }
  33. static void unselect_cols(void) {
  34. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  35. gpio_set_pin_input_high(col_pins[x]);
  36. }
  37. }
  38. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  39. uint16_t row_data;
  40. bool matrix_changed = false;
  41. // Select col and wait for col selecton to stabilize
  42. select_col(current_col);
  43. matrix_io_delay();
  44. gpio_write_pin_low(SR_LOAD_PIN);
  45. gpio_write_pin_high(SR_LOAD_PIN);
  46. row_data = spi_read() << 8;
  47. row_data |= spi_read();
  48. dprintf("%02X ", ~row_data);
  49. // For each row...
  50. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  51. // Store last value of row prior to reading
  52. matrix_row_t last_row_value = current_matrix[row_index];
  53. matrix_row_t current_row_value = last_row_value;
  54. // Check row pin state
  55. if ((row_data & row_bits[row_index]) == 0) {
  56. // Pin LO, set col bit
  57. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  58. } else {
  59. // Pin HI, clear col bit
  60. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  61. }
  62. // Determine if the matrix changed state
  63. if ((last_row_value != current_row_value)) {
  64. matrix_changed = true;
  65. current_matrix[row_index] = current_row_value;
  66. }
  67. }
  68. // Unselect col
  69. unselect_col(current_col);
  70. return matrix_changed;
  71. }
  72. void matrix_init_custom(void) {
  73. unselect_cols();
  74. // set 4MHz SPI clock
  75. SPSR = 0;
  76. SPCR = _BV(SPE) | _BV(MSTR) | _BV(CPOL);
  77. }
  78. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  79. bool changed = false;
  80. dprint("\r\nScan: ");
  81. // Set col, read rows
  82. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  83. changed |= read_rows_on_col(current_matrix, current_col);
  84. }
  85. update_layer_leds();
  86. return changed;
  87. }