matrix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /*
  25. * Infinity ErgoDox Pinusage:
  26. * Column pins are input with internal pull-down. Row pins are output and strobe with high.
  27. * Key is high or 1 when it turns on.
  28. *
  29. * col: { PTD1, PTD4, PTD5, PTD6, PTD7 }
  30. * row: { PTB2, PTB3, PTB18, PTB19, PTC0, PTC9, PTC10, PTC11, PTD0 }
  31. */
  32. /* matrix state(1:on, 0:off) */
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static matrix_row_t matrix_debouncing[LOCAL_MATRIX_ROWS];
  35. static bool debouncing = false;
  36. static uint16_t debouncing_time = 0;
  37. void matrix_init(void)
  38. {
  39. /* Column(sense) */
  40. palSetPadMode(GPIOD, 1, PAL_MODE_INPUT_PULLDOWN);
  41. palSetPadMode(GPIOD, 4, PAL_MODE_INPUT_PULLDOWN);
  42. palSetPadMode(GPIOD, 5, PAL_MODE_INPUT_PULLDOWN);
  43. palSetPadMode(GPIOD, 6, PAL_MODE_INPUT_PULLDOWN);
  44. palSetPadMode(GPIOD, 7, PAL_MODE_INPUT_PULLDOWN);
  45. /* Row(strobe) */
  46. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  47. palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL);
  48. palSetPadMode(GPIOB, 18, PAL_MODE_OUTPUT_PUSHPULL);
  49. palSetPadMode(GPIOB, 19, PAL_MODE_OUTPUT_PUSHPULL);
  50. palSetPadMode(GPIOC, 0, PAL_MODE_OUTPUT_PUSHPULL);
  51. palSetPadMode(GPIOC, 9, PAL_MODE_OUTPUT_PUSHPULL);
  52. palSetPadMode(GPIOC, 10, PAL_MODE_OUTPUT_PUSHPULL);
  53. palSetPadMode(GPIOC, 11, PAL_MODE_OUTPUT_PUSHPULL);
  54. palSetPadMode(GPIOD, 0, PAL_MODE_OUTPUT_PUSHPULL);
  55. memset(matrix, 0, MATRIX_ROWS);
  56. memset(matrix_debouncing, 0, LOCAL_MATRIX_ROWS);
  57. }
  58. uint8_t matrix_scan(void)
  59. {
  60. for (int row = 0; row < LOCAL_MATRIX_ROWS; row++) {
  61. matrix_row_t data = 0;
  62. // strobe row
  63. switch (row) {
  64. case 0: palSetPad(GPIOB, 2); break;
  65. case 1: palSetPad(GPIOB, 3); break;
  66. case 2: palSetPad(GPIOB, 18); break;
  67. case 3: palSetPad(GPIOB, 19); break;
  68. case 4: palSetPad(GPIOC, 0); break;
  69. case 5: palSetPad(GPIOC, 9); break;
  70. case 6: palSetPad(GPIOC, 10); break;
  71. case 7: palSetPad(GPIOC, 11); break;
  72. case 8: palSetPad(GPIOD, 0); break;
  73. }
  74. // need wait to settle pin state
  75. // if you wait too short, or have a too high update rate
  76. // the keyboard might freeze, or there might not be enough
  77. // processing power to update the LCD screen properly.
  78. // 20us, or two ticks at 100000Hz seems to be OK
  79. wait_us(20);
  80. // read col data: { PTD1, PTD4, PTD5, PTD6, PTD7 }
  81. data = ((palReadPort(GPIOD) & 0xF0) >> 3) |
  82. ((palReadPort(GPIOD) & 0x02) >> 1);
  83. // un-strobe row
  84. switch (row) {
  85. case 0: palClearPad(GPIOB, 2); break;
  86. case 1: palClearPad(GPIOB, 3); break;
  87. case 2: palClearPad(GPIOB, 18); break;
  88. case 3: palClearPad(GPIOB, 19); break;
  89. case 4: palClearPad(GPIOC, 0); break;
  90. case 5: palClearPad(GPIOC, 9); break;
  91. case 6: palClearPad(GPIOC, 10); break;
  92. case 7: palClearPad(GPIOC, 11); break;
  93. case 8: palClearPad(GPIOD, 0); break;
  94. }
  95. if (matrix_debouncing[row] != data) {
  96. matrix_debouncing[row] = data;
  97. debouncing = true;
  98. debouncing_time = timer_read();
  99. }
  100. }
  101. uint8_t offset = 0;
  102. #ifdef MASTER_IS_ON_RIGHT
  103. if (is_serial_link_master()) {
  104. offset = MATRIX_ROWS - LOCAL_MATRIX_ROWS;
  105. }
  106. #endif
  107. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  108. for (int row = 0; row < LOCAL_MATRIX_ROWS; row++) {
  109. matrix[offset + row] = matrix_debouncing[row];
  110. }
  111. debouncing = false;
  112. }
  113. return 1;
  114. }
  115. bool matrix_is_on(uint8_t row, uint8_t col)
  116. {
  117. return (matrix[row] & (1<<col));
  118. }
  119. matrix_row_t matrix_get_row(uint8_t row)
  120. {
  121. return matrix[row];
  122. }
  123. void matrix_print(void)
  124. {
  125. xprintf("\nr/c 01234567\n");
  126. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  127. xprintf("%X0: ", row);
  128. matrix_row_t data = matrix_get_row(row);
  129. for (int col = 0; col < MATRIX_COLS; col++) {
  130. if (data & (1<<col))
  131. xprintf("1");
  132. else
  133. xprintf("0");
  134. }
  135. xprintf("\n");
  136. }
  137. }
  138. void matrix_set_remote(matrix_row_t* rows, uint8_t index) {
  139. uint8_t offset = 0;
  140. #ifdef MASTER_IS_ON_RIGHT
  141. offset = MATRIX_ROWS - LOCAL_MATRIX_ROWS * (index + 2);
  142. #else
  143. offset = LOCAL_MATRIX_ROWS * (index + 1);
  144. #endif
  145. for (int row = 0; row < LOCAL_MATRIX_ROWS; row++) {
  146. matrix[offset + row] = rows[row];
  147. }
  148. }