]> git.gir.st - tmk_keyboard.git/blob - adb.c
minor fix for HHKB Makefile and doc
[tmk_keyboard.git] / adb.c
1 /*
2 Copyright (c) 2011 Jun WAKO <wakojun@gmail.com>
3
4 This software is licensed with a Modified BSD License.
5 All of this is supposed to be Free Software, Open Source, DFSG-free,
6 GPL-compatible, and OK to use in both free and proprietary applications.
7 Additions and corrections to this file are welcome.
8
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in
18 the documentation and/or other materials provided with the
19 distribution.
20
21 * Neither the name of the copyright holders nor the names of
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36 */
37 #include <stdbool.h>
38 #include <util/delay.h>
39 #include <avr/io.h>
40 #include "adb.h"
41
42
43 static inline void data_lo(void);
44 static inline void data_hi(void);
45 static inline bool data_in(void);
46 #ifdef ADB_PSW_BIT
47 static inline void psw_lo(void);
48 static inline void psw_hi(void);
49 static inline bool psw_in(void);
50 #endif
51
52 static inline void attention(void);
53 static inline void place_bit0(void);
54 static inline void place_bit1(void);
55 static inline void send_byte(uint8_t data);
56 static inline bool read_bit(void);
57 static inline uint8_t read_byte(void);
58 static inline uint8_t wait_data_lo(uint8_t us);
59 static inline uint8_t wait_data_hi(uint8_t us);
60
61
62 void adb_host_init(void)
63 {
64 data_hi();
65 #ifdef ADB_PSW_BIT
66 psw_hi();
67 #endif
68 }
69
70 #ifdef ADB_PSW_BIT
71 bool adb_host_psw(void)
72 {
73 return psw_in();
74 }
75 #endif
76
77 uint16_t adb_host_kbd_recv(void)
78 {
79 uint16_t data = 0;
80 attention();
81 send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
82 place_bit0(); // Stopbit(0)
83 if (!wait_data_lo(0xFF)) // Tlt/Stop to Start(140-260us)
84 return 0; // No data to send
85 if (!read_bit()) // Startbit(1)
86 return -2;
87 data = read_byte();
88 data = (data<<8) | read_byte();
89 if (read_bit()) // Stopbit(0)
90 return -3;
91 return data;
92 }
93
94 // send state of LEDs
95 void adb_host_kbd_led(uint8_t led)
96 {
97 attention();
98 send_byte(0x2A); // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
99 place_bit0(); // Stopbit(0)
100 _delay_us(200); // Tlt/Stop to Start
101 place_bit1(); // Startbit(1)
102 send_byte(0); // send upper byte (not used)
103 send_byte(led&0x07); // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0: NumLock)
104 place_bit0(); // Stopbit(0);
105 }
106
107
108 static inline void data_lo()
109 {
110 ADB_DDR |= (1<<ADB_DATA_BIT);
111 ADB_PORT &= ~(1<<ADB_DATA_BIT);
112 }
113 static inline void data_hi()
114 {
115 ADB_PORT |= (1<<ADB_DATA_BIT);
116 ADB_DDR &= ~(1<<ADB_DATA_BIT);
117 }
118 static inline bool data_in()
119 {
120 ADB_PORT |= (1<<ADB_DATA_BIT);
121 ADB_DDR &= ~(1<<ADB_DATA_BIT);
122 return ADB_PIN&(1<<ADB_DATA_BIT);
123 }
124
125 #ifdef ADB_PSW_BIT
126 static inline void psw_lo()
127 {
128 ADB_DDR |= (1<<ADB_PSW_BIT);
129 ADB_PORT &= ~(1<<ADB_PSW_BIT);
130 }
131 static inline void psw_hi()
132 {
133 ADB_PORT |= (1<<ADB_PSW_BIT);
134 ADB_DDR &= ~(1<<ADB_PSW_BIT);
135 }
136 static inline bool psw_in()
137 {
138 ADB_PORT |= (1<<ADB_PSW_BIT);
139 ADB_DDR &= ~(1<<ADB_PSW_BIT);
140 return ADB_PIN&(1<<ADB_PSW_BIT);
141 }
142 #endif
143
144 static inline void attention(void)
145 {
146 data_lo();
147 _delay_us(700);
148 place_bit1();
149 }
150
151 static inline void place_bit0(void)
152 {
153 data_lo();
154 _delay_us(65);
155 data_hi();
156 _delay_us(35);
157 }
158
159 static inline void place_bit1(void)
160 {
161 data_lo();
162 _delay_us(35);
163 data_hi();
164 _delay_us(65);
165 }
166
167 static inline void send_byte(uint8_t data)
168 {
169 for (int i = 0; i < 8; i++) {
170 if (data&(0x80>>i))
171 place_bit1();
172 else
173 place_bit0();
174 }
175 }
176
177 static inline bool read_bit(void)
178 {
179 // ADB Bit Cells
180 //
181 // bit0: ______~~~
182 // 65 :35us
183 //
184 // bit1: ___~~~~~~
185 // 35 :65us
186 //
187 // bit0 low time: 60-70% of bit cell(42-91us)
188 // bit1 low time: 30-40% of bit cell(21-52us)
189 // bit cell time: 70-130us
190 // [from Apple IIgs Hardware Reference Second Edition]
191 //
192 // After 55us if data line is low/high then bit is 0/1.
193 // Too simple to rely on?
194 bool bit;
195 wait_data_lo(75); // wait the beginning of bit cell
196 _delay_us(55);
197 bit = data_in();
198 wait_data_hi(36); // wait high part of bit cell
199 return bit;
200 }
201
202 static inline uint8_t read_byte(void)
203 {
204 uint8_t data = 0;
205 for (int i = 0; i < 8; i++) {
206 data <<= 1;
207 if (read_bit())
208 data = data | 1;
209 }
210 return data;
211 }
212
213 static inline uint8_t wait_data_lo(uint8_t us)
214 {
215 while (data_in() && us) {
216 _delay_us(1);
217 us--;
218 }
219 return us;
220 }
221
222 static inline uint8_t wait_data_hi(uint8_t us)
223 {
224 while (!data_in() && us) {
225 _delay_us(1);
226 us--;
227 }
228 return us;
229 }
230
231
232 /*
233 ADB Protocol
234 ============
235
236 Resources
237 ---------
238 ADB - The Untold Story: Space Aliens Ate My Mouse
239 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
240 Apple IIgs Hardware Reference Second Edition [p80(Chapter6 p121)]
241 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
242 ADB Keycode
243 http://72.0.193.250/Documentation/macppc/adbkeycodes/
244 http://m0115.web.fc2.com/m0115.jpg
245 [Inside Macintosh volume V, pages 191-192]
246 ADB Signaling
247 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
248 ADB Overview & History
249 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
250 Microchip Application Note: ADB device(with code for PIC16C)
251 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
252 AVR ATtiny2131 ADB to PS/2 converter(Japanese)
253 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
254
255
256 Pinouts
257 -------
258 ADB female socket from the front:
259 __________
260 | | <--- top
261 | 4o o3 |
262 |2o o1|
263 | == |
264 |________| <--- bottom
265 | | <--- 4pins
266
267
268 ADB female socket from bottom:
269
270 ========== <--- front
271 | |
272 | |
273 |2o o1|
274 |4o o3|
275 ---------- <--- back
276
277 1: Data
278 2: Power SW(low when press Power key)
279 3: Vcc(5V)
280 4: GND
281
282
283 Commands
284 --------
285 ADB command is 1byte and consists of 4bit-address, 2bit-command
286 type and 2bit-register. The commands are always sent by Host.
287
288 Command format:
289 7 6 5 4 3 2 1 0
290 | | | |------------ address
291 | |-------- command type
292 | |---- register
293
294 bits commands
295 ------------------------------------------------------
296 - - - - 0 0 0 0 Send Request(reset all devices)
297 A A A A 0 0 0 1 Flush(reset a device)
298 - - - - 0 0 1 0 Reserved
299 - - - - 0 0 1 1 Reserved
300 - - - - 0 1 - - Reserved
301 A A A A 1 0 R R Listen(write to a device)
302 A A A A 1 1 R R Talk(read from a device)
303
304 The command to read keycodes from keyboard is 0x2C which
305 consist of keyboard address 2 and Talk against register 0.
306
307 Address:
308 2: keyboard
309 3: mice
310
311 Registers:
312 0: application(keyobard uses this to store its data.)
313 1: application
314 2: application(keyboard uses this for LEDs and state of modifiers)
315 3: status and command
316
317
318 Communication
319 -------------
320 This is a minimum information for keyboard communication.
321 See "Resources" for detail.
322
323 Signaling:
324
325 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
326
327 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
328 +Attention | | | +Startbit(1)
329 +Startbit(1) | +Tlt(140-260us)
330 +stopbit(0)
331
332 Bit cells:
333
334 bit0: ______~~~
335 65 :35us
336
337 bit1: ___~~~~~~
338 35 :65us
339
340 bit0 low time: 60-70% of bit cell(42-91us)
341 bit1 low time: 30-40% of bit cell(21-52us)
342 bit cell time: 70-130us
343 [from Apple IIgs Hardware Reference Second Edition]
344
345 Criterion for bit0/1:
346 After 55us if line is low/high then bit is 0/1.
347
348 Attention & start bit:
349 Host asserts low in 560-1040us then places start bit(1).
350
351 Tlt(Stop to Start):
352 Bus stays high in 140-260us then device places start bit(1).
353
354 Global reset:
355 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
356
357 Send request from device(Srq):
358 Device can request to send at commad(Global only?) stop bit.
359 keep low for 300us to request.
360
361
362 Keyboard Data(Register0)
363 This 16bit data can contains two keycodes and two released flags.
364 First keycode is palced in upper byte. When one keyocode is sent,
365 lower byte is 0xFF.
366 Release flag is 1 when key is released.
367
368 1514 . . . . . 8 7 6 . . . . . 0
369 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
370 | | | | | | | | +--------------- Released2(1 when the key is released)
371 | +-+-+-+-+-+-+----------------- Keycode1
372 +------------------------------- Released1(1 when the key is released)
373
374 Keycodes:
375 Scancode consists of 7bit keycode and 1bit release flag.
376 Device can send two keycodes at once. If just one keycode is sent
377 keycode1 contains it and keyocode2 is 0xFF.
378
379 Power switch:
380 You can read the state from PSW line(active low) however
381 the switch has a special scancode 0x7F7F, so you can
382 also read from Data line. It uses 0xFFFF for release scancode.
383 Release code seems to delay about some 100ms. Due to Mac soft power?
384
385 Keyboard LEDs & state of keys(Register2)
386 This register hold current state of three LEDs and nine keys.
387 The state of LEDs can be changed by sending Listen command.
388
389 1514 . . . . . . 7 6 5 . 3 2 1 0
390 | | | | | | | | | | | | | | | +- LED1(NumLock)
391 | | | | | | | | | | | | | | +--- LED2(CapsLock)
392 | | | | | | | | | | | | | +----- LED3(ScrollLock)
393 | | | | | | | | | | +-+-+------- Reserved
394 | | | | | | | | | +------------- ScrollLock
395 | | | | | | | | +--------------- NumLock
396 | | | | | | | +----------------- Apple/Command
397 | | | | | | +------------------- Option
398 | | | | | +--------------------- Shift
399 | | | | +----------------------- Control
400 | | | +------------------------- Reset/Power
401 | | +--------------------------- CapsLock
402 | +----------------------------- Delete
403 +------------------------------- Reserved
404
405 END_OF_ADB
406 */
Imprint / Impressum