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