]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - ps2.c
added 'Keymap' section to adb/README.
[max/tmk_keyboard.git] / ps2.c
1 /*
2 Copyright (c) 2010 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 <util/delay.h>
40 #include "ps2.h"
41 #include "print.h"
42 #include "debug.h"
43
44
45 static inline void clock_lo(void);
46 static inline void clock_hi(void);
47 static inline bool clock_in(void);
48 static inline void data_lo(void);
49 static inline void data_hi(void);
50 static inline bool data_in(void);
51 static inline uint16_t wait_clock_lo(uint16_t us);
52 static inline uint16_t wait_clock_hi(uint16_t us);
53 static inline uint16_t wait_data_lo(uint16_t us);
54 static inline uint16_t wait_data_hi(uint16_t us);
55
56
57 /*
58 Primitive PS/2 Library for AVR
59 ==============================
60 Host side is only supported now.
61
62
63 I/O control
64 -----------
65 High state is asserted by input with pull up.
66
67
68 PS/2 References
69 ---------------
70 http://www.computer-engineering.org/ps2protocol/
71 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
72 */
73
74
75 #define WAIT(stat, us, err) do { \
76     if (!wait_##stat(us)) { \
77         ps2_error = err; \
78         goto ERROR; \
79     } \
80 } while (0)
81
82 #define WAIT_NORETRY(stat, us, err) do { \
83     if (!wait_##stat(us)) { \
84         ps2_error = err; \
85         return 0; \
86     } \
87 } while (0)
88
89
90 uint8_t ps2_error = PS2_ERR_NONE;
91
92
93 void ps2_host_init(void)
94 {
95     /* inhibit */
96     clock_lo();
97     data_hi();
98 }
99
100 uint8_t ps2_host_send(uint8_t data)
101 {
102     bool parity = true;
103     ps2_error = 0;
104
105     /* request to send */
106     clock_lo();
107     _delay_us(100);
108     /* start bit [1] */
109     data_lo();
110     clock_hi();
111     WAIT(clock_lo, 15000, 1);
112     /* data [2-9] */
113     for (uint8_t i = 0; i < 8; i++) {
114         if (data&(1<<i)) {
115             parity = !parity;
116             data_hi();
117         } else {
118             data_lo();
119         }
120         WAIT(clock_hi, 50, 2);
121         WAIT(clock_lo, 50, 3);
122     }
123     /* parity [10] */
124     if (parity) { data_hi(); } else { data_lo(); }
125     WAIT(clock_hi, 50, 4);
126     WAIT(clock_lo, 50, 5);
127     /* stop bit [11] */
128     data_hi();
129     /* ack [12] */
130     WAIT(data_lo, 50, 6);
131     WAIT(clock_lo, 50, 7);
132     WAIT(clock_hi, 50, 8);
133     WAIT(data_hi, 50, 9);
134
135     /* inhibit device to send */
136     clock_lo();
137
138     return 1;
139 ERROR:
140     /* inhibit device to send */
141     data_hi();
142     clock_lo();
143     return 0;
144 }
145
146 uint8_t ps2_host_recv(void)
147 {
148     uint8_t data = 0;
149     bool parity = true;
150     ps2_error = 0;
151
152     /* cancel to sync */
153     clock_lo();
154     _delay_us(100);
155
156     /* release lines(idle state) */
157     clock_hi();
158     data_hi();
159
160     /* start bit [1] */
161     WAIT(clock_lo, 20000, 1);
162     WAIT(data_lo, 1, 2);
163     WAIT(clock_hi, 50, 3);
164
165     /* data [2-9] */
166     for (uint8_t i = 0; i < 8; i++) {
167         WAIT(clock_lo, 50, 4);
168         if (data_in()) {
169             parity = !parity;
170             data |= (1<<i);
171         }
172         WAIT(clock_hi, 50, 5);
173     }
174
175     /* parity [10] */
176     WAIT(clock_lo, 50, 6);
177     if (data_in() != parity) {
178         ps2_error = PS2_ERR_PARITY;
179         goto ERROR;
180     }
181     WAIT(clock_hi, 50, 7);
182
183     /* stop bit [11] */
184     WAIT(clock_lo, 50, 8);
185     WAIT(data_hi, 1, 9);
186     WAIT(clock_hi, 50, 10);
187
188     /* inhibit device to send */
189     clock_lo();
190
191     return data;
192 ERROR:
193     /* inhibit device to send */
194     data_hi();
195     clock_lo();
196     return 0;
197 }
198
199
200 static inline void clock_lo()
201 {
202     PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
203     PS2_CLOCK_DDR  |=  (1<<PS2_CLOCK_BIT);
204 }
205 static inline void clock_hi()
206 {
207     /* input with pull up */
208     PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
209     PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
210 }
211 static inline bool clock_in()
212 {
213     PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
214     PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
215     return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
216 }
217 static inline void data_lo()
218 {
219     PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
220     PS2_DATA_DDR  |=  (1<<PS2_DATA_BIT);
221 }
222 static inline void data_hi()
223 {
224     /* input with pull up */
225     PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
226     PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
227 }
228 static inline bool data_in()
229 {
230     PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
231     PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
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 }