matrix.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #define MATRIX_SCAN_DELAY 10 //Delay after setting a col to output (in us)
  66. uint64_t mdebouncing = 0;
  67. uint8_t matrix_scan(void)
  68. {
  69. uint8_t mchanged;
  70. uint8_t row;
  71. uint8_t col;
  72. uint32_t scans[2]; //PA PB
  73. if (CLK_get_ms() < mdebouncing) return 1; //mdebouncing == 0 when no debouncing active
  74. //DBG_1_OFF; //Profiling scans
  75. memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer
  76. for (col = 0; col < MATRIX_COLS; col++)
  77. {
  78. PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output
  79. CLK_delay_us(MATRIX_SCAN_DELAY); //Delay for output
  80. scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data
  81. scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PB row pins data
  82. PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output
  83. for (row = 0; row < MATRIX_ROWS; row++)
  84. {
  85. //Move scan bits from scans array into proper row bit locations
  86. if (scans[row_ports[row]] & (1 << row_pins[row]))
  87. mlatest[row] |= 1 << col;
  88. }
  89. }
  90. mchanged = 0; //Default to no matrix change since last
  91. for (row = 0; row < MATRIX_ROWS; row++)
  92. {
  93. if (mlast[row] != mlatest[row])
  94. mchanged = 1;
  95. mlast[row] = mlatest[row];
  96. }
  97. if (!mchanged)
  98. {
  99. for (row = 0; row < MATRIX_ROWS; row++)
  100. mdebounced[row] = mlatest[row];
  101. mdebouncing = 0;
  102. }
  103. else
  104. {
  105. //Begin or extend debounce on change
  106. mdebouncing = CLK_get_ms() + DEBOUNCING_DELAY;
  107. }
  108. //DBG_1_ON; //Profiling scans
  109. matrix_scan_quantum();
  110. return 1;
  111. }
  112. matrix_row_t matrix_get_row(uint8_t row)
  113. {
  114. return mdebounced[row];
  115. }
  116. void matrix_print(void)
  117. {
  118. char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C";
  119. char *pbuf = buf+3;
  120. uint32_t cols;
  121. uint32_t rows;
  122. matrix_row_t row;
  123. for (cols = 1; cols <= MATRIX_COLS; cols++)
  124. {
  125. *pbuf = (cols%10)+48;
  126. pbuf++;
  127. }
  128. *pbuf = '\r'; pbuf++;
  129. *pbuf = '\n'; pbuf++;
  130. for (rows = 1; rows <= MATRIX_ROWS; rows++)
  131. {
  132. row = matrix_get_row(rows-1);
  133. if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; }
  134. else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; }
  135. for (cols = 0; cols < MATRIX_COLS; cols++)
  136. {
  137. if (row & 1 << cols) *pbuf = 'X';
  138. else *pbuf = '.';
  139. pbuf++;
  140. }
  141. *pbuf = '\r'; pbuf++;
  142. *pbuf = '\n'; pbuf++;
  143. }
  144. *pbuf = 0;
  145. dprint(buf);
  146. }