diff options
Diffstat (limited to 'protocol/ps2_busywait.c')
| -rw-r--r-- | protocol/ps2_busywait.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/protocol/ps2_busywait.c b/protocol/ps2_busywait.c new file mode 100644 index 000000000..1e2925889 --- /dev/null +++ b/protocol/ps2_busywait.c | |||
| @@ -0,0 +1,179 @@ | |||
| 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 | #include <stdbool.h> | ||
| 39 | #include <util/delay.h> | ||
| 40 | #include "ps2.h" | ||
| 41 | #include "debug.h" | ||
| 42 | |||
| 43 | |||
| 44 | /* | ||
| 45 | * PS/2 protocol busywait version | ||
| 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 | uint8_t ps2_error = PS2_ERR_NONE; | ||
| 56 | |||
| 57 | |||
| 58 | void ps2_host_init(void) | ||
| 59 | { | ||
| 60 | // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) | ||
| 61 | _delay_ms(2500); | ||
| 62 | |||
| 63 | inhibit(); | ||
| 64 | } | ||
| 65 | |||
| 66 | uint8_t ps2_host_send(uint8_t data) | ||
| 67 | { | ||
| 68 | uint8_t res = 0; | ||
| 69 | bool parity = true; | ||
| 70 | ps2_error = PS2_ERR_NONE; | ||
| 71 | /* terminate a transmission if we have */ | ||
| 72 | inhibit(); | ||
| 73 | _delay_us(200); // at least 100us | ||
| 74 | |||
| 75 | /* start bit [1] */ | ||
| 76 | data_lo(); | ||
| 77 | clock_hi(); | ||
| 78 | WAIT(clock_lo, 20000, 10); // may take 15ms at most until device starts clocking | ||
| 79 | /* data [2-9] */ | ||
| 80 | for (uint8_t i = 0; i < 8; i++) { | ||
| 81 | _delay_us(15); | ||
| 82 | if (data&(1<<i)) { | ||
| 83 | parity = !parity; | ||
| 84 | data_hi(); | ||
| 85 | } else { | ||
| 86 | data_lo(); | ||
| 87 | } | ||
| 88 | WAIT(clock_hi, 50, 2); | ||
| 89 | WAIT(clock_lo, 50, 3); | ||
| 90 | } | ||
| 91 | /* parity [10] */ | ||
| 92 | _delay_us(15); | ||
| 93 | if (parity) { data_hi(); } else { data_lo(); } | ||
| 94 | WAIT(clock_hi, 50, 4); | ||
| 95 | WAIT(clock_lo, 50, 5); | ||
| 96 | /* stop bit [11] */ | ||
| 97 | _delay_us(15); | ||
| 98 | data_hi(); | ||
| 99 | /* ack [12] */ | ||
| 100 | WAIT(data_lo, 50, 6); | ||
| 101 | WAIT(clock_lo, 50, 7); | ||
| 102 | |||
| 103 | /* wait for idle state */ | ||
| 104 | WAIT(clock_hi, 50, 8); | ||
| 105 | WAIT(data_hi, 50, 9); | ||
| 106 | |||
| 107 | res = ps2_host_recv_response(); | ||
| 108 | ERROR: | ||
| 109 | inhibit(); | ||
| 110 | return res; | ||
| 111 | } | ||
| 112 | |||
| 113 | /* receive data when host want else inhibit communication */ | ||
| 114 | uint8_t ps2_host_recv_response(void) | ||
| 115 | { | ||
| 116 | // TODO: | ||
| 117 | // Command might take 20ms to response([3]p.21) | ||
| 118 | // TrackPoint might take 25ms ([5]2.7) | ||
| 119 | uint8_t data = 0; | ||
| 120 | uint8_t try = 200; | ||
| 121 | while (try-- && (data = ps2_host_recv())) ; | ||
| 122 | return data; | ||
| 123 | } | ||
| 124 | |||
| 125 | /* send LED state to keyboard */ | ||
| 126 | void ps2_host_set_led(uint8_t led) | ||
| 127 | { | ||
| 128 | ps2_host_send(0xED); | ||
| 129 | ps2_host_send(led); | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | /* called after start bit comes */ | ||
| 134 | uint8_t ps2_host_recv(void) | ||
| 135 | { | ||
| 136 | uint8_t data = 0; | ||
| 137 | bool parity = true; | ||
| 138 | ps2_error = PS2_ERR_NONE; | ||
| 139 | |||
| 140 | /* release lines(idle state) */ | ||
| 141 | idle(); | ||
| 142 | |||
| 143 | /* start bit [1] */ | ||
| 144 | WAIT(clock_lo, 100, 1); // TODO: this is enough? | ||
| 145 | WAIT(data_lo, 1, 2); | ||
| 146 | WAIT(clock_hi, 50, 3); | ||
| 147 | |||
| 148 | /* data [2-9] */ | ||
| 149 | for (uint8_t i = 0; i < 8; i++) { | ||
| 150 | WAIT(clock_lo, 50, 4); | ||
| 151 | if (data_in()) { | ||
| 152 | parity = !parity; | ||
| 153 | data |= (1<<i); | ||
| 154 | } | ||
| 155 | WAIT(clock_hi, 50, 5); | ||
| 156 | } | ||
| 157 | |||
| 158 | /* parity [10] */ | ||
| 159 | WAIT(clock_lo, 50, 6); | ||
| 160 | if (data_in() != parity) { | ||
| 161 | ps2_error = PS2_ERR_PARITY; | ||
| 162 | goto ERROR; | ||
| 163 | } | ||
| 164 | WAIT(clock_hi, 50, 7); | ||
| 165 | |||
| 166 | /* stop bit [11] */ | ||
| 167 | WAIT(clock_lo, 50, 8); | ||
| 168 | WAIT(data_hi, 1, 9); | ||
| 169 | WAIT(clock_hi, 50, 10); | ||
| 170 | |||
| 171 | inhibit(); | ||
| 172 | return data; | ||
| 173 | ERROR: | ||
| 174 | if (ps2_error > PS2_ERR_STARTBIT3) { | ||
| 175 | printf("x%02X\n", ps2_error); | ||
| 176 | } | ||
| 177 | inhibit(); | ||
| 178 | return 0; | ||
| 179 | } | ||
