aboutsummaryrefslogtreecommitdiff
path: root/protocol/ps2_usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocol/ps2_usart.c')
-rw-r--r--protocol/ps2_usart.c324
1 files changed, 324 insertions, 0 deletions
diff --git a/protocol/ps2_usart.c b/protocol/ps2_usart.c
new file mode 100644
index 000000000..7d591c650
--- /dev/null
+++ b/protocol/ps2_usart.c
@@ -0,0 +1,324 @@
1/*
2Copyright 2010,2011 Jun WAKO <wakojun@gmail.com>
3
4This software is licensed with a Modified BSD License.
5All of this is supposed to be Free Software, Open Source, DFSG-free,
6GPL-compatible, and OK to use in both free and proprietary applications.
7Additions and corrections to this file are welcome.
8
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12
13* Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
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
19 distribution.
20
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.
24
25THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35POSSIBILITY OF SUCH DAMAGE.
36*/
37
38/*
39Primitive PS/2 Library for AVR
40==============================
41Host side is only supported now.
42Synchronous USART is used to receive data by hardware process
43rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
44cannot interpose. In the result it is prone to lost CLOCK edge.
45
46
47I/O control
48-----------
49High state is asserted by internal pull-up.
50If you have a signaling problem, you may need to have
51external pull-up resisters on CLOCK and DATA line.
52
53
54PS/2 References
55---------------
56http://www.computer-engineering.org/ps2protocol/
57http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
58*/
59#include <stdbool.h>
60#include <avr/io.h>
61#include <avr/interrupt.h>
62#include <util/delay.h>
63#include "ps2.h"
64#include "debug.h"
65
66
67#if 0
68#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
69#define DEBUGP(x) do { PORTC = x; } while (0)
70#else
71#define DEBUGP_INIT()
72#define DEBUGP(x)
73#endif
74
75#define WAIT(stat, us, err) do { \
76 if (!wait_##stat(us)) { \
77 ps2_error = err; \
78 goto ERROR; \
79 } \
80} while (0)
81
82
83uint8_t ps2_error = PS2_ERR_NONE;
84
85
86static inline void clock_lo(void);
87static inline void clock_hi(void);
88static inline bool clock_in(void);
89static inline void data_lo(void);
90static inline void data_hi(void);
91static inline bool data_in(void);
92static inline uint16_t wait_clock_lo(uint16_t us);
93static inline uint16_t wait_clock_hi(uint16_t us);
94static inline uint16_t wait_data_lo(uint16_t us);
95static inline uint16_t wait_data_hi(uint16_t us);
96static inline void idle(void);
97static inline void inhibit(void);
98static inline uint8_t pbuf_dequeue(void);
99static inline void pbuf_enqueue(uint8_t data);
100
101
102void ps2_host_init(void)
103{
104 DEBUGP_INIT();
105 DEBUGP(0x1);
106 idle();
107 PS2_USART_INIT();
108 PS2_USART_RX_INT_ON();
109}
110
111uint8_t ps2_host_send(uint8_t data)
112{
113 uint8_t res = 0;
114 bool parity = true;
115 ps2_error = PS2_ERR_NONE;
116
117 DEBUGP(0x6);
118 PS2_USART_OFF();
119
120 /* terminate a transmission if we have */
121 inhibit();
122 _delay_us(100);
123
124 /* start bit [1] */
125 data_lo();
126 clock_hi();
127 WAIT(clock_lo, 15000, 1);
128 /* data [2-9] */
129 for (uint8_t i = 0; i < 8; i++) {
130 _delay_us(15);
131 if (data&(1<<i)) {
132 parity = !parity;
133 data_hi();
134 } else {
135 data_lo();
136 }
137 WAIT(clock_hi, 50, 2);
138 WAIT(clock_lo, 50, 3);
139 }
140 /* parity [10] */
141 _delay_us(15);
142 if (parity) { data_hi(); } else { data_lo(); }
143 WAIT(clock_hi, 50, 4);
144 WAIT(clock_lo, 50, 5);
145 /* stop bit [11] */
146 _delay_us(15);
147 data_hi();
148 /* ack [12] */
149 WAIT(data_lo, 50, 6);
150 WAIT(clock_lo, 50, 7);
151
152 /* wait for idle state */
153 WAIT(clock_hi, 50, 8);
154 WAIT(data_hi, 50, 9);
155
156 res = ps2_host_recv_response();
157ERROR:
158 idle();
159 PS2_USART_INIT();
160 PS2_USART_RX_INT_ON();
161 return res;
162}
163
164// Do polling data from keyboard to get response to last command.
165uint8_t ps2_host_recv_response(void)
166{
167 uint8_t data = 0;
168 PS2_USART_INIT();
169 PS2_USART_RX_POLL_ON();
170 while (!PS2_USART_RX_READY)
171 ;
172 data = PS2_USART_RX_DATA;
173 PS2_USART_OFF();
174 DEBUGP(0x9);
175 return data;
176}
177
178uint8_t ps2_host_recv(void)
179{
180 return pbuf_dequeue();
181}
182
183ISR(PS2_USART_RX_VECT)
184{
185 DEBUGP(0x7);
186 uint8_t error = PS2_USART_ERROR;
187 uint8_t data = PS2_USART_RX_DATA;
188 if (error) {
189 DEBUGP(error>>2);
190 } else {
191 pbuf_enqueue(data);
192 }
193 DEBUGP(0x8);
194}
195
196/* send LED state to keyboard */
197void ps2_host_set_led(uint8_t led)
198{
199 // send 0xED then keyboard keeps waiting for next LED data
200 // and keyboard does not send any scan codes during waiting.
201 // If fail to send LED data keyboard looks like being freezed.
202 uint8_t retry = 3;
203 while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
204 ;
205 retry = 3;
206 while (retry-- && ps2_host_send(led) != PS2_ACK)
207 ;
208}
209
210
211/*--------------------------------------------------------------------
212 * static functions
213 *------------------------------------------------------------------*/
214static inline void clock_lo()
215{
216 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
217 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
218}
219static inline void clock_hi()
220{
221 /* input with pull up */
222 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
223 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
224}
225static inline bool clock_in()
226{
227 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
228 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
229 _delay_us(1);
230 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
231}
232static inline void data_lo()
233{
234 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
235 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
236}
237static inline void data_hi()
238{
239 /* input with pull up */
240 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
241 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
242}
243static inline bool data_in()
244{
245 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
246 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
247 _delay_us(1);
248 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
249}
250
251static inline uint16_t wait_clock_lo(uint16_t us)
252{
253 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
254 return us;
255}
256static inline uint16_t wait_clock_hi(uint16_t us)
257{
258 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
259 return us;
260}
261static inline uint16_t wait_data_lo(uint16_t us)
262{
263 while (data_in() && us) { asm(""); _delay_us(1); us--; }
264 return us;
265}
266static inline uint16_t wait_data_hi(uint16_t us)
267{
268 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
269 return us;
270}
271
272/* idle state that device can send */
273static inline void idle(void)
274{
275 clock_hi();
276 data_hi();
277}
278
279/* inhibit device to send */
280static inline void inhibit(void)
281{
282 clock_lo();
283 data_hi();
284}
285
286
287/*--------------------------------------------------------------------
288 * Ring buffer to store scan codes from keyboard
289 *------------------------------------------------------------------*/
290#define PBUF_SIZE 8
291static uint8_t pbuf[PBUF_SIZE];
292static uint8_t pbuf_head = 0;
293static uint8_t pbuf_tail = 0;
294static inline void pbuf_enqueue(uint8_t data)
295{
296 if (!data)
297 return;
298
299 uint8_t sreg = SREG;
300 cli();
301 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
302 if (next != pbuf_tail) {
303 pbuf[pbuf_head] = data;
304 pbuf_head = next;
305 } else {
306 debug("pbuf: full\n");
307 }
308 SREG = sreg;
309}
310
311static inline uint8_t pbuf_dequeue(void)
312{
313 uint8_t val = 0;
314
315 uint8_t sreg = SREG;
316 cli();
317 if (pbuf_head != pbuf_tail) {
318 val = pbuf[pbuf_tail];
319 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
320 }
321 SREG = sreg;
322
323 return val;
324}