logo

qmk_firmware

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

matrix.c (3775B)


  1. /*
  2. Copyright (c) 2022 David Kuehling <dvdkhlng TA posteo TOD de>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "spi_master.h"
  15. #include "matrix.h"
  16. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  17. static void unselect_rows(void);
  18. void matrix_init_custom(void) {
  19. /* initialize row pins */
  20. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  21. gpio_set_pin_output(row_pins[row]);
  22. }
  23. unselect_rows();
  24. /* columns read via shift-register on SPI lines */
  25. /* Enable SPI, Master, set clock rate fck/16. First bit already at Qh
  26. * output before clock edge (CPHA=0). SN74HC165 shift register shifts
  27. * on low-to-high transition (CPOL=1). Receive the LSB first (DORD=1).
  28. */
  29. bool lsbFirst = true;
  30. uint8_t mode = 2; /* CPOL=1, CPHA=0 */
  31. uint16_t divisor = 16;
  32. /* According to Atmega32U4 datasheet, PB0 *must* be set to output,
  33. * otherwise it will interfere with SPI master operation. On pro-micro
  34. * it's connected to a yellew LED. */
  35. pin_t slavePin = PB0;
  36. spi_init();
  37. spi_start(slavePin, lsbFirst, mode, divisor);
  38. /* Initialize pin controlling the shift register's SH/~LD pin */
  39. gpio_set_pin_output(ROW_SHIFT_PIN);
  40. }
  41. static void select_row(uint8_t row) {
  42. pin_t pin = row_pins[row];
  43. if (pin != NO_PIN) {
  44. gpio_write_pin_high(pin);
  45. }
  46. }
  47. static void unselect_row(uint8_t row) {
  48. pin_t pin = row_pins[row];
  49. if (pin != NO_PIN) {
  50. gpio_write_pin_low(pin);
  51. }
  52. }
  53. static void unselect_rows(void) {
  54. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  55. unselect_row(row);
  56. }
  57. }
  58. bool matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  59. /* Start with a clear matrix row */
  60. matrix_row_t current_row_value = 0;
  61. /* Set shift register SH/~LD pin to "load" mode */
  62. gpio_write_pin_low(ROW_SHIFT_PIN);
  63. select_row(current_row);
  64. matrix_output_select_delay();
  65. /* Set shift register SH/~LD pin to "shift" mode */
  66. gpio_write_pin_high(ROW_SHIFT_PIN);
  67. /* For each octet of columns... */
  68. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index += 8) {
  69. spi_status_t read_result = spi_read();
  70. if (read_result >= 0) {
  71. /* only if SPI read successful: populate the matrix row with the
  72. state of the 8 consecutive column bits */
  73. current_row_value |= ((matrix_row_t)read_result << col_index);
  74. }
  75. }
  76. /* Unselect row & wait for all columns signals to go high. */
  77. unselect_row(current_row);
  78. matrix_output_unselect_delay(current_row, current_row_value != 0);
  79. /* Update row in matrix. */
  80. if (current_row_value != current_matrix[current_row]) {
  81. current_matrix[current_row] = current_row_value;
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool matrix_scan_custom(matrix_row_t curr_matrix[]) {
  87. bool changed = false;
  88. /* set row, read cols */
  89. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  90. changed |= matrix_read_cols_on_row(curr_matrix, current_row);
  91. }
  92. return changed;
  93. }
  94. /*
  95. * Local Variables:
  96. * c-basic-offset:4
  97. * fill-column: 76
  98. * End:
  99. */