aboutsummaryrefslogtreecommitdiff
path: root/keyboards/stm32_f103_onekey/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/stm32_f103_onekey/matrix.c')
-rw-r--r--keyboards/stm32_f103_onekey/matrix.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/keyboards/stm32_f103_onekey/matrix.c b/keyboards/stm32_f103_onekey/matrix.c
new file mode 100644
index 000000000..ea9d8d057
--- /dev/null
+++ b/keyboards/stm32_f103_onekey/matrix.c
@@ -0,0 +1,177 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "ch.h"
19#include "hal.h"
20
21/*
22 * scan matrix
23 */
24#include "print.h"
25#include "debug.h"
26#include "util.h"
27#include "matrix.h"
28#include "wait.h"
29
30#ifndef DEBOUNCE
31# define DEBOUNCE 5
32#endif
33static uint8_t debouncing = DEBOUNCE;
34
35/* matrix state(1:on, 0:off) */
36static matrix_row_t matrix[MATRIX_ROWS];
37static matrix_row_t matrix_debouncing[MATRIX_ROWS];
38
39static matrix_row_t read_cols(void);
40static void init_cols(void);
41static void unselect_rows(void);
42static void select_row(uint8_t row);
43
44
45inline
46uint8_t matrix_rows(void)
47{
48 return MATRIX_ROWS;
49}
50
51inline
52uint8_t matrix_cols(void)
53{
54 return MATRIX_COLS;
55}
56
57/* generic STM32F103C8T6 board */
58#ifdef BOARD_GENERIC_STM32_F103
59#define LED_ON() do { palClearPad(GPIOC, GPIOC_LED) ;} while (0)
60#define LED_OFF() do { palSetPad(GPIOC, GPIOC_LED); } while (0)
61#define LED_TGL() do { palTogglePad(GPIOC, GPIOC_LED); } while (0)
62#endif
63
64/* Maple Mini */
65#ifdef BOARD_MAPLEMINI_STM32_F103
66#define LED_ON() do { palSetPad(GPIOB, 1) ;} while (0)
67#define LED_OFF() do { palClearPad(GPIOB, 1); } while (0)
68#define LED_TGL() do { palTogglePad(GPIOB, 1); } while (0)
69#endif
70
71void matrix_init(void)
72{
73 // initialize row and col
74 unselect_rows();
75 init_cols();
76
77 // initialize matrix state: all keys off
78 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
79 matrix[i] = 0;
80 matrix_debouncing[i] = 0;
81 }
82
83 //debug
84 debug_matrix = true;
85 LED_ON();
86 wait_ms(500);
87 LED_OFF();
88}
89
90uint8_t matrix_scan(void)
91{
92 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
93 select_row(i);
94 wait_us(30); // without this wait read unstable value.
95 matrix_row_t cols = read_cols();
96 if (matrix_debouncing[i] != cols) {
97 matrix_debouncing[i] = cols;
98 if (debouncing) {
99 debug("bounce!: "); debug_hex(debouncing); debug("\n");
100 }
101 debouncing = DEBOUNCE;
102 }
103 unselect_rows();
104 }
105
106 if (debouncing) {
107 if (--debouncing) {
108 wait_ms(1);
109 } else {
110 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
111 matrix[i] = matrix_debouncing[i];
112 }
113 }
114 }
115
116 return 1;
117}
118
119inline
120bool matrix_is_on(uint8_t row, uint8_t col)
121{
122 return (matrix[row] & ((matrix_row_t)1<<col));
123}
124
125inline
126matrix_row_t matrix_get_row(uint8_t row)
127{
128 return matrix[row];
129}
130
131void matrix_print(void)
132{
133 print("\nr/c 0123456789ABCDEF\n");
134 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
135 phex(row); print(": ");
136 pbin_reverse16(matrix_get_row(row));
137 print("\n");
138 }
139}
140
141/* Column pin configuration
142 */
143static void init_cols(void)
144{
145#ifdef BOARD_MAPLEMINI_STM32_F103
146 // don't need pullup/down, since it's pulled down in hardware
147 palSetPadMode(GPIOB, 8, PAL_MODE_INPUT);
148#else
149 palSetPadMode(GPIOB, 8, PAL_MODE_INPUT_PULLDOWN);
150#endif
151}
152
153/* Returns status of switches(1:on, 0:off) */
154static matrix_row_t read_cols(void)
155{
156 return ((palReadPad(GPIOB, 8)==PAL_LOW) ? 0 : (1<<0));
157 // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
158}
159
160/* Row pin configuration
161 */
162static void unselect_rows(void)
163{
164 // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_INPUT); // hi-Z
165}
166
167static void select_row(uint8_t row)
168{
169 (void)row;
170 // Output low to select
171 // switch (row) {
172 // case 0:
173 // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_OUTPUT_PUSHPULL);
174 // palSetPad(GPIOA, GPIOA_PIN10, PAL_LOW);
175 // break;
176 // }
177}