Codes appear to be detected correctly, the break codes are broken.
[tmk_keyboard.git] / tmk_core / 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 "pbuff.h"
46 #include "ps2.h"
47 #include "ps2_io.h"
48 #include "print.h"
49
50
51 #define WAIT(stat, us, err) do { \
52 if (!wait_##stat(us)) { \
53 ps2_error = err; \
54 goto ERROR; \
55 } \
56 } while (0)
57
58
59 uint8_t ps2_error = PS2_ERR_NONE;
60
61 void ps2_host_init(void)
62 {
63 idle();
64 PS2_INT_INIT();
65 PS2_INT_ON();
66 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
67 //_delay_ms(2500);
68 }
69
70 uint8_t ps2_host_send(uint8_t data)
71 {
72 bool parity = true;
73 ps2_error = PS2_ERR_NONE;
74
75 PS2_INT_OFF();
76
77 /* terminate a transmission if we have */
78 inhibit();
79 _delay_us(100); // 100us [4]p.13, [5]p.50
80
81 /* 'Request to Send' and Start bit */
82 data_lo();
83 clock_hi();
84 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
85
86 /* Data bit[2-9] */
87 for (uint8_t i = 0; i < 8; i++) {
88 _delay_us(15);
89 if (data&(1<<i)) {
90 parity = !parity;
91 data_hi();
92 } else {
93 data_lo();
94 }
95 WAIT(clock_hi, 50, 2);
96 WAIT(clock_lo, 50, 3);
97 }
98
99 /* Parity bit */
100 _delay_us(15);
101 if (parity) { data_hi(); } else { data_lo(); }
102 WAIT(clock_hi, 50, 4);
103 WAIT(clock_lo, 50, 5);
104
105 /* Stop bit */
106 _delay_us(15);
107 data_hi();
108
109 /* Ack */
110 WAIT(data_lo, 50, 6);
111 WAIT(clock_lo, 50, 7);
112
113 /* wait for idle state */
114 WAIT(clock_hi, 50, 8);
115 WAIT(data_hi, 50, 9);
116
117 idle();
118 PS2_INT_ON();
119 return ps2_host_recv_response();
120 ERROR:
121 idle();
122 PS2_INT_ON();
123 return 0;
124 }
125
126 uint8_t ps2_host_recv_response(void)
127 {
128 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
129 uint8_t retry = 25;
130 while (retry-- && !pbuf_has_data()) {
131 _delay_ms(1);
132 }
133 return pbuf_dequeue();
134 }
135
136 /* get data received by interrupt */
137 uint8_t ps2_host_recv(void)
138 {
139 if (pbuf_has_data()) {
140 ps2_error = PS2_ERR_NONE;
141 return pbuf_dequeue();
142 } else {
143 ps2_error = PS2_ERR_NODATA;
144 return 0;
145 }
146 }
147
148 ISR(PS2_INT_VECT)
149 {
150 static enum {
151 INIT,
152 START,
153 BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
154 PARITY,
155 STOP,
156 } state = INIT;
157 static uint8_t data = 0;
158 static uint8_t parity = 1;
159
160 // TODO: abort if elapse 100us from previous interrupt
161
162 // return unless falling edge
163 if (clock_in()) {
164 goto RETURN;
165 }
166
167 state++;
168 switch (state) {
169 case START:
170 if (data_in())
171 goto ERROR;
172 break;
173 case BIT0:
174 case BIT1:
175 case BIT2:
176 case BIT3:
177 case BIT4:
178 case BIT5:
179 case BIT6:
180 case BIT7:
181 data >>= 1;
182 if (data_in()) {
183 data |= 0x80;
184 parity++;
185 }
186 break;
187 case PARITY:
188 if (data_in()) {
189 if (!(parity & 0x01))
190 goto ERROR;
191 } else {
192 if (parity & 0x01)
193 goto ERROR;
194 }
195 break;
196 case STOP:
197 if (!data_in())
198 goto ERROR;
199 pbuf_enqueue(data);
200 goto DONE;
201 break;
202 default:
203 goto ERROR;
204 }
205 goto RETURN;
206 ERROR:
207 ps2_error = state;
208 DONE:
209 state = INIT;
210 data = 0;
211 parity = 1;
212 RETURN:
213 return;
214 }
215
216 /* send LED state to keyboard */
217 void ps2_host_set_led(uint8_t led)
218 {
219 ps2_host_send(0xED);
220 ps2_host_send(led);
221 }
Imprint / Impressum