Merge pull request #81 from bgould/master
[tmk_keyboard.git] / protocol / ps2_interrupt.c
1 /*
2 Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3
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.
8
9
10 Redistribution and use in source and binary forms, with or without
11 modification, 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
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.
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
57 uint8_t ps2_error = PS2_ERR_NONE;
58
59
60 static inline uint8_t pbuf_dequeue(void);
61 static inline void pbuf_enqueue(uint8_t data);
62 static inline bool pbuf_has_data(void);
63 static inline void pbuf_clear(void);
64
65
66 void 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
75 uint8_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();
125 ERROR:
126 idle();
127 PS2_INT_ON();
128 return 0;
129 }
130
131 uint8_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 */
142 uint8_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
153 ISR(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;
211 ERROR:
212 ps2_error = state;
213 DONE:
214 state = INIT;
215 data = 0;
216 parity = 1;
217 RETURN:
218 return;
219 }
220
221 /* send LED state to keyboard */
222 void 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
233 static uint8_t pbuf[PBUF_SIZE];
234 static uint8_t pbuf_head = 0;
235 static uint8_t pbuf_tail = 0;
236 static 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 }
249 static 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 }
263 static 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 }
271 static inline void pbuf_clear(void)
272 {
273 uint8_t sreg = SREG;
274 cli();
275 pbuf_head = pbuf_tail = 0;
276 SREG = sreg;
277 }
278
Imprint / Impressum