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