3 NeXT non-ADB Keyboard Protocol
5 Copyright 2013, Benjamin Gould (bgould@github.com)
8 TMK firmware code Copyright 2011,2012 Jun WAKO <wakojun@gmail.com>
9 Arduino code by "Ladyada" Limor Fried (http://ladyada.net/, http://adafruit.com/), released under BSD license
11 Timing reference thanks to http://m0115.web.fc2.com/ (dead link), http://cfile7.uf.tistory.com/image/14448E464F410BF22380BB
12 Pinouts thanks to http://www.68k.org/~degs/nextkeyboard.html
13 Keycodes from http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/sys/arch/next68k/dev/
15 This software is licensed with a Modified BSD License.
16 All of this is supposed to be Free Software, Open Source, DFSG-free,
17 GPL-compatible, and OK to use in both free and proprietary applications.
18 Additions and corrections to this file are welcome.
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
23 * Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
26 * Redistributions in binary form must reproduce the above copyright
27 notice, this list of conditions and the following disclaimer in
28 the documentation and/or other materials provided with the
31 * Neither the name of the copyright holders nor the names of
32 contributors may be used to endorse or promote products derived
33 from this software without specific prior written permission.
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
39 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 POSSIBILITY OF SUCH DAMAGE.
51 #include <util/atomic.h>
52 #include <util/delay.h>
56 static inline void out_lo(void);
57 static inline void out_hi(void);
58 static inline void query(void);
59 static inline void reset(void);
60 static inline uint32_t response(void);
62 #define out_hi_delay(intervals) do { out_hi(); _delay_us(NEXT_KBD_TIMING * intervals); } while (0);
63 #define out_lo_delay(intervals) do { out_lo(); _delay_us(NEXT_KBD_TIMING * intervals); } while (0);
64 #define query_delay(intervals) do { query(); _delay_us(NEXT_KBD_TIMING * intervals); } while (0);
65 #define reset_delay(intervals) do { reset(); _delay_us(NEXT_KBD_TIMING * intervals); } while (0);
67 void next_kbd_init(void)
70 NEXT_KBD_IN_DDR &= ~(1<<NEXT_KBD_IN_BIT); // KBD_IN to input
71 NEXT_KBD_IN_PORT |= (1<<NEXT_KBD_IN_BIT); // KBD_IN pull up
80 void next_kbd_set_leds(bool left, bool right)
103 #define NEXT_KBD_READ (NEXT_KBD_IN_PIN&(1<<NEXT_KBD_IN_BIT))
104 uint32_t next_kbd_recv(void)
107 // First check to make sure that the keyboard is actually connected;
108 // if not, just return
109 // TODO: reflect the status of the keyboard in a return code
110 if (!NEXT_KBD_READ) {
116 uint32_t resp = response();
121 static inline uint32_t response(void)
125 // try a 5ms read; this should be called after the query method has
126 // been run so if a key is pressed we should get a response within
127 // 5ms; if not then send a reset and exit
130 uint16_t reset_timeout = 50000;
131 while (NEXT_KBD_READ && reset_timeout) {
132 asm(""); _delay_us(1); reset_timeout--;
134 if (!reset_timeout) {
139 _delay_us(NEXT_KBD_TIMING / 2);
144 data |= ((uint32_t) 1 << i);
146 * My testing with the ATmega32u4 showed that there might
147 * something wrong with the timing here; by the end of the
148 * second data byte some of the modifiers can get bumped out
149 * to the next bit over if we just cycle through the data
150 * based on the expected interval. There is a bit (i = 10)
151 * in the middle of the data that is always on followed by
152 * one that is always off - so we'll use that to reset our
153 * timing in case we've gotten ahead of the keyboard;
158 while (NEXT_KBD_READ) ;
159 _delay_us(NEXT_KBD_TIMING / 2);
162 /* redundant - but I don't want to remove if it might screw
165 data |= ((uint32_t) 0 << i);
167 _delay_us(NEXT_KBD_TIMING);
175 static inline void out_lo(void)
177 NEXT_KBD_OUT_PORT &= ~(1<<NEXT_KBD_OUT_BIT);
178 NEXT_KBD_OUT_DDR |= (1<<NEXT_KBD_OUT_BIT);
181 static inline void out_hi(void)
183 /* input with pull up */
184 NEXT_KBD_OUT_DDR &= ~(1<<NEXT_KBD_OUT_BIT);
185 NEXT_KBD_OUT_PORT |= (1<<NEXT_KBD_OUT_BIT);
188 static inline void query(void)
196 static inline void reset(void)