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