]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/ibmpc.c
ibmpc: Fix stop bit check code in ISR
[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 "ringbuf.h"
46 #include "ibmpc.h"
47 #include "debug.h"
48 #include "timer.h"
49 #include "wait.h"
50
51
52 #define WAIT(stat, us, err) do { \
53     if (!wait_##stat(us)) { \
54         ibmpc_error = err; \
55         goto ERROR; \
56     } \
57 } while (0)
58
59
60 #define BUF_SIZE 16
61 static uint8_t buf[BUF_SIZE];
62 static ringbuf_t rb = {
63     .buffer = buf,
64     .head = 0,
65     .tail = 0,
66     .size_mask = BUF_SIZE - 1
67 };
68
69 volatile uint8_t ibmpc_protocol = IBMPC_PROTOCOL_AT;
70 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
71
72 void ibmpc_host_init(void)
73 {
74     // initialize reset pin to HiZ
75     IBMPC_RST_HIZ();
76     inhibit();
77     IBMPC_INT_INIT();
78     IBMPC_INT_OFF();
79 }
80
81 void ibmpc_host_enable(void)
82 {
83     IBMPC_INT_ON();
84     idle();
85 }
86
87 void ibmpc_host_disable(void)
88 {
89     IBMPC_INT_OFF();
90     inhibit();
91 }
92
93 int16_t ibmpc_host_send(uint8_t data)
94 {
95     bool parity = true;
96     ibmpc_error = IBMPC_ERR_NONE;
97
98     if (ibmpc_protocol == IBMPC_PROTOCOL_XT) return -1;
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     ringbuf_reset(&rb);
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 with ISR
160  */
161 static volatile int16_t recv_data = -1;
162 static volatile uint16_t isr_data = 0x8000;
163
164 void ibmpc_host_isr_clear(void)
165 {
166     isr_data = 0x8000;
167 }
168
169 int16_t ibmpc_host_recv(void)
170 {
171     int16_t data = 0;
172     ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
173         data = recv_data;
174         recv_data = -1;
175     }
176     if (data != -1) {
177         dprintf("r%04X ", data);
178     }
179     return data;
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 // NOTE: to read data line early as possible:
195 // write naked ISR with asembly code to read the line and call C func to do other job?
196 ISR(IBMPC_INT_VECT)
197 {
198     uint8_t dbit;
199     dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
200     isr_data = isr_data>>1;
201     if (dbit) isr_data |= 0x8000;
202
203     // isr_data:
204     //            15 14 13 12   11 10  9  8    7  6  5  4    3  2  1  0
205     //            -----------------------------------------------------
206     // Initial:   *1  0  0  0    0  0  0  0 |  0  0  0  0    0  0  0  0
207     // XT IBM:    b7 b6 b5 b4   b3 b2 b1 b0 | s1 s0 *1  0    0  0  0  0     after receiving **
208     // XT Clone:  b7 b6 b5 b4   b3 b2 b1 b0 | s1 *1  0  0    0  0  0  0     after receiving
209     // AT:        st pr b7 b6   b5 b4 b3 b2 | b1 b0 s0 *1    0  0  0  0     after receiving
210     // AT**:      pr b7 b6 b5   b4 b3 b2 b1 | b0 s0 *1  0    0  0  0  0     before stop bit **
211     //
212     //             x  x  x  x    x  x  x  x |  0  0  0  0    0  0  0  0     midway(0-7 bits received)
213     //             x  x  x  x    x  x  x  x |  1  0  0  0    0  0  0  0     midway(8 bits received)
214     //             x  x  x  x    x  x  x  x |  0  1  0  0    0  0  0  0     XT IBM-midway or AT-midway
215     //             x  x  x  x    x  x  x  x |  1  1  0  0    0  0  0  0     XT Clone-done
216     //             x  x  x  x    x  x  x  x |  0  0  1  0    0  0  0  0     AT-midway
217     //             x  x  x  x    x  x  x  x |  1  0  1  0    0  0  0  0     XT IBM-done or AT-midway **
218     //             x  x  x  x    x  x  x  x |  x  1  1  0    0  0  0  0     illegal
219     //             x  x  x  x    x  x  x  x |  x  x  0  1    0  0  0  0     AT-done
220     //             x  x  x  x    x  x  x  x |  x  x  1  1    0  0  0  0     illegal
221     //                                          other states than avobe     illegal
222     //
223     // **: AT can take same as end sate of XT IBM(1010 000) when b0 is 1,
224     // to discriminate between them we will have to wait a while for stop bit.
225     //
226     // mask for isr_data:
227     // 0x00A0(1010 0000) when XT IBM
228     // 0x00C0(1100 0000) when XT Clone
229     // 0x0010(xx01 0000) when AT
230     //
231     switch (isr_data & 0xFF) {
232         case 0b00000000:
233         case 0b10000000:
234         case 0b01000000:
235         case 0b00100000:
236             // midway
237             return;
238             break;
239         case 0b11000000:
240             // XT Clone-done
241             recv_data = (isr_data>>8) & 0xFF;
242             goto DONE;
243             break;
244         case 0b10100000:
245             {
246                 uint8_t us = 150;
247                 // wait for rising and falling edge of AT stop bit
248                 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
249                 while (  IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)  && us) { wait_us(1); us--; }
250
251                 if (us) {
252                     // found stop bit: return immediately and process the stop bit in ISR
253                     // AT-midway
254                     return;
255                 } else {
256                     // no stop bit
257                     // XT-IBM-done
258                     recv_data = (isr_data>>8) & 0xFF;
259                     goto DONE;
260                 }
261              }
262             break;
263         case 0b00010000:
264         case 0b10010000:
265         case 0b01010000:
266         case 0b11010000:
267             // AT-done
268             recv_data = (isr_data>>6) & 0xFF;
269             goto DONE;
270             break;
271         case 0b01100000:
272         case 0b11100000:
273         case 0b00110000:
274         case 0b10110000:
275         case 0b01110000:
276         case 0b11110000:
277         default:            // xxxx_oooo(any 1 in low nibble)
278             recv_data = isr_data;
279             break;
280     }
281 DONE:
282     // TODO: buffer for recv_data
283     isr_data = 0x8000;  // clear to next data
284     return;
285 }
286
287 /* send LED state to keyboard */
288 void ibmpc_host_set_led(uint8_t led)
289 {
290     ibmpc_host_send(0xED);
291     ibmpc_host_send(led);
292 }