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