aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/next_kbd.c
diff options
context:
space:
mode:
authorskullY <skullydazed@gmail.com>2019-08-30 11:19:03 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-08-30 15:01:52 -0700
commitb624f32f944acdc59dcb130674c09090c5c404cb (patch)
treebc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /tmk_core/protocol/next_kbd.c
parent61af76a10d00aba185b8338604171de490a13e3b (diff)
downloadqmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz
qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip
clang-format changes
Diffstat (limited to 'tmk_core/protocol/next_kbd.c')
-rw-r--r--tmk_core/protocol/next_kbd.c127
1 files changed, 67 insertions, 60 deletions
diff --git a/tmk_core/protocol/next_kbd.c b/tmk_core/protocol/next_kbd.c
index fa3034b3f..6f118e617 100644
--- a/tmk_core/protocol/next_kbd.c
+++ b/tmk_core/protocol/next_kbd.c
@@ -1,7 +1,7 @@
1/* 1/*
2 2
3NeXT non-ADB Keyboard Protocol 3NeXT non-ADB Keyboard Protocol
4 4
5Copyright 2013, Benjamin Gould (bgould@github.com) 5Copyright 2013, Benjamin Gould (bgould@github.com)
6 6
7Based on: 7Based on:
@@ -53,10 +53,10 @@ POSSIBILITY OF SUCH DAMAGE.
53#include "next_kbd.h" 53#include "next_kbd.h"
54#include "debug.h" 54#include "debug.h"
55 55
56static inline void out_lo(void); 56static inline void out_lo(void);
57static inline void out_hi(void); 57static inline void out_hi(void);
58static inline void query(void); 58static inline void query(void);
59static inline void reset(void); 59static inline void reset(void);
60static inline uint32_t response(void); 60static inline uint32_t response(void);
61 61
62/* The keyboard sends signal with 50us pulse width on OUT line 62/* The keyboard sends signal with 50us pulse width on OUT line
@@ -65,53 +65,65 @@ static inline uint32_t response(void);
65 * but it works well with 51us(+1us) on TMK converter(ATMeaga32u2) at least. 65 * but it works well with 51us(+1us) on TMK converter(ATMeaga32u2) at least.
66 * TODO: test on Teensy and Pro Micro configuration 66 * TODO: test on Teensy and Pro Micro configuration
67 */ 67 */
68#define out_hi_delay(intervals) do { out_hi(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); 68#define out_hi_delay(intervals) \
69#define out_lo_delay(intervals) do { out_lo(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); 69 do { \
70#define query_delay(intervals) do { query(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); 70 out_hi(); \
71#define reset_delay(intervals) do { reset(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); 71 _delay_us((NEXT_KBD_TIMING + 1) * intervals); \
72 72 } while (0);
73void next_kbd_init(void) 73#define out_lo_delay(intervals) \
74{ 74 do { \
75 out_lo(); \
76 _delay_us((NEXT_KBD_TIMING + 1) * intervals); \
77 } while (0);
78#define query_delay(intervals) \
79 do { \
80 query(); \
81 _delay_us((NEXT_KBD_TIMING + 1) * intervals); \
82 } while (0);
83#define reset_delay(intervals) \
84 do { \
85 reset(); \
86 _delay_us((NEXT_KBD_TIMING + 1) * intervals); \
87 } while (0);
88
89void next_kbd_init(void) {
75 out_hi(); 90 out_hi();
76 NEXT_KBD_IN_DDR &= ~(1<<NEXT_KBD_IN_BIT); // KBD_IN to input 91 NEXT_KBD_IN_DDR &= ~(1 << NEXT_KBD_IN_BIT); // KBD_IN to input
77 NEXT_KBD_IN_PORT |= (1<<NEXT_KBD_IN_BIT); // KBD_IN pull up 92 NEXT_KBD_IN_PORT |= (1 << NEXT_KBD_IN_BIT); // KBD_IN pull up
78 93
79 query_delay(5); 94 query_delay(5);
80 reset_delay(8); 95 reset_delay(8);
81 96
82 query_delay(5); 97 query_delay(5);
83 reset_delay(8); 98 reset_delay(8);
84} 99}
85 100
86void next_kbd_set_leds(bool left, bool right) 101void next_kbd_set_leds(bool left, bool right) {
87{
88 cli(); 102 cli();
89 out_lo_delay(9); 103 out_lo_delay(9);
90 104
91 out_hi_delay(3); 105 out_hi_delay(3);
92 out_lo_delay(1); 106 out_lo_delay(1);
93 107
94 if (left) { 108 if (left) {
95 out_hi_delay(1); 109 out_hi_delay(1);
96 } else { 110 } else {
97 out_lo_delay(1); 111 out_lo_delay(1);
98 } 112 }
99 113
100 if (right) { 114 if (right) {
101 out_hi_delay(1); 115 out_hi_delay(1);
102 } else { 116 } else {
103 out_lo_delay(1); 117 out_lo_delay(1);
104 } 118 }
105 119
106 out_lo_delay(7); 120 out_lo_delay(7);
107 out_hi(); 121 out_hi();
108 sei(); 122 sei();
109} 123}
110 124
111#define NEXT_KBD_READ (NEXT_KBD_IN_PIN&(1<<NEXT_KBD_IN_BIT)) 125#define NEXT_KBD_READ (NEXT_KBD_IN_PIN & (1 << NEXT_KBD_IN_BIT))
112uint32_t next_kbd_recv(void) 126uint32_t next_kbd_recv(void) {
113{
114
115 // First check to make sure that the keyboard is actually connected; 127 // First check to make sure that the keyboard is actually connected;
116 // if not, just return 128 // if not, just return
117 // TODO: reflect the status of the keyboard in a return code 129 // TODO: reflect the status of the keyboard in a return code
@@ -122,22 +134,23 @@ uint32_t next_kbd_recv(void)
122 134
123 query(); 135 query();
124 uint32_t resp = response(); 136 uint32_t resp = response();
125 137
126 return resp; 138 return resp;
127} 139}
128 140
129static inline uint32_t response(void) 141static inline uint32_t response(void) {
130{
131 cli(); 142 cli();
132 143
133 // try a 5ms read; this should be called after the query method has 144 // try a 5ms read; this should be called after the query method has
134 // been run so if a key is pressed we should get a response within 145 // been run so if a key is pressed we should get a response within
135 // 5ms; if not then send a reset and exit 146 // 5ms; if not then send a reset and exit
136 uint8_t i = 0; 147 uint8_t i = 0;
137 uint32_t data = 0; 148 uint32_t data = 0;
138 uint16_t reset_timeout = 50000; 149 uint16_t reset_timeout = 50000;
139 while (NEXT_KBD_READ && reset_timeout) { 150 while (NEXT_KBD_READ && reset_timeout) {
140 asm(""); _delay_us(1); reset_timeout--; 151 asm("");
152 _delay_us(1);
153 reset_timeout--;
141 } 154 }
142 if (!reset_timeout) { 155 if (!reset_timeout) {
143 reset(); 156 reset();
@@ -145,64 +158,58 @@ static inline uint32_t response(void)
145 return 0; 158 return 0;
146 } 159 }
147 _delay_us(NEXT_KBD_TIMING / 2); 160 _delay_us(NEXT_KBD_TIMING / 2);
148 for (; i < 22; i++) 161 for (; i < 22; i++) {
149 { 162 if (NEXT_KBD_READ) {
150 if (NEXT_KBD_READ) 163 data |= ((uint32_t)1 << i);
151 {
152 data |= ((uint32_t) 1 << i);
153 /* Note: 164 /* Note:
154 * My testing with the ATmega32u4 showed that there might 165 * My testing with the ATmega32u4 showed that there might
155 * something wrong with the timing here; by the end of the 166 * something wrong with the timing here; by the end of the
156 * second data byte some of the modifiers can get bumped out 167 * second data byte some of the modifiers can get bumped out
157 * to the next bit over if we just cycle through the data 168 * to the next bit over if we just cycle through the data
158 * based on the expected interval. There is a bit (i = 10) 169 * based on the expected interval. There is a bit (i = 10)
159 * in the middle of the data that is always on followed by 170 * in the middle of the data that is always on followed by
160 * one that is always off - so we'll use that to reset our 171 * one that is always off - so we'll use that to reset our
161 * timing in case we've gotten ahead of the keyboard; 172 * timing in case we've gotten ahead of the keyboard;
162 */ 173 */
163 if (i == 10) 174 if (i == 10) {
164 {
165 i++; 175 i++;
166 while (NEXT_KBD_READ) ; 176 while (NEXT_KBD_READ)
177 ;
167 _delay_us(NEXT_KBD_TIMING / 2); 178 _delay_us(NEXT_KBD_TIMING / 2);
168 } 179 }
169 } else { 180 } else {
170 /* redundant - but I don't want to remove if it might screw 181 /* redundant - but I don't want to remove if it might screw
171 * up the timing 182 * up the timing
172 */ 183 */
173 data |= ((uint32_t) 0 << i); 184 data |= ((uint32_t)0 << i);
174 } 185 }
175 _delay_us(NEXT_KBD_TIMING); 186 _delay_us(NEXT_KBD_TIMING);
176 } 187 }
177 188
178 sei(); 189 sei();
179 190
180 return data; 191 return data;
181} 192}
182 193
183static inline void out_lo(void) 194static inline void out_lo(void) {
184{ 195 NEXT_KBD_OUT_PORT &= ~(1 << NEXT_KBD_OUT_BIT);
185 NEXT_KBD_OUT_PORT &= ~(1<<NEXT_KBD_OUT_BIT); 196 NEXT_KBD_OUT_DDR |= (1 << NEXT_KBD_OUT_BIT);
186 NEXT_KBD_OUT_DDR |= (1<<NEXT_KBD_OUT_BIT);
187} 197}
188 198
189static inline void out_hi(void) 199static inline void out_hi(void) {
190{
191 /* input with pull up */ 200 /* input with pull up */
192 NEXT_KBD_OUT_DDR &= ~(1<<NEXT_KBD_OUT_BIT); 201 NEXT_KBD_OUT_DDR &= ~(1 << NEXT_KBD_OUT_BIT);
193 NEXT_KBD_OUT_PORT |= (1<<NEXT_KBD_OUT_BIT); 202 NEXT_KBD_OUT_PORT |= (1 << NEXT_KBD_OUT_BIT);
194} 203}
195 204
196static inline void query(void) 205static inline void query(void) {
197{
198 out_lo_delay(5); 206 out_lo_delay(5);
199 out_hi_delay(1); 207 out_hi_delay(1);
200 out_lo_delay(3); 208 out_lo_delay(3);
201 out_hi(); 209 out_hi();
202} 210}
203 211
204static inline void reset(void) 212static inline void reset(void) {
205{
206 out_lo_delay(1); 213 out_lo_delay(1);
207 out_hi_delay(4); 214 out_hi_delay(4);
208 out_lo_delay(1); 215 out_lo_delay(1);