]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/ps2_busywait.c
Merge branch 'rhaberkorn-serial-mouse'
[tmk_firmware.git] / protocol / ps2_busywait.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 busywait version
40  */
41
42 #include <stdbool.h>
43 #include <util/delay.h>
44 #include "ps2.h"
45 #include "debug.h"
46
47
48 #define WAIT(stat, us, err) do { \
49     if (!wait_##stat(us)) { \
50         ps2_error = err; \
51         goto ERROR; \
52     } \
53 } while (0)
54
55
56 uint8_t ps2_error = PS2_ERR_NONE;
57
58
59 void ps2_host_init(void)
60 {
61     // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
62     _delay_ms(2500);
63
64     inhibit();
65 }
66
67 uint8_t ps2_host_send(uint8_t data)
68 {
69     bool parity = true;
70     ps2_error = PS2_ERR_NONE;
71
72     /* terminate a transmission if we have */
73     inhibit();
74     _delay_us(100); // 100us [4]p.13, [5]p.50
75
76     /* 'Request to Send' and Start bit */
77     data_lo();
78     clock_hi();
79     WAIT(clock_lo, 10000, 10);   // 10ms [5]p.50
80
81     /* Data bit */
82     for (uint8_t i = 0; i < 8; i++) {
83         _delay_us(15);
84         if (data&(1<<i)) {
85             parity = !parity;
86             data_hi();
87         } else {
88             data_lo();
89         }
90         WAIT(clock_hi, 50, 2);
91         WAIT(clock_lo, 50, 3);
92     }
93
94     /* Parity bit */
95     _delay_us(15);
96     if (parity) { data_hi(); } else { data_lo(); }
97     WAIT(clock_hi, 50, 4);
98     WAIT(clock_lo, 50, 5);
99
100     /* Stop bit */
101     _delay_us(15);
102     data_hi();
103
104     /* Ack */
105     WAIT(data_lo, 50, 6);
106     WAIT(clock_lo, 50, 7);
107
108     /* wait for idle state */
109     WAIT(clock_hi, 50, 8);
110     WAIT(data_hi, 50, 9);
111
112     inhibit();
113     return ps2_host_recv_response();
114 ERROR:
115     inhibit();
116     return 0;
117 }
118
119 /* receive data when host want else inhibit communication */
120 uint8_t ps2_host_recv_response(void)
121 {
122     // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
123     // 250 * 100us(wait for start bit in ps2_host_recv)
124     uint8_t data = 0;
125     uint8_t try = 250;
126     do {
127         data = ps2_host_recv();
128     } while (try-- && ps2_error);
129     return data;
130 }
131
132 /* called after start bit comes */
133 uint8_t ps2_host_recv(void)
134 {
135     uint8_t data = 0;
136     bool parity = true;
137     ps2_error = PS2_ERR_NONE;
138
139     /* release lines(idle state) */
140     idle();
141
142     /* start bit [1] */
143     WAIT(clock_lo, 100, 1); // TODO: this is enough?
144     WAIT(data_lo, 1, 2);
145     WAIT(clock_hi, 50, 3);
146
147     /* data [2-9] */
148     for (uint8_t i = 0; i < 8; i++) {
149         WAIT(clock_lo, 50, 4);
150         if (data_in()) {
151             parity = !parity;
152             data |= (1<<i);
153         }
154         WAIT(clock_hi, 50, 5);
155     }
156
157     /* parity [10] */
158     WAIT(clock_lo, 50, 6);
159     if (data_in() != parity) {
160         ps2_error = PS2_ERR_PARITY;
161         goto ERROR;
162     }
163     WAIT(clock_hi, 50, 7);
164
165     /* stop bit [11] */
166     WAIT(clock_lo, 50, 8);
167     WAIT(data_hi, 1, 9);
168     WAIT(clock_hi, 50, 10);
169
170     inhibit();
171     return data;
172 ERROR:
173     if (ps2_error > PS2_ERR_STARTBIT3) {
174         xprintf("x%02X\n", ps2_error);
175     }
176     inhibit();
177     return 0;
178 }
179
180 /* send LED state to keyboard */
181 void ps2_host_set_led(uint8_t led)
182 {
183     ps2_host_send(0xED);
184     ps2_host_send(led);
185 }