]> git.gir.st - tmk_keyboard.git/blob - protocol/ps2_usart.c
Add ps2_usart.c and fix set_led at USB wake
[tmk_keyboard.git] / protocol / ps2_usart.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 USART version
40 */
41
42 #include <stdbool.h>
43 #include <avr/io.h>
44 #include <avr/interrupt.h>
45 #include <util/delay.h>
46 #include "ps2.h"
47 #include "debug.h"
48
49
50 #define WAIT(stat, us, err) do { \
51 if (!wait_##stat(us)) { \
52 ps2_error = err; \
53 goto ERROR; \
54 } \
55 } while (0)
56
57
58 uint8_t ps2_error = PS2_ERR_NONE;
59
60
61 static inline uint8_t pbuf_dequeue(void);
62 static inline void pbuf_enqueue(uint8_t data);
63 static inline bool pbuf_has_data(void);
64 static inline void pbuf_clear(void);
65
66
67 void ps2_host_init(void)
68 {
69 idle(); // without this many USART errors occur when cable is disconnected
70 PS2_USART_INIT();
71 PS2_USART_RX_INT_ON();
72 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
73 //_delay_ms(2500);
74 }
75
76 uint8_t ps2_host_send(uint8_t data)
77 {
78 bool parity = true;
79 ps2_error = PS2_ERR_NONE;
80
81 PS2_USART_OFF();
82
83 /* terminate a transmission if we have */
84 inhibit();
85 _delay_us(100);
86
87 /* start bit [1] */
88 data_lo();
89 clock_hi();
90 WAIT(clock_lo, 15000, 1);
91 /* data [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 /* parity [10] */
104 _delay_us(15);
105 if (parity) { data_hi(); } else { data_lo(); }
106 WAIT(clock_hi, 50, 4);
107 WAIT(clock_lo, 50, 5);
108 /* stop bit [11] */
109 _delay_us(15);
110 data_hi();
111 /* ack [12] */
112 WAIT(data_lo, 50, 6);
113 WAIT(clock_lo, 50, 7);
114
115 /* wait for idle state */
116 WAIT(clock_hi, 50, 8);
117 WAIT(data_hi, 50, 9);
118
119 idle();
120 PS2_USART_INIT();
121 PS2_USART_RX_INT_ON();
122 return ps2_host_recv_response();
123 ERROR:
124 idle();
125 PS2_USART_INIT();
126 PS2_USART_RX_INT_ON();
127 return 0;
128 }
129
130 // Do polling data from keyboard to get response to last command.
131 uint8_t ps2_host_recv_response(void)
132 {
133 uint8_t retry = 25;
134 while (retry-- && !pbuf_has_data()) {
135 _delay_ms(1); // without this delay it seems to fall into deadlock
136 }
137 return pbuf_dequeue();
138 }
139
140 uint8_t ps2_host_recv(void)
141 {
142 if (pbuf_has_data()) {
143 ps2_error = PS2_ERR_NONE;
144 return pbuf_dequeue();
145 } else {
146 ps2_error = PS2_ERR_NODATA;
147 return 0;
148 }
149 }
150
151 ISR(PS2_USART_RX_VECT)
152 {
153 // TODO: request RESEND when error occurs?
154 uint8_t error = PS2_USART_ERROR; // USART error should be read before data
155 uint8_t data = PS2_USART_RX_DATA;
156 if (!error) {
157 pbuf_enqueue(data);
158 } else {
159 xprintf("PS2 USART error: %02X data: %02X\n", error, data);
160 }
161 }
162
163 /* send LED state to keyboard */
164 void ps2_host_set_led(uint8_t led)
165 {
166 ps2_host_send(0xED);
167 ps2_host_send(led);
168 }
169
170
171 /*--------------------------------------------------------------------
172 * Ring buffer to store scan codes from keyboard
173 *------------------------------------------------------------------*/
174 #define PBUF_SIZE 32
175 static uint8_t pbuf[PBUF_SIZE];
176 static uint8_t pbuf_head = 0;
177 static uint8_t pbuf_tail = 0;
178 static inline void pbuf_enqueue(uint8_t data)
179 {
180 uint8_t sreg = SREG;
181 cli();
182 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
183 if (next != pbuf_tail) {
184 pbuf[pbuf_head] = data;
185 pbuf_head = next;
186 } else {
187 debug("pbuf: full\n");
188 }
189 SREG = sreg;
190 }
191
192 static inline uint8_t pbuf_dequeue(void)
193 {
194 uint8_t val = 0;
195
196 uint8_t sreg = SREG;
197 cli();
198 if (pbuf_head != pbuf_tail) {
199 val = pbuf[pbuf_tail];
200 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
201 }
202 SREG = sreg;
203
204 return val;
205 }
206 static inline bool pbuf_has_data(void)
207 {
208 uint8_t sreg = SREG;
209 cli();
210 bool has_data = (pbuf_head != pbuf_tail);
211 SREG = sreg;
212 return has_data;
213 }
214 static inline void pbuf_clear(void)
215 {
216 uint8_t sreg = SREG;
217 cli();
218 pbuf_head = pbuf_tail = 0;
219 SREG = sreg;
220 }
Imprint / Impressum