]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb/hhkb_avr.h
hhkb: Add power saving code of matrix scan for JP
[tmk_keyboard.git] / keyboard / hhkb / hhkb_avr.h
1 #ifndef HHKB_AVR_H
2 #define HHKB_AVR_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 #if defined(__AVR_ATmega32U4__)
29 /*
30 * For TMK HHKB alt controller(ATMega32U4)
31 *
32 * row: PB0-2
33 * col: PB3-5,6
34 * key: PD7(pull-uped)
35 * prev: PB7
36 * power: PD4(L:off/H:on)
37 * row-ext: PC6,7 for HHKB JP(active low)
38 */
39 static inline void KEY_ENABLE(void) { (PORTB &= ~(1<<6)); }
40 static inline void KEY_UNABLE(void) { (PORTB |= (1<<6)); }
41 static inline bool KEY_STATE(void) { return (PIND & (1<<7)); }
42 static inline void KEY_PREV_ON(void) { (PORTB |= (1<<7)); }
43 static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); }
44 #ifdef HHKB_POWER_SAVING
45 static inline void KEY_POWER_ON(void) {
46 DDRB = 0xFF; PORTB = 0x40; // change pins output
47 DDRD |= (1<<4); PORTD |= (1<<4); // MOS FET switch on
48 #ifdef HHKB_JP
49 DDRC |= (1<<6|1<<7);
50 PORTC |= (1<<6|1<<7);
51 #endif
52 /* Without this wait you will miss or get false key events. */
53 _delay_ms(5); // wait for powering up
54 }
55 static inline void KEY_POWER_OFF(void) {
56 /* input with pull-up consumes less than without it when pin is open. */
57 DDRB = 0x00; PORTB = 0xFF; // change pins input with pull-up
58 #ifdef HHKB_JP
59 DDRC &= ~(1<<6|1<<7);
60 PORTC |= (1<<6|1<<7);
61 #endif
62 DDRD |= (1<<4); PORTD &= ~(1<<4); // MOS FET switch off
63 }
64 static inline bool KEY_POWER_STATE(void) { return PORTD & (1<<4); }
65 #else
66 static inline void KEY_POWER_ON(void) {}
67 static inline void KEY_POWER_OFF(void) {}
68 static inline bool KEY_POWER_STATE(void) { return true; }
69 #endif
70 static inline void KEY_INIT(void)
71 {
72 /* row,col,prev: output */
73 DDRB = 0xFF;
74 PORTB = 0x40; // unable
75 /* key: input with pull-up */
76 DDRD &= ~0x80;
77 PORTD |= 0x80;
78 #ifdef HHKB_JP
79 /* row extention for HHKB JP */
80 DDRC |= (1<<6|1<<7);
81 PORTC |= (1<<6|1<<7);
82 #endif
83 KEY_UNABLE();
84 KEY_PREV_OFF();
85
86 KEY_POWER_OFF();
87 }
88 static inline void KEY_SELECT(uint8_t ROW, uint8_t COL)
89 {
90 PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07);
91 #ifdef HHKB_JP
92 if ((ROW) & 0x08) PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<6);
93 else PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<7);
94 #endif
95 }
96
97
98 #elif defined(__AVR_AT90USB1286__)
99 /*
100 * For Teensy++(AT90USB1286)
101 *
102 * HHKB pro HHKB pro2
103 * row: PB0-2 (6-8) (5-7)
104 * col: PB3-5,6 (9-12) (8-11)
105 * key: PE6(pull-uped) (4) (3)
106 * prev: PE7 (5) (4)
107 *
108 * TODO: convert into 'staitc inline' function
109 */
110 #define KEY_INIT() do { \
111 DDRB |= 0x7F; \
112 DDRE |= (1<<7); \
113 DDRE &= ~(1<<6); \
114 PORTE |= (1<<6); \
115 } while (0)
116 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
117 (((COL) & 0x07)<<3) | \
118 ((ROW) & 0x07))
119 #define KEY_ENABLE() (PORTB &= ~(1<<6))
120 #define KEY_UNABLE() (PORTB |= (1<<6))
121 #define KEY_STATE() (PINE & (1<<6))
122 #define KEY_PREV_ON() (PORTE |= (1<<7))
123 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
124 #define KEY_POWER_ON()
125 #define KEY_POWER_OFF()
126 #define KEY_POWER_STATE() true
127
128
129 #else
130 # error "define code for matrix scan"
131 #endif
132
133
134 #if 0
135 // For ATMega328P with V-USB
136 //
137 // #elif defined(__AVR_ATmega328P__)
138 // Ports for V-USB
139 // key: PB0(pull-uped)
140 // prev: PB1
141 // row: PB2-4
142 // col: PC0-2,3
143 // power: PB5(Low:on/Hi-z:off)
144 #define KEY_INIT() do { \
145 DDRB |= 0x3E; \
146 DDRB &= ~(1<<0); \
147 PORTB |= 1<<0; \
148 DDRC |= 0x0F; \
149 KEY_UNABLE(); \
150 KEY_PREV_OFF(); \
151 } while (0)
152 #define KEY_SELECT(ROW, COL) do { \
153 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
154 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
155 } while (0)
156 #define KEY_ENABLE() (PORTC &= ~(1<<3))
157 #define KEY_UNABLE() (PORTC |= (1<<3))
158 #define KEY_STATE() (PINB & (1<<0))
159 #define KEY_PREV_ON() (PORTB |= (1<<1))
160 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
161 // Power supply switching
162 #define KEY_POWER_ON() do { \
163 KEY_INIT(); \
164 PORTB &= ~(1<<5); \
165 _delay_ms(1); \
166 } while (0)
167 #define KEY_POWER_OFF() do { \
168 DDRB &= ~0x3F; \
169 PORTB &= ~0x3F; \
170 DDRC &= ~0x0F; \
171 PORTC &= ~0x0F; \
172 } while (0)
173 #endif
174
175 #endif
Imprint / Impressum