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.
38 Primitive PS/2 Library for AVR
39 ==============================
40 Host side is only supported now.
41 Synchronous USART is used to receive data by hardware process
42 rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
43 cannot interpose. In the result it is prone to lost CLOCK edge.
48 High state is asserted by internal pull-up.
49 If you have a signaling problem, you may need to have
50 external pull-up resisters on CLOCK and DATA line.
55 http://www.computer-engineering.org/ps2protocol/
56 http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
60 #include <avr/interrupt.h>
61 #include <util/delay.h>
67 #define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
68 #define DEBUGP(x) do { PORTC = x; } while (0)
74 #define WAIT(stat, us, err) do { \
75 if (!wait_##stat(us)) { \
82 uint8_t ps2_error = PS2_ERR_NONE;
85 static inline void clock_lo(void);
86 static inline void clock_hi(void);
87 static inline bool clock_in(void);
88 static inline void data_lo(void);
89 static inline void data_hi(void);
90 static inline bool data_in(void);
91 static inline uint16_t wait_clock_lo(uint16_t us);
92 static inline uint16_t wait_clock_hi(uint16_t us);
93 static inline uint16_t wait_data_lo(uint16_t us);
94 static inline uint16_t wait_data_hi(uint16_t us);
95 static inline void idle(void);
96 static inline void inhibit(void);
97 #if defined PS2_USE_INT || defined PS2_USE_USART
98 static inline uint8_t pbuf_dequeue(void);
99 static inline void pbuf_enqueue(uint8_t data);
103 void ps2_host_init(void)
109 PS2_USART_RX_INT_ON();
112 uint8_t ps2_host_send(uint8_t data)
116 ps2_error = PS2_ERR_NONE;
121 /* terminate a transmission if we have */
128 WAIT(clock_lo, 15000, 1);
130 for (uint8_t i = 0; i < 8; i++) {
138 WAIT(clock_hi, 50, 2);
139 WAIT(clock_lo, 50, 3);
143 if (parity) { data_hi(); } else { data_lo(); }
144 WAIT(clock_hi, 50, 4);
145 WAIT(clock_lo, 50, 5);
150 WAIT(data_lo, 50, 6);
151 WAIT(clock_lo, 50, 7);
153 /* wait for idle state */
154 WAIT(clock_hi, 50, 8);
155 WAIT(data_hi, 50, 9);
157 res = ps2_host_recv_response();
161 PS2_USART_RX_INT_ON();
165 // Do polling data from keyboard to get response to last command.
166 uint8_t ps2_host_recv_response(void)
170 PS2_USART_RX_POLL_ON();
171 while (!PS2_USART_RX_READY)
173 data = PS2_USART_RX_DATA;
179 uint8_t ps2_host_recv(void)
181 return pbuf_dequeue();
184 ISR(PS2_USART_RX_VECT)
187 uint8_t error = PS2_USART_ERROR;
188 uint8_t data = PS2_USART_RX_DATA;
197 /* send LED state to keyboard */
198 void ps2_host_set_led(uint8_t led)
200 // send 0xED then keyboard keeps waiting for next LED data
201 // and keyboard does not send any scan codes during waiting.
202 // If fail to send LED data keyboard looks like being freezed.
204 while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
207 while (retry-- && ps2_host_send(led) != PS2_ACK)
212 /*--------------------------------------------------------------------
214 *------------------------------------------------------------------*/
215 static inline void clock_lo()
217 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
218 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
220 static inline void clock_hi()
222 /* input with pull up */
223 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
224 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
226 static inline bool clock_in()
228 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
229 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
231 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
233 static inline void data_lo()
235 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
236 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
238 static inline void data_hi()
240 /* input with pull up */
241 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
242 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
244 static inline bool data_in()
246 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
247 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
249 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
252 static inline uint16_t wait_clock_lo(uint16_t us)
254 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
257 static inline uint16_t wait_clock_hi(uint16_t us)
259 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
262 static inline uint16_t wait_data_lo(uint16_t us)
264 while (data_in() && us) { asm(""); _delay_us(1); us--; }
267 static inline uint16_t wait_data_hi(uint16_t us)
269 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
273 /* idle state that device can send */
274 static inline void idle(void)
280 /* inhibit device to send */
281 static inline void inhibit(void)
288 /*--------------------------------------------------------------------
289 * Ring buffer to store scan codes from keyboard
290 *------------------------------------------------------------------*/
292 static uint8_t pbuf[PBUF_SIZE];
293 static uint8_t pbuf_head = 0;
294 static uint8_t pbuf_tail = 0;
295 static inline void pbuf_enqueue(uint8_t data)
302 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
303 if (next != pbuf_tail) {
304 pbuf[pbuf_head] = data;
307 debug("pbuf: full\n");
312 static inline uint8_t pbuf_dequeue(void)
318 if (pbuf_head != pbuf_tail) {
319 val = pbuf[pbuf_tail];
320 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;