aboutsummaryrefslogtreecommitdiff
path: root/ps2_usb/ps2_usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'ps2_usb/ps2_usart.c')
-rw-r--r--ps2_usb/ps2_usart.c325
1 files changed, 325 insertions, 0 deletions
diff --git a/ps2_usb/ps2_usart.c b/ps2_usb/ps2_usart.c
new file mode 100644
index 000000000..e45b94c93
--- /dev/null
+++ b/ps2_usb/ps2_usart.c
@@ -0,0 +1,325 @@
1/*
2Copyright (c) 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/*
38Primitive PS/2 Library for AVR
39==============================
40Host side is only supported now.
41Synchronous USART is used to receive data by hardware process
42rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
43cannot interpose. In the result it is prone to lost CLOCK edge.
44
45
46I/O control
47-----------
48High state is asserted by internal pull-up.
49If you have a signaling problem, you may need to have
50external pull-up resisters on CLOCK and DATA line.
51
52
53PS/2 References
54---------------
55http://www.computer-engineering.org/ps2protocol/
56http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
57*/
58#include <stdbool.h>
59#include <avr/io.h>
60#include <avr/interrupt.h>
61#include <util/delay.h>
62#include "ps2.h"
63#include "debug.h"
64
65
66#if 0
67#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
68#define DEBUGP(x) do { PORTC = x; } while (0)
69#else
70#define DEBUGP_INIT()
71#define DEBUGP(x)
72#endif
73
74#define WAIT(stat, us, err) do { \
75 if (!wait_##stat(us)) { \
76 ps2_error = err; \
77 goto ERROR; \
78 } \
79} while (0)
80
81
82uint8_t ps2_error = PS2_ERR_NONE;
83
84
85static inline void clock_lo(void);
86static inline void clock_hi(void);
87static inline bool clock_in(void);
88static inline void data_lo(void);
89static inline void data_hi(void);
90static inline bool data_in(void);
91static inline uint16_t wait_clock_lo(uint16_t us);
92static inline uint16_t wait_clock_hi(uint16_t us);
93static inline uint16_t wait_data_lo(uint16_t us);
94static inline uint16_t wait_data_hi(uint16_t us);
95static inline void idle(void);
96static inline void inhibit(void);
97#if defined PS2_USE_INT || defined PS2_USE_USART
98static inline uint8_t pbuf_dequeue(void);
99static inline void pbuf_enqueue(uint8_t data);
100#endif
101
102
103void ps2_host_init(void)
104{
105 DEBUGP_INIT();
106 DEBUGP(0x1);
107 idle();
108 PS2_USART_INIT();
109 PS2_USART_RX_INT_ON();
110}
111
112uint8_t ps2_host_send(uint8_t data)
113{
114 uint8_t res = 0;
115 bool parity = true;
116 ps2_error = PS2_ERR_NONE;
117
118 DEBUGP(0x6);
119 PS2_USART_OFF();
120
121 /* terminate a transmission if we have */
122 inhibit();
123 _delay_us(100);
124
125 /* start bit [1] */
126 data_lo();
127 clock_hi();
128 WAIT(clock_lo, 15000, 1);
129 /* data [2-9] */
130 for (uint8_t i = 0; i < 8; i++) {
131 _delay_us(15);
132 if (data&(1<<i)) {
133 parity = !parity;
134 data_hi();
135 } else {
136 data_lo();
137 }
138 WAIT(clock_hi, 50, 2);
139 WAIT(clock_lo, 50, 3);
140 }
141 /* parity [10] */
142 _delay_us(15);
143 if (parity) { data_hi(); } else { data_lo(); }
144 WAIT(clock_hi, 50, 4);
145 WAIT(clock_lo, 50, 5);
146 /* stop bit [11] */
147 _delay_us(15);
148 data_hi();
149 /* ack [12] */
150 WAIT(data_lo, 50, 6);
151 WAIT(clock_lo, 50, 7);
152
153 /* wait for idle state */
154 WAIT(clock_hi, 50, 8);
155 WAIT(data_hi, 50, 9);
156
157 res = ps2_host_recv_response();
158ERROR:
159 idle();
160 PS2_USART_INIT();
161 PS2_USART_RX_INT_ON();
162 return res;
163}
164
165// Do polling data from keyboard to get response to last command.
166uint8_t ps2_host_recv_response(void)
167{
168 uint8_t data = 0;
169 PS2_USART_INIT();
170 PS2_USART_RX_POLL_ON();
171 while (!PS2_USART_RX_READY)
172 ;
173 data = PS2_USART_RX_DATA;
174 PS2_USART_OFF();
175 DEBUGP(0x9);
176 return data;
177}
178
179uint8_t ps2_host_recv(void)
180{
181 return pbuf_dequeue();
182}
183
184ISR(PS2_USART_RX_VECT)
185{
186 DEBUGP(0x7);
187 uint8_t error = PS2_USART_ERROR;
188 uint8_t data = PS2_USART_RX_DATA;
189 if (error) {
190 DEBUGP(error>>2);
191 } else {
192 pbuf_enqueue(data);
193 }
194 DEBUGP(0x8);
195}
196
197/* send LED state to keyboard */
198void ps2_host_set_led(uint8_t led)
199{
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.
203 uint8_t retry = 3;
204 while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
205 ;
206 retry = 3;
207 while (retry-- && ps2_host_send(led) != PS2_ACK)
208 ;
209}
210
211
212/*--------------------------------------------------------------------
213 * static functions
214 *------------------------------------------------------------------*/
215static inline void clock_lo()
216{
217 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
218 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
219}
220static inline void clock_hi()
221{
222 /* input with pull up */
223 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
224 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
225}
226static inline bool clock_in()
227{
228 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
229 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
230 _delay_us(1);
231 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
232}
233static inline void data_lo()
234{
235 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
236 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
237}
238static inline void data_hi()
239{
240 /* input with pull up */
241 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
242 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
243}
244static inline bool data_in()
245{
246 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
247 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
248 _delay_us(1);
249 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
250}
251
252static inline uint16_t wait_clock_lo(uint16_t us)
253{
254 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
255 return us;
256}
257static inline uint16_t wait_clock_hi(uint16_t us)
258{
259 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
260 return us;
261}
262static inline uint16_t wait_data_lo(uint16_t us)
263{
264 while (data_in() && us) { asm(""); _delay_us(1); us--; }
265 return us;
266}
267static inline uint16_t wait_data_hi(uint16_t us)
268{
269 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
270 return us;
271}
272
273/* idle state that device can send */
274static inline void idle(void)
275{
276 clock_hi();
277 data_hi();
278}
279
280/* inhibit device to send */
281static inline void inhibit(void)
282{
283 clock_lo();
284 data_hi();
285}
286
287
288/*--------------------------------------------------------------------
289 * Ring buffer to store scan codes from keyboard
290 *------------------------------------------------------------------*/
291#define PBUF_SIZE 8
292static uint8_t pbuf[PBUF_SIZE];
293static uint8_t pbuf_head = 0;
294static uint8_t pbuf_tail = 0;
295static inline void pbuf_enqueue(uint8_t data)
296{
297 if (!data)
298 return;
299
300 uint8_t sreg = SREG;
301 cli();
302 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
303 if (next != pbuf_tail) {
304 pbuf[pbuf_head] = data;
305 pbuf_head = next;
306 } else {
307 debug("pbuf: full\n");
308 }
309 SREG = sreg;
310}
311
312static inline uint8_t pbuf_dequeue(void)
313{
314 uint8_t val = 0;
315
316 uint8_t sreg = SREG;
317 cli();
318 if (pbuf_head != pbuf_tail) {
319 val = pbuf[pbuf_tail];
320 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
321 }
322 SREG = sreg;
323
324 return val;
325}