2 Copyright 2018 Jun WAKO <wakojun@gmail.com>
3 Copyright 2016 Ethan Apodaca <papodaca@gmail.com>
5 This software is licensed with a Modified BSD License.
6 All of this is supposed to be Free Software, Open Source, DFSG-free,
7 GPL-compatible, and OK to use in both free and proprietary applications.
8 Additions and corrections to this file are welcome.
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in
19 the documentation and/or other materials provided with the
22 * Neither the name of the copyright holders nor the names of
23 contributors may be used to endorse or promote products derived
24 from this software without specific prior written permission.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
40 #include <avr/interrupt.h>
41 #include <util/delay.h>
46 static inline uint8_t pbuf_dequeue(void);
47 static inline void pbuf_enqueue(uint8_t data);
48 static inline bool pbuf_has_data(void);
49 static inline void pbuf_clear(void);
51 void xt_host_init(void)
61 /* soft reset: pull clock line down for 20ms */
66 /* input mode with pullup */
73 /* get data received by interrupt */
74 uint8_t xt_host_recv(void)
76 if (pbuf_has_data()) {
77 return pbuf_dequeue();
86 * XT signal format consits of 10 or 9 clocks and sends start bits and 8-bit data,
87 * which should be read on falling edge of clock.
89 * start(0), start(1), bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7
91 * Original IBM XT keyboard sends start(0) bit while some of clones don't.
92 * Start(0) bit is read as low on data line while start(1) as high.
94 * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
97 START, BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7
99 static uint8_t data = 0;
101 uint8_t dbit = XT_DATA_READ();
103 // This is needed if using PCINT which can be called on both falling and rising edge
104 //if (XT_CLOCK_READ()) return;
108 // ignore start(0) bit
117 if (state++ == BIT7) {
125 /*--------------------------------------------------------------------
126 * Ring buffer to store scan codes from keyboard
127 *------------------------------------------------------------------*/
129 static uint8_t pbuf[PBUF_SIZE];
130 static uint8_t pbuf_head = 0;
131 static uint8_t pbuf_tail = 0;
132 static inline void pbuf_enqueue(uint8_t data)
136 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
137 if (next != pbuf_tail) {
138 pbuf[pbuf_head] = data;
141 print("pbuf: full\n");
145 static inline uint8_t pbuf_dequeue(void)
151 if (pbuf_head != pbuf_tail) {
152 val = pbuf[pbuf_tail];
153 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
159 static inline bool pbuf_has_data(void)
163 bool has_data = (pbuf_head != pbuf_tail);
167 static inline void pbuf_clear(void)
171 pbuf_head = pbuf_tail = 0;