Run clang-format manually to fix recently changed files (#8552)
[jackhill/qmk/firmware.git] / tmk_core / protocol / ps2_interrupt.c
1 /*
2 Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3
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.
8
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
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
19 distribution.
20
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.
24
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.
36 */
37
38 /*
39 * PS/2 protocol Pin interrupt version
40 */
41
42 #include <stdbool.h>
43 #include <avr/interrupt.h>
44 #include <util/delay.h>
45 #include "ps2.h"
46 #include "ps2_io.h"
47 #include "print.h"
48
49 #define WAIT(stat, us, err) \
50 do { \
51 if (!wait_##stat(us)) { \
52 ps2_error = err; \
53 goto ERROR; \
54 } \
55 } while (0)
56
57 uint8_t ps2_error = PS2_ERR_NONE;
58
59 static inline uint8_t pbuf_dequeue(void);
60 static inline void pbuf_enqueue(uint8_t data);
61 static inline bool pbuf_has_data(void);
62 static inline void pbuf_clear(void);
63
64 void ps2_host_init(void) {
65 idle();
66 PS2_INT_INIT();
67 PS2_INT_ON();
68 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
69 //_delay_ms(2500);
70 }
71
72 uint8_t ps2_host_send(uint8_t data) {
73 bool parity = true;
74 ps2_error = PS2_ERR_NONE;
75
76 PS2_INT_OFF();
77
78 /* terminate a transmission if we have */
79 inhibit();
80 _delay_us(100); // 100us [4]p.13, [5]p.50
81
82 /* 'Request to Send' and Start bit */
83 data_lo();
84 clock_hi();
85 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
86
87 /* Data bit[2-9] */
88 for (uint8_t i = 0; i < 8; i++) {
89 _delay_us(15);
90 if (data & (1 << i)) {
91 parity = !parity;
92 data_hi();
93 } else {
94 data_lo();
95 }
96 WAIT(clock_hi, 50, 2);
97 WAIT(clock_lo, 50, 3);
98 }
99
100 /* Parity bit */
101 _delay_us(15);
102 if (parity) {
103 data_hi();
104 } else {
105 data_lo();
106 }
107 WAIT(clock_hi, 50, 4);
108 WAIT(clock_lo, 50, 5);
109
110 /* Stop bit */
111 _delay_us(15);
112 data_hi();
113
114 /* Ack */
115 WAIT(data_lo, 50, 6);
116 WAIT(clock_lo, 50, 7);
117
118 /* wait for idle state */
119 WAIT(clock_hi, 50, 8);
120 WAIT(data_hi, 50, 9);
121
122 idle();
123 PS2_INT_ON();
124 return ps2_host_recv_response();
125 ERROR:
126 idle();
127 PS2_INT_ON();
128 return 0;
129 }
130
131 uint8_t ps2_host_recv_response(void) {
132 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
133 uint8_t retry = 25;
134 while (retry-- && !pbuf_has_data()) {
135 _delay_ms(1);
136 }
137 return pbuf_dequeue();
138 }
139
140 /* get data received by interrupt */
141 uint8_t ps2_host_recv(void) {
142 if (pbuf_has_data()) {
143 ps2_error = PS2_ERR_NONE;
144 return pbuf_dequeue();
145 } else {
146 ps2_error = PS2_ERR_NODATA;
147 return 0;
148 }
149 }
150
151 ISR(PS2_INT_VECT) {
152 static enum {
153 INIT,
154 START,
155 BIT0,
156 BIT1,
157 BIT2,
158 BIT3,
159 BIT4,
160 BIT5,
161 BIT6,
162 BIT7,
163 PARITY,
164 STOP,
165 } state = INIT;
166 static uint8_t data = 0;
167 static uint8_t parity = 1;
168
169 // TODO: abort if elapse 100us from previous interrupt
170
171 // return unless falling edge
172 if (clock_in()) {
173 goto RETURN;
174 }
175
176 state++;
177 switch (state) {
178 case START:
179 if (data_in()) goto ERROR;
180 break;
181 case BIT0:
182 case BIT1:
183 case BIT2:
184 case BIT3:
185 case BIT4:
186 case BIT5:
187 case BIT6:
188 case BIT7:
189 data >>= 1;
190 if (data_in()) {
191 data |= 0x80;
192 parity++;
193 }
194 break;
195 case PARITY:
196 if (data_in()) {
197 if (!(parity & 0x01)) goto ERROR;
198 } else {
199 if (parity & 0x01) goto ERROR;
200 }
201 break;
202 case STOP:
203 if (!data_in()) goto ERROR;
204 pbuf_enqueue(data);
205 goto DONE;
206 break;
207 default:
208 goto ERROR;
209 }
210 goto RETURN;
211 ERROR:
212 ps2_error = state;
213 DONE:
214 state = INIT;
215 data = 0;
216 parity = 1;
217 RETURN:
218 return;
219 }
220
221 /* send LED state to keyboard */
222 void ps2_host_set_led(uint8_t led) {
223 ps2_host_send(0xED);
224 ps2_host_send(led);
225 }
226
227 /*--------------------------------------------------------------------
228 * Ring buffer to store scan codes from keyboard
229 *------------------------------------------------------------------*/
230 #define PBUF_SIZE 32
231 static uint8_t pbuf[PBUF_SIZE];
232 static uint8_t pbuf_head = 0;
233 static uint8_t pbuf_tail = 0;
234 static inline void pbuf_enqueue(uint8_t data) {
235 uint8_t sreg = SREG;
236 cli();
237 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
238 if (next != pbuf_tail) {
239 pbuf[pbuf_head] = data;
240 pbuf_head = next;
241 } else {
242 print("pbuf: full\n");
243 }
244 SREG = sreg;
245 }
246 static inline uint8_t pbuf_dequeue(void) {
247 uint8_t val = 0;
248
249 uint8_t sreg = SREG;
250 cli();
251 if (pbuf_head != pbuf_tail) {
252 val = pbuf[pbuf_tail];
253 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
254 }
255 SREG = sreg;
256
257 return val;
258 }
259 static inline bool pbuf_has_data(void) {
260 uint8_t sreg = SREG;
261 cli();
262 bool has_data = (pbuf_head != pbuf_tail);
263 SREG = sreg;
264 return has_data;
265 }
266 static inline void pbuf_clear(void) {
267 uint8_t sreg = SREG;
268 cli();
269 pbuf_head = pbuf_tail = 0;
270 SREG = sreg;
271 }