logo

qmk_firmware

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

matrix.c (4928B)


  1. /*
  2. Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar
  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 "atomic_util.h"
  15. #include "wait.h"
  16. #include "matrix.h"
  17. #include "i2c_master.h"
  18. #define PORT_EXPANDER_ADDRESS 0x20
  19. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  20. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  21. static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
  22. ATOMIC_BLOCK_FORCEON {
  23. gpio_set_pin_output(pin);
  24. gpio_write_pin_low(pin);
  25. }
  26. }
  27. static inline void gpio_atomic_set_pin_output_high(pin_t pin) {
  28. ATOMIC_BLOCK_FORCEON {
  29. gpio_set_pin_output(pin);
  30. gpio_write_pin_high(pin);
  31. }
  32. }
  33. static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
  34. ATOMIC_BLOCK_FORCEON {
  35. gpio_set_pin_input_high(pin);
  36. }
  37. }
  38. static inline uint8_t readMatrixPin(pin_t pin) {
  39. if (pin != NO_PIN) {
  40. return (gpio_read_pin(pin) == 0) ? 0 : 1;
  41. } else {
  42. return 1;
  43. }
  44. }
  45. static bool select_row(uint8_t row) {
  46. pin_t pin = row_pins[row];
  47. if (pin != NO_PIN) {
  48. gpio_atomic_set_pin_output_low(pin);
  49. return true;
  50. }
  51. return false;
  52. }
  53. static void unselect_row(uint8_t row) {
  54. pin_t pin = row_pins[row];
  55. if (pin != NO_PIN) {
  56. gpio_atomic_set_pin_input_high(pin);
  57. }
  58. }
  59. static void unselect_rows(void) {
  60. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  61. unselect_row(x);
  62. }
  63. }
  64. static void init_pins(void) {
  65. unselect_rows();
  66. // Set I/O
  67. uint8_t send_data = 0xFF;
  68. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20);
  69. // Set Pull-up
  70. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20);
  71. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  72. if (col_pins[x] != NO_PIN) {
  73. gpio_atomic_set_pin_input_high(col_pins[x]);
  74. }
  75. }
  76. }
  77. void matrix_init_custom(void) {
  78. // TODO: initialize hardware here
  79. // Initialize I2C
  80. i2c_init();
  81. // initialize key pins
  82. init_pins();
  83. wait_ms(50);
  84. }
  85. static bool matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  86. // Store last value of row prior to reading
  87. matrix_row_t last_row_value = current_matrix[current_row];
  88. // Clear data in matrix row
  89. current_matrix[current_row] = 0;
  90. // Select row and wait for row selecton to stabilize
  91. select_row(current_row);
  92. matrix_output_select_delay();
  93. uint8_t port_expander_buffer;
  94. i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x09, &port_expander_buffer, 1, 20);
  95. // For each col...
  96. // matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  97. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  98. uint8_t pin_state;
  99. // Select the col pin to read (active low)
  100. switch (col_index) {
  101. case 6 :
  102. pin_state = port_expander_buffer & (1 << 0);
  103. break;
  104. case 7 :
  105. pin_state = port_expander_buffer & (1 << 1);
  106. break;
  107. case 8 :
  108. pin_state = port_expander_buffer & (1 << 2);
  109. break;
  110. case 9 :
  111. pin_state = port_expander_buffer & (1 << 3);
  112. break;
  113. case 10 :
  114. pin_state = port_expander_buffer & (1 << 4);
  115. break;
  116. case 11 :
  117. pin_state = port_expander_buffer & (1 << 5);
  118. break;
  119. case 12 :
  120. pin_state = port_expander_buffer & (1 << 6);
  121. break;
  122. case 13 :
  123. pin_state = port_expander_buffer & (1 << 7);
  124. break;
  125. default :
  126. pin_state = readMatrixPin(col_pins[col_index]);
  127. }
  128. // Populate the matrix row with the state of the col pin
  129. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  130. }
  131. // Unselect row
  132. unselect_row(current_row);
  133. return (last_row_value != current_matrix[current_row]);
  134. }
  135. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  136. bool matrix_has_changed = false;
  137. // Set row, read cols
  138. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  139. matrix_has_changed |= matrix_read_cols_on_row(current_matrix, current_row);
  140. }
  141. return matrix_has_changed;
  142. }