aboutsummaryrefslogtreecommitdiff
path: root/keyboards/massdrop/alt/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/massdrop/alt/matrix.c')
-rw-r--r--keyboards/massdrop/alt/matrix.c178
1 files changed, 36 insertions, 142 deletions
diff --git a/keyboards/massdrop/alt/matrix.c b/keyboards/massdrop/alt/matrix.c
index f635f37f2..181f223e4 100644
--- a/keyboards/massdrop/alt/matrix.c
+++ b/keyboards/massdrop/alt/matrix.c
@@ -16,167 +16,61 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "alt.h" 18#include "alt.h"
19
20#include "d51_util.h" 19#include "d51_util.h"
21#include "debug.h"
22#include "clks.h"
23#include <string.h>
24
25#ifndef MATRIX_IO_DELAY
26# define MATRIX_IO_DELAY 1
27#endif
28
29matrix_row_t mlatest[MATRIX_ROWS];
30matrix_row_t mlast[MATRIX_ROWS];
31matrix_row_t mdebounced[MATRIX_ROWS];
32
33uint8_t row_ports[] = { MATRIX_ROW_PORTS };
34uint8_t row_pins[] = { MATRIX_ROW_PINS };
35uint8_t col_ports[] = { MATRIX_COL_PORTS };
36uint8_t col_pins[] = { MATRIX_COL_PINS };
37uint32_t row_masks[2]; //NOTE: If more than PA PB used in the future, adjust code to accomodate
38 20
39__attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } 21const uint8_t row_ports[] = {MATRIX_ROW_PORTS};
40 22const uint8_t row_pins[] = {MATRIX_ROW_PINS};
41__attribute__ ((weak)) 23const uint8_t col_ports[] = {MATRIX_COL_PORTS};
42void matrix_init_kb(void) { 24const uint8_t col_pins[] = {MATRIX_COL_PINS};
43 matrix_init_user(); 25uint32_t row_masks[2]; // NOTE: If more than PA PB used in the future, adjust code to accommodate
44}
45
46__attribute__ ((weak))
47void matrix_scan_kb(void) {
48 matrix_scan_user();
49}
50
51__attribute__ ((weak))
52void matrix_init_user(void) {
53}
54
55__attribute__ ((weak))
56void matrix_scan_user(void) {
57}
58
59void matrix_init(void)
60{
61 memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t));
62 memset(mlast, 0, MATRIX_ROWS * sizeof(matrix_row_t));
63 memset(mdebounced, 0, MATRIX_ROWS * sizeof(matrix_row_t));
64 26
27void matrix_init_custom(void) {
65 row_masks[PA] = 0; 28 row_masks[PA] = 0;
66 row_masks[PB] = 0; 29 row_masks[PB] = 0;
67 30
68 uint8_t row; 31 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
69 for (row = 0; row < MATRIX_ROWS; row++) 32 PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; // Input
70 { 33 PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; // Low
71 PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; //Input 34 PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; // Input Enable,
72 PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; //Low 35 PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; // Pull Enable
73 PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; //Input Enable, 36 row_masks[row_ports[row]] |= 1 << row_pins[row]; // Add pin to proper row mask
74 PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; //Pull Enable
75 row_masks[row_ports[row]] |= 1 << row_pins[row]; //Add pin to proper row mask
76 } 37 }
77 38
78 uint8_t col; 39 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
79 for (col = 0; col < MATRIX_COLS; col++) 40 PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; // Output
80 { 41 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; // Low
81 PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; //Output
82 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Low
83 } 42 }
84
85 matrix_init_quantum();
86} 43}
87 44
88uint64_t mdebouncing = 0; 45bool matrix_scan_custom(matrix_row_t current_matrix[]) {
89uint8_t matrix_scan(void) 46 matrix_row_t raw[MATRIX_ROWS] = {0};
90{ 47 uint32_t scans[2]; // PA PB
91 uint8_t mchanged;
92 uint8_t row;
93 uint8_t col;
94 uint32_t scans[2]; //PA PB
95
96 if (timer_read64() < mdebouncing) return 1; //mdebouncing == 0 when no debouncing active
97 48
98 memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer 49 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
50 PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; // Set col output
99 51
100 for (col = 0; col < MATRIX_COLS; col++) 52 matrix_io_delay(); // Delay for output
101 {
102 PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output
103 53
104 matrix_io_delay(); //Delay for output 54 scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; // Read PA row pins data
55 scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; // Read PB row pins data
105 56
106 scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data 57 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; // Clear col output
107 scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PB row pins data
108 58
109 PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output 59 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
110 60 // Move scan bits from scans array into proper row bit locations
111 for (row = 0; row < MATRIX_ROWS; row++) 61 if (scans[row_ports[row]] & (1 << row_pins[row])) {
112 { 62 raw[row] |= 1 << col;
113 //Move scan bits from scans array into proper row bit locations 63 }
114 if (scans[row_ports[row]] & (1 << row_pins[row]))
115 mlatest[row] |= 1 << col;
116 } 64 }
117 } 65 }
118 66
119 mchanged = 0; //Default to no matrix change since last 67 bool changed = false;
120 68 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
121 for (row = 0; row < MATRIX_ROWS; row++) 69 if (current_matrix[row] != raw[row]) {
122 { 70 current_matrix[row] = raw[row];
123 if (mlast[row] != mlatest[row]) 71 changed = true;
124 mchanged = 1;
125 mlast[row] = mlatest[row];
126 }
127
128 if (!mchanged)
129 {
130 for (row = 0; row < MATRIX_ROWS; row++)
131 mdebounced[row] = mlatest[row];
132 mdebouncing = 0;
133 }
134 else
135 {
136 //Begin or extend debounce on change
137 mdebouncing = timer_read64() + DEBOUNCE;
138 }
139
140 matrix_scan_quantum();
141
142 return 1;
143}
144
145matrix_row_t matrix_get_row(uint8_t row)
146{
147 return mdebounced[row];
148}
149
150void matrix_print(void)
151{
152 char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C";
153 char *pbuf = buf+3;
154 uint32_t cols;
155 uint32_t rows;
156 matrix_row_t row;
157
158 for (cols = 1; cols <= MATRIX_COLS; cols++)
159 {
160 *pbuf = (cols%10)+48;
161 pbuf++;
162 }
163 *pbuf = '\r'; pbuf++;
164 *pbuf = '\n'; pbuf++;
165
166 for (rows = 1; rows <= MATRIX_ROWS; rows++)
167 {
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++)
172 {
173 if (row & 1 << cols) *pbuf = 'X';
174 else *pbuf = '.';
175 pbuf++;
176 } 72 }
177 *pbuf = '\r'; pbuf++;
178 *pbuf = '\n'; pbuf++;
179 } 73 }
180 *pbuf = 0; 74
181 dprint(buf); 75 return changed;
182} 76}