2 Copyright (c) 2010 Jun WAKO <wakojun@gmail.com>
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.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
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
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.
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.
39 #include <util/delay.h>
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);
58 Primitive PS/2 Library for AVR
59 ==============================
60 Host side is only supported now.
65 High state is asserted by input with pull up.
70 http://www.computer-engineering.org/ps2protocol/
71 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
75 #define WAIT(stat, us, err) do { \
76 if (!wait_##stat(us)) { \
82 #define WAIT_NORETRY(stat, us, err) do { \
83 if (!wait_##stat(us)) { \
90 uint8_t ps2_error = PS2_ERR_NONE;
93 void ps2_host_init(void)
100 uint8_t ps2_host_send(uint8_t data)
105 /* request to send */
111 WAIT(clock_lo, 15000, 1);
113 for (uint8_t i = 0; i < 8; i++) {
120 WAIT(clock_hi, 50, 2);
121 WAIT(clock_lo, 50, 3);
124 if (parity) { data_hi(); } else { data_lo(); }
125 WAIT(clock_hi, 50, 4);
126 WAIT(clock_lo, 50, 5);
130 WAIT(data_lo, 50, 6);
131 WAIT(clock_lo, 50, 7);
132 WAIT(clock_hi, 50, 8);
133 WAIT(data_hi, 50, 9);
135 /* inhibit device to send */
140 /* inhibit device to send */
146 uint8_t ps2_host_recv(void)
152 /* terminate a transmission if we have */
156 /* release lines(idle state) */
161 WAIT(clock_lo, 2000, 1); // How long should we wait?
163 WAIT(clock_hi, 50, 3);
166 for (uint8_t i = 0; i < 8; i++) {
167 WAIT(clock_lo, 50, 4);
172 WAIT(clock_hi, 50, 5);
176 WAIT(clock_lo, 50, 6);
177 if (data_in() != parity) {
178 ps2_error = PS2_ERR_PARITY;
181 WAIT(clock_hi, 50, 7);
184 WAIT(clock_lo, 50, 8);
186 WAIT(clock_hi, 50, 10);
188 /* inhibit device to send */
193 /* inhibit device to send */
200 static inline void clock_lo()
202 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
203 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
205 static inline void clock_hi()
207 /* input with pull up */
208 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
209 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
211 static inline bool clock_in()
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);
217 static inline void data_lo()
219 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
220 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
222 static inline void data_hi()
224 /* input with pull up */
225 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
226 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
228 static inline bool data_in()
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);
235 static inline uint16_t wait_clock_lo(uint16_t us)
237 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
240 static inline uint16_t wait_clock_hi(uint16_t us)
242 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
245 static inline uint16_t wait_data_lo(uint16_t us)
247 while (data_in() && us) { asm(""); _delay_us(1); us--; }
250 static inline uint16_t wait_data_hi(uint16_t us)
252 while (!data_in() && us) { asm(""); _delay_us(1); us--; }