2 Copyright (c) 2010,2011 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 <avr/interrupt.h>
40 #include <util/delay.h>
45 static uint8_t recv_data(void);
46 static inline void clock_lo(void);
47 static inline void clock_hi(void);
48 static inline bool clock_in(void);
49 static inline void data_lo(void);
50 static inline void data_hi(void);
51 static inline bool data_in(void);
52 static inline uint16_t wait_clock_lo(uint16_t us);
53 static inline uint16_t wait_clock_hi(uint16_t us);
54 static inline uint16_t wait_data_lo(uint16_t us);
55 static inline uint16_t wait_data_hi(uint16_t us);
56 static inline void idle(void);
57 static inline void inhibit(void);
61 Primitive PS/2 Library for AVR
62 ==============================
63 Host side is only supported now.
68 High state is asserted by input with pull up.
73 http://www.computer-engineering.org/ps2protocol/
74 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
78 #define WAIT(stat, us, err) do { \
79 if (!wait_##stat(us)) { \
86 uint8_t ps2_error = PS2_ERR_NONE;
89 void ps2_host_init(void)
99 uint8_t ps2_host_send(uint8_t data)
106 /* terminate a transmission if we have */
113 WAIT(clock_lo, 15000, 1);
115 for (uint8_t i = 0; i < 8; i++) {
123 WAIT(clock_hi, 50, 2);
124 WAIT(clock_lo, 50, 3);
128 if (parity) { data_hi(); } else { data_lo(); }
129 WAIT(clock_hi, 50, 4);
130 WAIT(clock_lo, 50, 5);
135 WAIT(data_lo, 50, 6);
136 WAIT(clock_lo, 50, 7);
138 /* wait for idle state */
139 WAIT(clock_hi, 50, 8);
140 WAIT(data_hi, 50, 9);
142 uint8_t res = ps2_host_recv_response();
143 if (res == 0xFE && data != 0xFE)
153 /* receive data when host want else inhibit communication */
154 uint8_t ps2_host_recv_response(void)
158 /* terminate a transmission if we have */
162 /* release lines(idle state) */
174 uint8_t ps2_host_recv(void)
176 return ps2_host_recv_response();
179 /* ring buffer to store ps/2 key data */
181 static uint8_t pbuf[PBUF_SIZE];
182 static uint8_t pbuf_head = 0;
183 static uint8_t pbuf_tail = 0;
184 static inline void pbuf_enqueue(uint8_t data)
188 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
189 if (next != pbuf_tail) {
190 pbuf[pbuf_head] = data;
193 debug("pbuf: full\n");
196 static inline uint8_t pbuf_dequeue(void)
201 if (pbuf_head != pbuf_tail) {
202 val = pbuf[pbuf_tail];
203 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
209 /* get data received by interrupt */
210 uint8_t ps2_host_recv(void)
212 return pbuf_dequeue();
217 /* interrupt means start bit comes */
218 pbuf_enqueue(recv_data());
220 /* release lines(idle state) */
228 static void ps2_reset(void)
231 if (ps2_host_recv_response() == 0xFA) {
233 ps2_host_recv_response();
238 /* send LED state to keyboard */
239 void ps2_host_set_led(uint8_t led)
241 #ifdef PS2_INT_DISABLE
245 ps2_host_recv_response();
247 ps2_host_recv_response();
248 #ifdef PS2_INT_ENABLE
255 /* called after start bit comes */
256 static uint8_t recv_data(void)
263 WAIT(clock_lo, 1, 1);
265 WAIT(clock_hi, 50, 3);
268 for (uint8_t i = 0; i < 8; i++) {
269 WAIT(clock_lo, 50, 4);
274 WAIT(clock_hi, 50, 5);
278 WAIT(clock_lo, 50, 6);
279 if (data_in() != parity) {
280 ps2_error = PS2_ERR_PARITY;
283 WAIT(clock_hi, 50, 7);
286 WAIT(clock_lo, 50, 8);
288 WAIT(clock_hi, 50, 10);
295 static inline void clock_lo()
297 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
298 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
300 static inline void clock_hi()
302 /* input with pull up */
303 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
304 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
306 static inline bool clock_in()
308 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
309 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
310 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
312 static inline void data_lo()
314 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
315 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
317 static inline void data_hi()
319 /* input with pull up */
320 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
321 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
323 static inline bool data_in()
325 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
326 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
327 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
330 static inline uint16_t wait_clock_lo(uint16_t us)
332 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
335 static inline uint16_t wait_clock_hi(uint16_t us)
337 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
340 static inline uint16_t wait_data_lo(uint16_t us)
342 while (data_in() && us) { asm(""); _delay_us(1); us--; }
345 static inline uint16_t wait_data_hi(uint16_t us)
347 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
351 /* idle state that device can send */
352 static inline void idle(void)
358 /* inhibit device to send */
359 static inline void inhibit(void)