matrix.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright 2018 Massdrop Inc.
  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 "ctrl.h"
  15. #include "d51_util.h"
  16. #include "debug.h"
  17. #include "clks.h"
  18. #include <string.h>
  19. matrix_row_t mlatest[MATRIX_ROWS];
  20. matrix_row_t mlast[MATRIX_ROWS];
  21. matrix_row_t mdebounced[MATRIX_ROWS];
  22. uint8_t row_ports[] = { MATRIX_ROW_PORTS };
  23. uint8_t row_pins[] = { MATRIX_ROW_PINS };
  24. uint8_t col_ports[] = { MATRIX_COL_PORTS };
  25. uint8_t col_pins[] = { MATRIX_COL_PINS };
  26. uint32_t row_masks[2]; //NOTE: If more than PA PB used in the future, adjust code to accomodate
  27. __attribute__ ((weak))
  28. void matrix_init_kb(void) {
  29. matrix_init_user();
  30. }
  31. __attribute__ ((weak))
  32. void matrix_scan_kb(void) {
  33. matrix_scan_user();
  34. }
  35. __attribute__ ((weak))
  36. void matrix_init_user(void) {
  37. }
  38. __attribute__ ((weak))
  39. void matrix_scan_user(void) {
  40. }
  41. void matrix_init(void)
  42. {
  43. memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  44. memset(mlast, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  45. memset(mdebounced, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  46. row_masks[PA] = 0;
  47. row_masks[PB] = 0;
  48. uint8_t row;
  49. for (row = 0; row < MATRIX_ROWS; row++)
  50. {
  51. PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; //Input
  52. PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; //Low
  53. PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; //Input Enable,
  54. PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; //Pull Enable
  55. row_masks[row_ports[row]] |= 1 << row_pins[row]; //Add pin to proper row mask
  56. }
  57. uint8_t col;
  58. for (col = 0; col < MATRIX_COLS; col++)
  59. {
  60. PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; //Output
  61. PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Low
  62. }
  63. matrix_init_quantum();
  64. }
  65. uint64_t mdebouncing = 0;
  66. uint8_t matrix_scan(void)
  67. {
  68. uint8_t mchanged;
  69. uint8_t row;
  70. uint8_t col;
  71. uint32_t scans[2]; //PA PB
  72. if (timer_read64() < mdebouncing) return 1; //mdebouncing == 0 when no debouncing active
  73. memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer
  74. for (col = 0; col < MATRIX_COLS; col++)
  75. {
  76. PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output
  77. wait_us(1); //Delay for output
  78. scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data
  79. scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PB row pins data
  80. PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output
  81. for (row = 0; row < MATRIX_ROWS; row++)
  82. {
  83. //Move scan bits from scans array into proper row bit locations
  84. if (scans[row_ports[row]] & (1 << row_pins[row]))
  85. mlatest[row] |= 1 << col;
  86. }
  87. }
  88. mchanged = 0; //Default to no matrix change since last
  89. for (row = 0; row < MATRIX_ROWS; row++)
  90. {
  91. if (mlast[row] != mlatest[row])
  92. mchanged = 1;
  93. mlast[row] = mlatest[row];
  94. }
  95. if (!mchanged)
  96. {
  97. for (row = 0; row < MATRIX_ROWS; row++)
  98. mdebounced[row] = mlatest[row];
  99. mdebouncing = 0;
  100. }
  101. else
  102. {
  103. //Begin or extend debounce on change
  104. mdebouncing = timer_read64() + DEBOUNCE;
  105. }
  106. matrix_scan_quantum();
  107. return 1;
  108. }
  109. matrix_row_t matrix_get_row(uint8_t row)
  110. {
  111. return mdebounced[row];
  112. }
  113. void matrix_print(void)
  114. {
  115. char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C";
  116. char *pbuf = buf+3;
  117. uint32_t cols;
  118. uint32_t rows;
  119. matrix_row_t row;
  120. for (cols = 1; cols <= MATRIX_COLS; cols++)
  121. {
  122. *pbuf = (cols%10)+48;
  123. pbuf++;
  124. }
  125. *pbuf = '\r'; pbuf++;
  126. *pbuf = '\n'; pbuf++;
  127. for (rows = 1; rows <= MATRIX_ROWS; rows++)
  128. {
  129. row = matrix_get_row(rows-1);
  130. if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; }
  131. else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; }
  132. for (cols = 0; cols < MATRIX_COLS; cols++)
  133. {
  134. if (row & 1 << cols) *pbuf = 'X';
  135. else *pbuf = '.';
  136. pbuf++;
  137. }
  138. *pbuf = '\r'; pbuf++;
  139. *pbuf = '\n'; pbuf++;
  140. }
  141. *pbuf = 0;
  142. dprint(buf);
  143. }