diff options
| author | skullY <skullydazed@gmail.com> | 2019-08-30 11:19:03 -0700 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-08-30 15:01:52 -0700 |
| commit | b624f32f944acdc59dcb130674c09090c5c404cb (patch) | |
| tree | bc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /tmk_core/protocol/ps2_interrupt.c | |
| parent | 61af76a10d00aba185b8338604171de490a13e3b (diff) | |
| download | qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip | |
clang-format changes
Diffstat (limited to 'tmk_core/protocol/ps2_interrupt.c')
| -rw-r--r-- | tmk_core/protocol/ps2_interrupt.c | 112 |
1 files changed, 52 insertions, 60 deletions
diff --git a/tmk_core/protocol/ps2_interrupt.c b/tmk_core/protocol/ps2_interrupt.c index 8114442ba..5afc8a82e 100644 --- a/tmk_core/protocol/ps2_interrupt.c +++ b/tmk_core/protocol/ps2_interrupt.c | |||
| @@ -46,26 +46,22 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 46 | #include "ps2_io.h" | 46 | #include "ps2_io.h" |
| 47 | #include "print.h" | 47 | #include "print.h" |
| 48 | 48 | ||
| 49 | 49 | #define WAIT(stat, us, err) \ | |
| 50 | #define WAIT(stat, us, err) do { \ | 50 | do { \ |
| 51 | if (!wait_##stat(us)) { \ | 51 | if (!wait_##stat(us)) { \ |
| 52 | ps2_error = err; \ | 52 | ps2_error = err; \ |
| 53 | goto ERROR; \ | 53 | goto ERROR; \ |
| 54 | } \ | 54 | } \ |
| 55 | } while (0) | 55 | } while (0) |
| 56 | |||
| 57 | 56 | ||
| 58 | uint8_t ps2_error = PS2_ERR_NONE; | 57 | uint8_t ps2_error = PS2_ERR_NONE; |
| 59 | 58 | ||
| 60 | |||
| 61 | static inline uint8_t pbuf_dequeue(void); | 59 | static inline uint8_t pbuf_dequeue(void); |
| 62 | static inline void pbuf_enqueue(uint8_t data); | 60 | static inline void pbuf_enqueue(uint8_t data); |
| 63 | static inline bool pbuf_has_data(void); | 61 | static inline bool pbuf_has_data(void); |
| 64 | static inline void pbuf_clear(void); | 62 | static inline void pbuf_clear(void); |
| 65 | 63 | ||
| 66 | 64 | void ps2_host_init(void) { | |
| 67 | void ps2_host_init(void) | ||
| 68 | { | ||
| 69 | idle(); | 65 | idle(); |
| 70 | PS2_INT_INIT(); | 66 | PS2_INT_INIT(); |
| 71 | PS2_INT_ON(); | 67 | PS2_INT_ON(); |
| @@ -73,26 +69,25 @@ void ps2_host_init(void) | |||
| 73 | //_delay_ms(2500); | 69 | //_delay_ms(2500); |
| 74 | } | 70 | } |
| 75 | 71 | ||
| 76 | uint8_t ps2_host_send(uint8_t data) | 72 | uint8_t ps2_host_send(uint8_t data) { |
| 77 | { | ||
| 78 | bool parity = true; | 73 | bool parity = true; |
| 79 | ps2_error = PS2_ERR_NONE; | 74 | ps2_error = PS2_ERR_NONE; |
| 80 | 75 | ||
| 81 | PS2_INT_OFF(); | 76 | PS2_INT_OFF(); |
| 82 | 77 | ||
| 83 | /* terminate a transmission if we have */ | 78 | /* terminate a transmission if we have */ |
| 84 | inhibit(); | 79 | inhibit(); |
| 85 | _delay_us(100); // 100us [4]p.13, [5]p.50 | 80 | _delay_us(100); // 100us [4]p.13, [5]p.50 |
| 86 | 81 | ||
| 87 | /* 'Request to Send' and Start bit */ | 82 | /* 'Request to Send' and Start bit */ |
| 88 | data_lo(); | 83 | data_lo(); |
| 89 | clock_hi(); | 84 | clock_hi(); |
| 90 | WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 | 85 | WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 |
| 91 | 86 | ||
| 92 | /* Data bit[2-9] */ | 87 | /* Data bit[2-9] */ |
| 93 | for (uint8_t i = 0; i < 8; i++) { | 88 | for (uint8_t i = 0; i < 8; i++) { |
| 94 | _delay_us(15); | 89 | _delay_us(15); |
| 95 | if (data&(1<<i)) { | 90 | if (data & (1 << i)) { |
| 96 | parity = !parity; | 91 | parity = !parity; |
| 97 | data_hi(); | 92 | data_hi(); |
| 98 | } else { | 93 | } else { |
| @@ -104,7 +99,11 @@ uint8_t ps2_host_send(uint8_t data) | |||
| 104 | 99 | ||
| 105 | /* Parity bit */ | 100 | /* Parity bit */ |
| 106 | _delay_us(15); | 101 | _delay_us(15); |
| 107 | if (parity) { data_hi(); } else { data_lo(); } | 102 | if (parity) { |
| 103 | data_hi(); | ||
| 104 | } else { | ||
| 105 | data_lo(); | ||
| 106 | } | ||
| 108 | WAIT(clock_hi, 50, 4); | 107 | WAIT(clock_hi, 50, 4); |
| 109 | WAIT(clock_lo, 50, 5); | 108 | WAIT(clock_lo, 50, 5); |
| 110 | 109 | ||
| @@ -129,8 +128,7 @@ ERROR: | |||
| 129 | return 0; | 128 | return 0; |
| 130 | } | 129 | } |
| 131 | 130 | ||
| 132 | uint8_t ps2_host_recv_response(void) | 131 | uint8_t ps2_host_recv_response(void) { |
| 133 | { | ||
| 134 | // Command may take 25ms/20ms at most([5]p.46, [3]p.21) | 132 | // Command may take 25ms/20ms at most([5]p.46, [3]p.21) |
| 135 | uint8_t retry = 25; | 133 | uint8_t retry = 25; |
| 136 | while (retry-- && !pbuf_has_data()) { | 134 | while (retry-- && !pbuf_has_data()) { |
| @@ -140,8 +138,7 @@ uint8_t ps2_host_recv_response(void) | |||
| 140 | } | 138 | } |
| 141 | 139 | ||
| 142 | /* get data received by interrupt */ | 140 | /* get data received by interrupt */ |
| 143 | uint8_t ps2_host_recv(void) | 141 | uint8_t ps2_host_recv(void) { |
| 144 | { | ||
| 145 | if (pbuf_has_data()) { | 142 | if (pbuf_has_data()) { |
| 146 | ps2_error = PS2_ERR_NONE; | 143 | ps2_error = PS2_ERR_NONE; |
| 147 | return pbuf_dequeue(); | 144 | return pbuf_dequeue(); |
| @@ -151,16 +148,22 @@ uint8_t ps2_host_recv(void) | |||
| 151 | } | 148 | } |
| 152 | } | 149 | } |
| 153 | 150 | ||
| 154 | ISR(PS2_INT_VECT) | 151 | ISR(PS2_INT_VECT) { |
| 155 | { | ||
| 156 | static enum { | 152 | static enum { |
| 157 | INIT, | 153 | INIT, |
| 158 | START, | 154 | START, |
| 159 | BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, | 155 | BIT0, |
| 156 | BIT1, | ||
| 157 | BIT2, | ||
| 158 | BIT3, | ||
| 159 | BIT4, | ||
| 160 | BIT5, | ||
| 161 | BIT6, | ||
| 162 | BIT7, | ||
| 160 | PARITY, | 163 | PARITY, |
| 161 | STOP, | 164 | STOP, |
| 162 | } state = INIT; | 165 | } state = INIT; |
| 163 | static uint8_t data = 0; | 166 | static uint8_t data = 0; |
| 164 | static uint8_t parity = 1; | 167 | static uint8_t parity = 1; |
| 165 | 168 | ||
| 166 | // TODO: abort if elapse 100us from previous interrupt | 169 | // TODO: abort if elapse 100us from previous interrupt |
| @@ -173,8 +176,7 @@ ISR(PS2_INT_VECT) | |||
| 173 | state++; | 176 | state++; |
| 174 | switch (state) { | 177 | switch (state) { |
| 175 | case START: | 178 | case START: |
| 176 | if (data_in()) | 179 | if (data_in()) goto ERROR; |
| 177 | goto ERROR; | ||
| 178 | break; | 180 | break; |
| 179 | case BIT0: | 181 | case BIT0: |
| 180 | case BIT1: | 182 | case BIT1: |
| @@ -192,16 +194,13 @@ ISR(PS2_INT_VECT) | |||
| 192 | break; | 194 | break; |
| 193 | case PARITY: | 195 | case PARITY: |
| 194 | if (data_in()) { | 196 | if (data_in()) { |
| 195 | if (!(parity & 0x01)) | 197 | if (!(parity & 0x01)) goto ERROR; |
| 196 | goto ERROR; | ||
| 197 | } else { | 198 | } else { |
| 198 | if (parity & 0x01) | 199 | if (parity & 0x01) goto ERROR; |
| 199 | goto ERROR; | ||
| 200 | } | 200 | } |
| 201 | break; | 201 | break; |
| 202 | case STOP: | 202 | case STOP: |
| 203 | if (!data_in()) | 203 | if (!data_in()) goto ERROR; |
| 204 | goto ERROR; | ||
| 205 | pbuf_enqueue(data); | 204 | pbuf_enqueue(data); |
| 206 | goto DONE; | 205 | goto DONE; |
| 207 | break; | 206 | break; |
| @@ -212,68 +211,61 @@ ISR(PS2_INT_VECT) | |||
| 212 | ERROR: | 211 | ERROR: |
| 213 | ps2_error = state; | 212 | ps2_error = state; |
| 214 | DONE: | 213 | DONE: |
| 215 | state = INIT; | 214 | state = INIT; |
| 216 | data = 0; | 215 | data = 0; |
| 217 | parity = 1; | 216 | parity = 1; |
| 218 | RETURN: | 217 | RETURN: |
| 219 | return; | 218 | return; |
| 220 | } | 219 | } |
| 221 | 220 | ||
| 222 | /* send LED state to keyboard */ | 221 | /* send LED state to keyboard */ |
| 223 | void ps2_host_set_led(uint8_t led) | 222 | void ps2_host_set_led(uint8_t led) { |
| 224 | { | ||
| 225 | ps2_host_send(0xED); | 223 | ps2_host_send(0xED); |
| 226 | ps2_host_send(led); | 224 | ps2_host_send(led); |
| 227 | } | 225 | } |
| 228 | 226 | ||
| 229 | |||
| 230 | /*-------------------------------------------------------------------- | 227 | /*-------------------------------------------------------------------- |
| 231 | * Ring buffer to store scan codes from keyboard | 228 | * Ring buffer to store scan codes from keyboard |
| 232 | *------------------------------------------------------------------*/ | 229 | *------------------------------------------------------------------*/ |
| 233 | #define PBUF_SIZE 32 | 230 | #define PBUF_SIZE 32 |
| 234 | static uint8_t pbuf[PBUF_SIZE]; | 231 | static uint8_t pbuf[PBUF_SIZE]; |
| 235 | static uint8_t pbuf_head = 0; | 232 | static uint8_t pbuf_head = 0; |
| 236 | static uint8_t pbuf_tail = 0; | 233 | static uint8_t pbuf_tail = 0; |
| 237 | static inline void pbuf_enqueue(uint8_t data) | 234 | static inline void pbuf_enqueue(uint8_t data) { |
| 238 | { | ||
| 239 | uint8_t sreg = SREG; | 235 | uint8_t sreg = SREG; |
| 240 | cli(); | 236 | cli(); |
| 241 | uint8_t next = (pbuf_head + 1) % PBUF_SIZE; | 237 | uint8_t next = (pbuf_head + 1) % PBUF_SIZE; |
| 242 | if (next != pbuf_tail) { | 238 | if (next != pbuf_tail) { |
| 243 | pbuf[pbuf_head] = data; | 239 | pbuf[pbuf_head] = data; |
| 244 | pbuf_head = next; | 240 | pbuf_head = next; |
| 245 | } else { | 241 | } else { |
| 246 | print("pbuf: full\n"); | 242 | print("pbuf: full\n"); |
| 247 | } | 243 | } |
| 248 | SREG = sreg; | 244 | SREG = sreg; |
| 249 | } | 245 | } |
| 250 | static inline uint8_t pbuf_dequeue(void) | 246 | static inline uint8_t pbuf_dequeue(void) { |
| 251 | { | ||
| 252 | uint8_t val = 0; | 247 | uint8_t val = 0; |
| 253 | 248 | ||
| 254 | uint8_t sreg = SREG; | 249 | uint8_t sreg = SREG; |
| 255 | cli(); | 250 | cli(); |
| 256 | if (pbuf_head != pbuf_tail) { | 251 | if (pbuf_head != pbuf_tail) { |
| 257 | val = pbuf[pbuf_tail]; | 252 | val = pbuf[pbuf_tail]; |
| 258 | pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; | 253 | pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; |
| 259 | } | 254 | } |
| 260 | SREG = sreg; | 255 | SREG = sreg; |
| 261 | 256 | ||
| 262 | return val; | 257 | return val; |
| 263 | } | 258 | } |
| 264 | static inline bool pbuf_has_data(void) | 259 | static inline bool pbuf_has_data(void) { |
| 265 | { | ||
| 266 | uint8_t sreg = SREG; | 260 | uint8_t sreg = SREG; |
| 267 | cli(); | 261 | cli(); |
| 268 | bool has_data = (pbuf_head != pbuf_tail); | 262 | bool has_data = (pbuf_head != pbuf_tail); |
| 269 | SREG = sreg; | 263 | SREG = sreg; |
| 270 | return has_data; | 264 | return has_data; |
| 271 | } | 265 | } |
| 272 | static inline void pbuf_clear(void) | 266 | static inline void pbuf_clear(void) { |
| 273 | { | ||
| 274 | uint8_t sreg = SREG; | 267 | uint8_t sreg = SREG; |
| 275 | cli(); | 268 | cli(); |
| 276 | pbuf_head = pbuf_tail = 0; | 269 | pbuf_head = pbuf_tail = 0; |
| 277 | SREG = sreg; | 270 | SREG = sreg; |
| 278 | } | 271 | } |
| 279 | |||
