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>
52 #define WAIT(stat, us, err) do { \
53 if (!wait_##stat(us)) { \
61 static uint8_t buf[BUF_SIZE];
62 static ringbuf_t rb = {
66 .size_mask = BUF_SIZE - 1
69 volatile uint8_t ibmpc_protocol = IBMPC_PROTOCOL_AT;
70 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
72 void ibmpc_host_init(void)
74 // initialize reset pin
75 IBMPC_RST_PORT |= (1<<IBMPC_RST_BIT1);
76 IBMPC_RST_DDR |= (1<<IBMPC_RST_BIT1);
77 IBMPC_RST_PORT |= (1<<IBMPC_RST_BIT2);
78 IBMPC_RST_DDR |= (1<<IBMPC_RST_BIT2);
84 void ibmpc_host_enable(void)
90 void ibmpc_host_disable(void)
96 int16_t ibmpc_host_send(uint8_t data)
99 ibmpc_error = IBMPC_ERR_NONE;
101 if (ibmpc_protocol == IBMPC_PROTOCOL_XT) return -1;
103 dprintf("w%02X ", data);
107 /* terminate a transmission if we have */
109 wait_us(100); // 100us [4]p.13, [5]p.50
111 /* 'Request to Send' and Start bit */
114 WAIT(clock_lo, 10000, 1); // 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);
140 WAIT(data_lo, 50, 6);
141 WAIT(clock_lo, 50, 7);
143 /* wait for idle state */
144 WAIT(clock_hi, 50, 8);
145 WAIT(data_hi, 50, 9);
147 // clear buffer to get response correctly
149 ibmpc_host_isr_clear();
153 return ibmpc_host_recv_response();
155 ibmpc_error |= IBMPC_ERR_SEND;
162 * Receive data from keyboard with ISR
164 static volatile int16_t recv_data = -1;
165 static volatile uint16_t isr_data = 0x8000;
167 void ibmpc_host_isr_clear(void)
172 int16_t ibmpc_host_recv(void)
175 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
180 dprintf("r%04X ", data);
186 int16_t ibmpc_host_recv_response(void)
188 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
191 while (retry-- && (data = ibmpc_host_recv()) == -1) {
200 dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
201 isr_data = isr_data>>1;
202 if (dbit) isr_data |= 0x8000;
205 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
206 // -----------------------------------------------------
207 // Initial: *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0
208 // XT IBM: b7 b6 b5 b4 b3 b2 b1 b0 | s1 s0 *1 0 0 0 0 0 after receiving **
209 // XT Clone: b7 b6 b5 b4 b3 b2 b1 b0 | s1 *1 0 0 0 0 0 0 after receiving
210 // AT: st pr b7 b6 b5 b4 b3 b2 | b1 b0 s0 *1 0 0 0 0 after receiving
211 // AT**: pr b7 b6 b5 b4 b3 b2 b1 | b0 s0 *1 0 0 0 0 0 before stop bit **
213 // x x x x x x x x | 0 0 0 0 0 0 0 0 midway(0-7 bits received)
214 // x x x x x x x x | 1 0 0 0 0 0 0 0 midway(8 bits received)
215 // x x x x x x x x | 0 1 0 0 0 0 0 0 XT IBM-midway or AT-midway
216 // x x x x x x x x | 1 1 0 0 0 0 0 0 XT Clone-done
217 // x x x x x x x x | 0 0 1 0 0 0 0 0 AT-midway
218 // x x x x x x x x | 1 0 1 0 0 0 0 0 XT IBM-done or AT-midway **
219 // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
220 // x x x x x x x x | x x 0 1 0 0 0 0 AT-done
221 // x x x x x x x x | x x 1 1 0 0 0 0 illegal
222 // other states than avobe illegal
224 // **: AT can take same as end sate of XT IBM(1010 000) when b0 is 1,
225 // to discriminate between them we will have to wait a while for stop bit.
227 // mask for isr_data:
228 // 0x00A0(1010 0000) when XT IBM
229 // 0x00C0(1100 0000) when XT Clone
230 // 0x0010(xx01 0000) when AT
232 switch (isr_data & 0xFF) {
242 recv_data = (isr_data>>8) & 0xFF;
246 // XT IBM-done or AT-midway
247 // wait and check for clock of AT stop bit
248 if (wait_clock_hi(100) && wait_clock_lo(100)) { // FIXME this makes ISR prologe long
253 recv_data = (isr_data>>8) & 0xFF;
262 recv_data = (isr_data>>6) & 0xFF;
271 default: // xxxx_oooo(any 1 in low nibble)
272 recv_data = isr_data;
276 // TODO: buffer for recv_data
277 isr_data = 0x8000; // clear to next data
281 /* send LED state to keyboard */
282 void ibmpc_host_set_led(uint8_t led)
284 ibmpc_host_send(0xED);
285 ibmpc_host_send(led);