]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/ps2_usart.c
Remove MCU dependent code from common/keyboard.c
[tmk_firmware.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/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(); // without this many USART errors occur when cable is disconnected
69     PS2_USART_INIT();
70     PS2_USART_RX_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_USART_OFF();
81
82     /* terminate a transmission if we have */
83     inhibit();
84     _delay_us(100); // [4]p.13
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_USART_INIT();
124     PS2_USART_RX_INT_ON();
125     return ps2_host_recv_response();
126 ERROR:
127     idle();
128     PS2_USART_INIT();
129     PS2_USART_RX_INT_ON();
130     return 0;
131 }
132
133 uint8_t ps2_host_recv_response(void)
134 {
135     // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
136     uint8_t retry = 25;
137     while (retry-- && !pbuf_has_data()) {
138         _delay_ms(1);
139     }
140     return pbuf_dequeue();
141 }
142
143 uint8_t ps2_host_recv(void)
144 {
145     if (pbuf_has_data()) {
146         ps2_error = PS2_ERR_NONE;
147         return pbuf_dequeue();
148     } else {
149         ps2_error = PS2_ERR_NODATA;
150         return 0;
151     }
152 }
153
154 ISR(PS2_USART_RX_VECT)
155 {
156     // TODO: request RESEND when error occurs?
157     uint8_t error = PS2_USART_ERROR;    // USART error should be read before data
158     uint8_t data = PS2_USART_RX_DATA;
159     if (!error) {
160         pbuf_enqueue(data);
161     } else {
162         xprintf("PS2 USART error: %02X data: %02X\n", error, data);
163     }
164 }
165
166 /* send LED state to keyboard */
167 void ps2_host_set_led(uint8_t led)
168 {
169     ps2_host_send(0xED);
170     ps2_host_send(led);
171 }
172
173
174 /*--------------------------------------------------------------------
175  * Ring buffer to store scan codes from keyboard
176  *------------------------------------------------------------------*/
177 #define PBUF_SIZE 32
178 static uint8_t pbuf[PBUF_SIZE];
179 static uint8_t pbuf_head = 0;
180 static uint8_t pbuf_tail = 0;
181 static inline void pbuf_enqueue(uint8_t data)
182 {
183     uint8_t sreg = SREG;
184     cli();
185     uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
186     if (next != pbuf_tail) {
187         pbuf[pbuf_head] = data;
188         pbuf_head = next;
189     } else {
190         print("pbuf: full\n");
191     }
192     SREG = sreg;
193 }
194 static inline uint8_t pbuf_dequeue(void)
195 {
196     uint8_t val = 0;
197
198     uint8_t sreg = SREG;
199     cli();
200     if (pbuf_head != pbuf_tail) {
201         val = pbuf[pbuf_tail];
202         pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
203     }
204     SREG = sreg;
205
206     return val;
207 }
208 static inline bool pbuf_has_data(void)
209 {
210     uint8_t sreg = SREG;
211     cli();
212     bool has_data = (pbuf_head != pbuf_tail);
213     SREG = sreg;
214     return has_data;
215 }
216 static inline void pbuf_clear(void)
217 {
218     uint8_t sreg = SREG;
219     cli();
220     pbuf_head = pbuf_tail = 0;
221     SREG = sreg;
222 }