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.

173 lines
5.4 KiB

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