![](/media/cache/profiles/1006ff12c465532f8c574aeaa4461b16.50x50_q85.jpg)
A test program for PS2 library.
mylib/PS2/PS2MS_INIT.cpp@4:47b51250a168, 2010-10-13 (annotated)
- Committer:
- shintamainjp
- Date:
- Wed Oct 13 10:50:55 2010 +0000
- Revision:
- 4:47b51250a168
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 4:47b51250a168 | 1 | /** |
shintamainjp | 4:47b51250a168 | 2 | * PS/2 mouse interface control class (Version 0.0.1) |
shintamainjp | 4:47b51250a168 | 3 | * |
shintamainjp | 4:47b51250a168 | 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 4:47b51250a168 | 5 | * http://shinta.main.jp/ |
shintamainjp | 4:47b51250a168 | 6 | */ |
shintamainjp | 4:47b51250a168 | 7 | #include "PS2MS_INIT.h" |
shintamainjp | 4:47b51250a168 | 8 | |
shintamainjp | 4:47b51250a168 | 9 | /** |
shintamainjp | 4:47b51250a168 | 10 | * Create. |
shintamainjp | 4:47b51250a168 | 11 | */ |
shintamainjp | 4:47b51250a168 | 12 | PS2MS_INIT::PS2MS_INIT(PinName clk_pin, PinName dat_pin) |
shintamainjp | 4:47b51250a168 | 13 | : clk(clk_pin), dat(dat_pin) { |
shintamainjp | 4:47b51250a168 | 14 | clk.input(); |
shintamainjp | 4:47b51250a168 | 15 | dat.input(); |
shintamainjp | 4:47b51250a168 | 16 | clk.write(1); |
shintamainjp | 4:47b51250a168 | 17 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 18 | |
shintamainjp | 4:47b51250a168 | 19 | /* |
shintamainjp | 4:47b51250a168 | 20 | * 0xFF: Reset command. |
shintamainjp | 4:47b51250a168 | 21 | * 0xF3: Set sample rate. |
shintamainjp | 4:47b51250a168 | 22 | * 0xF2: Read device type. |
shintamainjp | 4:47b51250a168 | 23 | * 0xE8: Set resolution. |
shintamainjp | 4:47b51250a168 | 24 | * 0xE6: Set scaling. |
shintamainjp | 4:47b51250a168 | 25 | * 0xF4: Enable device. |
shintamainjp | 4:47b51250a168 | 26 | */ |
shintamainjp | 4:47b51250a168 | 27 | char txdat[17] = "\xFF\xFF\xFF\xF3\xC8\xF3\x64\xF3\x50\xF2\xE8\x03\xE6\xF3\x28\xF4"; |
shintamainjp | 4:47b51250a168 | 28 | const int n = sizeof(txdat); |
shintamainjp | 4:47b51250a168 | 29 | int txerrcnt = 0; |
shintamainjp | 4:47b51250a168 | 30 | int rxerrcnt = 0; |
shintamainjp | 4:47b51250a168 | 31 | for (int i = 0; i < n; i++) { |
shintamainjp | 4:47b51250a168 | 32 | if (send(txdat[i]) != 0) { |
shintamainjp | 4:47b51250a168 | 33 | txerrcnt++; |
shintamainjp | 4:47b51250a168 | 34 | } |
shintamainjp | 4:47b51250a168 | 35 | if (recv() < 0) { |
shintamainjp | 4:47b51250a168 | 36 | rxerrcnt++; |
shintamainjp | 4:47b51250a168 | 37 | } |
shintamainjp | 4:47b51250a168 | 38 | if (txdat[i] == 0xF2) { |
shintamainjp | 4:47b51250a168 | 39 | if (recv() < 0) { |
shintamainjp | 4:47b51250a168 | 40 | rxerrcnt++; |
shintamainjp | 4:47b51250a168 | 41 | } |
shintamainjp | 4:47b51250a168 | 42 | } |
shintamainjp | 4:47b51250a168 | 43 | if (txdat[i] == 0xFF) { |
shintamainjp | 4:47b51250a168 | 44 | if (recv() < 0) { |
shintamainjp | 4:47b51250a168 | 45 | rxerrcnt++; |
shintamainjp | 4:47b51250a168 | 46 | } |
shintamainjp | 4:47b51250a168 | 47 | if (recv() < 0) { |
shintamainjp | 4:47b51250a168 | 48 | rxerrcnt++; |
shintamainjp | 4:47b51250a168 | 49 | } |
shintamainjp | 4:47b51250a168 | 50 | } |
shintamainjp | 4:47b51250a168 | 51 | } |
shintamainjp | 4:47b51250a168 | 52 | |
shintamainjp | 4:47b51250a168 | 53 | if (txerrcnt > 0) { |
shintamainjp | 4:47b51250a168 | 54 | // printf("TX %d errors occured.\n", txerrcnt); |
shintamainjp | 4:47b51250a168 | 55 | } |
shintamainjp | 4:47b51250a168 | 56 | if (rxerrcnt > 0) { |
shintamainjp | 4:47b51250a168 | 57 | // printf("RX %d errors occured.\n", rxerrcnt); |
shintamainjp | 4:47b51250a168 | 58 | } |
shintamainjp | 4:47b51250a168 | 59 | } |
shintamainjp | 4:47b51250a168 | 60 | |
shintamainjp | 4:47b51250a168 | 61 | /** |
shintamainjp | 4:47b51250a168 | 62 | * Destroy. |
shintamainjp | 4:47b51250a168 | 63 | */ |
shintamainjp | 4:47b51250a168 | 64 | PS2MS_INIT::~PS2MS_INIT() { |
shintamainjp | 4:47b51250a168 | 65 | } |
shintamainjp | 4:47b51250a168 | 66 | |
shintamainjp | 4:47b51250a168 | 67 | /** |
shintamainjp | 4:47b51250a168 | 68 | * Send a byte data. |
shintamainjp | 4:47b51250a168 | 69 | * |
shintamainjp | 4:47b51250a168 | 70 | * @param c a character. |
shintamainjp | 4:47b51250a168 | 71 | * |
shintamainjp | 4:47b51250a168 | 72 | * @return Negative value is a error number. |
shintamainjp | 4:47b51250a168 | 73 | */ |
shintamainjp | 4:47b51250a168 | 74 | int PS2MS_INIT::send(uint8_t c) { |
shintamainjp | 4:47b51250a168 | 75 | clk.output(); |
shintamainjp | 4:47b51250a168 | 76 | dat.output(); |
shintamainjp | 4:47b51250a168 | 77 | |
shintamainjp | 4:47b51250a168 | 78 | clk.write(0); |
shintamainjp | 4:47b51250a168 | 79 | wait_us(200); |
shintamainjp | 4:47b51250a168 | 80 | |
shintamainjp | 4:47b51250a168 | 81 | dat.write(0); |
shintamainjp | 4:47b51250a168 | 82 | wait_us(10); |
shintamainjp | 4:47b51250a168 | 83 | clk.write(1); |
shintamainjp | 4:47b51250a168 | 84 | wait_us(10); |
shintamainjp | 4:47b51250a168 | 85 | |
shintamainjp | 4:47b51250a168 | 86 | clk.input(); |
shintamainjp | 4:47b51250a168 | 87 | int parcnt = 0; |
shintamainjp | 4:47b51250a168 | 88 | for (int i = 0; i < 10; i++) { |
shintamainjp | 4:47b51250a168 | 89 | if (!waitClockDownEdge()) { |
shintamainjp | 4:47b51250a168 | 90 | return -1; |
shintamainjp | 4:47b51250a168 | 91 | } |
shintamainjp | 4:47b51250a168 | 92 | if ((0 <= i) && (i <= 7)) { |
shintamainjp | 4:47b51250a168 | 93 | /* |
shintamainjp | 4:47b51250a168 | 94 | * Data bit. |
shintamainjp | 4:47b51250a168 | 95 | */ |
shintamainjp | 4:47b51250a168 | 96 | if ((c & (1 << i)) == 0) { |
shintamainjp | 4:47b51250a168 | 97 | dat.write(0); |
shintamainjp | 4:47b51250a168 | 98 | } else { |
shintamainjp | 4:47b51250a168 | 99 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 100 | parcnt++; |
shintamainjp | 4:47b51250a168 | 101 | } |
shintamainjp | 4:47b51250a168 | 102 | } |
shintamainjp | 4:47b51250a168 | 103 | if (i == 8) { |
shintamainjp | 4:47b51250a168 | 104 | /* |
shintamainjp | 4:47b51250a168 | 105 | * Parity bit. |
shintamainjp | 4:47b51250a168 | 106 | */ |
shintamainjp | 4:47b51250a168 | 107 | if ((parcnt % 2) == 0) { |
shintamainjp | 4:47b51250a168 | 108 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 109 | } else { |
shintamainjp | 4:47b51250a168 | 110 | dat.write(0); |
shintamainjp | 4:47b51250a168 | 111 | } |
shintamainjp | 4:47b51250a168 | 112 | } |
shintamainjp | 4:47b51250a168 | 113 | if (i == 9) { |
shintamainjp | 4:47b51250a168 | 114 | /* |
shintamainjp | 4:47b51250a168 | 115 | * Stop bit. |
shintamainjp | 4:47b51250a168 | 116 | */ |
shintamainjp | 4:47b51250a168 | 117 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 118 | } |
shintamainjp | 4:47b51250a168 | 119 | } |
shintamainjp | 4:47b51250a168 | 120 | dat.input(); |
shintamainjp | 4:47b51250a168 | 121 | |
shintamainjp | 4:47b51250a168 | 122 | /* |
shintamainjp | 4:47b51250a168 | 123 | * Check a ACK. |
shintamainjp | 4:47b51250a168 | 124 | */ |
shintamainjp | 4:47b51250a168 | 125 | if (!waitClockDownEdge()) { |
shintamainjp | 4:47b51250a168 | 126 | return -2; |
shintamainjp | 4:47b51250a168 | 127 | } |
shintamainjp | 4:47b51250a168 | 128 | if (dat.read() != 0) { |
shintamainjp | 4:47b51250a168 | 129 | return -3; |
shintamainjp | 4:47b51250a168 | 130 | } |
shintamainjp | 4:47b51250a168 | 131 | |
shintamainjp | 4:47b51250a168 | 132 | if (!waitClockUpLevel()) { |
shintamainjp | 4:47b51250a168 | 133 | return -4; |
shintamainjp | 4:47b51250a168 | 134 | } |
shintamainjp | 4:47b51250a168 | 135 | |
shintamainjp | 4:47b51250a168 | 136 | return 0; |
shintamainjp | 4:47b51250a168 | 137 | } |
shintamainjp | 4:47b51250a168 | 138 | |
shintamainjp | 4:47b51250a168 | 139 | /** |
shintamainjp | 4:47b51250a168 | 140 | * Receive a byte data. |
shintamainjp | 4:47b51250a168 | 141 | * |
shintamainjp | 4:47b51250a168 | 142 | * @return return a data. Negative value is a error number. |
shintamainjp | 4:47b51250a168 | 143 | */ |
shintamainjp | 4:47b51250a168 | 144 | int PS2MS_INIT::recv(void) { |
shintamainjp | 4:47b51250a168 | 145 | uint8_t c = 0; |
shintamainjp | 4:47b51250a168 | 146 | clk.input(); |
shintamainjp | 4:47b51250a168 | 147 | dat.input(); |
shintamainjp | 4:47b51250a168 | 148 | int parcnt = 0; |
shintamainjp | 4:47b51250a168 | 149 | for (int i = 0; i < 11; i++) { |
shintamainjp | 4:47b51250a168 | 150 | if (!waitClockDownEdge()) { |
shintamainjp | 4:47b51250a168 | 151 | return -1; |
shintamainjp | 4:47b51250a168 | 152 | } |
shintamainjp | 4:47b51250a168 | 153 | if (i == 0) { |
shintamainjp | 4:47b51250a168 | 154 | /* |
shintamainjp | 4:47b51250a168 | 155 | * Start bit. |
shintamainjp | 4:47b51250a168 | 156 | */ |
shintamainjp | 4:47b51250a168 | 157 | if (dat.read() != 0) { |
shintamainjp | 4:47b51250a168 | 158 | return -2; |
shintamainjp | 4:47b51250a168 | 159 | } |
shintamainjp | 4:47b51250a168 | 160 | } |
shintamainjp | 4:47b51250a168 | 161 | if ((1 <= i) && (i <= 8)) { |
shintamainjp | 4:47b51250a168 | 162 | /* |
shintamainjp | 4:47b51250a168 | 163 | * Data bit. |
shintamainjp | 4:47b51250a168 | 164 | */ |
shintamainjp | 4:47b51250a168 | 165 | if (dat.read() == 0) { |
shintamainjp | 4:47b51250a168 | 166 | c &= ~(1 << (i - 1)); |
shintamainjp | 4:47b51250a168 | 167 | } else { |
shintamainjp | 4:47b51250a168 | 168 | c |= (1 << (i - 1)); |
shintamainjp | 4:47b51250a168 | 169 | parcnt++; |
shintamainjp | 4:47b51250a168 | 170 | } |
shintamainjp | 4:47b51250a168 | 171 | } |
shintamainjp | 4:47b51250a168 | 172 | if (i == 9) { |
shintamainjp | 4:47b51250a168 | 173 | /* |
shintamainjp | 4:47b51250a168 | 174 | * Parity bit. |
shintamainjp | 4:47b51250a168 | 175 | */ |
shintamainjp | 4:47b51250a168 | 176 | if (dat.read() == 0) { |
shintamainjp | 4:47b51250a168 | 177 | if ((parcnt % 2) != 1) { |
shintamainjp | 4:47b51250a168 | 178 | return -3; |
shintamainjp | 4:47b51250a168 | 179 | } |
shintamainjp | 4:47b51250a168 | 180 | } else { |
shintamainjp | 4:47b51250a168 | 181 | if ((parcnt % 2) != 0) { |
shintamainjp | 4:47b51250a168 | 182 | return -4; |
shintamainjp | 4:47b51250a168 | 183 | } |
shintamainjp | 4:47b51250a168 | 184 | } |
shintamainjp | 4:47b51250a168 | 185 | } |
shintamainjp | 4:47b51250a168 | 186 | if (i == 10) { |
shintamainjp | 4:47b51250a168 | 187 | /* |
shintamainjp | 4:47b51250a168 | 188 | * Stop bit. |
shintamainjp | 4:47b51250a168 | 189 | */ |
shintamainjp | 4:47b51250a168 | 190 | if (dat.read() != 1) { |
shintamainjp | 4:47b51250a168 | 191 | return -5; |
shintamainjp | 4:47b51250a168 | 192 | } |
shintamainjp | 4:47b51250a168 | 193 | } |
shintamainjp | 4:47b51250a168 | 194 | } |
shintamainjp | 4:47b51250a168 | 195 | return (int)c; |
shintamainjp | 4:47b51250a168 | 196 | } |
shintamainjp | 4:47b51250a168 | 197 | |
shintamainjp | 4:47b51250a168 | 198 | /** |
shintamainjp | 4:47b51250a168 | 199 | * Wait a clock down edge. |
shintamainjp | 4:47b51250a168 | 200 | * |
shintamainjp | 4:47b51250a168 | 201 | * @return true if wait done. |
shintamainjp | 4:47b51250a168 | 202 | */ |
shintamainjp | 4:47b51250a168 | 203 | bool PS2MS_INIT::waitClockDownEdge(void) { |
shintamainjp | 4:47b51250a168 | 204 | int cnt; |
shintamainjp | 4:47b51250a168 | 205 | /* |
shintamainjp | 4:47b51250a168 | 206 | * Wait until clock is low. |
shintamainjp | 4:47b51250a168 | 207 | */ |
shintamainjp | 4:47b51250a168 | 208 | cnt = 0; |
shintamainjp | 4:47b51250a168 | 209 | while (clk.read() == 0) { |
shintamainjp | 4:47b51250a168 | 210 | cnt++; |
shintamainjp | 4:47b51250a168 | 211 | if (MAX_RETRY < cnt) { |
shintamainjp | 4:47b51250a168 | 212 | return false; |
shintamainjp | 4:47b51250a168 | 213 | } |
shintamainjp | 4:47b51250a168 | 214 | wait_us(1); |
shintamainjp | 4:47b51250a168 | 215 | } |
shintamainjp | 4:47b51250a168 | 216 | /* |
shintamainjp | 4:47b51250a168 | 217 | * Wait until clock is high. |
shintamainjp | 4:47b51250a168 | 218 | */ |
shintamainjp | 4:47b51250a168 | 219 | cnt = 0; |
shintamainjp | 4:47b51250a168 | 220 | while (clk.read() == 1) { |
shintamainjp | 4:47b51250a168 | 221 | cnt++; |
shintamainjp | 4:47b51250a168 | 222 | if (MAX_RETRY < cnt) { |
shintamainjp | 4:47b51250a168 | 223 | return false; |
shintamainjp | 4:47b51250a168 | 224 | } |
shintamainjp | 4:47b51250a168 | 225 | wait_us(1); |
shintamainjp | 4:47b51250a168 | 226 | } |
shintamainjp | 4:47b51250a168 | 227 | return true; |
shintamainjp | 4:47b51250a168 | 228 | } |
shintamainjp | 4:47b51250a168 | 229 | |
shintamainjp | 4:47b51250a168 | 230 | /** |
shintamainjp | 4:47b51250a168 | 231 | * Wait a clock up level. |
shintamainjp | 4:47b51250a168 | 232 | * |
shintamainjp | 4:47b51250a168 | 233 | * @return true if wait done. |
shintamainjp | 4:47b51250a168 | 234 | */ |
shintamainjp | 4:47b51250a168 | 235 | bool PS2MS_INIT::waitClockUpLevel(void) { |
shintamainjp | 4:47b51250a168 | 236 | int cnt; |
shintamainjp | 4:47b51250a168 | 237 | /* |
shintamainjp | 4:47b51250a168 | 238 | * Wait until clock is low. |
shintamainjp | 4:47b51250a168 | 239 | */ |
shintamainjp | 4:47b51250a168 | 240 | cnt = 0; |
shintamainjp | 4:47b51250a168 | 241 | while (clk.read() == 0) { |
shintamainjp | 4:47b51250a168 | 242 | cnt++; |
shintamainjp | 4:47b51250a168 | 243 | if (MAX_RETRY < cnt) { |
shintamainjp | 4:47b51250a168 | 244 | return false; |
shintamainjp | 4:47b51250a168 | 245 | } |
shintamainjp | 4:47b51250a168 | 246 | wait_us(1); |
shintamainjp | 4:47b51250a168 | 247 | } |
shintamainjp | 4:47b51250a168 | 248 | return true; |
shintamainjp | 4:47b51250a168 | 249 | } |