diff options
Diffstat (limited to 'ps2.c')
| -rw-r--r-- | ps2.c | 248 |
1 files changed, 248 insertions, 0 deletions
| @@ -0,0 +1,248 @@ | |||
| 1 | /* | ||
| 2 | Copyright (c) 2010 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 | #include <stdbool.h> | ||
| 38 | #include <avr/io.h> | ||
| 39 | #include <util/delay.h> | ||
| 40 | #include "ps2.h" | ||
| 41 | #include "print.h" | ||
| 42 | #include "debug.h" | ||
| 43 | |||
| 44 | |||
| 45 | static inline void clock_lo(void); | ||
| 46 | static inline void clock_hi(void); | ||
| 47 | static inline bool clock_in(void); | ||
| 48 | static inline void data_lo(void); | ||
| 49 | static inline void data_hi(void); | ||
| 50 | static inline bool data_in(void); | ||
| 51 | static inline uint16_t wait_clock_lo(uint16_t us); | ||
| 52 | static inline uint16_t wait_clock_hi(uint16_t us); | ||
| 53 | static inline uint16_t wait_data_lo(uint16_t us); | ||
| 54 | static inline uint16_t wait_data_hi(uint16_t us); | ||
| 55 | |||
| 56 | |||
| 57 | /* | ||
| 58 | Primitive PS/2 Library for AVR | ||
| 59 | ============================== | ||
| 60 | Host side is only supported now. | ||
| 61 | |||
| 62 | |||
| 63 | I/O control | ||
| 64 | ----------- | ||
| 65 | High state is asserted by input with pull up. | ||
| 66 | |||
| 67 | |||
| 68 | PS/2 References | ||
| 69 | --------------- | ||
| 70 | http://www.computer-engineering.org/ps2protocol/ | ||
| 71 | http://www.mcamafia.de/pdf/ibm_hitrc07.pdf | ||
| 72 | */ | ||
| 73 | |||
| 74 | |||
| 75 | #define WAIT(stat, us, err) do { \ | ||
| 76 | if (!wait_##stat(us)) { \ | ||
| 77 | ps2_error = err; \ | ||
| 78 | goto ERROR; \ | ||
| 79 | } \ | ||
| 80 | } while (0) | ||
| 81 | |||
| 82 | #define WAIT_NORETRY(stat, us, err) do { \ | ||
| 83 | if (!wait_##stat(us)) { \ | ||
| 84 | ps2_error = err; \ | ||
| 85 | return 0; \ | ||
| 86 | } \ | ||
| 87 | } while (0) | ||
| 88 | |||
| 89 | |||
| 90 | uint8_t ps2_error = PS2_ERR_NONE; | ||
| 91 | |||
| 92 | |||
| 93 | void ps2_host_init(void) | ||
| 94 | { | ||
| 95 | /* inhibit */ | ||
| 96 | clock_lo(); | ||
| 97 | data_hi(); | ||
| 98 | } | ||
| 99 | |||
| 100 | uint8_t ps2_host_send(uint8_t data) | ||
| 101 | { | ||
| 102 | bool parity = true; | ||
| 103 | ps2_error = 0; | ||
| 104 | |||
| 105 | /* request to send */ | ||
| 106 | clock_lo(); | ||
| 107 | data_lo(); | ||
| 108 | _delay_us(100); | ||
| 109 | /* start bit [1] */ | ||
| 110 | clock_hi(); | ||
| 111 | WAIT(clock_lo, 15000, 1); | ||
| 112 | /* data [2-9] */ | ||
| 113 | for (uint8_t i = 0; i < 8; i++) { | ||
| 114 | if (data&(1<<i)) { | ||
| 115 | parity = !parity; | ||
| 116 | data_hi(); | ||
| 117 | } else { | ||
| 118 | data_lo(); | ||
| 119 | } | ||
| 120 | WAIT(clock_hi, 50, 2); | ||
| 121 | WAIT(clock_lo, 50, 3); | ||
| 122 | } | ||
| 123 | /* parity [10] */ | ||
| 124 | if (parity) { data_hi(); } else { data_lo(); } | ||
| 125 | WAIT(clock_hi, 50, 4); | ||
| 126 | WAIT(clock_lo, 50, 5); | ||
| 127 | /* stop bit [11] */ | ||
| 128 | data_hi(); | ||
| 129 | /* ack [12] */ | ||
| 130 | WAIT(data_lo, 50, 6); | ||
| 131 | WAIT(clock_lo, 50, 7); | ||
| 132 | WAIT(clock_hi, 50, 8); | ||
| 133 | WAIT(data_hi, 50, 9); | ||
| 134 | |||
| 135 | /* inhibit device to send */ | ||
| 136 | clock_lo(); | ||
| 137 | |||
| 138 | return 1; | ||
| 139 | ERROR: | ||
| 140 | return 0; | ||
| 141 | } | ||
| 142 | |||
| 143 | uint8_t ps2_host_recv(void) | ||
| 144 | { | ||
| 145 | uint8_t data = 0; | ||
| 146 | bool parity = true; | ||
| 147 | ps2_error = 0; | ||
| 148 | |||
| 149 | /* cancel to sync */ | ||
| 150 | clock_lo(); | ||
| 151 | _delay_us(100); | ||
| 152 | |||
| 153 | /* release lines(idle state) */ | ||
| 154 | clock_hi(); | ||
| 155 | data_hi(); | ||
| 156 | |||
| 157 | /* start bit [1] */ | ||
| 158 | WAIT(clock_lo, 20000, 1); | ||
| 159 | WAIT(data_lo, 1, 2); | ||
| 160 | WAIT(clock_hi, 50, 3); | ||
| 161 | |||
| 162 | /* data [2-9] */ | ||
| 163 | for (uint8_t i = 0; i < 8; i++) { | ||
| 164 | WAIT(clock_lo, 50, 4); | ||
| 165 | if (data_in()) { | ||
| 166 | parity = !parity; | ||
| 167 | data |= (1<<i); | ||
| 168 | } | ||
| 169 | WAIT(clock_hi, 50, 5); | ||
| 170 | } | ||
| 171 | |||
| 172 | /* parity [10] */ | ||
| 173 | WAIT(clock_lo, 50, 6); | ||
| 174 | if (data_in() != parity) { | ||
| 175 | ps2_error = PS2_ERR_PARITY; | ||
| 176 | goto ERROR; | ||
| 177 | } | ||
| 178 | WAIT(clock_hi, 50, 7); | ||
| 179 | |||
| 180 | /* stop bit [11] */ | ||
| 181 | WAIT(clock_lo, 50, 8); | ||
| 182 | WAIT(data_hi, 1, 9); | ||
| 183 | WAIT(clock_hi, 50, 10); | ||
| 184 | |||
| 185 | /* inhibit device to send */ | ||
| 186 | clock_lo(); | ||
| 187 | |||
| 188 | return data; | ||
| 189 | ERROR: | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | static inline void clock_lo() | ||
| 195 | { | ||
| 196 | PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT); | ||
| 197 | PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT); | ||
| 198 | } | ||
| 199 | static inline void clock_hi() | ||
| 200 | { | ||
| 201 | /* input with pull up */ | ||
| 202 | PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); | ||
| 203 | PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT); | ||
| 204 | } | ||
| 205 | static inline bool clock_in() | ||
| 206 | { | ||
| 207 | PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); | ||
| 208 | PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT); | ||
| 209 | return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT); | ||
| 210 | } | ||
| 211 | static inline void data_lo() | ||
| 212 | { | ||
| 213 | PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT); | ||
| 214 | PS2_DATA_DDR |= (1<<PS2_DATA_BIT); | ||
| 215 | } | ||
| 216 | static inline void data_hi() | ||
| 217 | { | ||
| 218 | /* input with pull up */ | ||
| 219 | PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); | ||
| 220 | PS2_DATA_PORT |= (1<<PS2_DATA_BIT); | ||
| 221 | } | ||
| 222 | static inline bool data_in() | ||
| 223 | { | ||
| 224 | PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); | ||
| 225 | PS2_DATA_PORT |= (1<<PS2_DATA_BIT); | ||
| 226 | return PS2_DATA_PIN&(1<<PS2_DATA_BIT); | ||
| 227 | } | ||
| 228 | |||
| 229 | static inline uint16_t wait_clock_lo(uint16_t us) | ||
| 230 | { | ||
| 231 | while (clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 232 | return us; | ||
| 233 | } | ||
| 234 | static inline uint16_t wait_clock_hi(uint16_t us) | ||
| 235 | { | ||
| 236 | while (!clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 237 | return us; | ||
| 238 | } | ||
| 239 | static inline uint16_t wait_data_lo(uint16_t us) | ||
| 240 | { | ||
| 241 | while (data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 242 | return us; | ||
| 243 | } | ||
| 244 | static inline uint16_t wait_data_hi(uint16_t us) | ||
| 245 | { | ||
| 246 | while (!data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 247 | return us; | ||
| 248 | } | ||
