diff options
| author | tmk <nobody@nowhere> | 2013-11-28 10:42:41 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-11-28 11:31:48 +0900 |
| commit | 532e100450615ef1e63e2eed548c38d0c4fa688b (patch) | |
| tree | f09b8e4dc51fdca6a6ba0647b92cd672c7008ce9 /protocol | |
| parent | 9d26053f1c14da79336a64f800305660d1a71180 (diff) | |
| download | qmk_firmware-532e100450615ef1e63e2eed548c38d0c4fa688b.tar.gz qmk_firmware-532e100450615ef1e63e2eed548c38d0c4fa688b.zip | |
Add ps2_usart.c and fix set_led at USB wake
Diffstat (limited to 'protocol')
| -rw-r--r-- | protocol/lufa/lufa.c | 4 | ||||
| -rw-r--r-- | protocol/pjrc/usb.c | 3 | ||||
| -rw-r--r-- | protocol/ps2.h | 2 | ||||
| -rw-r--r-- | protocol/ps2_usart.c | 146 |
4 files changed, 29 insertions, 126 deletions
diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c index 04e8e78f3..eca51c878 100644 --- a/protocol/lufa/lufa.c +++ b/protocol/lufa/lufa.c | |||
| @@ -148,7 +148,6 @@ static void Console_Task(void) | |||
| 148 | */ | 148 | */ |
| 149 | void EVENT_USB_Device_Connect(void) | 149 | void EVENT_USB_Device_Connect(void) |
| 150 | { | 150 | { |
| 151 | led_set(0x1f); // all on | ||
| 152 | } | 151 | } |
| 153 | 152 | ||
| 154 | void EVENT_USB_Device_Disconnect(void) | 153 | void EVENT_USB_Device_Disconnect(void) |
| @@ -172,8 +171,9 @@ void EVENT_USB_Device_WakeUp() | |||
| 172 | 171 | ||
| 173 | #ifdef SLEEP_LED_ENABLE | 172 | #ifdef SLEEP_LED_ENABLE |
| 174 | sleep_led_disable(); | 173 | sleep_led_disable(); |
| 175 | #endif | 174 | // NOTE: converters may not accept this |
| 176 | led_set(host_keyboard_leds()); | 175 | led_set(host_keyboard_leds()); |
| 176 | #endif | ||
| 177 | } | 177 | } |
| 178 | 178 | ||
| 179 | void EVENT_USB_Device_StartOfFrame(void) | 179 | void EVENT_USB_Device_StartOfFrame(void) |
diff --git a/protocol/pjrc/usb.c b/protocol/pjrc/usb.c index 84c99972f..393b36f78 100644 --- a/protocol/pjrc/usb.c +++ b/protocol/pjrc/usb.c | |||
| @@ -662,8 +662,9 @@ ISR(USB_GEN_vect) | |||
| 662 | suspend_wakeup_init(); | 662 | suspend_wakeup_init(); |
| 663 | #ifdef SLEEP_LED_ENABLE | 663 | #ifdef SLEEP_LED_ENABLE |
| 664 | sleep_led_disable(); | 664 | sleep_led_disable(); |
| 665 | #endif | 665 | // NOTE: converters may not accept this |
| 666 | led_set(host_keyboard_leds()); | 666 | led_set(host_keyboard_leds()); |
| 667 | #endif | ||
| 667 | 668 | ||
| 668 | UDIEN |= (1<<SUSPE); | 669 | UDIEN |= (1<<SUSPE); |
| 669 | UDIEN &= ~(1<<WAKEUPE); | 670 | UDIEN &= ~(1<<WAKEUPE); |
diff --git a/protocol/ps2.h b/protocol/ps2.h index b780d01a2..483eea720 100644 --- a/protocol/ps2.h +++ b/protocol/ps2.h | |||
| @@ -70,11 +70,13 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 70 | #define PS2_RESEND 0xFE | 70 | #define PS2_RESEND 0xFE |
| 71 | #define PS2_SET_LED 0xED | 71 | #define PS2_SET_LED 0xED |
| 72 | 72 | ||
| 73 | // TODO: error numbers | ||
| 73 | #define PS2_ERR_NONE 0 | 74 | #define PS2_ERR_NONE 0 |
| 74 | #define PS2_ERR_STARTBIT1 1 | 75 | #define PS2_ERR_STARTBIT1 1 |
| 75 | #define PS2_ERR_STARTBIT2 2 | 76 | #define PS2_ERR_STARTBIT2 2 |
| 76 | #define PS2_ERR_STARTBIT3 3 | 77 | #define PS2_ERR_STARTBIT3 3 |
| 77 | #define PS2_ERR_PARITY 0x10 | 78 | #define PS2_ERR_PARITY 0x10 |
| 79 | #define PS2_ERR_NODATA 0x20 | ||
| 78 | 80 | ||
| 79 | #define PS2_LED_SCROLL_LOCK 0 | 81 | #define PS2_LED_SCROLL_LOCK 0 |
| 80 | #define PS2_LED_NUM_LOCK 1 | 82 | #define PS2_LED_NUM_LOCK 1 |
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 | /* |
| 39 | Primitive PS/2 Library for AVR | 39 | * PS/2 protocol USART version |
| 40 | ============================== | 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 | 41 | ||
| 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> | 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 | |||
| 75 | uint8_t ps2_error = PS2_ERR_NONE; | 58 | uint8_t ps2_error = PS2_ERR_NONE; |
| 76 | 59 | ||
| 77 | 60 | ||
| 78 | static inline void clock_lo(void); | ||
| 79 | static inline void clock_hi(void); | ||
| 80 | static inline bool clock_in(void); | ||
| 81 | static inline void data_lo(void); | ||
| 82 | static inline void data_hi(void); | ||
| 83 | static inline bool data_in(void); | ||
| 84 | static inline uint16_t wait_clock_lo(uint16_t us); | ||
| 85 | static inline uint16_t wait_clock_hi(uint16_t us); | ||
| 86 | static inline uint16_t wait_data_lo(uint16_t us); | ||
| 87 | static inline uint16_t wait_data_hi(uint16_t us); | ||
| 88 | static inline void idle(void); | ||
| 89 | static inline void inhibit(void); | ||
| 90 | static inline uint8_t pbuf_dequeue(void); | 61 | static inline uint8_t pbuf_dequeue(void); |
| 91 | static inline void pbuf_enqueue(uint8_t data); | 62 | static inline void pbuf_enqueue(uint8_t data); |
| 92 | static inline bool pbuf_has_data(void); | 63 | static inline bool pbuf_has_data(void); |
| @@ -95,14 +66,15 @@ static inline void pbuf_clear(void); | |||
| 95 | 66 | ||
| 96 | void ps2_host_init(void) | 67 | void 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 | ||
| 103 | uint8_t ps2_host_send(uint8_t data) | 76 | uint8_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(); |
| 150 | ERROR: | 123 | ERROR: |
| 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. |
| 158 | uint8_t ps2_host_recv_response(void) | 131 | uint8_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 | ||
| 166 | uint8_t ps2_host_recv(void) | 140 | uint8_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 | ||
| 171 | ISR(PS2_USART_RX_VECT) | 151 | ISR(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 */ |
| 181 | void ps2_host_set_led(uint8_t led) | 164 | void 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 | *------------------------------------------------------------------*/ | ||
| 198 | static inline void clock_lo() | ||
| 199 | { | ||
| 200 | PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT); | ||
| 201 | PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT); | ||
| 202 | } | ||
| 203 | static 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 | } | ||
| 209 | static 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 | } | ||
| 216 | static inline void data_lo() | ||
| 217 | { | ||
| 218 | PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT); | ||
| 219 | PS2_DATA_DDR |= (1<<PS2_DATA_BIT); | ||
| 220 | } | ||
| 221 | static 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 | } | ||
| 227 | static 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 | |||
| 235 | static inline uint16_t wait_clock_lo(uint16_t us) | ||
| 236 | { | ||
| 237 | while (clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 238 | return us; | ||
| 239 | } | ||
| 240 | static inline uint16_t wait_clock_hi(uint16_t us) | ||
| 241 | { | ||
| 242 | while (!clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 243 | return us; | ||
| 244 | } | ||
| 245 | static inline uint16_t wait_data_lo(uint16_t us) | ||
| 246 | { | ||
| 247 | while (data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 248 | return us; | ||
| 249 | } | ||
| 250 | static 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 */ | ||
| 257 | static inline void idle(void) | ||
| 258 | { | ||
| 259 | clock_hi(); | ||
| 260 | data_hi(); | ||
| 261 | } | ||
| 262 | |||
| 263 | /* inhibit device to send */ | ||
| 264 | static inline void inhibit(void) | ||
| 265 | { | ||
| 266 | clock_lo(); | ||
| 267 | data_hi(); | ||
| 268 | } | 168 | } |
| 269 | 169 | ||
| 270 | 170 | ||
