]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/ibmpc.c
a4b8dfd86d121a88a50243bf5594e063de6828de
[max/tmk_keyboard.git] / tmk_core / protocol / ibmpc.c
1 /*
2 Copyright 2010,2011,2012,2013,2019 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 /*
39  * IBM PC keyboard protocol
40  */
41
42 #include <stdbool.h>
43 #include <avr/interrupt.h>
44 #include <util/atomic.h>
45 #include "ibmpc.h"
46 #include "debug.h"
47 #include "timer.h"
48 #include "wait.h"
49
50
51 #define WAIT(stat, us, err) do { \
52     if (!wait_##stat(us)) { \
53         ibmpc_error = err; \
54         goto ERROR; \
55     } \
56 } while (0)
57
58
59 volatile uint16_t ibmpc_isr_debug = 0;
60 volatile uint8_t ibmpc_protocol = IBMPC_PROTOCOL_NO;
61 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
62
63 /* 2-byte buffer for data received from keyboard
64  * buffer states:
65  *      FFFF: empty
66  *      FFss: one data
67  *      sstt: two data
68  *      eeFF: error
69  * where ss, tt and ee are 0x00-0xFE. 0xFF means empty or no data in buffer.
70  */
71 static volatile uint16_t recv_data = 0xFFFF;
72 /* internal state of receiving data */
73 static volatile uint16_t isr_state = 0x8000;
74 static uint8_t timer_start = 0;
75
76 void ibmpc_host_init(void)
77 {
78     // initialize reset pin to HiZ
79     IBMPC_RST_HIZ();
80     inhibit();
81     IBMPC_INT_INIT();
82     IBMPC_INT_OFF();
83 }
84
85 void ibmpc_host_enable(void)
86 {
87     IBMPC_INT_ON();
88     idle();
89 }
90
91 void ibmpc_host_disable(void)
92 {
93     IBMPC_INT_OFF();
94     inhibit();
95 }
96
97 int16_t ibmpc_host_send(uint8_t data)
98 {
99     bool parity = true;
100     ibmpc_error = IBMPC_ERR_NONE;
101
102     dprintf("w%02X ", data);
103
104     IBMPC_INT_OFF();
105
106     /* terminate a transmission if we have */
107     inhibit();
108     wait_us(100); // 100us [4]p.13, [5]p.50
109
110     /* 'Request to Send' and Start bit */
111     data_lo();
112     clock_hi();
113     WAIT(clock_lo, 10000, 1);   // 10ms [5]p.50
114
115     /* Data bit[2-9] */
116     for (uint8_t i = 0; i < 8; i++) {
117         wait_us(15);
118         if (data&(1<<i)) {
119             parity = !parity;
120             data_hi();
121         } else {
122             data_lo();
123         }
124         WAIT(clock_hi, 50, 2);
125         WAIT(clock_lo, 50, 3);
126     }
127
128     /* Parity bit */
129     wait_us(15);
130     if (parity) { data_hi(); } else { data_lo(); }
131     WAIT(clock_hi, 50, 4);
132     WAIT(clock_lo, 50, 5);
133
134     /* Stop bit */
135     wait_us(15);
136     data_hi();
137
138     /* Ack */
139     WAIT(data_lo, 50, 6);
140     WAIT(clock_lo, 50, 7);
141
142     /* wait for idle state */
143     WAIT(clock_hi, 50, 8);
144     WAIT(data_hi, 50, 9);
145
146     // clear buffer to get response correctly
147     recv_data = 0xFFFF;
148     ibmpc_host_isr_clear();
149
150     idle();
151     IBMPC_INT_ON();
152     return ibmpc_host_recv_response();
153 ERROR:
154     ibmpc_error |= IBMPC_ERR_SEND;
155     idle();
156     IBMPC_INT_ON();
157     return -1;
158 }
159
160 /*
161  * Receive data from keyboard
162  */
163 int16_t ibmpc_host_recv(void)
164 {
165     uint16_t data = 0;
166     uint8_t ret = 0xFF;
167
168     ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
169         data = recv_data;
170
171         // remove data from buffer:
172         // FFFF(empty)      -> FFFF
173         // FFss(one data)   -> FFFF
174         // sstt(two data)   -> FFtt
175         // eeFF(errror)     -> FFFF
176         recv_data = data | (((data&0xFF00) == 0xFF00) ? 0x00FF : 0xFF00);
177     }
178
179     if ((data&0x00FF) == 0x00FF) {
180         // error: eeFF
181         switch (data>>8) {
182             case IBMPC_ERR_FF:
183                 // 0xFF(Overrun/Error) from keyboard
184                 dprintf("!FF! ");
185                 ret = 0xFF;
186                 break;
187             case IBMPC_ERR_FULL:
188                 // buffer full
189                 dprintf("!FULL! ");
190                 ret = 0xFF;
191                 break;
192             case 0xFF:
193                 // empty: FFFF
194                 return -1;
195             default:
196                 // other errors
197                 dprintf("e%02X ", data>>8);
198                 return -1;
199         }
200     } else {
201         if ((data | 0x00FF) != 0xFFFF) {
202             // two data: sstt
203             dprintf("b:%04X ", data);
204             ret = (data>>8);
205         } else {
206             // one data: FFss
207             ret = (data&0x00FF);
208         }
209     }
210
211     //dprintf("i%04X ", ibmpc_isr_debug); ibmpc_isr_debug = 0;
212     dprintf("r%02X ", ret);
213     return ret;
214 }
215
216 int16_t ibmpc_host_recv_response(void)
217 {
218     // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
219     uint8_t retry = 25;
220     int16_t data = -1;
221     while (retry-- && (data = ibmpc_host_recv()) == -1) {
222         wait_ms(1);
223     }
224     return data;
225 }
226
227 void ibmpc_host_isr_clear(void)
228 {
229     ibmpc_isr_debug = 0;
230     ibmpc_protocol = 0;
231     ibmpc_error = 0;
232     isr_state = 0x8000;
233     recv_data = 0xFFFF;
234 }
235
236 #define LO8(w)  (*((uint8_t *)&(w)))
237 #define HI8(w)  (*(((uint8_t *)&(w))+1))
238 // NOTE: With this ISR data line can be read within 2us after clock falling edge.
239 // To read data line early as possible:
240 // write naked ISR with asembly code to read the line and call C func to do other job?
241 ISR(IBMPC_INT_VECT)
242 {
243     uint8_t dbit;
244     dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
245
246     // Timeout check
247     uint8_t t;
248     // use only the least byte of millisecond timer
249     asm("lds %0, %1" : "=r" (t) : "p" (&timer_count));
250     //t = (uint8_t)timer_count;    // compiler uses four registers instead of one
251     if (isr_state == 0x8000) {
252         timer_start = t;
253     } else {
254         // should not take more than 1ms
255         if (timer_start != t && (uint8_t)(timer_start + 1) != t) {
256             ibmpc_isr_debug = isr_state;
257             ibmpc_error = IBMPC_ERR_TIMEOUT;
258             goto ERROR;
259
260             // timeout error recovery - start receiving new data
261             // it seems to work somehow but may not under unstable situation
262             //timer_start = t;
263             //isr_state = 0x8000;
264         }
265     }
266
267     isr_state = isr_state>>1;
268     if (dbit) isr_state |= 0x8000;
269
270     // isr_state: state of receiving data from keyboard
271     //
272     // This should be initialized with 0x8000 before receiving data and
273     // the MSB '*1' works as marker to discrimitate between protocols.
274     // It stores sampled bit at MSB after right shift on each clock falling edge.
275     //
276     // XT protocol has two variants of signaling; XT_IBM and XT_Clone.
277     // XT_IBM uses two start bits 0 and 1 while XT_Clone uses just start bit 1.
278     // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
279     //
280     //      15 14 13 12   11 10  9  8    7  6  5  4    3  2  1  0
281     //      -----------------------------------------------------
282     //      *1  0  0  0    0  0  0  0 |  0  0  0  0    0  0  0  0     Initial state(0x8000)
283     //
284     //       x  x  x  x    x  x  x  x |  0  0  0  0    0  0  0  0     midway(0-7 bits received)
285     //       x  x  x  x    x  x  x  x | *1  0  0  0    0  0  0  0     midway(8 bits received)
286     //      b6 b5 b4 b3   b2 b1 b0  1 |  0 *1  0  0    0  0  0  0     XT_IBM-midway ^1
287     //      b7 b6 b5 b4   b3 b2 b1 b0 |  0 *1  0  0    0  0  0  0     AT-midway ^1
288     //      b7 b6 b5 b4   b3 b2 b1 b0 |  1 *1  0  0    0  0  0  0     XT_Clone-done ^3
289     //      b6 b5 b4 b3   b2 b1 b0  1 |  1 *1  0  0    0  0  0  0     XT_IBM-error ^3
290     //      pr b7 b6 b5   b4 b3 b2 b1 |  0  0 *1  0    0  0  0  0     AT-midway[b0=0]
291     //      b7 b6 b5 b4   b3 b2 b1 b0 |  1  0 *1  0    0  0  0  0     XT_IBM-done ^2
292     //      pr b7 b6 b5   b4 b3 b2 b1 |  1  0 *1  0    0  0  0  0     AT-midway[b0=1] ^2
293     //      b7 b6 b5 b4   b3 b2 b1 b0 |  1  1 *1  0    0  0  0  0     XT_IBM-error-done
294     //       x  x  x  x    x  x  x  x |  x  1  1  0    0  0  0  0     illegal
295     //      st pr b7 b6   b5 b4 b3 b2 | b1 b0  0 *1    0  0  0  0     AT-done
296     //       x  x  x  x    x  x  x  x |  x  x  1 *1    0  0  0  0     illegal
297     //                                all other states than above     illegal
298     //
299     // ^1: AT and XT_IBM takes same state.
300     // ^2: AT and XT_IBM takes same state in case that AT b0 is 1,
301     // we have to check AT stop bit to discriminate between the two protocol.
302     switch (isr_state & 0xFF) {
303         case 0b00000000:
304         case 0b10000000:
305         case 0b01000000:    // ^1
306         case 0b00100000:
307             // midway
308             goto NEXT;
309             break;
310         case 0b11000000:    // ^3
311             {
312                 uint8_t us = 100;
313                 // wait for rising and falling edge of b7 of XT_IBM
314                 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
315                 while (  IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)  && us) { wait_us(1); us--; }
316
317                 if (us) {
318                     // XT_IBM-error: read start(0) as 1
319                     goto NEXT;
320                 } else {
321                     // XT_Clone-done
322                     ibmpc_isr_debug = isr_state;
323                     isr_state = isr_state>>8;
324                     ibmpc_protocol = IBMPC_PROTOCOL_XT_CLONE;
325                     goto DONE;
326                 }
327             }
328             break;
329         case 0b11100000:
330             // XT_IBM-error-done
331             ibmpc_isr_debug = isr_state;
332             isr_state = isr_state>>8;
333             ibmpc_protocol = IBMPC_PROTOCOL_XT_ERROR;
334             goto DONE;
335             break;
336         case 0b10100000:    // ^2
337             {
338                 uint8_t us = 100;
339                 // wait for rising and falling edge of AT stop bit to discriminate between XT and AT
340                 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
341                 while (  IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)  && us) { wait_us(1); us--; }
342
343                 if (us) {
344                     // found stop bit: AT-midway - process the stop bit in next ISR
345                     goto NEXT;
346                 } else {
347                     // no stop bit: XT_IBM-done
348                     ibmpc_isr_debug = isr_state;
349                     isr_state = isr_state>>8;
350                     ibmpc_protocol = IBMPC_PROTOCOL_XT_IBM;
351                     goto DONE;
352                 }
353              }
354             break;
355         case 0b00010000:
356         case 0b10010000:
357         case 0b01010000:
358         case 0b11010000:
359             // AT-done
360             // DO NOT check stop bit. Zenith Z-150(AT) asserts stop bit as low for no reason.
361             // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#zenith-z-150-beige
362             // TODO: parity check?
363             ibmpc_isr_debug = isr_state;
364             isr_state = isr_state>>6;
365             ibmpc_protocol = IBMPC_PROTOCOL_AT;
366             goto DONE;
367             break;
368         case 0b01100000:
369         case 0b00110000:
370         case 0b10110000:
371         case 0b01110000:
372         case 0b11110000:
373         default:            // xxxx_oooo(any 1 in low nibble)
374             // Illegal
375             ibmpc_isr_debug = isr_state;
376             ibmpc_error = IBMPC_ERR_ILLEGAL;
377             goto ERROR;
378             break;
379     }
380
381 ERROR:
382     // error: eeFF
383     recv_data = (ibmpc_error<<8) | 0x00FF;
384     goto CLEAR;
385 DONE:
386     if ((isr_state & 0x00FF) == 0x00FF) {
387         // receive error code 0xFF
388         ibmpc_error = IBMPC_ERR_FF;
389         goto ERROR;
390     }
391     if (HI8(recv_data) != 0xFF && LO8(recv_data) != 0xFF) {
392         // buffer full
393         ibmpc_error = IBMPC_ERR_FULL;
394         goto ERROR;
395     }
396     // store data
397     recv_data = recv_data<<8;
398     recv_data |= isr_state & 0xFF;
399 CLEAR:
400     // clear for next data
401     isr_state = 0x8000;
402 NEXT:
403     return;
404 }
405
406 /* send LED state to keyboard */
407 void ibmpc_host_set_led(uint8_t led)
408 {
409     if (0xFA == ibmpc_host_send(0xED)) {
410         ibmpc_host_send(led);
411     }
412 }