![](/media/cache/profiles/1006ff12c465532f8c574aeaa4461b16.50x50_q85.jpg)
A test program for PS2 library.
mylib/PS2/PS2KB_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 keyboard 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 "PS2KB_INIT.h" |
shintamainjp | 4:47b51250a168 | 8 | |
shintamainjp | 4:47b51250a168 | 9 | /** |
shintamainjp | 4:47b51250a168 | 10 | * Create. |
shintamainjp | 4:47b51250a168 | 11 | */ |
shintamainjp | 4:47b51250a168 | 12 | PS2KB_INIT::PS2KB_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. |
shintamainjp | 4:47b51250a168 | 21 | * 0xED: Set/Reset status indicators. |
shintamainjp | 4:47b51250a168 | 22 | * 0xF2: Read ID. |
shintamainjp | 4:47b51250a168 | 23 | * 0xF3: Set typematic rate/delay. |
shintamainjp | 4:47b51250a168 | 24 | * 0xF4: Enable. |
shintamainjp | 4:47b51250a168 | 25 | */ |
shintamainjp | 4:47b51250a168 | 26 | char txdat[12] = "\xFF\xED\x07\xF2\xED\x00\xF3\x20\xF3\x00\xF4"; |
shintamainjp | 4:47b51250a168 | 27 | const int n = sizeof(txdat); |
shintamainjp | 4:47b51250a168 | 28 | int txerrcnt = 0; |
shintamainjp | 4:47b51250a168 | 29 | int rxerrcnt = 0; |
shintamainjp | 4:47b51250a168 | 30 | for (int i = 0; i < n; i++) { |
shintamainjp | 4:47b51250a168 | 31 | if (send(txdat[i]) != 0) { |
shintamainjp | 4:47b51250a168 | 32 | txerrcnt++; |
shintamainjp | 4:47b51250a168 | 33 | } |
shintamainjp | 4:47b51250a168 | 34 | if (recv() < 0) { |
shintamainjp | 4:47b51250a168 | 35 | rxerrcnt++; |
shintamainjp | 4:47b51250a168 | 36 | } |
shintamainjp | 4:47b51250a168 | 37 | if (txdat[i] == 0xF2) { |
shintamainjp | 4:47b51250a168 | 38 | if (recv() < 0) { |
shintamainjp | 4:47b51250a168 | 39 | rxerrcnt++; |
shintamainjp | 4:47b51250a168 | 40 | } |
shintamainjp | 4:47b51250a168 | 41 | } |
shintamainjp | 4:47b51250a168 | 42 | } |
shintamainjp | 4:47b51250a168 | 43 | |
shintamainjp | 4:47b51250a168 | 44 | if (txerrcnt > 0) { |
shintamainjp | 4:47b51250a168 | 45 | // printf("TX %d errors occured.\n", txerrcnt); |
shintamainjp | 4:47b51250a168 | 46 | } |
shintamainjp | 4:47b51250a168 | 47 | if (rxerrcnt > 0) { |
shintamainjp | 4:47b51250a168 | 48 | // printf("RX %d errors occured.\n", rxerrcnt); |
shintamainjp | 4:47b51250a168 | 49 | } |
shintamainjp | 4:47b51250a168 | 50 | } |
shintamainjp | 4:47b51250a168 | 51 | |
shintamainjp | 4:47b51250a168 | 52 | /** |
shintamainjp | 4:47b51250a168 | 53 | * Destroy. |
shintamainjp | 4:47b51250a168 | 54 | */ |
shintamainjp | 4:47b51250a168 | 55 | PS2KB_INIT::~PS2KB_INIT() { |
shintamainjp | 4:47b51250a168 | 56 | } |
shintamainjp | 4:47b51250a168 | 57 | |
shintamainjp | 4:47b51250a168 | 58 | /** |
shintamainjp | 4:47b51250a168 | 59 | * Send a byte data. |
shintamainjp | 4:47b51250a168 | 60 | * |
shintamainjp | 4:47b51250a168 | 61 | * @param c a character. |
shintamainjp | 4:47b51250a168 | 62 | * |
shintamainjp | 4:47b51250a168 | 63 | * @return Negative value is a error number. |
shintamainjp | 4:47b51250a168 | 64 | */ |
shintamainjp | 4:47b51250a168 | 65 | int PS2KB_INIT::send(uint8_t c) { |
shintamainjp | 4:47b51250a168 | 66 | clk.output(); |
shintamainjp | 4:47b51250a168 | 67 | dat.output(); |
shintamainjp | 4:47b51250a168 | 68 | |
shintamainjp | 4:47b51250a168 | 69 | clk.write(0); |
shintamainjp | 4:47b51250a168 | 70 | wait_us(200); |
shintamainjp | 4:47b51250a168 | 71 | |
shintamainjp | 4:47b51250a168 | 72 | dat.write(0); |
shintamainjp | 4:47b51250a168 | 73 | wait_us(10); |
shintamainjp | 4:47b51250a168 | 74 | clk.write(1); |
shintamainjp | 4:47b51250a168 | 75 | wait_us(10); |
shintamainjp | 4:47b51250a168 | 76 | |
shintamainjp | 4:47b51250a168 | 77 | clk.input(); |
shintamainjp | 4:47b51250a168 | 78 | int parcnt = 0; |
shintamainjp | 4:47b51250a168 | 79 | for (int i = 0; i < 10; i++) { |
shintamainjp | 4:47b51250a168 | 80 | if (!waitClockDownEdge()) { |
shintamainjp | 4:47b51250a168 | 81 | return -1; |
shintamainjp | 4:47b51250a168 | 82 | } |
shintamainjp | 4:47b51250a168 | 83 | if ((0 <= i) && (i <= 7)) { |
shintamainjp | 4:47b51250a168 | 84 | /* |
shintamainjp | 4:47b51250a168 | 85 | * Data bit. |
shintamainjp | 4:47b51250a168 | 86 | */ |
shintamainjp | 4:47b51250a168 | 87 | if ((c & (1 << i)) == 0) { |
shintamainjp | 4:47b51250a168 | 88 | dat.write(0); |
shintamainjp | 4:47b51250a168 | 89 | } else { |
shintamainjp | 4:47b51250a168 | 90 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 91 | parcnt++; |
shintamainjp | 4:47b51250a168 | 92 | } |
shintamainjp | 4:47b51250a168 | 93 | } |
shintamainjp | 4:47b51250a168 | 94 | if (i == 8) { |
shintamainjp | 4:47b51250a168 | 95 | /* |
shintamainjp | 4:47b51250a168 | 96 | * Parity bit. |
shintamainjp | 4:47b51250a168 | 97 | */ |
shintamainjp | 4:47b51250a168 | 98 | if ((parcnt % 2) == 0) { |
shintamainjp | 4:47b51250a168 | 99 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 100 | } else { |
shintamainjp | 4:47b51250a168 | 101 | dat.write(0); |
shintamainjp | 4:47b51250a168 | 102 | } |
shintamainjp | 4:47b51250a168 | 103 | } |
shintamainjp | 4:47b51250a168 | 104 | if (i == 9) { |
shintamainjp | 4:47b51250a168 | 105 | /* |
shintamainjp | 4:47b51250a168 | 106 | * Stop bit. |
shintamainjp | 4:47b51250a168 | 107 | */ |
shintamainjp | 4:47b51250a168 | 108 | dat.write(1); |
shintamainjp | 4:47b51250a168 | 109 | } |
shintamainjp | 4:47b51250a168 | 110 | } |
shintamainjp | 4:47b51250a168 | 111 | dat.input(); |
shintamainjp | 4:47b51250a168 | 112 | |
shintamainjp | 4:47b51250a168 | 113 | /* |
shintamainjp | 4:47b51250a168 | 114 | * Check a ACK. |
shintamainjp | 4:47b51250a168 | 115 | */ |
shintamainjp | 4:47b51250a168 | 116 | if (!waitClockDownEdge()) { |
shintamainjp | 4:47b51250a168 | 117 | return -2; |
shintamainjp | 4:47b51250a168 | 118 | } |
shintamainjp | 4:47b51250a168 | 119 | if (dat.read() != 0) { |
shintamainjp | 4:47b51250a168 | 120 | return -3; |
shintamainjp | 4:47b51250a168 | 121 | } |
shintamainjp | 4:47b51250a168 | 122 | |
shintamainjp | 4:47b51250a168 | 123 | if (!waitClockUpLevel()) { |
shintamainjp | 4:47b51250a168 | 124 | return -4; |
shintamainjp | 4:47b51250a168 | 125 | } |
shintamainjp | 4:47b51250a168 | 126 | |
shintamainjp | 4:47b51250a168 | 127 | return 0; |
shintamainjp | 4:47b51250a168 | 128 | } |
shintamainjp | 4:47b51250a168 | 129 | |
shintamainjp | 4:47b51250a168 | 130 | /** |
shintamainjp | 4:47b51250a168 | 131 | * Receive a byte data. |
shintamainjp | 4:47b51250a168 | 132 | * |
shintamainjp | 4:47b51250a168 | 133 | * @return return a data. Negative value is a error number. |
shintamainjp | 4:47b51250a168 | 134 | */ |
shintamainjp | 4:47b51250a168 | 135 | int PS2KB_INIT::recv(void) { |
shintamainjp | 4:47b51250a168 | 136 | uint8_t c = 0; |
shintamainjp | 4:47b51250a168 | 137 | clk.input(); |
shintamainjp | 4:47b51250a168 | 138 | dat.input(); |
shintamainjp | 4:47b51250a168 | 139 | int parcnt = 0; |
shintamainjp | 4:47b51250a168 | 140 | for (int i = 0; i < 11; i++) { |
shintamainjp | 4:47b51250a168 | 141 | if (!waitClockDownEdge()) { |
shintamainjp | 4:47b51250a168 | 142 | return -1; |
shintamainjp | 4:47b51250a168 | 143 | } |
shintamainjp | 4:47b51250a168 | 144 | if (i == 0) { |
shintamainjp | 4:47b51250a168 | 145 | /* |
shintamainjp | 4:47b51250a168 | 146 | * Start bit. |
shintamainjp | 4:47b51250a168 | 147 | */ |
shintamainjp | 4:47b51250a168 | 148 | if (dat.read() != 0) { |
shintamainjp | 4:47b51250a168 | 149 | return -2; |
shintamainjp | 4:47b51250a168 | 150 | } |
shintamainjp | 4:47b51250a168 | 151 | } |
shintamainjp | 4:47b51250a168 | 152 | if ((1 <= i) && (i <= 8)) { |
shintamainjp | 4:47b51250a168 | 153 | /* |
shintamainjp | 4:47b51250a168 | 154 | * Data bit. |
shintamainjp | 4:47b51250a168 | 155 | */ |
shintamainjp | 4:47b51250a168 | 156 | if (dat.read() == 0) { |
shintamainjp | 4:47b51250a168 | 157 | c &= ~(1 << (i - 1)); |
shintamainjp | 4:47b51250a168 | 158 | } else { |
shintamainjp | 4:47b51250a168 | 159 | c |= (1 << (i - 1)); |
shintamainjp | 4:47b51250a168 | 160 | parcnt++; |
shintamainjp | 4:47b51250a168 | 161 | } |
shintamainjp | 4:47b51250a168 | 162 | } |
shintamainjp | 4:47b51250a168 | 163 | if (i == 9) { |
shintamainjp | 4:47b51250a168 | 164 | /* |
shintamainjp | 4:47b51250a168 | 165 | * Parity bit. |
shintamainjp | 4:47b51250a168 | 166 | */ |
shintamainjp | 4:47b51250a168 | 167 | if (dat.read() == 0) { |
shintamainjp | 4:47b51250a168 | 168 | if ((parcnt % 2) != 1) { |
shintamainjp | 4:47b51250a168 | 169 | return -3; |
shintamainjp | 4:47b51250a168 | 170 | } |
shintamainjp | 4:47b51250a168 | 171 | } else { |
shintamainjp | 4:47b51250a168 | 172 | if ((parcnt % 2) != 0) { |
shintamainjp | 4:47b51250a168 | 173 | return -4; |
shintamainjp | 4:47b51250a168 | 174 | } |
shintamainjp | 4:47b51250a168 | 175 | } |
shintamainjp | 4:47b51250a168 | 176 | } |
shintamainjp | 4:47b51250a168 | 177 | if (i == 10) { |
shintamainjp | 4:47b51250a168 | 178 | /* |
shintamainjp | 4:47b51250a168 | 179 | * Stop bit. |
shintamainjp | 4:47b51250a168 | 180 | */ |
shintamainjp | 4:47b51250a168 | 181 | if (dat.read() != 1) { |
shintamainjp | 4:47b51250a168 | 182 | return -5; |
shintamainjp | 4:47b51250a168 | 183 | } |
shintamainjp | 4:47b51250a168 | 184 | } |
shintamainjp | 4:47b51250a168 | 185 | } |
shintamainjp | 4:47b51250a168 | 186 | return (int)c; |
shintamainjp | 4:47b51250a168 | 187 | } |
shintamainjp | 4:47b51250a168 | 188 | |
shintamainjp | 4:47b51250a168 | 189 | /** |
shintamainjp | 4:47b51250a168 | 190 | * Wait a clock down edge. |
shintamainjp | 4:47b51250a168 | 191 | * |
shintamainjp | 4:47b51250a168 | 192 | * @return true if wait done. |
shintamainjp | 4:47b51250a168 | 193 | */ |
shintamainjp | 4:47b51250a168 | 194 | bool PS2KB_INIT::waitClockDownEdge(void) { |
shintamainjp | 4:47b51250a168 | 195 | int cnt; |
shintamainjp | 4:47b51250a168 | 196 | /* |
shintamainjp | 4:47b51250a168 | 197 | * Wait until clock is low. |
shintamainjp | 4:47b51250a168 | 198 | */ |
shintamainjp | 4:47b51250a168 | 199 | cnt = 0; |
shintamainjp | 4:47b51250a168 | 200 | while (clk.read() == 0) { |
shintamainjp | 4:47b51250a168 | 201 | cnt++; |
shintamainjp | 4:47b51250a168 | 202 | if (MAX_RETRY < cnt) { |
shintamainjp | 4:47b51250a168 | 203 | return false; |
shintamainjp | 4:47b51250a168 | 204 | } |
shintamainjp | 4:47b51250a168 | 205 | wait_us(1); |
shintamainjp | 4:47b51250a168 | 206 | } |
shintamainjp | 4:47b51250a168 | 207 | /* |
shintamainjp | 4:47b51250a168 | 208 | * Wait until clock is high. |
shintamainjp | 4:47b51250a168 | 209 | */ |
shintamainjp | 4:47b51250a168 | 210 | cnt = 0; |
shintamainjp | 4:47b51250a168 | 211 | while (clk.read() == 1) { |
shintamainjp | 4:47b51250a168 | 212 | cnt++; |
shintamainjp | 4:47b51250a168 | 213 | if (MAX_RETRY < cnt) { |
shintamainjp | 4:47b51250a168 | 214 | return false; |
shintamainjp | 4:47b51250a168 | 215 | } |
shintamainjp | 4:47b51250a168 | 216 | wait_us(1); |
shintamainjp | 4:47b51250a168 | 217 | } |
shintamainjp | 4:47b51250a168 | 218 | return true; |
shintamainjp | 4:47b51250a168 | 219 | } |
shintamainjp | 4:47b51250a168 | 220 | |
shintamainjp | 4:47b51250a168 | 221 | /** |
shintamainjp | 4:47b51250a168 | 222 | * Wait a clock up level. |
shintamainjp | 4:47b51250a168 | 223 | * |
shintamainjp | 4:47b51250a168 | 224 | * @return true if wait done. |
shintamainjp | 4:47b51250a168 | 225 | */ |
shintamainjp | 4:47b51250a168 | 226 | bool PS2KB_INIT::waitClockUpLevel(void) { |
shintamainjp | 4:47b51250a168 | 227 | int cnt; |
shintamainjp | 4:47b51250a168 | 228 | /* |
shintamainjp | 4:47b51250a168 | 229 | * Wait until clock is low. |
shintamainjp | 4:47b51250a168 | 230 | */ |
shintamainjp | 4:47b51250a168 | 231 | cnt = 0; |
shintamainjp | 4:47b51250a168 | 232 | while (clk.read() == 0) { |
shintamainjp | 4:47b51250a168 | 233 | cnt++; |
shintamainjp | 4:47b51250a168 | 234 | if (MAX_RETRY < cnt) { |
shintamainjp | 4:47b51250a168 | 235 | return false; |
shintamainjp | 4:47b51250a168 | 236 | } |
shintamainjp | 4:47b51250a168 | 237 | wait_us(1); |
shintamainjp | 4:47b51250a168 | 238 | } |
shintamainjp | 4:47b51250a168 | 239 | return true; |
shintamainjp | 4:47b51250a168 | 240 | } |