aboutsummaryrefslogtreecommitdiff
path: root/ps2.c
diff options
context:
space:
mode:
Diffstat (limited to 'ps2.c')
-rw-r--r--ps2.c248
1 files changed, 248 insertions, 0 deletions
diff --git a/ps2.c b/ps2.c
new file mode 100644
index 000000000..adb425ae3
--- /dev/null
+++ b/ps2.c
@@ -0,0 +1,248 @@
1/*
2Copyright (c) 2010 Jun WAKO <wakojun@gmail.com>
3
4This software is licensed with a Modified BSD License.
5All of this is supposed to be Free Software, Open Source, DFSG-free,
6GPL-compatible, and OK to use in both free and proprietary applications.
7Additions and corrections to this file are welcome.
8
9
10Redistribution and use in source and binary forms, with or without
11modification, 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
25THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35POSSIBILITY 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
45static inline void clock_lo(void);
46static inline void clock_hi(void);
47static inline bool clock_in(void);
48static inline void data_lo(void);
49static inline void data_hi(void);
50static inline bool data_in(void);
51static inline uint16_t wait_clock_lo(uint16_t us);
52static inline uint16_t wait_clock_hi(uint16_t us);
53static inline uint16_t wait_data_lo(uint16_t us);
54static inline uint16_t wait_data_hi(uint16_t us);
55
56
57/*
58Primitive PS/2 Library for AVR
59==============================
60Host side is only supported now.
61
62
63I/O control
64-----------
65High state is asserted by input with pull up.
66
67
68PS/2 References
69---------------
70http://www.computer-engineering.org/ps2protocol/
71http://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
90uint8_t ps2_error = PS2_ERR_NONE;
91
92
93void ps2_host_init(void)
94{
95 /* inhibit */
96 clock_lo();
97 data_hi();
98}
99
100uint8_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 data_lo();
108 _delay_us(100);
109 /* start bit [1] */
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;
139ERROR:
140 return 0;
141}
142
143uint8_t ps2_host_recv(void)
144{
145 uint8_t data = 0;
146 bool parity = true;
147 ps2_error = 0;
148
149 /* cancel to sync */
150 clock_lo();
151 _delay_us(100);
152
153 /* release lines(idle state) */
154 clock_hi();
155 data_hi();
156
157 /* start bit [1] */
158 WAIT(clock_lo, 20000, 1);
159 WAIT(data_lo, 1, 2);
160 WAIT(clock_hi, 50, 3);
161
162 /* data [2-9] */
163 for (uint8_t i = 0; i < 8; i++) {
164 WAIT(clock_lo, 50, 4);
165 if (data_in()) {
166 parity = !parity;
167 data |= (1<<i);
168 }
169 WAIT(clock_hi, 50, 5);
170 }
171
172 /* parity [10] */
173 WAIT(clock_lo, 50, 6);
174 if (data_in() != parity) {
175 ps2_error = PS2_ERR_PARITY;
176 goto ERROR;
177 }
178 WAIT(clock_hi, 50, 7);
179
180 /* stop bit [11] */
181 WAIT(clock_lo, 50, 8);
182 WAIT(data_hi, 1, 9);
183 WAIT(clock_hi, 50, 10);
184
185 /* inhibit device to send */
186 clock_lo();
187
188 return data;
189ERROR:
190 return 0;
191}
192
193
194static inline void clock_lo()
195{
196 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
197 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
198}
199static inline void clock_hi()
200{
201 /* input with pull up */
202 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
203 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
204}
205static inline bool clock_in()
206{
207 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
208 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
209 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
210}
211static inline void data_lo()
212{
213 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
214 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
215}
216static inline void data_hi()
217{
218 /* input with pull up */
219 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
220 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
221}
222static inline bool data_in()
223{
224 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
225 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
226 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
227}
228
229static inline uint16_t wait_clock_lo(uint16_t us)
230{
231 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
232 return us;
233}
234static inline uint16_t wait_clock_hi(uint16_t us)
235{
236 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
237 return us;
238}
239static inline uint16_t wait_data_lo(uint16_t us)
240{
241 while (data_in() && us) { asm(""); _delay_us(1); us--; }
242 return us;
243}
244static inline uint16_t wait_data_hi(uint16_t us)
245{
246 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
247 return us;
248}