]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/ibmpc.c
ibmpc: Protocol detection between AT and XT
[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 uint8_t ibmpc_protocol = IBMPC_PROTOCOL_NO;
60 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
61
62 /* 2-byte buffer for data received from keyhboard
63  * buffer states:
64  *      FFFF: empty
65  *      FFss: one data
66  *      sstt: two data(full)
67  *  0xFF can not be stored as data in buffer because it means empty or no data.
68  */
69 static volatile uint16_t recv_data = 0xFFFF;
70 /* internal state of receiving data */
71 static volatile uint16_t isr_state = 0x8000;
72 static uint8_t timer_start = 0;
73
74 void ibmpc_host_init(void)
75 {
76     // initialize reset pin to HiZ
77     IBMPC_RST_HIZ();
78     inhibit();
79     IBMPC_INT_INIT();
80     IBMPC_INT_OFF();
81 }
82
83 void ibmpc_host_enable(void)
84 {
85     IBMPC_INT_ON();
86     idle();
87 }
88
89 void ibmpc_host_disable(void)
90 {
91     IBMPC_INT_OFF();
92     inhibit();
93 }
94
95 int16_t ibmpc_host_send(uint8_t data)
96 {
97     bool parity = true;
98     ibmpc_error = IBMPC_ERR_NONE;
99
100     dprintf("w%02X ", data);
101
102     IBMPC_INT_OFF();
103
104     /* terminate a transmission if we have */
105     inhibit();
106     wait_us(100); // 100us [4]p.13, [5]p.50
107
108     /* 'Request to Send' and Start bit */
109     data_lo();
110     clock_hi();
111     WAIT(clock_lo, 10000, 1);   // 10ms [5]p.50
112
113     /* Data bit[2-9] */
114     for (uint8_t i = 0; i < 8; i++) {
115         wait_us(15);
116         if (data&(1<<i)) {
117             parity = !parity;
118             data_hi();
119         } else {
120             data_lo();
121         }
122         WAIT(clock_hi, 50, 2);
123         WAIT(clock_lo, 50, 3);
124     }
125
126     /* Parity bit */
127     wait_us(15);
128     if (parity) { data_hi(); } else { data_lo(); }
129     WAIT(clock_hi, 50, 4);
130     WAIT(clock_lo, 50, 5);
131
132     /* Stop bit */
133     wait_us(15);
134     data_hi();
135
136     /* Ack */
137     WAIT(data_lo, 50, 6);
138     WAIT(clock_lo, 50, 7);
139
140     /* wait for idle state */
141     WAIT(clock_hi, 50, 8);
142     WAIT(data_hi, 50, 9);
143
144     // clear buffer to get response correctly
145     recv_data = 0xFFFF;
146     ibmpc_host_isr_clear();
147
148     idle();
149     IBMPC_INT_ON();
150     return ibmpc_host_recv_response();
151 ERROR:
152     ibmpc_error |= IBMPC_ERR_SEND;
153     idle();
154     IBMPC_INT_ON();
155     return -1;
156 }
157
158 /*
159  * Receive data from keyboard
160  */
161 int16_t ibmpc_host_recv(void)
162 {
163     uint16_t data = 0;
164     uint8_t ret = 0xFF;
165
166     ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
167         data = recv_data;
168         if ((data&0xFF00) != 0xFF00) {      // recv_data:sstt -> recv_data:FFtt, ret:ss
169             ret = (data>>8)&0x00FF;
170             recv_data = data | 0xFF00;
171         } else if (data != 0xFFFF) {        // recv_data:FFss -> recv_data:FFFF, ret:ss
172             ret = data&0x00FF;
173             recv_data = data | 0x00FF;
174         }
175     }
176
177     if (ret != 0xFF) dprintf("r%02X ", ret);
178     if (recv_data != 0xFFFF) dprintf("b%04X ", recv_data);
179     return ((ret != 0xFF) ? ret : -1);
180
181 }
182
183 int16_t ibmpc_host_recv_response(void)
184 {
185     // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
186     uint8_t retry = 25;
187     int16_t data = -1;
188     while (retry-- && (data = ibmpc_host_recv()) == -1) {
189         wait_ms(1);
190     }
191     return data;
192 }
193
194 void ibmpc_host_isr_clear(void)
195 {
196     isr_state = 0x8000;
197     recv_data = 0xFFFF;
198 }
199
200 // NOTE: With this ISR data line can be read within 2us after clock falling edge.
201 // To read data line early as possible:
202 // write naked ISR with asembly code to read the line and call C func to do other job?
203 ISR(IBMPC_INT_VECT)
204 {
205     uint8_t dbit;
206     dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
207
208     // Timeout check
209     uint8_t t;
210     // use only the least byte of millisecond timer
211     asm("lds %0, %1" : "=r" (t) : "p" (&timer_count));
212     //t = (uint8_t)timer_count;    // compiler uses four registers instead of one
213     if (isr_state == 0x8000) {
214         timer_start = t;
215     } else {
216         // should not take more than 1ms
217         if (timer_start != t && (uint8_t)(timer_start + 1) != t) {
218             ibmpc_error = IBMPC_ERR_TIMEOUT;
219             //goto ERROR;
220             // timeout error recovery by clearing isr_state?
221             timer_start = t;
222             isr_state = 0x8000;
223         }
224     }
225
226     isr_state = isr_state>>1;
227     if (dbit) isr_state |= 0x8000;
228
229     // isr_state: state of receiving data from keyboard
230     //
231     // This should be initialized with 0x8000 before receiving data and
232     // the MSB '*1' works as marker to discrimitate between protocols.
233     // It stores sampled bit at MSB after right shift on each clock falling edge.
234     //
235     // XT protocol has two variants of signaling; XT_IBM and XT_Clone.
236     // XT_IBM uses two start bits 0 and 1 while XT_Clone uses just start bit 1.
237     // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
238     //
239     //      15 14 13 12   11 10  9  8    7  6  5  4    3  2  1  0
240     //      -----------------------------------------------------
241     //      *1  0  0  0    0  0  0  0 |  0  0  0  0    0  0  0  0     Initial state(0x8000)
242     //
243     //       x  x  x  x    x  x  x  x |  0  0  0  0    0  0  0  0     midway(0-7 bits received)
244     //       x  x  x  x    x  x  x  x | *1  0  0  0    0  0  0  0     midway(8 bits received)
245     //      b6 b5 b4 b3   b2 b1 b0  1 |  0 *1  0  0    0  0  0  0     XT_IBM-midway ^1
246     //      b7 b6 b5 b4   b3 b2 b1 b0 |  0 *1  0  0    0  0  0  0     AT-midway ^1
247     //      b7 b6 b5 b4   b3 b2 b1 b0 |  1 *1  0  0    0  0  0  0     XT_Clone-done
248     //      pr b7 b6 b5   b4 b3 b2 b1 |  0  0 *1  0    0  0  0  0     AT-midway[b0=0]
249     //      b7 b6 b5 b4   b3 b2 b1 b0 |  1  0 *1  0    0  0  0  0     XT_IBM-done ^2
250     //      pr b7 b6 b5   b4 b3 b2 b1 |  1  0 *1  0    0  0  0  0     AT-midway[b0=1] ^2
251     //       x  x  x  x    x  x  x  x |  x  1  1  0    0  0  0  0     illegal
252     //      st pr b7 b6   b5 b4 b3 b2 | b1 b0  0 *1    0  0  0  0     AT-done
253     //       x  x  x  x    x  x  x  x |  x  x  1 *1    0  0  0  0     illegal
254     //                                all other states than above     illegal
255     //
256     // ^1: AT and XT_IBM takes same state.
257     // ^2: AT and XT_IBM takes same state in case that AT b0 is 1,
258     // we have to check AT stop bit to discriminate between the two protocol.
259     switch (isr_state & 0xFF) {
260         case 0b00000000:
261         case 0b10000000:
262         case 0b01000000:    // ^1
263         case 0b00100000:
264             // midway
265             goto NEXT;
266             break;
267         case 0b11000000:
268             // XT_Clone-done
269             isr_state = isr_state>>8;
270             ibmpc_protocol = IBMPC_PROTOCOL_XT_CLONE;
271             goto DONE;
272             break;
273         case 0b10100000:    // ^2
274             {
275                 uint8_t us = 100;
276                 // wait for rising and falling edge of AT stop bit to discriminate between XT and AT
277                 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
278                 while (  IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)  && us) { wait_us(1); us--; }
279
280                 if (us) {
281                     // found stop bit: AT-midway - process the stop bit in next ISR
282                     goto NEXT;
283                 } else {
284                     // no stop bit: XT_IBM-done
285                     isr_state = isr_state>>8;
286                     ibmpc_protocol = IBMPC_PROTOCOL_XT_IBM;
287                     goto DONE;
288                 }
289              }
290             break;
291         case 0b00010000:
292         case 0b10010000:
293         case 0b01010000:
294         case 0b11010000:
295             // AT-done
296             // TODO: parity check?
297             isr_state = isr_state>>6;
298             ibmpc_protocol = IBMPC_PROTOCOL_AT;
299             goto DONE;
300             break;
301         case 0b01100000:
302         case 0b11100000:
303         case 0b00110000:
304         case 0b10110000:
305         case 0b01110000:
306         case 0b11110000:
307         default:            // xxxx_oooo(any 1 in low nibble)
308             // Illegal
309             ibmpc_error = IBMPC_ERR_ILLEGAL;
310             goto ERROR;
311             break;
312     }
313
314 ERROR:
315     isr_state = 0x8000;
316     recv_data = 0xFF00; // clear data and scancode of error 0x00
317     return;
318 DONE:
319     if ((isr_state & 0x00FF) == 0x00FF) {
320         // receive error code 0xFF
321         ibmpc_error = IBMPC_ERR_FF;
322     }
323     if ((recv_data & 0xFF00) != 0xFF00) {
324         // buffer full and overwritten
325         ibmpc_error = IBMPC_ERR_FULL;
326     }
327     recv_data = recv_data<<8;
328     recv_data |= isr_state & 0xFF;
329     isr_state = 0x8000;  // clear to next data
330 NEXT:
331     return;
332 }
333
334 /* send LED state to keyboard */
335 void ibmpc_host_set_led(uint8_t led)
336 {
337     ibmpc_host_send(0xED);
338     ibmpc_host_send(led);
339 }