aboutsummaryrefslogtreecommitdiff
path: root/protocol/ps2_interrupt.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocol/ps2_interrupt.c')
-rw-r--r--protocol/ps2_interrupt.c278
1 files changed, 278 insertions, 0 deletions
diff --git a/protocol/ps2_interrupt.c b/protocol/ps2_interrupt.c
new file mode 100644
index 000000000..259d25400
--- /dev/null
+++ b/protocol/ps2_interrupt.c
@@ -0,0 +1,278 @@
1/*
2Copyright 2010,2011,2012,2013 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/*
39 * PS/2 protocol Pin interrupt version
40 */
41
42#include <stdbool.h>
43#include <avr/interrupt.h>
44#include <util/delay.h>
45#include "ps2.h"
46#include "print.h"
47
48
49#define WAIT(stat, us, err) do { \
50 if (!wait_##stat(us)) { \
51 ps2_error = err; \
52 goto ERROR; \
53 } \
54} while (0)
55
56
57uint8_t ps2_error = PS2_ERR_NONE;
58
59
60static inline uint8_t pbuf_dequeue(void);
61static inline void pbuf_enqueue(uint8_t data);
62static inline bool pbuf_has_data(void);
63static inline void pbuf_clear(void);
64
65
66void ps2_host_init(void)
67{
68 idle();
69 PS2_INT_INIT();
70 PS2_INT_ON();
71 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
72 //_delay_ms(2500);
73}
74
75uint8_t ps2_host_send(uint8_t data)
76{
77 bool parity = true;
78 ps2_error = PS2_ERR_NONE;
79
80 PS2_INT_OFF();
81
82 /* terminate a transmission if we have */
83 inhibit();
84 _delay_us(100); // 100us [4]p.13, [5]p.50
85
86 /* 'Request to Send' and Start bit */
87 data_lo();
88 clock_hi();
89 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
90
91 /* Data bit[2-9] */
92 for (uint8_t i = 0; i < 8; i++) {
93 _delay_us(15);
94 if (data&(1<<i)) {
95 parity = !parity;
96 data_hi();
97 } else {
98 data_lo();
99 }
100 WAIT(clock_hi, 50, 2);
101 WAIT(clock_lo, 50, 3);
102 }
103
104 /* Parity bit */
105 _delay_us(15);
106 if (parity) { data_hi(); } else { data_lo(); }
107 WAIT(clock_hi, 50, 4);
108 WAIT(clock_lo, 50, 5);
109
110 /* Stop bit */
111 _delay_us(15);
112 data_hi();
113
114 /* Ack */
115 WAIT(data_lo, 50, 6);
116 WAIT(clock_lo, 50, 7);
117
118 /* wait for idle state */
119 WAIT(clock_hi, 50, 8);
120 WAIT(data_hi, 50, 9);
121
122 idle();
123 PS2_INT_ON();
124 return ps2_host_recv_response();
125ERROR:
126 idle();
127 PS2_INT_ON();
128 return 0;
129}
130
131uint8_t ps2_host_recv_response(void)
132{
133 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
134 uint8_t retry = 25;
135 while (retry-- && !pbuf_has_data()) {
136 _delay_ms(1);
137 }
138 return pbuf_dequeue();
139}
140
141/* get data received by interrupt */
142uint8_t ps2_host_recv(void)
143{
144 if (pbuf_has_data()) {
145 ps2_error = PS2_ERR_NONE;
146 return pbuf_dequeue();
147 } else {
148 ps2_error = PS2_ERR_NODATA;
149 return 0;
150 }
151}
152
153ISR(PS2_INT_VECT)
154{
155 static enum {
156 INIT,
157 START,
158 BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
159 PARITY,
160 STOP,
161 } state = INIT;
162 static uint8_t data = 0;
163 static uint8_t parity = 1;
164
165 // TODO: abort if elapse 100us from previous interrupt
166
167 // return unless falling edge
168 if (clock_in()) {
169 goto RETURN;
170 }
171
172 state++;
173 switch (state) {
174 case START:
175 if (data_in())
176 goto ERROR;
177 break;
178 case BIT0:
179 case BIT1:
180 case BIT2:
181 case BIT3:
182 case BIT4:
183 case BIT5:
184 case BIT6:
185 case BIT7:
186 data >>= 1;
187 if (data_in()) {
188 data |= 0x80;
189 parity++;
190 }
191 break;
192 case PARITY:
193 if (data_in()) {
194 if (!(parity & 0x01))
195 goto ERROR;
196 } else {
197 if (parity & 0x01)
198 goto ERROR;
199 }
200 break;
201 case STOP:
202 if (!data_in())
203 goto ERROR;
204 pbuf_enqueue(data);
205 goto DONE;
206 break;
207 default:
208 goto ERROR;
209 }
210 goto RETURN;
211ERROR:
212 ps2_error = state;
213DONE:
214 state = INIT;
215 data = 0;
216 parity = 1;
217RETURN:
218 return;
219}
220
221/* send LED state to keyboard */
222void ps2_host_set_led(uint8_t led)
223{
224 ps2_host_send(0xED);
225 ps2_host_send(led);
226}
227
228
229/*--------------------------------------------------------------------
230 * Ring buffer to store scan codes from keyboard
231 *------------------------------------------------------------------*/
232#define PBUF_SIZE 32
233static uint8_t pbuf[PBUF_SIZE];
234static uint8_t pbuf_head = 0;
235static uint8_t pbuf_tail = 0;
236static inline void pbuf_enqueue(uint8_t data)
237{
238 uint8_t sreg = SREG;
239 cli();
240 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
241 if (next != pbuf_tail) {
242 pbuf[pbuf_head] = data;
243 pbuf_head = next;
244 } else {
245 print("pbuf: full\n");
246 }
247 SREG = sreg;
248}
249static inline uint8_t pbuf_dequeue(void)
250{
251 uint8_t val = 0;
252
253 uint8_t sreg = SREG;
254 cli();
255 if (pbuf_head != pbuf_tail) {
256 val = pbuf[pbuf_tail];
257 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
258 }
259 SREG = sreg;
260
261 return val;
262}
263static inline bool pbuf_has_data(void)
264{
265 uint8_t sreg = SREG;
266 cli();
267 bool has_data = (pbuf_head != pbuf_tail);
268 SREG = sreg;
269 return has_data;
270}
271static inline void pbuf_clear(void)
272{
273 uint8_t sreg = SREG;
274 cli();
275 pbuf_head = pbuf_tail = 0;
276 SREG = sreg;
277}
278