]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - ps2.c
f7aaf96e3fa975cec9400d84deb819b3330ad4f4
[max/tmk_keyboard.git] / ps2.c
1 /*
2 Copyright (c) 2010,2011 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 #include <stdbool.h>
38 #include <avr/io.h>
39 #include <avr/interrupt.h>
40 #include <util/delay.h>
41 #include "ps2.h"
42 #include "debug.h"
43
44
45 static uint8_t recv_data(void);
46 static inline void clock_lo(void);
47 static inline void clock_hi(void);
48 static inline bool clock_in(void);
49 static inline void data_lo(void);
50 static inline void data_hi(void);
51 static inline bool data_in(void);
52 static inline uint16_t wait_clock_lo(uint16_t us);
53 static inline uint16_t wait_clock_hi(uint16_t us);
54 static inline uint16_t wait_data_lo(uint16_t us);
55 static inline uint16_t wait_data_hi(uint16_t us);
56 static inline void idle(void);
57 static inline void inhibit(void);
58
59
60 /*
61 Primitive PS/2 Library for AVR
62 ==============================
63 Host side is only supported now.
64
65
66 I/O control
67 -----------
68 High state is asserted by input with pull up.
69
70
71 PS/2 References
72 ---------------
73 http://www.computer-engineering.org/ps2protocol/
74 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
75 */
76
77
78 #define WAIT(stat, us, err) do { \
79     if (!wait_##stat(us)) { \
80         ps2_error = err; \
81         goto ERROR; \
82     } \
83 } while (0)
84
85
86 uint8_t ps2_error = PS2_ERR_NONE;
87
88
89 void ps2_host_init(void)
90 {
91 #ifdef PS2_INT_ENABLE
92     PS2_INT_ENABLE();
93     idle();
94 #else
95     inhibit();
96 #endif
97 }
98
99 uint8_t ps2_host_send(uint8_t data)
100 {
101     bool parity;
102 RETRY:
103     parity = true;
104     ps2_error = 0;
105
106     /* terminate a transmission if we have */
107     inhibit();
108     _delay_us(100);
109
110     /* start bit [1] */
111     data_lo();
112     clock_hi();
113     WAIT(clock_lo, 15000, 1);
114     /* data [2-9] */
115     for (uint8_t i = 0; i < 8; i++) {
116         _delay_us(15);
117         if (data&(1<<i)) {
118             parity = !parity;
119             data_hi();
120         } else {
121             data_lo();
122         }
123         WAIT(clock_hi, 50, 2);
124         WAIT(clock_lo, 50, 3);
125     }
126     /* parity [10] */
127     _delay_us(15);
128     if (parity) { data_hi(); } else { data_lo(); }
129     WAIT(clock_hi, 50, 4);
130     WAIT(clock_lo, 50, 5);
131     /* stop bit [11] */
132     _delay_us(15);
133     data_hi();
134     /* ack [12] */
135     WAIT(data_lo, 50, 6);
136     WAIT(clock_lo, 50, 7);
137
138     /* wait for idle state */
139     WAIT(clock_hi, 50, 8);
140     WAIT(data_hi, 50, 9);
141
142     uint8_t res = ps2_host_recv_response();
143     if (res == 0xFE && data != 0xFE)
144         goto RETRY;
145
146     inhibit();
147     return res;
148 ERROR:
149     inhibit();
150     return 0;
151 }
152
153 /* receive data when host want else inhibit communication */
154 uint8_t ps2_host_recv_response(void)
155 {
156     uint8_t data = 0;
157
158     /* terminate a transmission if we have */
159     inhibit();
160     _delay_us(100);
161
162     /* release lines(idle state) */
163     idle();
164
165     /* wait start bit */
166     wait_clock_lo(2000);
167     data = recv_data();
168
169     inhibit();
170     return data;
171 }
172
173 #ifndef PS2_INT_VECT
174 uint8_t ps2_host_recv(void)
175 {
176     return ps2_host_recv_response();
177 }
178 #else
179 /* ring buffer to store ps/2 key data */
180 #define PBUF_SIZE 8
181 static uint8_t pbuf[PBUF_SIZE];
182 static uint8_t pbuf_head = 0;
183 static uint8_t pbuf_tail = 0;
184 static inline void pbuf_enqueue(uint8_t data)
185 {
186     if (!data)
187         return;
188     uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
189     if (next != pbuf_tail) {
190         pbuf[pbuf_head] = data;
191         pbuf_head = next;
192     } else {
193         print("pbuf: full\n");
194     }
195 }
196 static inline uint8_t pbuf_dequeue(void)
197 {
198     uint8_t val = 0;
199     uint8_t sreg = SREG;
200     cli();
201     if (pbuf_head != pbuf_tail) {
202         val = pbuf[pbuf_tail];
203         pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
204     }
205     SREG = sreg;
206     return val;
207 }
208
209 /* get data received by interrupt */
210 uint8_t ps2_host_recv(void)
211 {
212     return pbuf_dequeue();
213 }
214
215 ISR(PS2_INT_VECT)
216 {
217 PORTC = 0xFF;
218     /* interrupt means start bit comes */
219     pbuf_enqueue(recv_data());
220
221     /* release lines(idle state) */
222     idle();
223     _delay_us(5);
224 PORTC = 0x00;
225 }
226 #endif
227
228
229 /*
230 static void ps2_reset(void)
231 {
232     ps2_host_send(0xFF);
233     if (ps2_host_recv_response() == 0xFA) {
234         _delay_ms(1000);
235         ps2_host_recv_response();
236     }
237 }
238 */
239
240 /* send LED state to keyboard */
241 void ps2_host_set_led(uint8_t led)
242 {
243 #ifdef PS2_INT_DISABLE
244     PS2_INT_DISABLE();
245 #endif
246     ps2_host_send(0xED);
247     ps2_host_recv_response();
248     ps2_host_send(led);
249     ps2_host_recv_response();
250 #ifdef PS2_INT_ENABLE
251     PS2_INT_ENABLE();
252     idle();
253 #endif
254 }
255
256
257 /* called after start bit comes */
258 static uint8_t recv_data(void)
259 {
260     uint8_t data = 0;
261     bool parity = true;
262     ps2_error = 0;
263
264     /* start bit [1] */
265     WAIT(clock_lo, 1, 1);
266     WAIT(data_lo, 1, 2);
267     WAIT(clock_hi, 50, 3);
268
269     /* data [2-9] */
270     for (uint8_t i = 0; i < 8; i++) {
271         WAIT(clock_lo, 50, 4);
272         if (data_in()) {
273             parity = !parity;
274             data |= (1<<i);
275         }
276         WAIT(clock_hi, 50, 5);
277     }
278
279     /* parity [10] */
280     WAIT(clock_lo, 50, 6);
281     if (data_in() != parity) {
282         ps2_error = PS2_ERR_PARITY;
283         goto ERROR;
284     }
285     WAIT(clock_hi, 50, 7);
286
287     /* stop bit [11] */
288     WAIT(clock_lo, 50, 8);
289     WAIT(data_hi, 1, 9);
290     WAIT(clock_hi, 50, 10);
291
292     return data;
293 ERROR:
294     return 0;
295 }
296
297 static inline void clock_lo()
298 {
299     PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
300     PS2_CLOCK_DDR  |=  (1<<PS2_CLOCK_BIT);
301 }
302 static inline void clock_hi()
303 {
304     /* input with pull up */
305     PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
306     PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
307 }
308 static inline bool clock_in()
309 {
310     PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
311     PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
312     return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
313 }
314 static inline void data_lo()
315 {
316     PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
317     PS2_DATA_DDR  |=  (1<<PS2_DATA_BIT);
318 }
319 static inline void data_hi()
320 {
321     /* input with pull up */
322     PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
323     PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
324 }
325 static inline bool data_in()
326 {
327     PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
328     PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
329     return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
330 }
331
332 static inline uint16_t wait_clock_lo(uint16_t us)
333 {
334     while (clock_in()  && us) { asm(""); _delay_us(1); us--; }
335     return us;
336 }
337 static inline uint16_t wait_clock_hi(uint16_t us)
338 {
339     while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
340     return us;
341 }
342 static inline uint16_t wait_data_lo(uint16_t us)
343 {
344     while (data_in() && us)  { asm(""); _delay_us(1); us--; }
345     return us;
346 }
347 static inline uint16_t wait_data_hi(uint16_t us)
348 {
349     while (!data_in() && us)  { asm(""); _delay_us(1); us--; }
350     return us;
351 }
352
353 /* idle state that device can send */
354 static inline void idle(void)
355 {
356     clock_hi();
357     data_hi();
358 }
359
360 /* inhibit device to send */
361 static inline void inhibit(void)
362 {
363     clock_lo();
364     data_hi();
365 }