2 Copyright 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.
40 #include <avr/interrupt.h>
41 #include <util/delay.h>
46 static uint8_t recv_data(void);
47 static inline void clock_lo(void);
48 static inline void clock_hi(void);
49 static inline bool clock_in(void);
50 static inline void data_lo(void);
51 static inline void data_hi(void);
52 static inline bool data_in(void);
53 static inline uint16_t wait_clock_lo(uint16_t us);
54 static inline uint16_t wait_clock_hi(uint16_t us);
55 static inline uint16_t wait_data_lo(uint16_t us);
56 static inline uint16_t wait_data_hi(uint16_t us);
57 static inline void idle(void);
58 static inline void inhibit(void);
62 Primitive PS/2 Library for AVR
63 ==============================
64 Host side is only supported now.
69 High state is asserted by input with pull up.
74 http://www.computer-engineering.org/ps2protocol/
75 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
79 #define WAIT(stat, us, err) do { \
80 if (!wait_##stat(us)) { \
87 uint8_t ps2_error = PS2_ERR_NONE;
90 void ps2_host_init(void)
100 // TODO: send using interrupt if available
101 uint8_t ps2_host_send(uint8_t data)
105 ps2_error = PS2_ERR_NONE;
106 #ifdef PS2_INT_DISABLE
109 /* terminate a transmission if we have */
116 WAIT(clock_lo, 15000, 1);
118 for (uint8_t i = 0; i < 8; i++) {
126 WAIT(clock_hi, 50, 2);
127 WAIT(clock_lo, 50, 3);
131 if (parity) { data_hi(); } else { data_lo(); }
132 WAIT(clock_hi, 50, 4);
133 WAIT(clock_lo, 50, 5);
138 WAIT(data_lo, 50, 6);
139 WAIT(clock_lo, 50, 7);
141 /* wait for idle state */
142 WAIT(clock_hi, 50, 8);
143 WAIT(data_hi, 50, 9);
145 res = ps2_host_recv_response();
147 #ifdef PS2_INT_ENABLE
156 /* receive data when host want else inhibit communication */
157 uint8_t ps2_host_recv_response(void)
161 /* terminate a transmission if we have */
165 /* release lines(idle state) */
177 uint8_t ps2_host_recv(void)
179 return ps2_host_recv_response();
182 /* ring buffer to store ps/2 key data */
184 static uint8_t pbuf[PBUF_SIZE];
185 static uint8_t pbuf_head = 0;
186 static uint8_t pbuf_tail = 0;
187 static inline void pbuf_enqueue(uint8_t data)
194 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
195 if (next != pbuf_tail) {
196 pbuf[pbuf_head] = data;
199 debug("pbuf: full\n");
203 static inline uint8_t pbuf_dequeue(void)
209 if (pbuf_head != pbuf_tail) {
210 val = pbuf[pbuf_tail];
211 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
218 /* get data received by interrupt */
219 uint8_t ps2_host_recv(void)
224 ps2_host_send(0xFE); // request to resend
225 ps2_error = PS2_ERR_NONE;
228 return pbuf_dequeue();
232 #define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
233 #define DEBUGP(x) do { PORTC = x; } while (0)
235 #define DEBUGP_INIT()
243 BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
247 static uint8_t data = 0;
248 static uint8_t parity = 1;
250 // TODO: abort if elapse 100us from previous interrupt
252 // return unless falling edge
280 if (!(parity & 0x01))
311 static void ps2_reset(void)
316 /* send LED state to keyboard */
317 void ps2_host_set_led(uint8_t led)
324 /* called after start bit comes */
325 static uint8_t recv_data(void)
329 ps2_error = PS2_ERR_NONE;
332 WAIT(clock_lo, 1, 1);
334 WAIT(clock_hi, 50, 3);
337 for (uint8_t i = 0; i < 8; i++) {
338 WAIT(clock_lo, 50, 4);
343 WAIT(clock_hi, 50, 5);
347 WAIT(clock_lo, 50, 6);
348 if (data_in() != parity) {
349 ps2_error = PS2_ERR_PARITY;
352 WAIT(clock_hi, 50, 7);
355 WAIT(clock_lo, 50, 8);
357 WAIT(clock_hi, 50, 10);
364 static inline void clock_lo()
366 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
367 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
369 static inline void clock_hi()
371 /* input with pull up */
372 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
373 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
375 static inline bool clock_in()
377 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
378 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
380 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
382 static inline void data_lo()
384 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
385 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
387 static inline void data_hi()
389 /* input with pull up */
390 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
391 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
393 static inline bool data_in()
395 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
396 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
398 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
401 static inline uint16_t wait_clock_lo(uint16_t us)
403 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
406 static inline uint16_t wait_clock_hi(uint16_t us)
408 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
411 static inline uint16_t wait_data_lo(uint16_t us)
413 while (data_in() && us) { asm(""); _delay_us(1); us--; }
416 static inline uint16_t wait_data_hi(uint16_t us)
418 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
422 /* idle state that device can send */
423 static inline void idle(void)
429 /* inhibit device to send */
430 static inline void inhibit(void)