aboutsummaryrefslogtreecommitdiff
path: root/protocol/ps2_usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocol/ps2_usart.c')
-rw-r--r--protocol/ps2_usart.c146
1 files changed, 23 insertions, 123 deletions
diff --git a/protocol/ps2_usart.c b/protocol/ps2_usart.c
index 40c46c497..d49114286 100644
--- a/protocol/ps2_usart.c
+++ b/protocol/ps2_usart.c
@@ -36,26 +36,9 @@ POSSIBILITY OF SUCH DAMAGE.
36*/ 36*/
37 37
38/* 38/*
39Primitive PS/2 Library for AVR 39 * PS/2 protocol USART version
40============================== 40 */
41Host side is only supported now.
42Synchronous USART is used to receive data by hardware process
43rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
44cannot interpose. In the result it is prone to lost CLOCK edge.
45 41
46
47I/O control
48-----------
49High state is asserted by internal pull-up.
50If you have a signaling problem, you may need to have
51external pull-up resisters on CLOCK and DATA line.
52
53
54PS/2 References
55---------------
56http://www.computer-engineering.org/ps2protocol/
57http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
58*/
59#include <stdbool.h> 42#include <stdbool.h>
60#include <avr/io.h> 43#include <avr/io.h>
61#include <avr/interrupt.h> 44#include <avr/interrupt.h>
@@ -75,18 +58,6 @@ http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
75uint8_t ps2_error = PS2_ERR_NONE; 58uint8_t ps2_error = PS2_ERR_NONE;
76 59
77 60
78static inline void clock_lo(void);
79static inline void clock_hi(void);
80static inline bool clock_in(void);
81static inline void data_lo(void);
82static inline void data_hi(void);
83static inline bool data_in(void);
84static inline uint16_t wait_clock_lo(uint16_t us);
85static inline uint16_t wait_clock_hi(uint16_t us);
86static inline uint16_t wait_data_lo(uint16_t us);
87static inline uint16_t wait_data_hi(uint16_t us);
88static inline void idle(void);
89static inline void inhibit(void);
90static inline uint8_t pbuf_dequeue(void); 61static inline uint8_t pbuf_dequeue(void);
91static inline void pbuf_enqueue(uint8_t data); 62static inline void pbuf_enqueue(uint8_t data);
92static inline bool pbuf_has_data(void); 63static inline bool pbuf_has_data(void);
@@ -95,14 +66,15 @@ static inline void pbuf_clear(void);
95 66
96void ps2_host_init(void) 67void ps2_host_init(void)
97{ 68{
98 idle(); 69 idle(); // without this many USART errors occur when cable is disconnected
99 PS2_USART_INIT(); 70 PS2_USART_INIT();
100 PS2_USART_RX_INT_ON(); 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);
101} 74}
102 75
103uint8_t ps2_host_send(uint8_t data) 76uint8_t ps2_host_send(uint8_t data)
104{ 77{
105 uint8_t res = 0;
106 bool parity = true; 78 bool parity = true;
107 ps2_error = PS2_ERR_NONE; 79 ps2_error = PS2_ERR_NONE;
108 80
@@ -144,20 +116,22 @@ uint8_t ps2_host_send(uint8_t data)
144 WAIT(clock_hi, 50, 8); 116 WAIT(clock_hi, 50, 8);
145 WAIT(data_hi, 50, 9); 117 WAIT(data_hi, 50, 9);
146 118
119 idle();
147 PS2_USART_INIT(); 120 PS2_USART_INIT();
148 PS2_USART_RX_INT_ON(); 121 PS2_USART_RX_INT_ON();
149 res = ps2_host_recv_response(); 122 return ps2_host_recv_response();
150ERROR: 123ERROR:
151 idle(); 124 idle();
152 PS2_USART_INIT(); 125 PS2_USART_INIT();
153 PS2_USART_RX_INT_ON(); 126 PS2_USART_RX_INT_ON();
154 return res; 127 return 0;
155} 128}
156 129
157// Do polling data from keyboard to get response to last command. 130// Do polling data from keyboard to get response to last command.
158uint8_t ps2_host_recv_response(void) 131uint8_t ps2_host_recv_response(void)
159{ 132{
160 while (!pbuf_has_data()) { 133 uint8_t retry = 25;
134 while (retry-- && !pbuf_has_data()) {
161 _delay_ms(1); // without this delay it seems to fall into deadlock 135 _delay_ms(1); // without this delay it seems to fall into deadlock
162 } 136 }
163 return pbuf_dequeue(); 137 return pbuf_dequeue();
@@ -165,106 +139,32 @@ uint8_t ps2_host_recv_response(void)
165 139
166uint8_t ps2_host_recv(void) 140uint8_t ps2_host_recv(void)
167{ 141{
168 return pbuf_dequeue(); 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 }
169} 149}
170 150
171ISR(PS2_USART_RX_VECT) 151ISR(PS2_USART_RX_VECT)
172{ 152{
173 uint8_t error = PS2_USART_ERROR; 153 // TODO: request RESEND when error occurs?
154 uint8_t error = PS2_USART_ERROR; // USART error should be read before data
174 uint8_t data = PS2_USART_RX_DATA; 155 uint8_t data = PS2_USART_RX_DATA;
175 if (!error) { 156 if (!error) {
176 pbuf_enqueue(data); 157 pbuf_enqueue(data);
158 } else {
159 xprintf("PS2 USART error: %02X data: %02X\n", error, data);
177 } 160 }
178} 161}
179 162
180/* send LED state to keyboard */ 163/* send LED state to keyboard */
181void ps2_host_set_led(uint8_t led) 164void ps2_host_set_led(uint8_t led)
182{ 165{
183 // send 0xED then keyboard keeps waiting for next LED data 166 ps2_host_send(0xED);
184 // and keyboard does not send any scan codes during waiting. 167 ps2_host_send(led);
185 // If fail to send LED data keyboard looks like being freezed.
186 uint8_t retry = 3;
187 while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
188 ;
189 retry = 3;
190 while (retry-- && ps2_host_send(led) != PS2_ACK)
191 ;
192}
193
194
195/*--------------------------------------------------------------------
196 * static functions
197 *------------------------------------------------------------------*/
198static inline void clock_lo()
199{
200 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
201 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
202}
203static inline void clock_hi()
204{
205 /* input with pull up */
206 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
207 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
208}
209static inline bool clock_in()
210{
211 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
212 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
213 _delay_us(1);
214 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
215}
216static inline void data_lo()
217{
218 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
219 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
220}
221static inline void data_hi()
222{
223 /* input with pull up */
224 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
225 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
226}
227static inline bool data_in()
228{
229 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
230 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
231 _delay_us(1);
232 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
233}
234
235static inline uint16_t wait_clock_lo(uint16_t us)
236{
237 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
238 return us;
239}
240static inline uint16_t wait_clock_hi(uint16_t us)
241{
242 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
243 return us;
244}
245static inline uint16_t wait_data_lo(uint16_t us)
246{
247 while (data_in() && us) { asm(""); _delay_us(1); us--; }
248 return us;
249}
250static inline uint16_t wait_data_hi(uint16_t us)
251{
252 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
253 return us;
254}
255
256/* idle state that device can send */
257static inline void idle(void)
258{
259 clock_hi();
260 data_hi();
261}
262
263/* inhibit device to send */
264static inline void inhibit(void)
265{
266 clock_lo();
267 data_hi();
268} 168}
269 169
270 170