]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/xt_interrupt.c
05afefd36017ffdd30d5716857a16f2ded695d2a
[max/tmk_keyboard.git] / tmk_core / protocol / xt_interrupt.c
1 /*
2 Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3
4 This software is licensed with a Modified BSD License.
5 All of this is supposed to be Free Software, Open Source, DFSG-free,
6 GPL-compatible, and OK to use in both free and proprietary applications.
7 Additions and corrections to this file are welcome.
8
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14   notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright
17   notice, this list of conditions and the following disclaimer in
18   the documentation and/or other materials provided with the
19   distribution.
20
21 * Neither the name of the copyright holders nor the names of
22   contributors may be used to endorse or promote products derived
23   from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39  * PS/2 protocol Pin interrupt version
40  */
41
42 #include <stdbool.h>
43 #include <avr/interrupt.h>
44 #include <util/delay.h>
45 #include "pbuff.h"
46 #include "xt.h"
47 #include "xt_io.h"
48 #include "wait.h"
49 #include "print.h"
50
51 void xt_host_init(void)
52 {
53     XT_INT_INIT();
54
55     /* hard reset */
56 #ifdef XT_RESET
57     XT_RESET();
58 #endif
59
60     /* soft reset: pull clock line down for 20ms */
61     XT_INT_OFF();
62     data_lo(); clock_lo();
63     _delay_ms(20);
64     data_in(); clock_in();
65     XT_INT_ON();
66 }
67
68 /* get data received by interrupt */
69 uint8_t xt_host_recv(void)
70 {
71     if (pbuf_has_data()) {
72         return pbuf_dequeue();
73     } else {
74         return 0;
75     }
76 }
77
78 ISR(XT_INT_VECT)
79 {
80     /*
81      * XT signal format consits of 10 or 9 clocks and sends start bits and 8-bit data,
82      * which should be read on falling edge of clock.
83      *
84      *  start(0), start(1), bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7
85      *
86      * Original IBM XT keyboard sends start(0) bit while some of clones don't.
87      * Start(0) bit is read as low on data line while start(1) as high.
88      *
89      * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
90      */
91     static enum {
92         START, BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, END
93     } state = START;
94     static uint8_t data = 0;
95
96     uint8_t dbit = data_in();
97
98     // This is needed if using PCINT which can be called on both falling and rising edge
99     //if (clock_in()) return;
100
101     switch (state) {
102         case START:
103             // ignore start(0) bit
104             if (!dbit) return;
105             break;
106         case BIT0 ... BIT7:
107             data >>= 1;
108             if (dbit)
109                 data |= 0x80;
110             break;
111     }
112     if (++state == END) {
113         pbuf_enqueue(data);
114         state = START;
115         data = 0;
116     }
117     return;
118 }