]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/ps2_usart.c
Fix PS/2 USART version
[tmk_firmware.git] / protocol / ps2_usart.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 Primitive PS/2 Library for AVR
40 ==============================
41 Host side is only supported now.
42 Synchronous USART is used to receive data by hardware process
43 rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
44 cannot interpose. In the result it is prone to lost CLOCK edge.
45
46
47 I/O control
48 -----------
49 High state is asserted by internal pull-up.
50 If you have a signaling problem, you may need to have
51 external pull-up resisters on CLOCK and DATA line.
52
53
54 PS/2 References
55 ---------------
56 http://www.computer-engineering.org/ps2protocol/
57 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
58 */
59 #include <stdbool.h>
60 #include <avr/io.h>
61 #include <avr/interrupt.h>
62 #include <util/delay.h>
63 #include "ps2.h"
64 #include "debug.h"
65
66
67 #define WAIT(stat, us, err) do { \
68     if (!wait_##stat(us)) { \
69         ps2_error = err; \
70         goto ERROR; \
71     } \
72 } while (0)
73
74
75 uint8_t ps2_error = PS2_ERR_NONE;
76
77
78 static inline void clock_lo(void);
79 static inline void clock_hi(void);
80 static inline bool clock_in(void);
81 static inline void data_lo(void);
82 static inline void data_hi(void);
83 static inline bool data_in(void);
84 static inline uint16_t wait_clock_lo(uint16_t us);
85 static inline uint16_t wait_clock_hi(uint16_t us);
86 static inline uint16_t wait_data_lo(uint16_t us);
87 static inline uint16_t wait_data_hi(uint16_t us);
88 static inline void idle(void);
89 static inline void inhibit(void);
90 static inline uint8_t pbuf_dequeue(void);
91 static inline void pbuf_enqueue(uint8_t data);
92 static inline bool pbuf_has_data(void);
93 static inline void pbuf_clear(void);
94
95
96 void ps2_host_init(void)
97 {
98     idle();
99     PS2_USART_INIT();
100     PS2_USART_RX_INT_ON();
101 }
102
103 uint8_t ps2_host_send(uint8_t data)
104 {
105     uint8_t res = 0;
106     bool parity = true;
107     ps2_error = PS2_ERR_NONE;
108
109     PS2_USART_OFF();
110
111     /* terminate a transmission if we have */
112     inhibit();
113     _delay_us(100);
114
115     /* start bit [1] */
116     data_lo();
117     clock_hi();
118     WAIT(clock_lo, 15000, 1);
119     /* data [2-9] */
120     for (uint8_t i = 0; i < 8; i++) {
121         _delay_us(15);
122         if (data&(1<<i)) {
123             parity = !parity;
124             data_hi();
125         } else {
126             data_lo();
127         }
128         WAIT(clock_hi, 50, 2);
129         WAIT(clock_lo, 50, 3);
130     }
131     /* parity [10] */
132     _delay_us(15);
133     if (parity) { data_hi(); } else { data_lo(); }
134     WAIT(clock_hi, 50, 4);
135     WAIT(clock_lo, 50, 5);
136     /* stop bit [11] */
137     _delay_us(15);
138     data_hi();
139     /* ack [12] */
140     WAIT(data_lo, 50, 6);
141     WAIT(clock_lo, 50, 7);
142
143     /* wait for idle state */
144     WAIT(clock_hi, 50, 8);
145     WAIT(data_hi, 50, 9);
146
147     PS2_USART_INIT();
148     PS2_USART_RX_INT_ON();
149     res = ps2_host_recv_response();
150 ERROR:
151     idle();
152     PS2_USART_INIT();
153     PS2_USART_RX_INT_ON();
154     return res;
155 }
156
157 // Do polling data from keyboard to get response to last command.
158 uint8_t ps2_host_recv_response(void)
159 {
160     while (!pbuf_has_data()) {
161         _delay_ms(1);   // without this delay it seems to fall into deadlock
162     }
163     return pbuf_dequeue();
164 }
165
166 uint8_t ps2_host_recv(void)
167 {
168     return pbuf_dequeue();
169 }
170
171 ISR(PS2_USART_RX_VECT)
172 {
173     uint8_t error = PS2_USART_ERROR;
174     uint8_t data = PS2_USART_RX_DATA;
175     if (!error) {
176         pbuf_enqueue(data);
177     }
178 }
179
180 /* send LED state to keyboard */
181 void ps2_host_set_led(uint8_t led)
182 {
183     // send 0xED then keyboard keeps waiting for next LED data
184     // and keyboard does not send any scan codes during waiting.
185     // If fail to send LED data keyboard looks like being freezed.
186     uint8_t retry = 3;
187     while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
188         ;
189     retry = 3;
190     while (retry-- && ps2_host_send(led) != PS2_ACK)
191         ;
192 }
193
194
195 /*--------------------------------------------------------------------
196  * static functions
197  *------------------------------------------------------------------*/
198 static inline void clock_lo()
199 {
200     PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
201     PS2_CLOCK_DDR  |=  (1<<PS2_CLOCK_BIT);
202 }
203 static inline void clock_hi()
204 {
205     /* input with pull up */
206     PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
207     PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
208 }
209 static inline bool clock_in()
210 {
211     PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
212     PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
213     _delay_us(1);
214     return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
215 }
216 static inline void data_lo()
217 {
218     PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
219     PS2_DATA_DDR  |=  (1<<PS2_DATA_BIT);
220 }
221 static inline void data_hi()
222 {
223     /* input with pull up */
224     PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
225     PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
226 }
227 static inline bool data_in()
228 {
229     PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
230     PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
231     _delay_us(1);
232     return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
233 }
234
235 static inline uint16_t wait_clock_lo(uint16_t us)
236 {
237     while (clock_in()  && us) { asm(""); _delay_us(1); us--; }
238     return us;
239 }
240 static inline uint16_t wait_clock_hi(uint16_t us)
241 {
242     while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
243     return us;
244 }
245 static inline uint16_t wait_data_lo(uint16_t us)
246 {
247     while (data_in() && us)  { asm(""); _delay_us(1); us--; }
248     return us;
249 }
250 static inline uint16_t wait_data_hi(uint16_t us)
251 {
252     while (!data_in() && us)  { asm(""); _delay_us(1); us--; }
253     return us;
254 }
255
256 /* idle state that device can send */
257 static inline void idle(void)
258 {
259     clock_hi();
260     data_hi();
261 }
262
263 /* inhibit device to send */
264 static inline void inhibit(void)
265 {
266     clock_lo();
267     data_hi();
268 }
269
270
271 /*--------------------------------------------------------------------
272  * Ring buffer to store scan codes from keyboard
273  *------------------------------------------------------------------*/
274 #define PBUF_SIZE 32
275 static uint8_t pbuf[PBUF_SIZE];
276 static uint8_t pbuf_head = 0;
277 static uint8_t pbuf_tail = 0;
278 static inline void pbuf_enqueue(uint8_t data)
279 {
280     uint8_t sreg = SREG;
281     cli();
282     uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
283     if (next != pbuf_tail) {
284         pbuf[pbuf_head] = data;
285         pbuf_head = next;
286     } else {
287         debug("pbuf: full\n");
288     }
289     SREG = sreg;
290 }
291
292 static inline uint8_t pbuf_dequeue(void)
293 {
294     uint8_t val = 0;
295
296     uint8_t sreg = SREG;
297     cli();
298     if (pbuf_head != pbuf_tail) {
299         val = pbuf[pbuf_tail];
300         pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
301     }
302     SREG = sreg;
303
304     return val;
305 }
306 static inline bool pbuf_has_data(void)
307 {
308     uint8_t sreg = SREG;
309     cli();
310     bool has_data = (pbuf_head != pbuf_tail);
311     SREG = sreg;
312     return has_data;
313 }
314 static inline void pbuf_clear(void)
315 {
316     uint8_t sreg = SREG;
317     cli();
318     pbuf_head = pbuf_tail = 0;
319     SREG = sreg;
320 }