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