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 uint8_t ibmpc_protocol = IBMPC_PROTOCOL_AT;
60 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
62 /* 2-byte buffer for data received from keyhboard
66 * sstt: two data(full)
67 * 0xFF can not be stored as data in buffer because it means empty or no data.
69 static volatile uint16_t recv_data = 0xFFFF;
70 /* internal state of receiving data */
71 static volatile uint16_t isr_state = 0x8000;
73 void ibmpc_host_init(void)
75 // initialize reset pin to HiZ
82 void ibmpc_host_enable(void)
88 void ibmpc_host_disable(void)
94 int16_t ibmpc_host_send(uint8_t data)
97 ibmpc_error = IBMPC_ERR_NONE;
99 if (ibmpc_protocol == IBMPC_PROTOCOL_XT) return -1;
101 dprintf("w%02X ", data);
105 /* terminate a transmission if we have */
107 wait_us(100); // 100us [4]p.13, [5]p.50
109 /* 'Request to Send' and Start bit */
112 WAIT(clock_lo, 10000, 1); // 10ms [5]p.50
115 for (uint8_t i = 0; i < 8; i++) {
123 WAIT(clock_hi, 50, 2);
124 WAIT(clock_lo, 50, 3);
129 if (parity) { data_hi(); } else { data_lo(); }
130 WAIT(clock_hi, 50, 4);
131 WAIT(clock_lo, 50, 5);
138 WAIT(data_lo, 50, 6);
139 WAIT(clock_lo, 50, 7);
141 /* wait for idle state */
142 WAIT(clock_hi, 50, 8);
143 WAIT(data_hi, 50, 9);
145 // clear buffer to get response correctly
147 ibmpc_host_isr_clear();
151 return ibmpc_host_recv_response();
153 ibmpc_error |= IBMPC_ERR_SEND;
160 * Receive data from keyboard
162 int16_t ibmpc_host_recv(void)
167 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
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
174 recv_data = data | 0x00FF;
178 if (ret != 0xFF) dprintf("r%02X ", ret);
179 if (recv_data != 0xFFFF) dprintf("b%04X ", recv_data);
180 return ((ret != 0xFF) ? ret : -1);
184 int16_t ibmpc_host_recv_response(void)
186 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
189 while (retry-- && (data = ibmpc_host_recv()) == -1) {
195 void ibmpc_host_isr_clear(void)
201 // NOTE: With this ISR data line can be read within 2us after clock falling edge.
202 // To read data line early as possible:
203 // write naked ISR with asembly code to read the line and call C func to do other job?
207 dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
208 isr_state = isr_state>>1;
209 if (dbit) isr_state |= 0x8000;
211 // isr_state: state of receiving data from keyboard
212 // This is initialized with 0x8000 before start receiving data and MSB '1' of
213 // the initial value is marker bit to discrimitate between protocols.
214 // It stores sampled bit at MSB after right shift on each clock falling edge.
216 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
217 // -----------------------------------------------------
218 // Initial: *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 with MSB marker *1
219 // XT_Type-1: b7 b6 b5 b4 b3 b2 b1 b0 | 1 0 *1 0 0 0 0 0 start bit 0 and 1 **
220 // XT_Type-2: b7 b6 b5 b4 b3 b2 b1 b0 | 1 *1 0 0 0 0 0 0 start bit 1
221 // AT: st pr b7 b6 b5 b4 b3 b2 | b1 b0 0 *1 0 0 0 0 start bit 0
222 // AT**: pr b7 b6 b5 b4 b3 b2 b1 | b0 0 *1 0 0 0 0 0 before stop bit **
224 // x x x x x x x x | 0 0 0 0 0 0 0 0 midway(0-7 bits received)
225 // x x x x x x x x | *1 0 0 0 0 0 0 0 midway(8 bits received)
226 // x x x x x x x x | 0 *1 0 0 0 0 0 0 XT_Type1-midway or AT-midway
227 // x x x x x x x x | 1 *1 0 0 0 0 0 0 XT_Type2-done
228 // x x x x x x x x | 0 0 *1 0 0 0 0 0 AT-midway[b0=0]
229 // x x x x x x x x | 1 0 *1 0 0 0 0 0 XT_Type1-done or AT-midway[b0=1]
230 // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
231 // x x x x x x x x | x x 0 1 0 0 0 0 AT-done
232 // x x x x x x x x | x x 1 1 0 0 0 0 illegal
233 // other states than above illegal
235 // **: AT takes same state as XT_Type1-done(1010 000) in case that AT b0 is 1,
236 // to discriminate between the two protocol we will have to wait a while for AT stop bit.
237 switch (isr_state & 0xFF) {
247 recv_data = recv_data<<8;
248 recv_data |= (isr_state>>8) & 0xFF;
254 // wait for rising and falling edge of AT stop bit
255 while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
256 while ( IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT) && us) { wait_us(1); us--; }
259 // found stop bit: AT-midway - process the stop bit in next ISR
262 // no stop bit: XT_Type1-done
263 recv_data = recv_data<<8;
264 recv_data |= (isr_state>>8) & 0xFF;
274 recv_data = recv_data<<8;
275 recv_data |= (isr_state>>6) & 0xFF;
284 default: // xxxx_oooo(any 1 in low nibble)
292 recv_data = 0xFF00; // clear data and scancode of error 0x00
296 // TODO: process error code: 0x00(AT), 0xFF(XT) in particular
297 isr_state = 0x8000; // clear to next data
302 /* send LED state to keyboard */
303 void ibmpc_host_set_led(uint8_t led)
305 ibmpc_host_send(0xED);
306 ibmpc_host_send(led);