]>
git.gir.st - tmk_keyboard.git/blob - keyboard/phantom/matrix.c
1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This is heavily based on phantom/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
9 * As for liscensing consult with the original files or its author.
14 #include <util/delay.h>
24 static uint8_t debouncing
= DEBOUNCE
;
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
];
33 #define _DDRA (uint8_t *const)&DDRA
34 #define _DDRB (uint8_t *const)&DDRB
35 #define _DDRC (uint8_t *const)&DDRC
36 #define _DDRD (uint8_t *const)&DDRD
37 #define _DDRE (uint8_t *const)&DDRE
38 #define _DDRF (uint8_t *const)&DDRF
40 #define _PINA (uint8_t *const)&PINA
41 #define _PINB (uint8_t *const)&PINB
42 #define _PINC (uint8_t *const)&PINC
43 #define _PIND (uint8_t *const)&PIND
44 #define _PINE (uint8_t *const)&PINE
45 #define _PINF (uint8_t *const)&PINF
47 #define _PORTA (uint8_t *const)&PORTA
48 #define _PORTB (uint8_t *const)&PORTB
49 #define _PORTC (uint8_t *const)&PORTC
50 #define _PORTD (uint8_t *const)&PORTD
51 #define _PORTE (uint8_t *const)&PORTE
52 #define _PORTF (uint8_t *const)&PORTF
63 /* Specifies the ports and pin numbers for the rows */
65 uint8_t *const row_ddr
[MATRIX_ROWS
] = {_DDRB
, _DDRB
, _DDRB
, _DDRB
, _DDRB
, _DDRB
};
68 uint8_t *const row_port
[MATRIX_ROWS
] = {_PORTB
, _PORTB
, _PORTB
, _PORTB
, _PORTB
, _PORTB
};
71 uint8_t *const row_pin
[MATRIX_ROWS
] = {_PINB
, _PINB
, _PINB
, _PINB
, _PINB
, _PINB
};
74 const uint8_t row_bit
[MATRIX_ROWS
] = { _BIT0
, _BIT1
, _BIT2
, _BIT3
, _BIT4
, _BIT5
};
76 /* Specifies the ports and pin numbers for the columns */
78 uint8_t *const col_ddr
[MATRIX_COLS
] = { _DDRD
, _DDRC
, _DDRC
, _DDRD
, _DDRD
, _DDRE
,
79 _DDRF
, _DDRF
, _DDRF
, _DDRF
, _DDRF
, _DDRF
,
80 _DDRD
, _DDRD
, _DDRD
, _DDRD
, _DDRD
};
83 uint8_t *const col_port
[MATRIX_COLS
] = {_PORTD
, _PORTC
, _PORTC
, _PORTD
, _PORTD
, _PORTE
,
84 _PORTF
, _PORTF
, _PORTF
, _PORTF
, _PORTF
, _PORTF
,
85 _PORTD
, _PORTD
, _PORTD
, _PORTD
, _PORTD
};
88 const uint8_t col_bit
[MATRIX_COLS
] = { _BIT5
, _BIT7
, _BIT6
, _BIT4
, _BIT0
, _BIT6
,
89 _BIT0
, _BIT1
, _BIT4
, _BIT5
, _BIT6
, _BIT7
,
90 _BIT7
, _BIT6
, _BIT1
, _BIT2
, _BIT3
};
93 inline void pull_column(int col
) {
94 *col_port
[col
] &= ~col_bit
[col
];
98 inline void release_column(int col
) {
99 *col_port
[col
] |= col_bit
[col
];
102 /* PORTB is set as input with pull-up resistors
103 PORTC,D,E,F are set to high output */
106 void setup_io_pins(void) {
108 for(row
= 0; row
< MATRIX_ROWS
; row
++) {
109 *row_ddr
[row
] &= ~row_bit
[row
];
110 *row_port
[row
] |= row_bit
[row
];
112 for(col
= 0; col
< MATRIX_COLS
; col
++) {
113 *col_ddr
[col
] |= col_bit
[col
];
114 *col_port
[col
] |= col_bit
[col
];
118 /* LEDs are on output compare pins OC1B OC1C
119 This activates fast PWM mode on them.
120 Prescaler 256 and 8-bit counter results in
121 16000000/256/256 = 244 Hz blink frequency.
123 LED_B: Scroll Lock */
124 /* Output on PWM pins are turned off when the timer
125 reaches the value in the output compare register,
126 and are turned on when it reaches TOP (=256). */
128 void setup_leds(void) {
129 TCCR1A
|= // Timer control register 1A
130 (1<<WGM10
) | // Fast PWM 8-bit
131 (1<<COM1B1
)| // Clear OC1B on match, set at TOP
132 (1<<COM1C1
); // Clear OC1C on match, set at TOP
133 TCCR1B
|= // Timer control register 1B
134 (1<<WGM12
) | // Fast PWM 8-bit
135 (1<<CS12
); // Prescaler 256
136 OCR1B
= 250; // Output compare register 1B
137 OCR1C
= 250; // Output compare register 1C
138 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
145 uint8_t matrix_rows(void)
151 uint8_t matrix_cols(void)
156 void matrix_init(void)
158 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
162 // initialize row and col
166 // initialize matrix state: all keys off
167 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
168 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0x00;
170 matrix_debounced
= _matrix1
;
173 uint8_t matrix_scan(void)
176 matrix_row_t
*tmp
= matrix_debounced
;
177 matrix_debounced
= matrix
;
181 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) { // 0-16
182 pull_column(col
); // output hi on theline
183 _delay_us(3); // without this wait it won't read stable value.
184 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) { // 0-5
185 bool prev_bit
= matrix
[row
] & ((matrix_row_t
)1<<col
);
186 bool curr_bit
= !(*row_pin
[row
] & row_bit
[row
]);
187 if (prev_bit
!= curr_bit
) {
188 matrix
[row
] ^= ((matrix_row_t
)1<<col
);
190 debug("bounce!: "); debug_hex(debouncing
); print("\n");
192 debouncing
= DEBOUNCE
;
205 bool matrix_is_modified(void)
207 // NOTE: no longer used
212 bool matrix_has_ghost(void)
218 bool matrix_is_on(uint8_t row
, uint8_t col
)
220 return (matrix_debounced
[row
] & ((matrix_row_t
)1<<col
));
224 matrix_row_t
matrix_get_row(uint8_t row
)
226 return matrix_debounced
[row
];
229 void matrix_print(void)
231 print("\nr/c 01234567\n");
232 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
233 phex(row
); print(": ");
234 pbin_reverse(matrix_get_row(row
));
239 uint8_t matrix_key_count(void)
242 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
243 for (uint8_t j
= 0; j
< MATRIX_COLS
; j
++) {
244 if (matrix_is_on(i
, j
))