]>
git.gir.st - tmk_keyboard.git/blob - m0110.c
2 Copyright 2011 Jun WAKO <wakojun@gmail.com>
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.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
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
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.
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.
40 #include <avr/interrupt.h>
41 #include <util/delay.h>
46 static inline void clock_lo(void);
47 static inline void clock_hi(void);
48 static inline bool clock_in(void);
49 static inline void data_lo(void);
50 static inline void data_hi(void);
51 static inline bool data_in(void);
52 static inline uint16_t wait_clock_lo(uint16_t us
);
53 static inline uint16_t wait_clock_hi(uint16_t us
);
54 static inline uint16_t wait_data_lo(uint16_t us
);
55 static inline uint16_t wait_data_hi(uint16_t us
);
56 static inline void idle(void);
57 static inline void request(void);
61 Primitive M0110 Library for AVR
62 ==============================
67 CLOCK is always from KEYBOARD. DATA are sent with MSB first.
69 1) IDLE: both line is high.
70 CLOCK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71 DATA ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73 2) KEYBOARD->HOST: HOST reads bit on rising edge.
74 CLOCK ~~~~~~~~~~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~~~~~~~~~
75 DATA ~~~~~~~~~~~~X777777X666666X555555X444444X333333X222222X111111X000000X~~~~~~~
77 <---> 180us(clock high)
79 3) HOST->KEYBOARD: HOST asserts bit on falling edge.
80 CLOCK ~~~~~~~~~~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~~~~~~~~~
81 DATA ~~~~~~|_____X777777X666666X555555X444444X333333X222222X111111X000000X~~~~~~~
82 <----> 840us(request to send by host) <-> 80us(hold DATA)
84 <---> 220us(clock high)
90 Inquiry 0x10 get key event
91 Instant 0x12 get key event
92 Model 0x14 get model number(M0110 responds with 0x09)
93 bit 7 1 if another device connected(used when keypad exists?)
94 bit4-6 next device model number
95 bit1-3 keyboard model number
97 Test 0x16 test(ACK:0x7D/NAK:0x77)
100 bit 7 key state(0:press 1:release)
103 To get scan code, use ((bits&(1<<7)) | ((bits&7F))>>1).
107 ,---------------------------------------------------------.
108 | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backs|
109 |---------------------------------------------------------|
110 |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \|
111 |---------------------------------------------------------|
112 |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return|
113 |---------------------------------------------------------|
114 |Shift | Z| X| C| V| B| N| M| ,| ,| /| |
115 `---------------------------------------------------------'
116 |Opt|Mac | Space |Enter|Opt|
117 `------------------------------------------------'
118 ,---------------------------------------------------------.
119 | 32| 12| 13| 14| 15| 17| 16| 1A| 1C| 19| 1D| 1B| 18| 33|
120 |---------------------------------------------------------|
121 | 30| 0C| 0D| 0E| 0F| 10| 11| 20| 22| 1F| 23| 21| 1E| 2A|
122 |---------------------------------------------------------|
123 | 39| 00| 01| 02| 03| 05| 04| 26| 28| 25| 29| 27| 24|
124 |---------------------------------------------------------|
125 | 38| 06| 07| 08| 09| 0B| 2D| 2E| 2B| 2F| 2C| 38|
126 `---------------------------------------------------------'
127 | 3A| 37| 31 | 34| 3A|
128 `------------------------------------------------'
134 http://www.mac.linux-m68k.org/devel/plushw.php
136 http://www.kbdbabel.org/conn/kbd_connector_macplus.png
138 http://www.kbdbabel.org/signaling/kbd_signaling_mac.png
139 http://typematic.blog.shinobi.jp/Entry/14/
141 http://m0115.web.fc2.com/m0110.jpg
142 http://m0115.web.fc2.com/m0110a.jpg
146 #define WAIT(stat, us, err) do { \
147 if (!wait_##stat(us)) { \
154 uint8_t m0110_error
= 0;
157 void m0110_init(void)
163 m0110_send(M0110_MODLE
);
165 print("m0110_init model: "); phex(data
); print("\n");
167 m0110_send(M0110_TEST
);
169 print("m0110_init test: "); phex(data
); print("\n");
172 uint8_t m0110_send(uint8_t data
)
177 WAIT(clock_lo
, 1000, 0);
178 for (uint8_t bit
= 0x80; bit
; bit
>>= 1) {
179 WAIT(clock_lo
, 250, 3);
186 WAIT(clock_hi
, 200, 4);
188 _delay_us(100); // hold last bit for 80us
193 print("m0110_send err: "); phex(m0110_error
); print("\n");
199 uint8_t m0110_recv(void)
204 WAIT(clock_lo
, -1, 0); // need 250ms? insted 0xffff(16bit max)us
205 for (uint8_t i
= 0; i
< 8; i
++) {
207 WAIT(clock_lo
, 200, 2);
208 WAIT(clock_hi
, 200, 3);
214 print("m0110_send recv data: "); phex(data
); print("\n");
218 print("m0110_recv err: "); phex(m0110_error
); print("\n");
224 uint8_t m0110_recv_key(void)
227 m0110_send(M0110_INQUIRY
);
229 if (key
== 0xFF || key
== M0110_NULL
)
232 return (key
&(1<<7) | (key
&0x7F)>>1);
236 static inline void clock_lo()
238 M0110_CLOCK_PORT
&= ~(1<<M0110_CLOCK_BIT
);
239 M0110_CLOCK_DDR
|= (1<<M0110_CLOCK_BIT
);
241 static inline void clock_hi()
243 /* input with pull up */
244 M0110_CLOCK_DDR
&= ~(1<<M0110_CLOCK_BIT
);
245 M0110_CLOCK_PORT
|= (1<<M0110_CLOCK_BIT
);
247 static inline bool clock_in()
249 M0110_CLOCK_DDR
&= ~(1<<M0110_CLOCK_BIT
);
250 M0110_CLOCK_PORT
|= (1<<M0110_CLOCK_BIT
);
252 return M0110_CLOCK_PIN
&(1<<M0110_CLOCK_BIT
);
254 static inline void data_lo()
256 M0110_DATA_PORT
&= ~(1<<M0110_DATA_BIT
);
257 M0110_DATA_DDR
|= (1<<M0110_DATA_BIT
);
259 static inline void data_hi()
261 /* input with pull up */
262 M0110_DATA_DDR
&= ~(1<<M0110_DATA_BIT
);
263 M0110_DATA_PORT
|= (1<<M0110_DATA_BIT
);
265 static inline bool data_in()
267 M0110_DATA_DDR
&= ~(1<<M0110_DATA_BIT
);
268 M0110_DATA_PORT
|= (1<<M0110_DATA_BIT
);
270 return M0110_DATA_PIN
&(1<<M0110_DATA_BIT
);
273 static inline uint16_t wait_clock_lo(uint16_t us
)
275 while (clock_in() && us
) { asm(""); _delay_us(1); us
--; }
278 static inline uint16_t wait_clock_hi(uint16_t us
)
280 while (!clock_in() && us
) { asm(""); _delay_us(1); us
--; }
283 static inline uint16_t wait_data_lo(uint16_t us
)
285 while (data_in() && us
) { asm(""); _delay_us(1); us
--; }
288 static inline uint16_t wait_data_hi(uint16_t us
)
290 while (!data_in() && us
) { asm(""); _delay_us(1); us
--; }
294 static inline void idle(void)
300 static inline void request(void)