]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/fc660c/fc660c.h
fca0fded26a9c760ae29f437d043ff2adaedfabf
[max/tmk_keyboard.git] / keyboard / fc660c / fc660c.h
1 #ifndef FC660C_H
2 #define FC660C_H
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <avr/io.h>
7 #include <avr/interrupt.h>
8 #include <util/delay.h>
9
10
11 // Timer resolution check
12 #if (1000000/TIMER_RAW_FREQ > 20)
13 #   error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
14 #endif
15
16
17 /*
18  * HHKB Matrix I/O
19  *
20  * row:     HC4051[A,B,C]  selects scan row0-7
21  * row-ext: [En0,En1] row extention for JP
22  * col:     LS145[A,B,C,D] selects scan col0-7 and enable(D)
23  * key:     on: 0/off: 1
24  * prev:    hysteresis control: assert(1) when previous key state is on
25  */
26
27
28 /*
29  * Pin configuration for ATMega32U4
30  *
31  * Row:     PD4-6, 7(~EN)
32  * Col:     PB0-2, 3(Z5 ~EN), 4(Z4 ~EN)
33  * Key:     PC6(pull-uped)
34  * Hys:     PC7
35  */
36 static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
37 static inline void KEY_UNABLE(void) { (PORTD |=  (1<<7)); }
38 static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
39 static inline void KEY_HYS_ON(void) { (PORTC |=  (1<<7)); }
40 static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
41 static inline void KEY_INIT(void)
42 {
43     /* Col */
44     DDRB  |=  0x1F;
45     /* Key: input with pull-up */
46     DDRC  &= ~(1<<6);
47     PORTC |=  (1<<6);
48     /* Hys */
49     DDRC  |=  (1<<7);
50     /* Row */
51     DDRD  |=  0xF0;
52
53     KEY_UNABLE();
54     KEY_HYS_OFF();
55 }
56 static inline void SET_ROW(uint8_t ROW)
57 {
58     // set row with unabling key
59     PORTD = (PORTD & 0x0F) | (1<<7) | ((ROW & 0x07) << 4);
60 }
61 static inline void SET_COL(uint8_t COL)
62 {
63     //         |PB3(Z5 ~EN)|PB4(Z4 ~EN)
64     // --------|-----------|-----------
65     // Col:0-7 |high       |low
66     // Col:8-F |low        |high
67     PORTB = (PORTB & 0xE0) | ((COL & 0x08) ? 1<<4 : 1<<3) | (COL & 0x07);
68 }
69
70 #endif