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 #define MATRIX_SCAN_DELAY 10 //Delay after setting a col to output (in us)
84 uint64_t mdebouncing = 0;
85 uint8_t matrix_scan(void)
90 uint32_t scans[2]; //PA PB
92 if (CLK_get_ms() < mdebouncing) return 1; //mdebouncing == 0 when no debouncing active
94 //m15_off; //Profiling scans
96 memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer
98 for (col = 0; col < MATRIX_COLS; col++)
100 PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output
102 CLK_delay_us(MATRIX_SCAN_DELAY); //Delay for output
104 scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data
105 scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PB row pins data
107 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output
109 for (row = 0; row < MATRIX_ROWS; row++)
111 //Move scan bits from scans array into proper row bit locations
112 if (scans[row_ports[row]] & (1 << row_pins[row]))
113 mlatest[row] |= 1 << col;
117 mchanged = 0; //Default to no matrix change since last
119 for (row = 0; row < MATRIX_ROWS; row++)
121 if (mlast[row] != mlatest[row])
123 mlast[row] = mlatest[row];
128 for (row = 0; row < MATRIX_ROWS; row++)
129 mdebounced[row] = mlatest[row];
134 //Begin or extend debounce on change
135 mdebouncing = CLK_get_ms() + DEBOUNCING_DELAY;
138 //m15_on; //Profiling scans
140 matrix_scan_quantum();
145 matrix_row_t matrix_get_row(uint8_t row)
147 return mdebounced[row];
150 void matrix_print(void)
152 char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C";
158 for (cols = 1; cols <= MATRIX_COLS; cols++)
160 *pbuf = (cols%10)+48;
163 *pbuf = '\r'; pbuf++;
164 *pbuf = '\n'; pbuf++;
166 for (rows = 1; rows <= MATRIX_ROWS; rows++)
168 row = matrix_get_row(rows-1);
169 if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; }
170 else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; }
171 for (cols = 0; cols < MATRIX_COLS; cols++)
173 if (row & 1 << cols) *pbuf = 'X';
177 *pbuf = '\r'; pbuf++;
178 *pbuf = '\n'; pbuf++;