2 Copyright 2018 Massdrop Inc.
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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 matrix_row_t mlatest[MATRIX_ROWS];
26 matrix_row_t mlast[MATRIX_ROWS];
27 matrix_row_t mdebounced[MATRIX_ROWS];
29 uint8_t row_ports[] = { MATRIX_ROW_PORTS };
30 uint8_t row_pins[] = { MATRIX_ROW_PINS };
31 uint8_t col_ports[] = { MATRIX_COL_PORTS };
32 uint8_t col_pins[] = { MATRIX_COL_PINS };
33 uint32_t row_masks[2]; //NOTE: If more than PA PB used in the future, adjust code to accomodate
35 __attribute__ ((weak))
36 void matrix_init_kb(void) {
40 __attribute__ ((weak))
41 void matrix_scan_kb(void) {
45 __attribute__ ((weak))
46 void matrix_init_user(void) {
49 __attribute__ ((weak))
50 void matrix_scan_user(void) {
53 void matrix_init(void)
55 memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t));
56 memset(mlast, 0, MATRIX_ROWS * sizeof(matrix_row_t));
57 memset(mdebounced, 0, MATRIX_ROWS * sizeof(matrix_row_t));
63 for (row = 0; row < MATRIX_ROWS; row++)
65 PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; //Input
66 PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; //Low
67 PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; //Input Enable,
68 PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; //Pull Enable
69 row_masks[row_ports[row]] |= 1 << row_pins[row]; //Add pin to proper row mask
73 for (col = 0; col < MATRIX_COLS; col++)
75 PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; //Output
76 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Low
79 matrix_init_quantum();
82 uint64_t mdebouncing = 0;
83 uint8_t matrix_scan(void)
88 uint32_t scans[2]; //PA PB
90 if (timer_read64() < mdebouncing) return 1; //mdebouncing == 0 when no debouncing active
92 memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer
94 for (col = 0; col < MATRIX_COLS; col++)
96 PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output
98 wait_us(1); //Delay for output
100 scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data
101 scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PB row pins data
103 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output
105 for (row = 0; row < MATRIX_ROWS; row++)
107 //Move scan bits from scans array into proper row bit locations
108 if (scans[row_ports[row]] & (1 << row_pins[row]))
109 mlatest[row] |= 1 << col;
113 mchanged = 0; //Default to no matrix change since last
115 for (row = 0; row < MATRIX_ROWS; row++)
117 if (mlast[row] != mlatest[row])
119 mlast[row] = mlatest[row];
124 for (row = 0; row < MATRIX_ROWS; row++)
125 mdebounced[row] = mlatest[row];
130 //Begin or extend debounce on change
131 mdebouncing = timer_read64() + DEBOUNCING_DELAY;
134 matrix_scan_quantum();
139 matrix_row_t matrix_get_row(uint8_t row)
141 return mdebounced[row];
144 void matrix_print(void)
146 char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C";
152 for (cols = 1; cols <= MATRIX_COLS; cols++)
154 *pbuf = (cols%10)+48;
157 *pbuf = '\r'; pbuf++;
158 *pbuf = '\n'; pbuf++;
160 for (rows = 1; rows <= MATRIX_ROWS; rows++)
162 row = matrix_get_row(rows-1);
163 if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; }
164 else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; }
165 for (cols = 0; cols < MATRIX_COLS; cols++)
167 if (row & 1 << cols) *pbuf = 'X';
171 *pbuf = '\r'; pbuf++;
172 *pbuf = '\n'; pbuf++;