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