]> git.gir.st - tmk_keyboard.git/blob - protocol/adb.c
Fix comment of ADB signaling.
[tmk_keyboard.git] / protocol / adb.c
1 /*
2 Copyright 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
38 #include <stdbool.h>
39 #include <util/delay.h>
40 #include <avr/io.h>
41 #include <avr/interrupt.h>
42 #include "adb.h"
43
44
45 static inline void data_lo(void);
46 static inline void data_hi(void);
47 static inline bool data_in(void);
48 #ifdef ADB_PSW_BIT
49 static inline void psw_lo(void);
50 static inline void psw_hi(void);
51 static inline bool psw_in(void);
52 #endif
53
54 static inline void attention(void);
55 static inline void place_bit0(void);
56 static inline void place_bit1(void);
57 static inline void send_byte(uint8_t data);
58 static inline bool read_bit(void);
59 static inline uint8_t read_byte(void);
60 static inline uint8_t wait_data_lo(uint8_t us);
61 static inline uint8_t wait_data_hi(uint8_t us);
62
63
64 void adb_host_init(void)
65 {
66 data_hi();
67 #ifdef ADB_PSW_BIT
68 psw_hi();
69 #endif
70
71 // Enable keyboard left/right modifier distinction
72 // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
73 // upper byte: reserved bits 0000, device address 0010
74 // lower byte: device handler 00000011
75 adb_host_listen(0x2B,0x02,0x03);
76 }
77
78 #ifdef ADB_PSW_BIT
79 bool adb_host_psw(void)
80 {
81 return psw_in();
82 }
83 #endif
84
85 uint16_t adb_host_kbd_recv(void)
86 {
87 uint16_t data = 0;
88 attention();
89 send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
90 place_bit0(); // Stopbit(0)
91 if (!wait_data_lo(0xFF)) // Tlt/Stop to Start(140-260us)
92 return 0; // No data to send
93 if (!read_bit()) // Startbit(1)
94 return -2;
95
96 // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
97 cli();
98 data = read_byte();
99 data = (data<<8) | read_byte();
100 sei();
101
102 if (read_bit()) // Stopbit(0)
103 return -3;
104 return data;
105 }
106
107 void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
108 {
109 attention();
110 send_byte(cmd);
111 place_bit0(); // Stopbit(0)
112 _delay_us(200); // Tlt/Stop to Start
113 place_bit1(); // Startbit(1)
114 send_byte(data_h);
115 send_byte(data_l);
116 place_bit0(); // Stopbit(0);
117 }
118
119 // send state of LEDs
120 void adb_host_kbd_led(uint8_t led)
121 {
122 // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
123 // send upper byte (not used)
124 // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
125 adb_host_listen(0x2A,0,led&0x07);
126 }
127
128
129 static inline void data_lo()
130 {
131 ADB_DDR |= (1<<ADB_DATA_BIT);
132 ADB_PORT &= ~(1<<ADB_DATA_BIT);
133 }
134 static inline void data_hi()
135 {
136 ADB_PORT |= (1<<ADB_DATA_BIT);
137 ADB_DDR &= ~(1<<ADB_DATA_BIT);
138 }
139 static inline bool data_in()
140 {
141 ADB_PORT |= (1<<ADB_DATA_BIT);
142 ADB_DDR &= ~(1<<ADB_DATA_BIT);
143 return ADB_PIN&(1<<ADB_DATA_BIT);
144 }
145
146 #ifdef ADB_PSW_BIT
147 static inline void psw_lo()
148 {
149 ADB_DDR |= (1<<ADB_PSW_BIT);
150 ADB_PORT &= ~(1<<ADB_PSW_BIT);
151 }
152 static inline void psw_hi()
153 {
154 ADB_PORT |= (1<<ADB_PSW_BIT);
155 ADB_DDR &= ~(1<<ADB_PSW_BIT);
156 }
157 static inline bool psw_in()
158 {
159 ADB_PORT |= (1<<ADB_PSW_BIT);
160 ADB_DDR &= ~(1<<ADB_PSW_BIT);
161 return ADB_PIN&(1<<ADB_PSW_BIT);
162 }
163 #endif
164
165 static inline void attention(void)
166 {
167 data_lo();
168 _delay_us(700);
169 place_bit1();
170 }
171
172 static inline void place_bit0(void)
173 {
174 data_lo();
175 _delay_us(65);
176 data_hi();
177 _delay_us(35);
178 }
179
180 static inline void place_bit1(void)
181 {
182 data_lo();
183 _delay_us(35);
184 data_hi();
185 _delay_us(65);
186 }
187
188 static inline void send_byte(uint8_t data)
189 {
190 for (int i = 0; i < 8; i++) {
191 if (data&(0x80>>i))
192 place_bit1();
193 else
194 place_bit0();
195 }
196 }
197
198 static inline bool read_bit(void)
199 {
200 // ADB Bit Cells
201 //
202 // bit cell time: 70-130us
203 // low part of bit0: 60-70% of bit cell
204 // low part of bit1: 30-40% of bit cell
205 //
206 // bit cell time 70us 130us
207 // --------------------------------------------
208 // low part of bit0 42-49 78-91
209 // high part of bit0 21-28 39-52
210 // low part of bit1 21-28 39-52
211 // high part of bit1 42-49 78-91
212 //
213 //
214 // bit0:
215 // 70us bit cell:
216 // ____________~~~~~~
217 // 42-49 21-28
218 //
219 // 130us bit cell:
220 // ____________~~~~~~
221 // 78-91 39-52
222 //
223 // bit1:
224 // 70us bit cell:
225 // ______~~~~~~~~~~~~
226 // 21-28 42-49
227 //
228 // 130us bit cell:
229 // ______~~~~~~~~~~~~
230 // 39-52 78-91
231 //
232 // read:
233 // ________|~~~~~~~~~
234 // 55us
235 // Read data line after 55us. If data line is low/high then bit is 0/1.
236 // This method might not work at <90us bit cell time.
237 //
238 // [from Apple IIgs Hardware Reference Second Edition]
239 bool bit;
240 wait_data_lo(75); // wait the start of bit cell at least 130ms(55+0+75)
241 _delay_us(55);
242 bit = data_in();
243 wait_data_hi(36); // wait high part of bit cell at least 91ms(55+36)
244 return bit;
245 }
246
247 static inline uint8_t read_byte(void)
248 {
249 uint8_t data = 0;
250 for (int i = 0; i < 8; i++) {
251 data <<= 1;
252 if (read_bit())
253 data = data | 1;
254 }
255 return data;
256 }
257
258 static inline uint8_t wait_data_lo(uint8_t us)
259 {
260 while (data_in() && us) {
261 _delay_us(1);
262 us--;
263 }
264 return us;
265 }
266
267 static inline uint8_t wait_data_hi(uint8_t us)
268 {
269 while (!data_in() && us) {
270 _delay_us(1);
271 us--;
272 }
273 return us;
274 }
275
276
277 /*
278 ADB Protocol
279 ============
280
281 Resources
282 ---------
283 ADB - The Untold Story: Space Aliens Ate My Mouse
284 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
285 Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
286 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
287 ADB Keycode
288 http://72.0.193.250/Documentation/macppc/adbkeycodes/
289 http://m0115.web.fc2.com/m0115.jpg
290 [Inside Macintosh volume V, pages 191-192]
291 http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
292 ADB Signaling
293 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
294 ADB Overview & History
295 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
296 Microchip Application Note: ADB device(with code for PIC16C)
297 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
298 AVR ATtiny2131 ADB to PS/2 converter(Japanese)
299 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
300
301
302 Pinouts
303 -------
304 ADB female socket from the front:
305 __________
306 | | <--- top
307 | 4o o3 |
308 |2o o1|
309 | == |
310 |________| <--- bottom
311 | | <--- 4pins
312
313
314 ADB female socket from bottom:
315
316 ========== <--- front
317 | |
318 | |
319 |2o o1|
320 |4o o3|
321 ---------- <--- back
322
323 1: Data
324 2: Power SW(low when press Power key)
325 3: Vcc(5V)
326 4: GND
327
328
329 Commands
330 --------
331 ADB command is 1byte and consists of 4bit-address, 2bit-command
332 type and 2bit-register. The commands are always sent by Host.
333
334 Command format:
335 7 6 5 4 3 2 1 0
336 | | | |------------ address
337 | |-------- command type
338 | |---- register
339
340 bits commands
341 ------------------------------------------------------
342 - - - - 0 0 0 0 Send Request(reset all devices)
343 A A A A 0 0 0 1 Flush(reset a device)
344 - - - - 0 0 1 0 Reserved
345 - - - - 0 0 1 1 Reserved
346 - - - - 0 1 - - Reserved
347 A A A A 1 0 R R Listen(write to a device)
348 A A A A 1 1 R R Talk(read from a device)
349
350 The command to read keycodes from keyboard is 0x2C which
351 consist of keyboard address 2 and Talk against register 0.
352
353 Address:
354 2: keyboard
355 3: mice
356
357 Registers:
358 0: application(keyobard uses this to store its data.)
359 1: application
360 2: application(keyboard uses this for LEDs and state of modifiers)
361 3: status and command
362
363
364 Communication
365 -------------
366 This is a minimum information for keyboard communication.
367 See "Resources" for detail.
368
369 Signaling:
370
371 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
372
373 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
374 +Attention | | | +Startbit(1)
375 +Startbit(1) | +Tlt(140-260us)
376 +stopbit(0)
377
378 Bit cells:
379
380 bit0: ______~~~
381 65 :35us
382
383 bit1: ___~~~~~~
384 35 :65us
385
386 bit0 low time: 60-70% of bit cell(42-91us)
387 bit1 low time: 30-40% of bit cell(21-52us)
388 bit cell time: 70-130us
389 [from Apple IIgs Hardware Reference Second Edition]
390
391 Criterion for bit0/1:
392 After 55us if line is low/high then bit is 0/1.
393
394 Attention & start bit:
395 Host asserts low in 560-1040us then places start bit(1).
396
397 Tlt(Stop to Start):
398 Bus stays high in 140-260us then device places start bit(1).
399
400 Global reset:
401 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
402
403 Service request from device(Srq):
404 Device can request to send at commad(Global only?) stop bit.
405 Requesting device keeps low for 140-260us at stop bit of command.
406
407
408 Keyboard Data(Register0)
409 This 16bit data can contains two keycodes and two released flags.
410 First keycode is palced in upper byte. When one keyocode is sent,
411 lower byte is 0xFF.
412 Release flag is 1 when key is released.
413
414 1514 . . . . . 8 7 6 . . . . . 0
415 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
416 | | | | | | | | +--------------- Released2(1 when the key is released)
417 | +-+-+-+-+-+-+----------------- Keycode1
418 +------------------------------- Released1(1 when the key is released)
419
420 Keycodes:
421 Scancode consists of 7bit keycode and 1bit release flag.
422 Device can send two keycodes at once. If just one keycode is sent
423 keycode1 contains it and keyocode2 is 0xFF.
424
425 Power switch:
426 You can read the state from PSW line(active low) however
427 the switch has a special scancode 0x7F7F, so you can
428 also read from Data line. It uses 0xFFFF for release scancode.
429
430 Keyboard LEDs & state of keys(Register2)
431 This register hold current state of three LEDs and nine keys.
432 The state of LEDs can be changed by sending Listen command.
433
434 1514 . . . . . . 7 6 5 . 3 2 1 0
435 | | | | | | | | | | | | | | | +- LED1(NumLock)
436 | | | | | | | | | | | | | | +--- LED2(CapsLock)
437 | | | | | | | | | | | | | +----- LED3(ScrollLock)
438 | | | | | | | | | | +-+-+------- Reserved
439 | | | | | | | | | +------------- ScrollLock
440 | | | | | | | | +--------------- NumLock
441 | | | | | | | +----------------- Apple/Command
442 | | | | | | +------------------- Option
443 | | | | | +--------------------- Shift
444 | | | | +----------------------- Control
445 | | | +------------------------- Reset/Power
446 | | +--------------------------- CapsLock
447 | +----------------------------- Delete
448 +------------------------------- Reserved
449
450 END_OF_ADB
451 */
Imprint / Impressum