diff options
| author | tmk <nobody@nowhere> | 2013-11-21 12:15:33 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-11-21 12:23:29 +0900 |
| commit | ccbc1dd8e74e89624b6ac4ce6cf474ce6e9fc7ed (patch) | |
| tree | fc3a34c188a2a0fefa6a1087d1151aa299855b18 | |
| parent | 416a5b849e9d579b702b92680a4a188962e4c130 (diff) | |
| download | qmk_firmware-ccbc1dd8e74e89624b6ac4ce6cf474ce6e9fc7ed.tar.gz qmk_firmware-ccbc1dd8e74e89624b6ac4ce6cf474ce6e9fc7ed.zip | |
Fix PS/2 USART version
| -rw-r--r-- | protocol/ps2_usart.c | 54 |
1 files changed, 25 insertions, 29 deletions
diff --git a/protocol/ps2_usart.c b/protocol/ps2_usart.c index 9ea6b7786..40c46c497 100644 --- a/protocol/ps2_usart.c +++ b/protocol/ps2_usart.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2010,2011 Jun WAKO <wakojun@gmail.com> | 2 | Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com> |
| 3 | 3 | ||
| 4 | This software is licensed with a Modified BSD License. | 4 | This software is licensed with a Modified BSD License. |
| 5 | All of this is supposed to be Free Software, Open Source, DFSG-free, | 5 | All of this is supposed to be Free Software, Open Source, DFSG-free, |
| @@ -64,14 +64,6 @@ http://www.mcamafia.de/pdf/ibm_hitrc07.pdf | |||
| 64 | #include "debug.h" | 64 | #include "debug.h" |
| 65 | 65 | ||
| 66 | 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 { \ | 67 | #define WAIT(stat, us, err) do { \ |
| 76 | if (!wait_##stat(us)) { \ | 68 | if (!wait_##stat(us)) { \ |
| 77 | ps2_error = err; \ | 69 | ps2_error = err; \ |
| @@ -97,12 +89,12 @@ static inline void idle(void); | |||
| 97 | static inline void inhibit(void); | 89 | static inline void inhibit(void); |
| 98 | static inline uint8_t pbuf_dequeue(void); | 90 | static inline uint8_t pbuf_dequeue(void); |
| 99 | static inline void pbuf_enqueue(uint8_t data); | 91 | static inline void pbuf_enqueue(uint8_t data); |
| 92 | static inline bool pbuf_has_data(void); | ||
| 93 | static inline void pbuf_clear(void); | ||
| 100 | 94 | ||
| 101 | 95 | ||
| 102 | void ps2_host_init(void) | 96 | void ps2_host_init(void) |
| 103 | { | 97 | { |
| 104 | DEBUGP_INIT(); | ||
| 105 | DEBUGP(0x1); | ||
| 106 | idle(); | 98 | idle(); |
| 107 | PS2_USART_INIT(); | 99 | PS2_USART_INIT(); |
| 108 | PS2_USART_RX_INT_ON(); | 100 | PS2_USART_RX_INT_ON(); |
| @@ -114,7 +106,6 @@ uint8_t ps2_host_send(uint8_t data) | |||
| 114 | bool parity = true; | 106 | bool parity = true; |
| 115 | ps2_error = PS2_ERR_NONE; | 107 | ps2_error = PS2_ERR_NONE; |
| 116 | 108 | ||
| 117 | DEBUGP(0x6); | ||
| 118 | PS2_USART_OFF(); | 109 | PS2_USART_OFF(); |
| 119 | 110 | ||
| 120 | /* terminate a transmission if we have */ | 111 | /* terminate a transmission if we have */ |
| @@ -153,6 +144,8 @@ uint8_t ps2_host_send(uint8_t data) | |||
| 153 | WAIT(clock_hi, 50, 8); | 144 | WAIT(clock_hi, 50, 8); |
| 154 | WAIT(data_hi, 50, 9); | 145 | WAIT(data_hi, 50, 9); |
| 155 | 146 | ||
| 147 | PS2_USART_INIT(); | ||
| 148 | PS2_USART_RX_INT_ON(); | ||
| 156 | res = ps2_host_recv_response(); | 149 | res = ps2_host_recv_response(); |
| 157 | ERROR: | 150 | ERROR: |
| 158 | idle(); | 151 | idle(); |
| @@ -164,15 +157,10 @@ ERROR: | |||
| 164 | // Do polling data from keyboard to get response to last command. | 157 | // Do polling data from keyboard to get response to last command. |
| 165 | uint8_t ps2_host_recv_response(void) | 158 | uint8_t ps2_host_recv_response(void) |
| 166 | { | 159 | { |
| 167 | uint8_t data = 0; | 160 | while (!pbuf_has_data()) { |
| 168 | PS2_USART_INIT(); | 161 | _delay_ms(1); // without this delay it seems to fall into deadlock |
| 169 | PS2_USART_RX_POLL_ON(); | 162 | } |
| 170 | while (!PS2_USART_RX_READY) | 163 | return pbuf_dequeue(); |
| 171 | ; | ||
| 172 | data = PS2_USART_RX_DATA; | ||
| 173 | PS2_USART_OFF(); | ||
| 174 | DEBUGP(0x9); | ||
| 175 | return data; | ||
| 176 | } | 164 | } |
| 177 | 165 | ||
| 178 | uint8_t ps2_host_recv(void) | 166 | uint8_t ps2_host_recv(void) |
| @@ -182,15 +170,11 @@ uint8_t ps2_host_recv(void) | |||
| 182 | 170 | ||
| 183 | ISR(PS2_USART_RX_VECT) | 171 | ISR(PS2_USART_RX_VECT) |
| 184 | { | 172 | { |
| 185 | DEBUGP(0x7); | ||
| 186 | uint8_t error = PS2_USART_ERROR; | 173 | uint8_t error = PS2_USART_ERROR; |
| 187 | uint8_t data = PS2_USART_RX_DATA; | 174 | uint8_t data = PS2_USART_RX_DATA; |
| 188 | if (error) { | 175 | if (!error) { |
| 189 | DEBUGP(error>>2); | ||
| 190 | } else { | ||
| 191 | pbuf_enqueue(data); | 176 | pbuf_enqueue(data); |
| 192 | } | 177 | } |
| 193 | DEBUGP(0x8); | ||
| 194 | } | 178 | } |
| 195 | 179 | ||
| 196 | /* send LED state to keyboard */ | 180 | /* send LED state to keyboard */ |
| @@ -293,9 +277,6 @@ static uint8_t pbuf_head = 0; | |||
| 293 | static uint8_t pbuf_tail = 0; | 277 | static uint8_t pbuf_tail = 0; |
| 294 | static inline void pbuf_enqueue(uint8_t data) | 278 | static inline void pbuf_enqueue(uint8_t data) |
| 295 | { | 279 | { |
| 296 | if (!data) | ||
| 297 | return; | ||
| 298 | |||
| 299 | uint8_t sreg = SREG; | 280 | uint8_t sreg = SREG; |
| 300 | cli(); | 281 | cli(); |
| 301 | uint8_t next = (pbuf_head + 1) % PBUF_SIZE; | 282 | uint8_t next = (pbuf_head + 1) % PBUF_SIZE; |
| @@ -322,3 +303,18 @@ static inline uint8_t pbuf_dequeue(void) | |||
| 322 | 303 | ||
| 323 | return val; | 304 | return val; |
| 324 | } | 305 | } |
| 306 | static inline bool pbuf_has_data(void) | ||
| 307 | { | ||
| 308 | uint8_t sreg = SREG; | ||
| 309 | cli(); | ||
| 310 | bool has_data = (pbuf_head != pbuf_tail); | ||
| 311 | SREG = sreg; | ||
| 312 | return has_data; | ||
| 313 | } | ||
| 314 | static inline void pbuf_clear(void) | ||
| 315 | { | ||
| 316 | uint8_t sreg = SREG; | ||
| 317 | cli(); | ||
| 318 | pbuf_head = pbuf_tail = 0; | ||
| 319 | SREG = sreg; | ||
| 320 | } | ||
