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>
51 #define WAIT(stat, us, err) do { \
52 if (!wait_##stat(us)) { \
60 static uint8_t buf[BUF_SIZE];
61 static ringbuf_t rb = {
65 .size_mask = BUF_SIZE - 1
68 volatile uint8_t ibmpc_protocol = IBMPC_PROTOCOL_AT;
69 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
71 void ibmpc_host_init(void)
78 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
82 int16_t ibmpc_host_send(uint8_t data)
85 ibmpc_error = IBMPC_ERR_NONE;
87 if (ibmpc_protocol == IBMPC_PROTOCOL_XT) return -1;
89 dprintf("w%02X ", data);
93 /* terminate a transmission if we have */
95 wait_us(100); // 100us [4]p.13, [5]p.50
97 /* 'Request to Send' and Start bit */
100 WAIT(clock_lo, 10000, 1); // 10ms [5]p.50
103 for (uint8_t i = 0; i < 8; i++) {
111 WAIT(clock_hi, 50, 2);
112 WAIT(clock_lo, 50, 3);
117 if (parity) { data_hi(); } else { data_lo(); }
118 WAIT(clock_hi, 50, 4);
119 WAIT(clock_lo, 50, 5);
126 WAIT(data_lo, 50, 6);
127 WAIT(clock_lo, 50, 7);
129 /* wait for idle state */
130 WAIT(clock_hi, 50, 8);
131 WAIT(data_hi, 50, 9);
135 return ibmpc_host_recv_response();
137 ibmpc_error |= IBMPC_ERR_SEND;
143 int16_t ibmpc_host_recv_response(void)
145 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
147 while (retry-- && ringbuf_is_empty(&rb)) {
150 int16_t data = ringbuf_get(&rb);
151 if (data != -1) dprintf("r%02X ", data);
155 /* get data received by interrupt */
156 int16_t ibmpc_host_recv(void)
158 int16_t data = ringbuf_get(&rb);
159 if (data != -1) dprintf("r%02X ", data);
165 static uint16_t last_time = 0;
168 BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
172 static uint8_t data = 0;
173 static uint8_t parity = 1;
175 uint8_t dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
177 // Reset state when taking more than 1ms
178 if (last_time && timer_elapsed(last_time) > 10) {
179 ibmpc_error = IBMPC_ERR_TIMEOUT | IBMPC_ERR_RECV | state;
184 last_time = timer_read();
188 if (ibmpc_protocol == IBMPC_PROTOCOL_XT) {
189 // ignore start(0) bit
209 if (state == BIT7 && ibmpc_protocol == IBMPC_PROTOCOL_XT) {
210 if (!ringbuf_put(&rb, data)) {
211 ibmpc_error = IBMPC_ERR_FULL;
214 ibmpc_error = IBMPC_ERR_NONE;
220 if (!(parity & 0x01))
230 if (!ringbuf_put(&rb, data)) {
231 ibmpc_error = IBMPC_ERR_FULL;
234 ibmpc_error = IBMPC_ERR_NONE;
243 ibmpc_error |= state;
244 ibmpc_error |= IBMPC_ERR_RECV;
257 /* send LED state to keyboard */
258 void ibmpc_host_set_led(uint8_t led)
260 ibmpc_host_send(0xED);
261 ibmpc_host_send(led);