You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.6 KiB

  1. /*
  2. Copyright 2012-2018 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 <stdint.h>
  15. #include <stdbool.h>
  16. #include "matrix.h"
  17. #include "quantum.h"
  18. #include "sn74x138.h"
  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. /* Columns 6 and 9-15 use a 74HC138 3-to-8 demultiplexer.
  22. * D4 is the enable pin, must be set high to use it.
  23. *
  24. * 0: F7
  25. * 1: F5
  26. * 2: F6
  27. * 3: F1
  28. * 4: F4
  29. * 5: F0
  30. *
  31. * A2 A1 A0
  32. * D0 D1 D2
  33. * 6: 1 1 1
  34. *
  35. * 7: D5
  36. * 8: D3
  37. *
  38. * 9: 1 1 0
  39. * 10: 1 0 1
  40. * 11: 1 0 0
  41. * 12: 0 1 1
  42. * 13: 0 1 0
  43. * 14: 0 0 1
  44. * 15: 0 0 0
  45. */
  46. static void select_col(uint8_t col) {
  47. if (col_pins[col] != NO_PIN) {
  48. writePinLow(col_pins[col]);
  49. } else {
  50. sn74x138_set_addr((col == 6) ? 7 : 15 - col);
  51. sn74x138_set_enabled(true);
  52. }
  53. }
  54. static void unselect_col(uint8_t col) {
  55. if (col_pins[col] != NO_PIN) {
  56. setPinOutput(col_pins[col]);
  57. gpio_write_pin_high(col_pins[col]);
  58. } else {
  59. sn74x138_set_enabled(false);
  60. }
  61. }
  62. static void unselect_cols(void) {
  63. // Native
  64. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  65. if (col_pins[x] != NO_PIN) {
  66. setPinOutput(col_pins[x]);
  67. gpio_write_pin_high(col_pins[x]);
  68. }
  69. }
  70. // Demultiplexer
  71. sn74x138_set_enabled(false);
  72. }
  73. static void init_pins(void) {
  74. unselect_cols();
  75. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  76. setPinInputHigh(row_pins[x]);
  77. }
  78. }
  79. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  80. bool matrix_changed = false;
  81. // Select col and wait for col selection to stabilize
  82. select_col(current_col);
  83. matrix_io_delay();
  84. // For each row...
  85. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  86. // Store last value of row prior to reading
  87. matrix_row_t last_row_value = current_matrix[row_index];
  88. // Check row pin state
  89. if (gpio_read_pin(row_pins[row_index]) == 0) {
  90. // Pin LO, set col bit
  91. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  92. } else {
  93. // Pin HI, clear col bit
  94. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  95. }
  96. // Determine if the matrix changed state
  97. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  98. matrix_changed = true;
  99. }
  100. }
  101. // Unselect col
  102. unselect_col(current_col);
  103. return matrix_changed;
  104. }
  105. void matrix_init_custom(void) {
  106. // initialize demultiplexer
  107. sn74x138_init();
  108. // initialize key pins
  109. init_pins();
  110. }
  111. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  112. bool changed = false;
  113. // Set col, read rows
  114. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  115. changed |= read_rows_on_col(current_matrix, current_col);
  116. }
  117. return changed;
  118. }