PS2 keyboard

Dependents:   uVGAII_WebBrowser

Committer:
yaolu23
Date:
Wed Mar 12 16:15:14 2014 +0000
Revision:
0:33c8907eb27a
na

Who changed what in which revision?

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