]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/ibmpc.c
2e1309f89a24a4219043b54dc7a527c2df9d7d46
[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_AT;
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_data = 0x8000;
72
73 void ibmpc_host_init(void)
74 {
75     // initialize reset pin to HiZ
76     IBMPC_RST_HIZ();
77     inhibit();
78     IBMPC_INT_INIT();
79     IBMPC_INT_OFF();
80 }
81
82 void ibmpc_host_enable(void)
83 {
84     IBMPC_INT_ON();
85     idle();
86 }
87
88 void ibmpc_host_disable(void)
89 {
90     IBMPC_INT_OFF();
91     inhibit();
92 }
93
94 int16_t ibmpc_host_send(uint8_t data)
95 {
96     bool parity = true;
97     ibmpc_error = IBMPC_ERR_NONE;
98
99     if (ibmpc_protocol == IBMPC_PROTOCOL_XT) return -1;
100
101     dprintf("w%02X ", data);
102
103     IBMPC_INT_OFF();
104
105     /* terminate a transmission if we have */
106     inhibit();
107     wait_us(100); // 100us [4]p.13, [5]p.50
108
109     /* 'Request to Send' and Start bit */
110     data_lo();
111     clock_hi();
112     WAIT(clock_lo, 10000, 1);   // 10ms [5]p.50
113
114     /* Data bit[2-9] */
115     for (uint8_t i = 0; i < 8; i++) {
116         wait_us(15);
117         if (data&(1<<i)) {
118             parity = !parity;
119             data_hi();
120         } else {
121             data_lo();
122         }
123         WAIT(clock_hi, 50, 2);
124         WAIT(clock_lo, 50, 3);
125     }
126
127     /* Parity bit */
128     wait_us(15);
129     if (parity) { data_hi(); } else { data_lo(); }
130     WAIT(clock_hi, 50, 4);
131     WAIT(clock_lo, 50, 5);
132
133     /* Stop bit */
134     wait_us(15);
135     data_hi();
136
137     /* Ack */
138     WAIT(data_lo, 50, 6);
139     WAIT(clock_lo, 50, 7);
140
141     /* wait for idle state */
142     WAIT(clock_hi, 50, 8);
143     WAIT(data_hi, 50, 9);
144
145     // clear buffer to get response correctly
146     recv_data = 0xFFFF;
147     ibmpc_host_isr_clear();
148
149     idle();
150     IBMPC_INT_ON();
151     return ibmpc_host_recv_response();
152 ERROR:
153     ibmpc_error |= IBMPC_ERR_SEND;
154     idle();
155     IBMPC_INT_ON();
156     return -1;
157 }
158
159 /*
160  * Receive data from keyboard
161  */
162 int16_t ibmpc_host_recv(void)
163 {
164     uint16_t data = 0;
165     uint8_t ret = 0xFF;
166
167     ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
168         data = recv_data;
169         if ((data&0xFF00) != 0xFF00) {      // recv_data:sstt -> recv_data:FFtt, ret:ss
170             ret = (data>>8)&0x00FF;
171             recv_data = data | 0xFF00;
172         } else if (data != 0xFFFF) {        // recv_data:FFss -> recv_data:FFFF, ret:ss
173             ret = data&0x00FF;
174             recv_data = data | 0x00FF;
175         }
176     }
177
178     if (ret != 0xFF) {
179         dprintf("r%02X(%04X) ", ret, recv_data);
180     }
181     return ((ret != 0xFF) ? ret : -1);
182
183 }
184
185 int16_t ibmpc_host_recv_response(void)
186 {
187     // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
188     uint8_t retry = 25;
189     int16_t data = -1;
190     while (retry-- && (data = ibmpc_host_recv()) == -1) {
191         wait_ms(1);
192     }
193     return data;
194 }
195
196 void ibmpc_host_isr_clear(void)
197 {
198     isr_data = 0x8000;
199     recv_data = 0xFFFF;
200 }
201
202 // NOTE: With this ISR data line can be read within 2us after clock falling edge.
203 // To read data line early as possible:
204 // write naked ISR with asembly code to read the line and call C func to do other job?
205 ISR(IBMPC_INT_VECT)
206 {
207     uint8_t dbit;
208     dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
209     isr_data = isr_data>>1;
210     if (dbit) isr_data |= 0x8000;
211
212     // isr_data: state of receiving data from keyboard
213     //            15 14 13 12   11 10  9  8    7  6  5  4    3  2  1  0
214     //            -----------------------------------------------------
215     // Initial:   *1  0  0  0    0  0  0  0 |  0  0  0  0    0  0  0  0
216     // XT IBM:    b7 b6 b5 b4   b3 b2 b1 b0 | s1 s0 *1  0    0  0  0  0     after receiving **
217     // XT Clone:  b7 b6 b5 b4   b3 b2 b1 b0 | s1 *1  0  0    0  0  0  0     after receiving
218     // AT:        st pr b7 b6   b5 b4 b3 b2 | b1 b0 s0 *1    0  0  0  0     after receiving
219     // AT**:      pr b7 b6 b5   b4 b3 b2 b1 | b0 s0 *1  0    0  0  0  0     before stop bit **
220     //
221     //             x  x  x  x    x  x  x  x |  0  0  0  0    0  0  0  0     midway(0-7 bits received)
222     //             x  x  x  x    x  x  x  x |  1  0  0  0    0  0  0  0     midway(8 bits received)
223     //             x  x  x  x    x  x  x  x |  0  1  0  0    0  0  0  0     XT IBM-midway or AT-midway
224     //             x  x  x  x    x  x  x  x |  1  1  0  0    0  0  0  0     XT Clone-done
225     //             x  x  x  x    x  x  x  x |  0  0  1  0    0  0  0  0     AT-midway
226     //             x  x  x  x    x  x  x  x |  1  0  1  0    0  0  0  0     XT IBM-done or AT-midway **
227     //             x  x  x  x    x  x  x  x |  x  1  1  0    0  0  0  0     illegal
228     //             x  x  x  x    x  x  x  x |  x  x  0  1    0  0  0  0     AT-done
229     //             x  x  x  x    x  x  x  x |  x  x  1  1    0  0  0  0     illegal
230     //                                          other states than avobe     illegal
231     //
232     // **: AT can take same as end sate of XT IBM(1010 000) when b0 is 1,
233     // to discriminate between them we will have to wait a while for stop bit.
234     //
235     // mask for isr_data:
236     // 0x00A0(1010 0000) when XT IBM
237     // 0x00C0(1100 0000) when XT Clone
238     // 0x0010(xx01 0000) when AT
239     //
240     switch (isr_data & 0xFF) {
241         case 0b00000000:
242         case 0b10000000:
243         case 0b01000000:
244         case 0b00100000:
245             // midway
246             return;
247             break;
248         case 0b11000000:
249             // XT Clone-done
250             recv_data = recv_data<<8;
251             recv_data |= (isr_data>>8) & 0xFF;
252             goto DONE;
253             break;
254         case 0b10100000:
255             {
256                 uint8_t us = 150;
257                 // wait for rising and falling edge of AT stop bit
258                 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
259                 while (  IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)  && us) { wait_us(1); us--; }
260
261                 if (us) {
262                     // found stop bit: return immediately and process the stop bit in ISR
263                     // AT-midway
264                     return;
265                 } else {
266                     // no stop bit
267                     // XT-IBM-done
268                     recv_data = recv_data<<8;
269                     recv_data |= (isr_data>>8) & 0xFF;
270                     goto DONE;
271                 }
272              }
273             break;
274         case 0b00010000:
275         case 0b10010000:
276         case 0b01010000:
277         case 0b11010000:
278             // AT-done
279             recv_data = recv_data<<8;
280             recv_data |= (isr_data>>6) & 0xFF;
281             goto DONE;
282             break;
283         case 0b01100000:
284         case 0b11100000:
285         case 0b00110000:
286         case 0b10110000:
287         case 0b01110000:
288         case 0b11110000:
289         default:            // xxxx_oooo(any 1 in low nibble)
290             recv_data = isr_data;
291             break;
292     }
293 DONE:
294     // TODO: check protocol change to support keyboard howswap
295     //       not correct if there is clock edge within short time like 100us after receving data
296     // TODO: process error code: 0x00(AT), 0xFF(XT) in particular
297     isr_data = 0x8000;  // clear to next data
298     return;
299 }
300
301 /* send LED state to keyboard */
302 void ibmpc_host_set_led(uint8_t led)
303 {
304     ibmpc_host_send(0xED);
305     ibmpc_host_send(led);
306 }