Commit | Line | Data |
---|---|---|
332fa7c4 PA |
1 | /* Copyright 2020 Purdea Andrei |
2 | * | |
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 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | */ | |
16 | ||
17 | #include "quantum.h" | |
18 | ||
405cc8cf PA |
19 | #define DAC_SCLK B1 |
20 | #define DAC_DIN B2 | |
21 | #define DAC_SYNC_N B0 | |
22 | ||
23 | #define SHIFT_DIN D4 | |
24 | #define SHIFT_OE D5 | |
25 | #define SHIFT_STCP D6 | |
26 | #define SHIFT_SHCP D7 | |
27 | ||
28 | #define KEYBOARD_SETTLE_TIME_US 8 | |
29 | #define DAC_SETTLE_TIME_US 8 | |
30 | ||
31 | #define REPS 15 | |
32 | ||
33 | inline uint8_t read_rows(void) | |
34 | { | |
35 | uint8_t pc = PINC; | |
36 | uint8_t pd = PIND; | |
37 | return (pc >> 4) | (pd << 4); | |
38 | } | |
39 | ||
40 | void dac_init(void) | |
41 | { | |
42 | setPinOutput(DAC_SCLK); | |
43 | setPinOutput(DAC_DIN); | |
44 | setPinOutput(DAC_SYNC_N); | |
45 | writePin(DAC_SYNC_N, 1); | |
46 | writePin(DAC_SCLK, 0); | |
47 | writePin(DAC_SCLK, 1); | |
48 | writePin(DAC_SCLK, 0); | |
49 | } | |
50 | ||
51 | void dac_write_threshold(uint16_t value) | |
52 | { | |
53 | writePin(DAC_SYNC_N, 0); | |
54 | int i; | |
55 | for (i=0;i<16;i++) | |
56 | { | |
57 | writePin(DAC_DIN, (value >> 15) & 1); | |
58 | value <<= 1; | |
59 | writePin(DAC_SCLK, 1); | |
60 | writePin(DAC_SCLK, 0); | |
61 | } | |
62 | writePin(DAC_SYNC_N, 1); | |
63 | writePin(DAC_SCLK, 1); | |
64 | writePin(DAC_SCLK, 0); | |
65 | wait_us(DAC_SETTLE_TIME_US); | |
66 | } | |
67 | ||
68 | void shift_select_nothing(void) | |
69 | { | |
70 | writePin(SHIFT_DIN, 0); | |
71 | int i; | |
72 | for (i=0;i<16;i++) | |
73 | { | |
74 | writePin(SHIFT_SHCP, 1); | |
75 | writePin(SHIFT_SHCP, 0); | |
76 | } | |
77 | writePin(SHIFT_STCP, 1); | |
78 | writePin(SHIFT_STCP, 0); | |
79 | } | |
80 | ||
81 | void shift_select_col(uint8_t col) | |
82 | { | |
83 | int i; | |
84 | for (i=15; i>=0; i--) | |
85 | { | |
86 | writePin(SHIFT_DIN, !!(col == i)); | |
87 | writePin(SHIFT_SHCP, 1); | |
88 | writePin(SHIFT_SHCP, 0); | |
89 | } | |
90 | writePin(SHIFT_STCP, 1); | |
91 | writePin(SHIFT_STCP, 0); | |
92 | } | |
93 | ||
94 | void shift_init(void) | |
95 | { | |
96 | setPinOutput(SHIFT_DIN); | |
97 | setPinOutput(SHIFT_OE); | |
98 | setPinOutput(SHIFT_STCP); | |
99 | setPinOutput(SHIFT_SHCP); | |
100 | writePin(SHIFT_STCP, 0); | |
101 | writePin(SHIFT_SHCP, 0); | |
102 | shift_select_nothing(); | |
103 | wait_us(KEYBOARD_SETTLE_TIME_US); | |
104 | } | |
105 | ||
106 | ||
107 | void test_col(uint8_t col, uint8_t *array) | |
108 | { | |
109 | cli(); | |
110 | shift_select_col(col); | |
111 | int i; | |
112 | for (i=0;i<16;i++) | |
113 | { | |
114 | uint8_t data = read_rows(); | |
115 | array[i] = data; | |
116 | } | |
117 | sei(); | |
118 | shift_select_nothing(); | |
119 | wait_us(KEYBOARD_SETTLE_TIME_US); | |
120 | } | |
121 | ||
122 | void test_col_print_data(uint8_t col) | |
123 | { | |
124 | uprintf("%d: ", col); | |
125 | uint8_t data[16]; | |
126 | uint8_t sums[16 * 8]; | |
127 | uint8_t i; | |
128 | for (i=0;i<16*8;i++) | |
129 | { | |
130 | sums[i] = 0; | |
131 | } | |
132 | for (i=0;i<REPS;i++) | |
133 | { | |
134 | test_col(col, data); | |
135 | uint8_t j; | |
136 | uint8_t ii = 0; | |
137 | for (j=0;j<16;j++) | |
138 | { | |
139 | uint8_t dataj = data[j]; | |
140 | uint8_t k; | |
141 | for (k=0; k<8;k++) | |
142 | { | |
143 | ii += 1; | |
144 | sums[ii] += (dataj & 1); | |
145 | dataj >>= 1; | |
146 | } | |
147 | } | |
148 | } | |
149 | for (i=0;i<16*8;i++) | |
150 | { | |
151 | uprintf("%X", sums[i]); | |
152 | } | |
153 | print("\n"); | |
154 | } | |
155 | ||
332fa7c4 PA |
156 | void matrix_init_custom(void) { |
157 | // TODO: initialize hardware here | |
405cc8cf PA |
158 | int i; |
159 | for (i=7;i>0;i--) { | |
160 | uprintf("Starting test in %d\n", i); | |
161 | wait_ms(1000); | |
162 | } | |
163 | uprintf("shift_init()"); | |
164 | shift_init(); | |
165 | uprintf(" DONE\n"); | |
166 | uprintf("dac_init()"); | |
167 | dac_init(); | |
168 | uprintf(" DONE\n"); | |
169 | int d; | |
170 | for (d=0;d<1024;d++) | |
171 | { | |
172 | uprintf("Testing threshold: %d\n", d); | |
173 | dac_write_threshold(d); | |
174 | int c; | |
175 | for (c=0; c<10;c++) | |
176 | { | |
177 | test_col_print_data(c); | |
178 | } | |
179 | test_col_print_data(15); | |
180 | } | |
181 | uprintf("TEST DONE\n"); | |
182 | while(1); | |
332fa7c4 PA |
183 | } |
184 | ||
185 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | |
186 | bool matrix_has_changed = false; | |
405cc8cf PA |
187 | /* current_matrix[0] ^= 1; |
188 | uprintf("test %d\n", current_matrix[0]); | |
189 | bool matrix_has_changed = true; | |
190 | */ | |
332fa7c4 PA |
191 | // TODO: add matrix scanning routine here |
192 | ||
193 | return matrix_has_changed; | |
194 | } |