diff options
Diffstat (limited to 'tmk_core/protocol/ps2_usart.c')
-rw-r--r-- | tmk_core/protocol/ps2_usart.c | 226 |
1 files changed, 0 insertions, 226 deletions
diff --git a/tmk_core/protocol/ps2_usart.c b/tmk_core/protocol/ps2_usart.c deleted file mode 100644 index 5f7008369..000000000 --- a/tmk_core/protocol/ps2_usart.c +++ /dev/null | |||
@@ -1,226 +0,0 @@ | |||
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 "ps2_io.h" | ||
47 | #include "print.h" | ||
48 | |||
49 | #ifndef PS2_CLOCK_DDR | ||
50 | # define PS2_CLOCK_DDR PORTx_ADDRESS(PS2_CLOCK_PIN) | ||
51 | #endif | ||
52 | #ifndef PS2_CLOCK_BIT | ||
53 | # define PS2_CLOCK_BIT (PS2_CLOCK_PIN & 0xF) | ||
54 | #endif | ||
55 | #ifndef PS2_DATA_DDR | ||
56 | # define PS2_DATA_DDR PORTx_ADDRESS(PS2_DATA_PIN) | ||
57 | #endif | ||
58 | #ifndef PS2_DATA_BIT | ||
59 | # define PS2_DATA_BIT (PS2_DATA_PIN & 0xF) | ||
60 | #endif | ||
61 | |||
62 | #define WAIT(stat, us, err) \ | ||
63 | do { \ | ||
64 | if (!wait_##stat(us)) { \ | ||
65 | ps2_error = err; \ | ||
66 | goto ERROR; \ | ||
67 | } \ | ||
68 | } while (0) | ||
69 | |||
70 | uint8_t ps2_error = PS2_ERR_NONE; | ||
71 | |||
72 | static inline uint8_t pbuf_dequeue(void); | ||
73 | static inline void pbuf_enqueue(uint8_t data); | ||
74 | static inline bool pbuf_has_data(void); | ||
75 | static inline void pbuf_clear(void); | ||
76 | |||
77 | void ps2_host_init(void) { | ||
78 | idle(); // without this many USART errors occur when cable is disconnected | ||
79 | PS2_USART_INIT(); | ||
80 | PS2_USART_RX_INT_ON(); | ||
81 | // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) | ||
82 | //_delay_ms(2500); | ||
83 | } | ||
84 | |||
85 | uint8_t ps2_host_send(uint8_t data) { | ||
86 | bool parity = true; | ||
87 | ps2_error = PS2_ERR_NONE; | ||
88 | |||
89 | PS2_USART_OFF(); | ||
90 | |||
91 | /* terminate a transmission if we have */ | ||
92 | inhibit(); | ||
93 | _delay_us(100); // [4]p.13 | ||
94 | |||
95 | /* 'Request to Send' and Start bit */ | ||
96 | data_lo(); | ||
97 | clock_hi(); | ||
98 | WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 | ||
99 | |||
100 | /* Data bit[2-9] */ | ||
101 | for (uint8_t i = 0; i < 8; i++) { | ||
102 | _delay_us(15); | ||
103 | if (data & (1 << i)) { | ||
104 | parity = !parity; | ||
105 | data_hi(); | ||
106 | } else { | ||
107 | data_lo(); | ||
108 | } | ||
109 | WAIT(clock_hi, 50, 2); | ||
110 | WAIT(clock_lo, 50, 3); | ||
111 | } | ||
112 | |||
113 | /* Parity bit */ | ||
114 | _delay_us(15); | ||
115 | if (parity) { | ||
116 | data_hi(); | ||
117 | } else { | ||
118 | data_lo(); | ||
119 | } | ||
120 | WAIT(clock_hi, 50, 4); | ||
121 | WAIT(clock_lo, 50, 5); | ||
122 | |||
123 | /* Stop bit */ | ||
124 | _delay_us(15); | ||
125 | data_hi(); | ||
126 | |||
127 | /* Ack */ | ||
128 | WAIT(data_lo, 50, 6); | ||
129 | WAIT(clock_lo, 50, 7); | ||
130 | |||
131 | /* wait for idle state */ | ||
132 | WAIT(clock_hi, 50, 8); | ||
133 | WAIT(data_hi, 50, 9); | ||
134 | |||
135 | idle(); | ||
136 | PS2_USART_INIT(); | ||
137 | PS2_USART_RX_INT_ON(); | ||
138 | return ps2_host_recv_response(); | ||
139 | ERROR: | ||
140 | idle(); | ||
141 | PS2_USART_INIT(); | ||
142 | PS2_USART_RX_INT_ON(); | ||
143 | return 0; | ||
144 | } | ||
145 | |||
146 | uint8_t ps2_host_recv_response(void) { | ||
147 | // Command may take 25ms/20ms at most([5]p.46, [3]p.21) | ||
148 | uint8_t retry = 25; | ||
149 | while (retry-- && !pbuf_has_data()) { | ||
150 | _delay_ms(1); | ||
151 | } | ||
152 | return pbuf_dequeue(); | ||
153 | } | ||
154 | |||
155 | uint8_t ps2_host_recv(void) { | ||
156 | if (pbuf_has_data()) { | ||
157 | ps2_error = PS2_ERR_NONE; | ||
158 | return pbuf_dequeue(); | ||
159 | } else { | ||
160 | ps2_error = PS2_ERR_NODATA; | ||
161 | return 0; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | ISR(PS2_USART_RX_VECT) { | ||
166 | // TODO: request RESEND when error occurs? | ||
167 | uint8_t error = PS2_USART_ERROR; // USART error should be read before data | ||
168 | uint8_t data = PS2_USART_RX_DATA; | ||
169 | if (!error) { | ||
170 | pbuf_enqueue(data); | ||
171 | } else { | ||
172 | xprintf("PS2 USART error: %02X data: %02X\n", error, data); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | /* send LED state to keyboard */ | ||
177 | void ps2_host_set_led(uint8_t led) { | ||
178 | ps2_host_send(0xED); | ||
179 | ps2_host_send(led); | ||
180 | } | ||
181 | |||
182 | /*-------------------------------------------------------------------- | ||
183 | * Ring buffer to store scan codes from keyboard | ||
184 | *------------------------------------------------------------------*/ | ||
185 | #define PBUF_SIZE 32 | ||
186 | static uint8_t pbuf[PBUF_SIZE]; | ||
187 | static uint8_t pbuf_head = 0; | ||
188 | static uint8_t pbuf_tail = 0; | ||
189 | static inline void pbuf_enqueue(uint8_t data) { | ||
190 | uint8_t sreg = SREG; | ||
191 | cli(); | ||
192 | uint8_t next = (pbuf_head + 1) % PBUF_SIZE; | ||
193 | if (next != pbuf_tail) { | ||
194 | pbuf[pbuf_head] = data; | ||
195 | pbuf_head = next; | ||
196 | } else { | ||
197 | print("pbuf: full\n"); | ||
198 | } | ||
199 | SREG = sreg; | ||
200 | } | ||
201 | static inline uint8_t pbuf_dequeue(void) { | ||
202 | uint8_t val = 0; | ||
203 | |||
204 | uint8_t sreg = SREG; | ||
205 | cli(); | ||
206 | if (pbuf_head != pbuf_tail) { | ||
207 | val = pbuf[pbuf_tail]; | ||
208 | pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; | ||
209 | } | ||
210 | SREG = sreg; | ||
211 | |||
212 | return val; | ||
213 | } | ||
214 | static inline bool pbuf_has_data(void) { | ||
215 | uint8_t sreg = SREG; | ||
216 | cli(); | ||
217 | bool has_data = (pbuf_head != pbuf_tail); | ||
218 | SREG = sreg; | ||
219 | return has_data; | ||
220 | } | ||
221 | static inline void pbuf_clear(void) { | ||
222 | uint8_t sreg = SREG; | ||
223 | cli(); | ||
224 | pbuf_head = pbuf_tail = 0; | ||
225 | SREG = sreg; | ||
226 | } | ||