2 Copyright 2010,2011,2012,2013,2019 Jun WAKO <wakojun@gmail.com>
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.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
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
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.
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.
39 * IBM PC keyboard protocol
43 #include <avr/interrupt.h>
44 #include <util/atomic.h>
51 #define WAIT(stat, us, err) do { \
52 if (!wait_##stat(us)) { \
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;
63 /* 2-byte buffer for data received from keyboard
69 * where ss, tt and ee are 0x00-0xFE. 0xFF means empty or no data in buffer.
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;
76 void ibmpc_host_init(void)
78 // initialize reset pin to HiZ
85 void ibmpc_host_enable(void)
91 void ibmpc_host_disable(void)
97 int16_t ibmpc_host_send(uint8_t data)
100 ibmpc_error = IBMPC_ERR_NONE;
102 dprintf("w%02X ", data);
106 /* terminate a transmission if we have */
108 wait_us(100); // [5]p.54
110 /* 'Request to Send' and Start bit */
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
117 for (uint8_t i = 0; i < 8; i++) {
125 WAIT(clock_hi, 50, 2);
126 WAIT(clock_lo, 50, 3);
131 if (parity) { data_hi(); } else { data_lo(); }
132 WAIT(clock_hi, 50, 4);
133 WAIT(clock_lo, 50, 5);
138 WAIT(clock_hi, 50, 6);
139 if (ibmpc_protocol == IBMPC_PROTOCOL_AT_Z150) { goto RECV; }
140 WAIT(clock_lo, 50, 7);
143 WAIT(data_lo, 50, 8);
145 /* wait for idle state */
146 WAIT(clock_hi, 50, 9);
147 WAIT(data_hi, 50, 10);
150 // clear buffer to get response correctly
152 ibmpc_host_isr_clear();
156 return ibmpc_host_recv_response();
158 ibmpc_error |= IBMPC_ERR_SEND;
165 * Receive data from keyboard
167 int16_t ibmpc_host_recv(void)
172 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
175 // remove data from buffer:
176 // FFFF(empty) -> FFFF
177 // FFss(one data) -> FFFF
178 // sstt(two data) -> FFtt
179 // eeFF(errror) -> FFFF
180 recv_data = data | (((data&0xFF00) == 0xFF00) ? 0x00FF : 0xFF00);
183 if ((data&0x00FF) == 0x00FF) {
187 // 0xFF(Overrun/Error) from keyboard
201 dprintf("e%02X ", data>>8);
205 if ((data | 0x00FF) != 0xFFFF) {
207 dprintf("b:%04X ", data);
215 //dprintf("i%04X ", ibmpc_isr_debug); ibmpc_isr_debug = 0;
216 dprintf("r%02X ", ret);
220 int16_t ibmpc_host_recv_response(void)
222 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
225 while (retry-- && (data = ibmpc_host_recv()) == -1) {
231 void ibmpc_host_isr_clear(void)
240 #define LO8(w) (*((uint8_t *)&(w)))
241 #define HI8(w) (*(((uint8_t *)&(w))+1))
242 // NOTE: With this ISR data line can be read within 2us after clock falling edge.
243 // To read data line early as possible:
244 // write naked ISR with asembly code to read the line and call C func to do other job?
248 dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
252 // use only the least byte of millisecond timer
253 asm("lds %0, %1" : "=r" (t) : "p" (&timer_count));
254 //t = (uint8_t)timer_count; // compiler uses four registers instead of one
255 if (isr_state == 0x8000) {
258 // This gives 2.0ms at least before timeout
259 if ((uint8_t)(t - timer_start) >= 3) {
260 ibmpc_isr_debug = isr_state;
261 ibmpc_error = IBMPC_ERR_TIMEOUT;
264 // timeout error recovery - start receiving new data
265 // it seems to work somehow but may not under unstable situation
267 //isr_state = 0x8000;
271 isr_state = isr_state>>1;
272 if (dbit) isr_state |= 0x8000;
274 // isr_state: state of receiving data from keyboard
276 // This should be initialized with 0x8000 before receiving data and
277 // the MSB '*1' works as marker to discrimitate between protocols.
278 // It stores sampled bit at MSB after right shift on each clock falling edge.
280 // XT protocol has two variants of signaling; XT_IBM and XT_Clone.
281 // XT_IBM uses two start bits 0 and 1 while XT_Clone uses just start bit 1.
282 // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
284 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
285 // -----------------------------------------------------
286 // *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 Initial state(0x8000)
288 // x x x x x x x x | 0 0 0 0 0 0 0 0 midway(0-7 bits received)
289 // x x x x x x x x | *1 0 0 0 0 0 0 0 midway(8 bits received)
290 // b6 b5 b4 b3 b2 b1 b0 1 | 0 *1 0 0 0 0 0 0 XT_IBM-midway ^1
291 // b7 b6 b5 b4 b3 b2 b1 b0 | 0 *1 0 0 0 0 0 0 AT-midway ^1
292 // b7 b6 b5 b4 b3 b2 b1 b0 | 1 *1 0 0 0 0 0 0 XT_Clone-done ^3
293 // b6 b5 b4 b3 b2 b1 b0 1 | 1 *1 0 0 0 0 0 0 XT_IBM-error ^3
294 // pr b7 b6 b5 b4 b3 b2 b1 | 0 0 *1 0 0 0 0 0 AT-midway[b0=0]
295 // b7 b6 b5 b4 b3 b2 b1 b0 | 1 0 *1 0 0 0 0 0 XT_IBM-done ^2
296 // pr b7 b6 b5 b4 b3 b2 b1 | 1 0 *1 0 0 0 0 0 AT-midway[b0=1] ^2
297 // b7 b6 b5 b4 b3 b2 b1 b0 | 1 1 *1 0 0 0 0 0 XT_IBM-error-done
298 // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
299 // st pr b7 b6 b5 b4 b3 b2 | b1 b0 0 *1 0 0 0 0 AT-done
300 // x x x x x x x x | x x 1 *1 0 0 0 0 illegal
301 // all other states than above illegal
303 // ^1: AT and XT_IBM takes same state.
304 // ^2: AT and XT_IBM takes same state in case that AT b0 is 1,
305 // we have to check AT stop bit to discriminate between the two protocol.
306 switch (isr_state & 0xFF) {
309 case 0b01000000: // ^1
314 case 0b11000000: // ^3
317 // wait for rising and falling edge of b7 of XT_IBM
318 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
319 while ( IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT) && us) { wait_us(1); us--; }
322 // XT_IBM-error: read start(0) as 1
326 ibmpc_isr_debug = isr_state;
327 isr_state = isr_state>>8;
328 ibmpc_protocol = IBMPC_PROTOCOL_XT_CLONE;
335 ibmpc_isr_debug = isr_state;
336 isr_state = isr_state>>8;
337 ibmpc_protocol = IBMPC_PROTOCOL_XT_ERROR;
340 case 0b10100000: // ^2
343 // wait for rising and falling edge of AT stop bit to discriminate between XT and AT
344 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
345 while ( IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT) && us) { wait_us(1); us--; }
348 // found stop bit: AT-midway - process the stop bit in next ISR
351 // no stop bit: XT_IBM-done
352 ibmpc_isr_debug = isr_state;
353 isr_state = isr_state>>8;
354 ibmpc_protocol = IBMPC_PROTOCOL_XT_IBM;
364 // TODO: parity check?
365 ibmpc_isr_debug = isr_state;
367 if (isr_state & 0x8000) {
368 ibmpc_protocol = IBMPC_PROTOCOL_AT;
370 // Zenith Z-150 AT(beige/white lable) asserts stop bit as low
371 // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#zenith-z-150-beige
372 ibmpc_protocol = IBMPC_PROTOCOL_AT_Z150;
374 isr_state = isr_state>>6;
382 default: // xxxx_oooo(any 1 in low nibble)
384 ibmpc_isr_debug = isr_state;
385 ibmpc_error = IBMPC_ERR_ILLEGAL;
392 recv_data = (ibmpc_error<<8) | 0x00FF;
395 if ((isr_state & 0x00FF) == 0x00FF) {
396 // receive error code 0xFF
397 ibmpc_error = IBMPC_ERR_FF;
400 if (HI8(recv_data) != 0xFF && LO8(recv_data) != 0xFF) {
402 ibmpc_error = IBMPC_ERR_FULL;
406 recv_data = recv_data<<8;
407 recv_data |= isr_state & 0xFF;
409 // clear for next data
415 /* send LED state to keyboard */
416 void ibmpc_host_set_led(uint8_t led)
418 if (0xFA == ibmpc_host_send(0xED)) {
419 ibmpc_host_send(led);