diff options
| author | tmk <hasu@tmk-kbd.com> | 2015-04-10 01:32:04 +0900 |
|---|---|---|
| committer | tmk <hasu@tmk-kbd.com> | 2015-04-10 01:32:04 +0900 |
| commit | 1a02ebcc612e9a9c0d87e02295c7258de3a70ccc (patch) | |
| tree | e517f3c70bb2d542797e57d13e9023c84af230fb /tmk_core/protocol/ps2_interrupt.c | |
| parent | 6746e37088ce8ba03529c1226bd216705edb2b1f (diff) | |
| parent | a074364c3731d66b56d988c8a6c960a83ea0e0a1 (diff) | |
| download | qmk_firmware-1a02ebcc612e9a9c0d87e02295c7258de3a70ccc.tar.gz qmk_firmware-1a02ebcc612e9a9c0d87e02295c7258de3a70ccc.zip | |
Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core'
Diffstat (limited to 'tmk_core/protocol/ps2_interrupt.c')
| -rw-r--r-- | tmk_core/protocol/ps2_interrupt.c | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/tmk_core/protocol/ps2_interrupt.c b/tmk_core/protocol/ps2_interrupt.c new file mode 100644 index 000000000..8114442ba --- /dev/null +++ b/tmk_core/protocol/ps2_interrupt.c | |||
| @@ -0,0 +1,279 @@ | |||
| 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 Pin interrupt 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 | |||
| 50 | #define WAIT(stat, us, err) do { \ | ||
| 51 | if (!wait_##stat(us)) { \ | ||
| 52 | ps2_error = err; \ | ||
| 53 | goto ERROR; \ | ||
| 54 | } \ | ||
| 55 | } while (0) | ||
| 56 | |||
| 57 | |||
| 58 | uint8_t ps2_error = PS2_ERR_NONE; | ||
| 59 | |||
| 60 | |||
| 61 | static inline uint8_t pbuf_dequeue(void); | ||
| 62 | static inline void pbuf_enqueue(uint8_t data); | ||
| 63 | static inline bool pbuf_has_data(void); | ||
| 64 | static inline void pbuf_clear(void); | ||
| 65 | |||
| 66 | |||
| 67 | void ps2_host_init(void) | ||
| 68 | { | ||
| 69 | idle(); | ||
| 70 | PS2_INT_INIT(); | ||
| 71 | PS2_INT_ON(); | ||
| 72 | // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) | ||
| 73 | //_delay_ms(2500); | ||
| 74 | } | ||
| 75 | |||
| 76 | uint8_t ps2_host_send(uint8_t data) | ||
| 77 | { | ||
| 78 | bool parity = true; | ||
| 79 | ps2_error = PS2_ERR_NONE; | ||
| 80 | |||
| 81 | PS2_INT_OFF(); | ||
| 82 | |||
| 83 | /* terminate a transmission if we have */ | ||
| 84 | inhibit(); | ||
| 85 | _delay_us(100); // 100us [4]p.13, [5]p.50 | ||
| 86 | |||
| 87 | /* 'Request to Send' and Start bit */ | ||
| 88 | data_lo(); | ||
| 89 | clock_hi(); | ||
| 90 | WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 | ||
| 91 | |||
| 92 | /* Data bit[2-9] */ | ||
| 93 | for (uint8_t i = 0; i < 8; i++) { | ||
| 94 | _delay_us(15); | ||
| 95 | if (data&(1<<i)) { | ||
| 96 | parity = !parity; | ||
| 97 | data_hi(); | ||
| 98 | } else { | ||
| 99 | data_lo(); | ||
| 100 | } | ||
| 101 | WAIT(clock_hi, 50, 2); | ||
| 102 | WAIT(clock_lo, 50, 3); | ||
| 103 | } | ||
| 104 | |||
| 105 | /* Parity bit */ | ||
| 106 | _delay_us(15); | ||
| 107 | if (parity) { data_hi(); } else { data_lo(); } | ||
| 108 | WAIT(clock_hi, 50, 4); | ||
| 109 | WAIT(clock_lo, 50, 5); | ||
| 110 | |||
| 111 | /* Stop bit */ | ||
| 112 | _delay_us(15); | ||
| 113 | data_hi(); | ||
| 114 | |||
| 115 | /* Ack */ | ||
| 116 | WAIT(data_lo, 50, 6); | ||
| 117 | WAIT(clock_lo, 50, 7); | ||
| 118 | |||
| 119 | /* wait for idle state */ | ||
| 120 | WAIT(clock_hi, 50, 8); | ||
| 121 | WAIT(data_hi, 50, 9); | ||
| 122 | |||
| 123 | idle(); | ||
| 124 | PS2_INT_ON(); | ||
| 125 | return ps2_host_recv_response(); | ||
| 126 | ERROR: | ||
| 127 | idle(); | ||
| 128 | PS2_INT_ON(); | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | |||
| 132 | uint8_t ps2_host_recv_response(void) | ||
| 133 | { | ||
| 134 | // Command may take 25ms/20ms at most([5]p.46, [3]p.21) | ||
| 135 | uint8_t retry = 25; | ||
| 136 | while (retry-- && !pbuf_has_data()) { | ||
| 137 | _delay_ms(1); | ||
| 138 | } | ||
| 139 | return pbuf_dequeue(); | ||
| 140 | } | ||
| 141 | |||
| 142 | /* get data received by interrupt */ | ||
| 143 | uint8_t ps2_host_recv(void) | ||
| 144 | { | ||
| 145 | if (pbuf_has_data()) { | ||
| 146 | ps2_error = PS2_ERR_NONE; | ||
| 147 | return pbuf_dequeue(); | ||
| 148 | } else { | ||
| 149 | ps2_error = PS2_ERR_NODATA; | ||
| 150 | return 0; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | ISR(PS2_INT_VECT) | ||
| 155 | { | ||
| 156 | static enum { | ||
| 157 | INIT, | ||
| 158 | START, | ||
| 159 | BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, | ||
| 160 | PARITY, | ||
| 161 | STOP, | ||
| 162 | } state = INIT; | ||
| 163 | static uint8_t data = 0; | ||
| 164 | static uint8_t parity = 1; | ||
| 165 | |||
| 166 | // TODO: abort if elapse 100us from previous interrupt | ||
| 167 | |||
| 168 | // return unless falling edge | ||
| 169 | if (clock_in()) { | ||
| 170 | goto RETURN; | ||
| 171 | } | ||
| 172 | |||
| 173 | state++; | ||
| 174 | switch (state) { | ||
| 175 | case START: | ||
| 176 | if (data_in()) | ||
| 177 | goto ERROR; | ||
| 178 | break; | ||
| 179 | case BIT0: | ||
| 180 | case BIT1: | ||
| 181 | case BIT2: | ||
| 182 | case BIT3: | ||
| 183 | case BIT4: | ||
| 184 | case BIT5: | ||
| 185 | case BIT6: | ||
| 186 | case BIT7: | ||
| 187 | data >>= 1; | ||
| 188 | if (data_in()) { | ||
| 189 | data |= 0x80; | ||
| 190 | parity++; | ||
| 191 | } | ||
| 192 | break; | ||
| 193 | case PARITY: | ||
| 194 | if (data_in()) { | ||
| 195 | if (!(parity & 0x01)) | ||
| 196 | goto ERROR; | ||
| 197 | } else { | ||
| 198 | if (parity & 0x01) | ||
| 199 | goto ERROR; | ||
| 200 | } | ||
| 201 | break; | ||
| 202 | case STOP: | ||
| 203 | if (!data_in()) | ||
| 204 | goto ERROR; | ||
| 205 | pbuf_enqueue(data); | ||
| 206 | goto DONE; | ||
| 207 | break; | ||
| 208 | default: | ||
| 209 | goto ERROR; | ||
| 210 | } | ||
| 211 | goto RETURN; | ||
| 212 | ERROR: | ||
| 213 | ps2_error = state; | ||
| 214 | DONE: | ||
| 215 | state = INIT; | ||
| 216 | data = 0; | ||
| 217 | parity = 1; | ||
| 218 | RETURN: | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | |||
| 222 | /* send LED state to keyboard */ | ||
| 223 | void ps2_host_set_led(uint8_t led) | ||
| 224 | { | ||
| 225 | ps2_host_send(0xED); | ||
| 226 | ps2_host_send(led); | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | /*-------------------------------------------------------------------- | ||
| 231 | * Ring buffer to store scan codes from keyboard | ||
| 232 | *------------------------------------------------------------------*/ | ||
| 233 | #define PBUF_SIZE 32 | ||
| 234 | static uint8_t pbuf[PBUF_SIZE]; | ||
| 235 | static uint8_t pbuf_head = 0; | ||
| 236 | static uint8_t pbuf_tail = 0; | ||
| 237 | static inline void pbuf_enqueue(uint8_t data) | ||
| 238 | { | ||
| 239 | uint8_t sreg = SREG; | ||
| 240 | cli(); | ||
| 241 | uint8_t next = (pbuf_head + 1) % PBUF_SIZE; | ||
| 242 | if (next != pbuf_tail) { | ||
| 243 | pbuf[pbuf_head] = data; | ||
| 244 | pbuf_head = next; | ||
| 245 | } else { | ||
| 246 | print("pbuf: full\n"); | ||
| 247 | } | ||
| 248 | SREG = sreg; | ||
| 249 | } | ||
| 250 | static inline uint8_t pbuf_dequeue(void) | ||
| 251 | { | ||
| 252 | uint8_t val = 0; | ||
| 253 | |||
| 254 | uint8_t sreg = SREG; | ||
| 255 | cli(); | ||
| 256 | if (pbuf_head != pbuf_tail) { | ||
| 257 | val = pbuf[pbuf_tail]; | ||
| 258 | pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; | ||
| 259 | } | ||
| 260 | SREG = sreg; | ||
| 261 | |||
| 262 | return val; | ||
| 263 | } | ||
| 264 | static inline bool pbuf_has_data(void) | ||
| 265 | { | ||
| 266 | uint8_t sreg = SREG; | ||
| 267 | cli(); | ||
| 268 | bool has_data = (pbuf_head != pbuf_tail); | ||
| 269 | SREG = sreg; | ||
| 270 | return has_data; | ||
| 271 | } | ||
| 272 | static inline void pbuf_clear(void) | ||
| 273 | { | ||
| 274 | uint8_t sreg = SREG; | ||
| 275 | cli(); | ||
| 276 | pbuf_head = pbuf_tail = 0; | ||
| 277 | SREG = sreg; | ||
| 278 | } | ||
| 279 | |||
