2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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/>.
35 static uint8_t debouncing = DEBOUNCE;
37 /* matrix state(1:on, 0:off) */
38 static matrix_row_t matrix[MATRIX_ROWS];
39 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
41 static matrix_row_t read_cols(void);
42 static void init_cols(void);
43 static void unselect_rows(void);
44 static void select_row(uint8_t row);
46 inline uint8_t matrix_rows(void){
50 inline uint8_t matrix_cols(void){
54 /* generic STM32F103C8T6 board */
55 #ifdef BOARD_GENERIC_STM32_F103
56 // This could be removed, only used now in matrix_init()
57 #define LED_ON() do { palClearPad(GPIOA, 1) ;} while (0)
58 #define LED_OFF() do { palSetPad(GPIOA, 1); } while (0)
61 __attribute__ ((weak))
62 void matrix_init_kb(void) {
66 __attribute__ ((weak))
67 void matrix_scan_kb(void) {
71 __attribute__ ((weak))
72 void matrix_init_user(void) {
75 __attribute__ ((weak))
76 void matrix_scan_user(void) {
79 void matrix_init(void)
81 // initialize row and col
84 // initialize matrix state: all keys off
85 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
87 matrix_debouncing[i] = 0;
95 matrix_init_quantum();
98 uint8_t matrix_scan(void){
99 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 wait_us(30); // without this wait read unstable value.
102 matrix_row_t cols = read_cols();
103 if (matrix_debouncing[i] != cols) {
104 matrix_debouncing[i] = cols;
106 debug("bounce!: "); debug_hex(debouncing); debug("\n");
108 debouncing = DEBOUNCE;
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 matrix[i] = matrix_debouncing[i];
122 matrix_scan_quantum();
126 inline bool matrix_is_on(uint8_t row, uint8_t col){
127 return (matrix[row] & ((matrix_row_t)1<<col));
130 inline matrix_row_t matrix_get_row(uint8_t row){
134 void matrix_print(void){
135 print("\nr/c 0123456789ABCDEF\n");
136 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
137 phex(row); print(": ");
138 pbin_reverse16(matrix_get_row(row));
143 /* Column pin configuration
145 // Modified by Xydane
146 static void init_cols(void){
147 palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_PULLUP);
148 palSetPadMode(GPIOA, 15, PAL_MODE_INPUT_PULLUP);
149 palSetPadMode(GPIOA, 10, PAL_MODE_INPUT_PULLUP);
150 palSetPadMode(GPIOA, 9, PAL_MODE_INPUT_PULLUP);
151 palSetPadMode(GPIOA, 8, PAL_MODE_INPUT_PULLUP);
152 palSetPadMode(GPIOB, 15, PAL_MODE_INPUT_PULLUP);
153 palSetPadMode(GPIOB, 14, PAL_MODE_INPUT_PULLUP);
154 palSetPadMode(GPIOB, 13, PAL_MODE_INPUT_PULLUP);
155 palSetPadMode(GPIOB, 12, PAL_MODE_INPUT_PULLUP);
156 palSetPadMode(GPIOB, 11, PAL_MODE_INPUT_PULLUP);
157 palSetPadMode(GPIOB, 10, PAL_MODE_INPUT_PULLUP);
158 palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLUP);
159 palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLUP);
160 palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLUP);
161 palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLUP);
164 /* Returns status of switches(1:on, 0:off) */
165 // Modified by Xydane
166 static matrix_row_t read_cols(void){
167 return ((palReadPad(GPIOA, 5)==PAL_HIGH) ? 0 : (1<<0))
168 | ((palReadPad(GPIOA, 15)==PAL_HIGH) ? 0 : (1<<1))
169 | ((palReadPad(GPIOA, 10)==PAL_HIGH) ? 0 : (1<<2))
170 | ((palReadPad(GPIOA, 9)==PAL_HIGH) ? 0 : (1<<3))
171 | ((palReadPad(GPIOA, 8)==PAL_HIGH) ? 0 : (1<<4))
172 | ((palReadPad(GPIOB, 15)==PAL_HIGH) ? 0 : (1<<5))
173 | ((palReadPad(GPIOB, 14)==PAL_HIGH) ? 0 : (1<<6))
174 | ((palReadPad(GPIOB, 13)==PAL_HIGH) ? 0 : (1<<7))
175 | ((palReadPad(GPIOB, 12)==PAL_HIGH) ? 0 : (1<<8))
176 | ((palReadPad(GPIOB, 11)==PAL_HIGH) ? 0 : (1<<9))
177 | ((palReadPad(GPIOB, 10)==PAL_HIGH) ? 0 : (1<<10))
178 | ((palReadPad(GPIOB, 1)==PAL_HIGH) ? 0 : (1<<11))
179 | ((palReadPad(GPIOB, 0)==PAL_HIGH) ? 0 : (1<<12))
180 | ((palReadPad(GPIOA, 7)==PAL_HIGH) ? 0 : (1<<13))
181 | ((palReadPad(GPIOA, 6)==PAL_HIGH) ? 0 : (1<<14));
184 /* Row pin configuration
186 // Modified by Xydane
187 static void unselect_rows(void){
188 palSetPadMode(GPIOB, 9, PAL_MODE_INPUT);
189 palSetPadMode(GPIOB, 8, PAL_MODE_INPUT);
190 palSetPadMode(GPIOB, 7, PAL_MODE_INPUT);
191 palSetPadMode(GPIOB, 6, PAL_MODE_INPUT);
192 palSetPadMode(GPIOB, 5, PAL_MODE_INPUT);
193 palSetPadMode(GPIOA, 4, PAL_MODE_INPUT);
196 // Modified by Xydane
197 static void select_row(uint8_t row){
201 palSetPadMode(GPIOB, 9, PAL_MODE_OUTPUT_PUSHPULL);
202 palClearPad(GPIOB, 9);
205 palSetPadMode(GPIOB, 8, PAL_MODE_OUTPUT_PUSHPULL);
206 palClearPad(GPIOB, 8);
209 palSetPadMode(GPIOB, 7, PAL_MODE_OUTPUT_PUSHPULL);
210 palClearPad(GPIOB, 7);
213 palSetPadMode(GPIOB, 6, PAL_MODE_OUTPUT_PUSHPULL);
214 palClearPad(GPIOB, 6);
217 palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL);
218 palClearPad(GPIOB, 5);
221 palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
222 palClearPad(GPIOA, 4);