]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hid_liber/matrix.c
Disable JTAG function for hid_liber to use PORTF.
[tmk_firmware.git] / keyboard / hid_liber / matrix.c
1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
2  *
3  * This is heavily based on hid_liber/board.{c|h}.
4  * https://github.com/BathroomEpiphanies/AVR-Keyboard
5  *
6  * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7  * http://bathroomepiphanies.com
8  *
9  * As for liscensing consult with the original files or its author.
10  */
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include <avr/io.h>
14 #include <util/delay.h>
15 #include "print.h"
16 #include "debug.h"
17 #include "util.h"
18 #include "matrix.h"
19
20
21 #ifndef DEBOUNCE
22 #   define DEBOUNCE     5
23 #endif
24 static uint8_t debouncing = DEBOUNCE;
25
26 // bit array of key state(1:on, 0:off)
27 static matrix_row_t *matrix;
28 static matrix_row_t *matrix_debounced;
29 static matrix_row_t _matrix0[MATRIX_ROWS];
30 static matrix_row_t _matrix1[MATRIX_ROWS];
31
32
33 #define NROW 18
34 #define NCOL 8
35 #define _DDRA (uint8_t *const)&DDRA
36 #define _DDRB (uint8_t *const)&DDRB
37 #define _DDRC (uint8_t *const)&DDRC
38 #define _DDRD (uint8_t *const)&DDRD
39 #define _DDRE (uint8_t *const)&DDRE
40 #define _DDRF (uint8_t *const)&DDRF
41
42 #define _PINA (uint8_t *const)&PINA
43 #define _PINB (uint8_t *const)&PINB
44 #define _PINC (uint8_t *const)&PINC
45 #define _PIND (uint8_t *const)&PIND
46 #define _PINE (uint8_t *const)&PINE
47 #define _PINF (uint8_t *const)&PINF
48
49 #define _PORTA (uint8_t *const)&PORTA
50 #define _PORTB (uint8_t *const)&PORTB
51 #define _PORTC (uint8_t *const)&PORTC
52 #define _PORTD (uint8_t *const)&PORTD
53 #define _PORTE (uint8_t *const)&PORTE
54 #define _PORTF (uint8_t *const)&PORTF
55
56 #define _BIT0 0x01
57 #define _BIT1 0x02
58 #define _BIT2 0x04
59 #define _BIT3 0x08
60 #define _BIT4 0x10
61 #define _BIT5 0x20
62 #define _BIT6 0x40
63 #define _BIT7 0x80
64
65 /* Specifies the ports and pin numbers for the rows */
66 static
67 uint8_t *const  row_ddr[NROW] = {                                 _DDRB,                  _DDRB,
68                                                                                   _DDRC,  _DDRC,
69                                   _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,
70                                   _DDRF,  _DDRF,                  _DDRF,  _DDRF,  _DDRF,  _DDRF};
71
72 static
73 uint8_t *const row_port[NROW] = {                                _PORTB,                 _PORTB,
74                                  _PORTC, _PORTC,
75                                  _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
76                                  _PORTF, _PORTF,                 _PORTF, _PORTF, _PORTF, _PORTF};
77
78 static
79 uint8_t *const  row_pin[NROW] = {                                 _PINB,                  _PINB,
80                                   _PINC,  _PINC,
81                                   _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,
82                                   _PINF,  _PINF,                  _PINF,  _PINF,  _PINF,  _PINF};
83
84 static
85 const uint8_t   row_bit[NROW] = {                                 _BIT4,                  _BIT7,
86                                                                                   _BIT6,  _BIT7,
87                                   _BIT0,  _BIT1,  _BIT2,  _BIT3,  _BIT4,  _BIT5,  _BIT6,  _BIT7,
88                                   _BIT0,  _BIT1,                  _BIT4,  _BIT5,  _BIT6,  _BIT7};
89
90 static
91 const uint8_t mask = 0x0E;
92
93 /* Specifies the ports and pin numbers for the columns */
94 static
95 const uint8_t   col_bit[NCOL] = {  0x00,   0x02,   0x04,   0x06,   0x08,   0x0A,   0x0C,   0x0E};
96
97 static
98 inline void pull_column(int col) {
99   PORTB = col_bit[col] | (PORTB & ~mask);
100 }
101
102 static
103 inline void release_column(int col) {
104 }
105
106 /* PORTB is set as input with pull-up resistors
107    PORTC,D,E,F are set to high output */
108 static
109 void setup_io_pins(void) {
110   uint8_t row;
111   DDRB  |=  0x0E;
112   PORTB &= ~0x0E;
113   for(row = 0; row < NROW; row++) {
114     *row_ddr[row]  &= ~row_bit[row];
115     *row_port[row] &= ~row_bit[row];
116   }
117 }
118
119 static
120 void setup_leds(void) {
121   DDRB  |=  0x60;
122   PORTB |=  0x60;
123 }
124
125
126 inline
127 uint8_t matrix_rows(void)
128 {
129     return MATRIX_ROWS;
130 }
131
132 inline
133 uint8_t matrix_cols(void)
134 {
135     return MATRIX_COLS;
136 }
137
138 void matrix_init(void)
139 {
140     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
141     MCUCR |= (1<<JTD);
142     MCUCR |= (1<<JTD);
143
144     // initialize row and col
145     setup_io_pins();
146     setup_leds();
147
148     // initialize matrix state: all keys off
149     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
150     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
151     matrix = _matrix0;
152     matrix_debounced = _matrix1;
153 }
154
155 uint8_t matrix_scan(void)
156 {
157     if (!debouncing) {
158         uint8_t *tmp = matrix_debounced;
159         matrix_debounced = matrix;
160         matrix = tmp;
161     }
162
163     for (uint8_t col = 0; col < NCOL; col++) {  // 0-7
164         pull_column(col);   // output hi on theline
165         _delay_us(1);       // without this wait it won't read stable value.
166         for (uint8_t row = 0; row < NROW; row++) {  // 0-17
167             bool prev_bit = matrix[row] & (1<<col);
168             bool curr_bit = *row_pin[row] & row_bit[row];
169             if (prev_bit != curr_bit) {
170                 matrix[row] ^= (1<<col);
171                 if (debouncing) {
172                     debug("bounce!: "); debug_hex(debouncing); print("\n");
173                 }
174                 debouncing = DEBOUNCE;
175             }
176         }
177         release_column(col);
178     }
179
180     if (debouncing) {
181         debouncing--;
182     }
183
184     return 1;
185 }
186
187 bool matrix_is_modified(void)
188 {
189     // NOTE: no longer used
190     return true;
191 }
192
193 inline
194 bool matrix_has_ghost(void)
195 {
196     return false;
197 }
198
199 inline
200 bool matrix_is_on(uint8_t row, uint8_t col)
201 {
202     return (matrix_debounced[row] & (1<<col));
203 }
204
205 inline
206 matrix_row_t matrix_get_row(uint8_t row)
207 {
208     return matrix_debounced[row];
209 }
210
211 void matrix_print(void)
212 {
213     print("\nr/c 01234567\n");
214     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
215         phex(row); print(": ");
216         pbin_reverse(matrix_get_row(row));
217         print("\n");
218     }
219 }
220
221 uint8_t matrix_key_count(void)
222 {
223     uint8_t count = 0;
224     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
225         for (uint8_t j = 0; j < MATRIX_COLS; j++) {
226             if (matrix_is_on(i, j))
227                 count++;
228         }
229     }
230     return count;
231 }