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 */
143 uint8_t ps2_host_recv(void)
153 /* release lines(idle state) */
158 WAIT(clock_lo, 20000, 1);
160 WAIT(clock_hi, 50, 3);
163 for (uint8_t i = 0; i < 8; i++) {
164 WAIT(clock_lo, 50, 4);
169 WAIT(clock_hi, 50, 5);
173 WAIT(clock_lo, 50, 6);
174 if (data_in() != parity) {
175 ps2_error = PS2_ERR_PARITY;
178 WAIT(clock_hi, 50, 7);
181 WAIT(clock_lo, 50, 8);
183 WAIT(clock_hi, 50, 10);
185 /* inhibit device to send */
194 static inline void clock_lo()
196 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
197 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
199 static inline void clock_hi()
201 /* input with pull up */
202 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
203 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
205 static inline bool clock_in()
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);
211 static inline void data_lo()
213 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
214 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
216 static inline void data_hi()
218 /* input with pull up */
219 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
220 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
222 static inline bool data_in()
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);
229 static inline uint16_t wait_clock_lo(uint16_t us)
231 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
234 static inline uint16_t wait_clock_hi(uint16_t us)
236 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
239 static inline uint16_t wait_data_lo(uint16_t us)
241 while (data_in() && us) { asm(""); _delay_us(1); us--; }
244 static inline uint16_t wait_data_hi(uint16_t us)
246 while (!data_in() && us) { asm(""); _delay_us(1); us--; }