PS2 Library

Dependents:   Pong Brickbreaker

Committer:
wjohnsto
Date:
Sun Feb 27 23:34:31 2011 +0000
Revision:
0:ce15490e89e9

        

Who changed what in which revision?

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