]> git.donarmstrong.com Git - qmk_firmware.git/blob - m0110.c
Added M0110A support contributed by skagon@github.
[qmk_firmware.git] / m0110.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 /* M0110A Support was contributed by skagon@github */
38
39 #include <stdbool.h>
40 #include <avr/io.h>
41 #include <avr/interrupt.h>
42 #include <util/delay.h>
43 #include "m0110.h"
44 #include "debug.h"
45
46
47 static inline uint8_t inquiry(void);
48 static inline uint8_t instant(void);
49 static inline void clock_lo(void);
50 static inline void clock_hi(void);
51 static inline bool clock_in(void);
52 static inline void data_lo(void);
53 static inline void data_hi(void);
54 static inline bool data_in(void);
55 static inline uint16_t wait_clock_lo(uint16_t us);
56 static inline uint16_t wait_clock_hi(uint16_t us);
57 static inline uint16_t wait_data_lo(uint16_t us);
58 static inline uint16_t wait_data_hi(uint16_t us);
59 static inline void idle(void);
60 static inline void request(void);
61
62
63 /*
64 Primitive M0110 Library for AVR
65 ==============================
66
67
68 Signaling
69 ---------
70 CLOCK is always from KEYBOARD. DATA are sent with MSB first.
71
72 1) IDLE: both lines are high.
73     CLOCK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74     DATA  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
76 2) KEYBOARD->HOST: HOST reads bit on rising edge.
77     CLOCK ~~~~~~~~~~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~~~~~~~~~
78     DATA  ~~~~~~~~~~~~X777777X666666X555555X444444X333333X222222X111111X000000X~~~~~~~
79                       <--> 160us(clock low)
80                          <---> 180us(clock high)
81
82 3) HOST->KEYBOARD: HOST asserts bit on falling edge.
83     CLOCK ~~~~~~~~~~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~~~~~~~~~
84     DATA  ~~~~~~|_____X777777X666666X555555X444444X333333X222222X111111X000000X~~~~~~~
85                 <----> 840us(request to send by host)                     <---> 80us(hold DATA)
86                       <--> 180us(clock low)
87                          <---> 220us(clock high)
88
89
90 Protocol
91 --------
92 COMMAND:
93     Inquiry     0x10    get key event
94     Instant     0x12    get key event
95     Model       0x14    get model number(M0110 responds with 0x09)
96                         bit 7   1 if another device connected(used when keypad exists?)
97                         bit4-6  next device model number
98                         bit1-3  keyboard model number
99                         bit 0   always 1
100     Test        0x16    test(ACK:0x7D/NAK:0x77)
101
102 KEY EVENT:
103     bit 7       key state(0:press 1:release)
104     bit 6-1     scan code(see below)
105     bit 0       always 1
106     To get scan code use this: ((bits&(1<<7)) | ((bits&0x7F))>>1).
107
108     Note: On the M0110A, the numpad keys and the arrow keys are preceded by 0x79.
109           Moreover, the numpad keys =, /, * and + are preceded by shift-down 0x71 on press and shift-up 0xF1 on release.
110           So, the data transferred by nupmad 5 is "79 2F" whereas for numpad + it's "71 79 0D".
111
112 ARROW KEYS:
113     Arrow keys and Pad+,*,/,=(Calc keys) share same byte sequence and its preceding byte
114     0x71 and 0xF1 means press and release event of SHIFT. These cause very confusing situation.
115     It is difficult or impossible to tell Calc key from Arrow key with SHIFT in some cases.
116
117     Raw key events:
118             press               release
119             ----------------    ----------------
120     Left:         0x79, 0x0D          0x79, 0x8D
121     Right:        0x79, 0x05          0x79, 0x85
122     Up:           0x79, 0x1B          0x79, 0x9B
123     Down:         0x79, 0x11          0x79, 0x91
124     Pad+:   0x71, 0x79, 0x0D    0xF1, 0x79, 0x8D
125     Pad*:   0x71, 0x79, 0x05    0xF1, 0x79, 0x85
126     Pad/:   0x71, 0x79, 0x1B    0xF1, 0x79, 0x9B
127     Pad=:   0x71, 0x79, 0x11    0xF1, 0x79, 0x91
128
129 SCAN CODE:
130     m0111_recv_key() function returns follwing scan codes instead of raw key events.
131     Scan codes are 1 byte long and bit7 is set when key is released. 
132
133     M0110
134     ,---------------------------------------------------------.
135     |  `|  1|  2|  3|  4|  5|  6|  7|  8|  9|  0|  -|  =|Backs|
136     |---------------------------------------------------------|
137     |Tab  |  Q|  W|  E|  R|  T|  Y|  U|  I|  O|  P|  [|  ]|  \|
138     |---------------------------------------------------------|
139     |CapsLo|  A|  S|  D|  F|  G|  H|  J|  K|  L|  ;|  '|Return|
140     |---------------------------------------------------------|
141     |Shift   |  Z|  X|  C|  V|  B|  N|  M|  ,|  ,|  /|        |
142     `---------------------------------------------------------'
143          |Opt|Mac |         Space               |Enter|Opt|
144          `------------------------------------------------'
145     ,---------------------------------------------------------.
146     | 32| 12| 13| 14| 15| 17| 16| 1A| 1C| 19| 1D| 1B| 18|   33|
147     |---------------------------------------------------------|
148     |   30| 0C| 0D| 0E| 0F| 10| 11| 20| 22| 1F| 23| 21| 1E| 2A|
149     |---------------------------------------------------------|
150     |    39| 00| 01| 02| 03| 05| 04| 26| 28| 25| 29| 27|    24|
151     |---------------------------------------------------------|
152     |      38| 06| 07| 08| 09| 0B| 2D| 2E| 2B| 2F| 2C|      38|
153     `---------------------------------------------------------'
154          | 3A|  37|             31              |   34| 3A|
155          `------------------------------------------------'
156
157     M0110A
158     ,---------------------------------------------------------. ,---------------.
159     |  `|  1|  2|  3|  4|  5|  6|  7|  8|  9|  0|  -|  =|Bcksp| |Clr|  =|  /|  *|
160     |---------------------------------------------------------| |---------------|
161     |Tab  |  Q|  W|  E|  R|  T|  Y|  U|  I|  O|  P|  [|  ]|   | |  7|  8|  9|  -|
162     |-----------------------------------------------------'   | |---------------|
163     |CapsLo|  A|  S|  D|  F|  G|  H|  J|  K|  L|  ;|  '|Return| |  4|  5|  6|  +|
164     |---------------------------------------------------------| |---------------|
165     |Shift   |  Z|  X|  C|  V|  B|  N|  M|  ,|  ,|  /|Shft|Up | |  1|  2|  3|   |
166     |---------------------------------------------------------' |-----------|Ent|
167     |Optio|Mac    |           Space           |  \|Lft|Rgt|Dn | |      0|  .|   |
168     `---------------------------------------------------------' `---------------'
169     ,---------------------------------------------------------. ,---------------.
170     | 32| 12| 13| 14| 15| 17| 16| 1A| 1C| 19| 1D| 1B| 18|   33| | 47| 68| 6D| 62|
171     |---------------------------------------------------------| |---------------|
172     |   30| 0C| 0D| 0E| 0F| 10| 11| 20| 22| 1F| 23| 21| 1E|   | | 59| 5B| 5C| 4E|
173     |-----------------------------------------------------'   | |---------------|
174     |    39| 00| 01| 02| 03| 05| 04| 26| 28| 25| 29| 27|    24| | 56| 57| 58| 66|
175     |---------------------------------------------------------| |---------------|
176     |      38| 06| 07| 08| 09| 0B| 2D| 2E| 2B| 2F| 2C|  38| 4D| | 53| 54| 55|   |
177     |---------------------------------------------------------' |-----------| 4C|
178     |   3A|     37|            31             | 2A| 46| 42| 48| |     52| 41|   |
179     `---------------------------------------------------------' `---------------'
180
181
182 References
183 ----------
184 Technical Info for 128K/512K and Plus
185     ftp://ftp.apple.asimov.net/pub/apple_II/documentation/macintosh/Mac%20Hardware%20Info%20-%20Mac%20128K.pdf
186     ftp://ftp.apple.asimov.net/pub/apple_II/documentation/macintosh/Mac%20Hardware%20Info%20-%20Mac%20Plus.pdf
187 Protocol:
188     Page 20 of Tech Info for 128K/512K
189     http://www.mac.linux-m68k.org/devel/plushw.php
190 Connector:
191     Page 20 of Tech Info for 128K/512K
192     http://www.kbdbabel.org/conn/kbd_connector_macplus.png
193 Signaling:
194     http://www.kbdbabel.org/signaling/kbd_signaling_mac.png
195     http://typematic.blog.shinobi.jp/Entry/14/
196 Scan Codes:
197     Page 22 of Tech Info for 128K/512K
198     Page 07 of Tech Info for Plus
199     http://m0115.web.fc2.com/m0110.jpg
200     http://m0115.web.fc2.com/m0110a.jpg
201 */
202
203
204 #define WAIT_US(stat, us, err) do { \
205     if (!wait_##stat(us)) { \
206         m0110_error = err; \
207         goto ERROR; \
208     } \
209 } while (0)
210
211 #define WAIT_MS(stat, ms, err) do { \
212     uint16_t _ms = ms; \
213     while (_ms) { \
214         if (wait_##stat(1000)) { \
215             break; \
216         } \
217         _ms--; \
218     } \
219     if (_ms == 0) { \
220         m0110_error = err; \
221         goto ERROR; \
222     } \
223 } while (0)
224
225
226 uint8_t m0110_error = 0;
227
228
229 void m0110_init(void)
230 {
231     uint8_t data;
232     idle();
233     _delay_ms(1000);
234
235     // Model Number
236     // M0110 : 0x09  00001001 : model number 4 (100)
237     // M0110A: 0x0B  00001011 : model number 5 (101)
238     // M0110 & M0120: ???
239     m0110_send(M0110_MODEL);
240     data = m0110_recv();
241     print("m0110_init model: "); phex(data); print("\n");
242
243     m0110_send(M0110_TEST);
244     data = m0110_recv();
245     print("m0110_init test: "); phex(data); print("\n");
246 }
247
248 uint8_t m0110_send(uint8_t data)
249 {
250     m0110_error = 0;
251
252     request();
253     WAIT_MS(clock_lo, 250, 1);  // keyboard may block long time
254     for (uint8_t bit = 0x80; bit; bit >>= 1) {
255         WAIT_US(clock_lo, 250, 3);
256         if (data&bit) {
257             data_hi();
258         } else {
259             data_lo();
260         }
261         WAIT_US(clock_hi, 200, 4);
262     }
263     _delay_us(100); // hold last bit for 80us
264     idle();
265     return 1;
266 ERROR:
267     print("m0110_send err: "); phex(m0110_error); print("\n");
268     _delay_ms(500);
269     idle();
270     return 0;
271 }
272
273 uint8_t m0110_recv(void)
274 {
275     uint8_t data = 0;
276     m0110_error = 0;
277
278     WAIT_MS(clock_lo, 250, 1);  // keyboard may block long time
279     for (uint8_t i = 0; i < 8; i++) {
280         data <<= 1;
281         WAIT_US(clock_lo, 200, 2);
282         WAIT_US(clock_hi, 200, 3);
283         if (data_in()) {
284             data |= 1;
285         }
286     }
287     idle();
288     return data;
289 ERROR:
290     print("m0110_recv err: "); phex(m0110_error); print("\n");
291     _delay_ms(500);
292     idle();
293     return 0xFF;
294 }
295
296 uint8_t m0110_recv_key(void)
297 {
298     static uint8_t keybuf = 0x00;
299     uint8_t key, key2, key3;
300
301     if (keybuf) {
302         key = keybuf;
303         keybuf = 0x00;
304         return key;
305     }
306     key = instant();  // Use INSTANT for better response. Should be INQUIRY ?
307     switch (key) {
308         case M0110_KEYPAD:
309             // Pad/Arrow keys
310             return (M0110_RAW2SCAN(instant()) | M0110_KEYPAD_OFFSET);
311             break;
312         case M0110_SHIFT_MAKE:
313         case M0110_SHIFT_BREAK:
314             key2 = instant();
315             if (key2 == M0110_KEYPAD) {
316                 key3 = instant();
317                 switch (key3) {
318                     case M0110_ARROW_UP:
319                     case M0110_ARROW_DOWN:
320                     case M0110_ARROW_LEFT:
321                     case M0110_ARROW_RIGHT:
322                         // Calc keys
323                         return (M0110_RAW2SCAN(key3) | M0110_CALC_OFFSET);
324                     default:
325                         // Shift + Pad/Arrow keys
326                         keybuf = M0110_RAW2SCAN(key3);
327                         return (M0110_RAW2SCAN(key) | M0110_KEYPAD_OFFSET);
328                 }
329             } else {
330                 // Shift + other keys
331                 keybuf = M0110_RAW2SCAN(key2);
332                 return M0110_RAW2SCAN(key);
333             }
334             break;
335         default:
336             // other keys
337             return M0110_RAW2SCAN(key);
338             break;
339     }
340 }
341
342
343 static inline uint8_t inquiry(void)
344 {
345     m0110_send(M0110_INQUIRY);
346     return m0110_recv();
347 }
348
349 static inline uint8_t instant(void)
350 {
351     m0110_send(M0110_INSTANT);
352     return m0110_recv();
353 }
354
355 static inline void clock_lo()
356 {
357     M0110_CLOCK_PORT &= ~(1<<M0110_CLOCK_BIT);
358     M0110_CLOCK_DDR  |=  (1<<M0110_CLOCK_BIT);
359 }
360 static inline void clock_hi()
361 {
362     /* input with pull up */
363     M0110_CLOCK_DDR  &= ~(1<<M0110_CLOCK_BIT);
364     M0110_CLOCK_PORT |=  (1<<M0110_CLOCK_BIT);
365 }
366 static inline bool clock_in()
367 {
368     M0110_CLOCK_DDR  &= ~(1<<M0110_CLOCK_BIT);
369     M0110_CLOCK_PORT |=  (1<<M0110_CLOCK_BIT);
370     _delay_us(1);
371     return M0110_CLOCK_PIN&(1<<M0110_CLOCK_BIT);
372 }
373 static inline void data_lo()
374 {
375     M0110_DATA_PORT &= ~(1<<M0110_DATA_BIT);
376     M0110_DATA_DDR  |=  (1<<M0110_DATA_BIT);
377 }
378 static inline void data_hi()
379 {
380     /* input with pull up */
381     M0110_DATA_DDR  &= ~(1<<M0110_DATA_BIT);
382     M0110_DATA_PORT |=  (1<<M0110_DATA_BIT);
383 }
384 static inline bool data_in()
385 {
386     M0110_DATA_DDR  &= ~(1<<M0110_DATA_BIT);
387     M0110_DATA_PORT |=  (1<<M0110_DATA_BIT);
388     _delay_us(1);
389     return M0110_DATA_PIN&(1<<M0110_DATA_BIT);
390 }
391
392 static inline uint16_t wait_clock_lo(uint16_t us)
393 {
394     while (clock_in()  && us) { asm(""); _delay_us(1); us--; }
395     return us;
396 }
397 static inline uint16_t wait_clock_hi(uint16_t us)
398 {
399     while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
400     return us;
401 }
402 static inline uint16_t wait_data_lo(uint16_t us)
403 {
404     while (data_in() && us)  { asm(""); _delay_us(1); us--; }
405     return us;
406 }
407 static inline uint16_t wait_data_hi(uint16_t us)
408 {
409     while (!data_in() && us)  { asm(""); _delay_us(1); us--; }
410     return us;
411 }
412
413 static inline void idle(void)
414 {
415     clock_hi();
416     data_hi();
417 }
418
419 static inline void request(void)
420 {
421     clock_hi();
422     data_lo();
423 }