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
wjohnsto 0:ce15490e89e9 8 #include "PS2KB.h"
wjohnsto 0:ce15490e89e9 9
wjohnsto 0:ce15490e89e9 10 /**
wjohnsto 0:ce15490e89e9 11 * Create.
wjohnsto 0:ce15490e89e9 12 *
wjohnsto 0:ce15490e89e9 13 * @param clk_pin Clock pin.
wjohnsto 0:ce15490e89e9 14 * @param dat_pin Data pin.
wjohnsto 0:ce15490e89e9 15 */
wjohnsto 0:ce15490e89e9 16 PS2KB::PS2KB(PinName clk_pin, PinName dat_pin)
wjohnsto 0:ce15490e89e9 17 : clk(clk_pin), dat(dat_pin) {
wjohnsto 0:ce15490e89e9 18 init_work();
wjohnsto 0:ce15490e89e9 19 clk.fall(this, &PS2KB::func_fall);
wjohnsto 0:ce15490e89e9 20 timeout = 1;
wjohnsto 0:ce15490e89e9 21 }
wjohnsto 0:ce15490e89e9 22
wjohnsto 0:ce15490e89e9 23 /**
wjohnsto 0:ce15490e89e9 24 * Destory.
wjohnsto 0:ce15490e89e9 25 */
wjohnsto 0:ce15490e89e9 26 PS2KB::~PS2KB() {
wjohnsto 0:ce15490e89e9 27 wdt.detach();
wjohnsto 0:ce15490e89e9 28 }
wjohnsto 0:ce15490e89e9 29
wjohnsto 0:ce15490e89e9 30 /**
wjohnsto 0:ce15490e89e9 31 * Get a data from a PS/2 device.
wjohnsto 0:ce15490e89e9 32 *
wjohnsto 0:ce15490e89e9 33 * @return A data from a PS/2 device.
wjohnsto 0:ce15490e89e9 34 */
wjohnsto 0:ce15490e89e9 35 int PS2KB::getc() {
wjohnsto 0:ce15490e89e9 36 tot.reset();
wjohnsto 0:ce15490e89e9 37 tot.start();
wjohnsto 0:ce15490e89e9 38 while (work.cStart == work.cEnd) {
wjohnsto 0:ce15490e89e9 39 wait_ms(1);
wjohnsto 0:ce15490e89e9 40 if ((timeout > 0) && (tot.read_ms() > timeout)) {
wjohnsto 0:ce15490e89e9 41 // printf("Timeout occured.\n");
wjohnsto 0:ce15490e89e9 42 return EOF;
wjohnsto 0:ce15490e89e9 43 }
wjohnsto 0:ce15490e89e9 44 }
wjohnsto 0:ce15490e89e9 45 tot.stop();
wjohnsto 0:ce15490e89e9 46
wjohnsto 0:ce15490e89e9 47 char c = work.buffer[work.cStart++];
wjohnsto 0:ce15490e89e9 48 work.cStart = work.cStart % RINGBUFSIZ;
wjohnsto 0:ce15490e89e9 49
wjohnsto 0:ce15490e89e9 50 return c;
wjohnsto 0:ce15490e89e9 51 }
wjohnsto 0:ce15490e89e9 52
wjohnsto 0:ce15490e89e9 53 /**
wjohnsto 0:ce15490e89e9 54 * Set timeout.
wjohnsto 0:ce15490e89e9 55 *
wjohnsto 0:ce15490e89e9 56 * @param ms Timeout ms.
wjohnsto 0:ce15490e89e9 57 */
wjohnsto 0:ce15490e89e9 58 void PS2KB::setTimeout(int ms) {
wjohnsto 0:ce15490e89e9 59 timeout = ms;
wjohnsto 0:ce15490e89e9 60 }
wjohnsto 0:ce15490e89e9 61
wjohnsto 0:ce15490e89e9 62 void PS2KB::func_timeout(void) {
wjohnsto 0:ce15490e89e9 63 work.bitcnt = 0;
wjohnsto 0:ce15490e89e9 64 }
wjohnsto 0:ce15490e89e9 65
wjohnsto 0:ce15490e89e9 66 void PS2KB::func_fall(void) {
wjohnsto 0:ce15490e89e9 67 int oddpar = 0;
wjohnsto 0:ce15490e89e9 68 /*
wjohnsto 0:ce15490e89e9 69 */
wjohnsto 0:ce15490e89e9 70 switch (work.bitcnt) {
wjohnsto 0:ce15490e89e9 71 case 0:
wjohnsto 0:ce15490e89e9 72 /*
wjohnsto 0:ce15490e89e9 73 * Start bit.
wjohnsto 0:ce15490e89e9 74 */
wjohnsto 0:ce15490e89e9 75 if (dat.read() != 0) {
wjohnsto 0:ce15490e89e9 76 // printf("Illegal start bit condition.\n");
wjohnsto 0:ce15490e89e9 77 }
wjohnsto 0:ce15490e89e9 78 work.bitcnt++;
wjohnsto 0:ce15490e89e9 79 break;
wjohnsto 0:ce15490e89e9 80 case 9:
wjohnsto 0:ce15490e89e9 81 /*
wjohnsto 0:ce15490e89e9 82 * Parity bit.
wjohnsto 0:ce15490e89e9 83 */
wjohnsto 0:ce15490e89e9 84 for (int i = 0; i < 8; i++) {
wjohnsto 0:ce15490e89e9 85 if ((work.buffer[work.cEnd] & (1 << i)) != 0) {
wjohnsto 0:ce15490e89e9 86 oddpar++;
wjohnsto 0:ce15490e89e9 87 }
wjohnsto 0:ce15490e89e9 88 }
wjohnsto 0:ce15490e89e9 89 if (dat.read() == 1) {
wjohnsto 0:ce15490e89e9 90 oddpar++;
wjohnsto 0:ce15490e89e9 91 }
wjohnsto 0:ce15490e89e9 92 if ((oddpar % 2) != 1) {
wjohnsto 0:ce15490e89e9 93 // printf("Data parity error.\n");
wjohnsto 0:ce15490e89e9 94 }
wjohnsto 0:ce15490e89e9 95 work.bitcnt++;
wjohnsto 0:ce15490e89e9 96 break;
wjohnsto 0:ce15490e89e9 97 case 10:
wjohnsto 0:ce15490e89e9 98 /*
wjohnsto 0:ce15490e89e9 99 * Stop bit.
wjohnsto 0:ce15490e89e9 100 */
wjohnsto 0:ce15490e89e9 101 if (dat.read() != 1) {
wjohnsto 0:ce15490e89e9 102 // printf("Illegal stop bit condition.\n");
wjohnsto 0:ce15490e89e9 103 }
wjohnsto 0:ce15490e89e9 104 if (work.cStart != ((work.cEnd + 1) % RINGBUFSIZ)) {
wjohnsto 0:ce15490e89e9 105 work.cEnd++;
wjohnsto 0:ce15490e89e9 106 work.cEnd = work.cEnd % RINGBUFSIZ;
wjohnsto 0:ce15490e89e9 107 work.bitcnt = 0;
wjohnsto 0:ce15490e89e9 108 } else {
wjohnsto 0:ce15490e89e9 109 // printf("Buffer overrun.\n");
wjohnsto 0:ce15490e89e9 110 }
wjohnsto 0:ce15490e89e9 111 break;
wjohnsto 0:ce15490e89e9 112 default:
wjohnsto 0:ce15490e89e9 113 if ((1 <= work.bitcnt) && (work.bitcnt <= 8)) {
wjohnsto 0:ce15490e89e9 114 /*
wjohnsto 0:ce15490e89e9 115 * data bit.
wjohnsto 0:ce15490e89e9 116 */
wjohnsto 0:ce15490e89e9 117 if (dat.read() == 1) {
wjohnsto 0:ce15490e89e9 118 work.buffer[work.cEnd] |= (1 << (work.bitcnt - 1));
wjohnsto 0:ce15490e89e9 119 } else {
wjohnsto 0:ce15490e89e9 120 work.buffer[work.cEnd] &= ~(1 << (work.bitcnt - 1));
wjohnsto 0:ce15490e89e9 121 }
wjohnsto 0:ce15490e89e9 122 work.bitcnt++;
wjohnsto 0:ce15490e89e9 123 } else {
wjohnsto 0:ce15490e89e9 124 /*
wjohnsto 0:ce15490e89e9 125 * Illegal internal state.
wjohnsto 0:ce15490e89e9 126 */
wjohnsto 0:ce15490e89e9 127 // printf("Illegal internal state found.\n");
wjohnsto 0:ce15490e89e9 128 init_work();
wjohnsto 0:ce15490e89e9 129 }
wjohnsto 0:ce15490e89e9 130 break;
wjohnsto 0:ce15490e89e9 131 }
wjohnsto 0:ce15490e89e9 132 wdt.detach();
wjohnsto 0:ce15490e89e9 133 wdt.attach_us(this, &PS2KB::func_timeout, 250);
wjohnsto 0:ce15490e89e9 134 }
wjohnsto 0:ce15490e89e9 135
wjohnsto 0:ce15490e89e9 136 void PS2KB::init_work(void) {
wjohnsto 0:ce15490e89e9 137 work.bitcnt = 0;
wjohnsto 0:ce15490e89e9 138 work.cStart = 0;
wjohnsto 0:ce15490e89e9 139 work.cEnd = 0;
wjohnsto 0:ce15490e89e9 140 }