matrix.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
  3. Jun Wako <wakojun@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <string.h>
  18. #include <hal.h>
  19. #include "timer.h"
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "matrix.h"
  24. #include "keyboard.h"
  25. #include "serial_link/system/serial_link.h"
  26. /*
  27. * Infinity ErgoDox Pinusage:
  28. * Column pins are input with internal pull-down. Row pins are output and strobe with high.
  29. * Key is high or 1 when it turns on.
  30. *
  31. * col: { PTD1, PTD4, PTD5, PTD6, PTD7 }
  32. * row: { PTB2, PTB3, PTB18, PTB19, PTC0, PTC9, PTC10, PTC11, PTD0 }
  33. */
  34. /* matrix state(1:on, 0:off) */
  35. static matrix_row_t matrix[MATRIX_ROWS];
  36. static matrix_row_t matrix_debouncing[LOCAL_MATRIX_ROWS];
  37. static bool debouncing = false;
  38. static uint16_t debouncing_time = 0;
  39. void matrix_init(void)
  40. {
  41. /* Row(sense) */
  42. palSetPadMode(GPIOD, 1, PAL_MODE_INPUT_PULLDOWN);
  43. palSetPadMode(GPIOD, 4, PAL_MODE_INPUT_PULLDOWN);
  44. palSetPadMode(GPIOD, 5, PAL_MODE_INPUT_PULLDOWN);
  45. palSetPadMode(GPIOD, 6, PAL_MODE_INPUT_PULLDOWN);
  46. palSetPadMode(GPIOD, 7, PAL_MODE_INPUT_PULLDOWN);
  47. /* Column(strobe) */
  48. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  49. palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL);
  50. palSetPadMode(GPIOB, 18, PAL_MODE_OUTPUT_PUSHPULL);
  51. palSetPadMode(GPIOB, 19, PAL_MODE_OUTPUT_PUSHPULL);
  52. palSetPadMode(GPIOC, 0, PAL_MODE_OUTPUT_PUSHPULL);
  53. palSetPadMode(GPIOC, 9, PAL_MODE_OUTPUT_PUSHPULL);
  54. palSetPadMode(GPIOC, 10, PAL_MODE_OUTPUT_PUSHPULL);
  55. palSetPadMode(GPIOC, 11, PAL_MODE_OUTPUT_PUSHPULL);
  56. palSetPadMode(GPIOD, 0, PAL_MODE_OUTPUT_PUSHPULL);
  57. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  58. memset(matrix_debouncing, 0, LOCAL_MATRIX_ROWS * sizeof(matrix_row_t));
  59. matrix_init_quantum();
  60. }
  61. uint8_t matrix_scan(void)
  62. {
  63. for (int row = 0; row < LOCAL_MATRIX_ROWS; row++) {
  64. matrix_row_t data = 0;
  65. // strobe row
  66. switch (row) {
  67. case 0: palSetPad(GPIOB, 2); break;
  68. case 1: palSetPad(GPIOB, 3); break;
  69. case 2: palSetPad(GPIOB, 18); break;
  70. case 3: palSetPad(GPIOB, 19); break;
  71. case 4: palSetPad(GPIOC, 0); break;
  72. case 5: palSetPad(GPIOC, 9); break;
  73. case 6: palSetPad(GPIOC, 10); break;
  74. case 7: palSetPad(GPIOC, 11); break;
  75. case 8: palSetPad(GPIOD, 0); break;
  76. }
  77. // need wait to settle pin state
  78. // if you wait too short, or have a too high update rate
  79. // the keyboard might freeze, or there might not be enough
  80. // processing power to update the LCD screen properly.
  81. // 20us, or two ticks at 100000Hz seems to be OK
  82. wait_us(20);
  83. // read col data: { PTD1, PTD4, PTD5, PTD6, PTD7 }
  84. data = ((palReadPort(GPIOD) & 0xF0) >> 3) |
  85. ((palReadPort(GPIOD) & 0x02) >> 1);
  86. // un-strobe row
  87. switch (row) {
  88. case 0: palClearPad(GPIOB, 2); break;
  89. case 1: palClearPad(GPIOB, 3); break;
  90. case 2: palClearPad(GPIOB, 18); break;
  91. case 3: palClearPad(GPIOB, 19); break;
  92. case 4: palClearPad(GPIOC, 0); break;
  93. case 5: palClearPad(GPIOC, 9); break;
  94. case 6: palClearPad(GPIOC, 10); break;
  95. case 7: palClearPad(GPIOC, 11); break;
  96. case 8: palClearPad(GPIOD, 0); break;
  97. }
  98. if (matrix_debouncing[row] != data) {
  99. matrix_debouncing[row] = data;
  100. debouncing = true;
  101. debouncing_time = timer_read();
  102. }
  103. }
  104. uint8_t offset = 0;
  105. if (is_serial_link_master() && !is_keyboard_left()) {
  106. offset = MATRIX_ROWS - LOCAL_MATRIX_ROWS;
  107. }
  108. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  109. for (int row = 0; row < LOCAL_MATRIX_ROWS; row++) {
  110. matrix[offset + row] = matrix_debouncing[row];
  111. }
  112. debouncing = false;
  113. }
  114. matrix_scan_quantum();
  115. return 1;
  116. }
  117. bool matrix_is_on(uint8_t row, uint8_t col)
  118. {
  119. return (matrix[row] & (1<<col));
  120. }
  121. matrix_row_t matrix_get_row(uint8_t row)
  122. {
  123. return matrix[row];
  124. }
  125. void matrix_print(void)
  126. {
  127. xprintf("\nr/c 01234567\n");
  128. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  129. xprintf("%X0: ", row);
  130. matrix_row_t data = matrix_get_row(row);
  131. for (int col = 0; col < MATRIX_COLS; col++) {
  132. if (data & (1<<col))
  133. xprintf("1");
  134. else
  135. xprintf("0");
  136. }
  137. xprintf("\n");
  138. }
  139. }
  140. void matrix_set_remote(matrix_row_t* rows, uint8_t index) {
  141. uint8_t offset = 0;
  142. if (is_keyboard_left()) {
  143. offset = LOCAL_MATRIX_ROWS * (index + 1);
  144. } else {
  145. offset = MATRIX_ROWS - LOCAL_MATRIX_ROWS * (index + 2);
  146. }
  147. for (int row = 0; row < LOCAL_MATRIX_ROWS; row++) {
  148. matrix[offset + row] = rows[row];
  149. }
  150. }