]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/ibmpc.h
ibmpc: Read data line earlier in ISR as possible
[max/tmk_keyboard.git] / tmk_core / protocol / ibmpc.h
1 /*
2 Copyright 2010,2011,2012,2013,2019 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 #ifndef IBMPC_H
39 #define IBMPC_H
40
41 #include <stdbool.h>
42 #include "wait.h"
43 #include "print.h"
44
45 /*
46  * IBM PC keyboard protocol
47  *
48  * PS/2 Resources
49  * --------------
50  * [1] The PS/2 Mouse/Keyboard Protocol
51  * http://www.computer-engineering.org/ps2protocol/
52  * Concise and thorough primer of PS/2 protocol.
53  *
54  * [2] Keyboard and Auxiliary Device Controller
55  * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
56  * Signal Timing and Format
57  *
58  * [3] Keyboards(101- and 102-key)
59  * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf
60  * Keyboard Layout, Scan Code Set, POR, and Commands.
61  *
62  * [4] PS/2 Reference Manuals
63  * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
64  * Collection of IBM Personal System/2 documents.
65  *
66  * [5] TrackPoint Engineering Specifications for version 3E
67  * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html
68  */
69 #define IBMPC_ACK         0xFA
70 #define IBMPC_RESEND      0xFE
71 #define IBMPC_SET_LED     0xED
72
73 #define IBMPC_PROTOCOL_AT   0
74 #define IBMPC_PROTOCOL_XT   1
75
76 // TODO: error numbers
77 #define IBMPC_ERR_NONE        0
78 #define IBMPC_ERR_RECV        0x00
79 #define IBMPC_ERR_SEND        0x10
80 #define IBMPC_ERR_TIMEOUT     0x20
81 #define IBMPC_ERR_FULL        0x40
82
83 #define IBMPC_LED_SCROLL_LOCK 0
84 #define IBMPC_LED_NUM_LOCK    1
85 #define IBMPC_LED_CAPS_LOCK   2
86
87
88 extern volatile uint8_t ibmpc_protocol;
89 extern volatile uint8_t ibmpc_error;
90
91 void ibmpc_host_init(void);
92 int16_t ibmpc_host_send(uint8_t data);
93 int16_t ibmpc_host_recv_response(void);
94 int16_t ibmpc_host_recv(void);
95 void ibmpc_host_set_led(uint8_t usb_led);
96
97
98 /*--------------------------------------------------------------------
99  * static functions
100  *------------------------------------------------------------------*/
101 #if defined(__AVR__)
102 /*
103  * Clock
104  */
105 static inline void clock_init(void)
106 {
107     IBMPC_CLOCK_PORT &= ~(1<<IBMPC_CLOCK_BIT);
108     IBMPC_CLOCK_DDR  |=  (1<<IBMPC_CLOCK_BIT);
109 }
110
111 static inline void clock_lo(void)
112 {
113     IBMPC_CLOCK_PORT &= ~(1<<IBMPC_CLOCK_BIT);
114     IBMPC_CLOCK_DDR  |=  (1<<IBMPC_CLOCK_BIT);
115 }
116
117 static inline void clock_hi(void)
118 {
119     /* input with pull up */
120     IBMPC_CLOCK_DDR  &= ~(1<<IBMPC_CLOCK_BIT);
121     IBMPC_CLOCK_PORT |=  (1<<IBMPC_CLOCK_BIT);
122 }
123
124 static inline bool clock_in(void)
125 {
126     IBMPC_CLOCK_DDR  &= ~(1<<IBMPC_CLOCK_BIT);
127     IBMPC_CLOCK_PORT |=  (1<<IBMPC_CLOCK_BIT);
128     wait_us(1);
129     return IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT);
130 }
131
132 /*
133  * Data
134  */
135 static inline void data_init(void)
136 {
137     IBMPC_DATA_DDR  &= ~(1<<IBMPC_DATA_BIT);
138     IBMPC_DATA_PORT |=  (1<<IBMPC_DATA_BIT);
139 }
140
141 static inline void data_lo(void)
142 {
143     IBMPC_DATA_PORT &= ~(1<<IBMPC_DATA_BIT);
144     IBMPC_DATA_DDR  |=  (1<<IBMPC_DATA_BIT);
145 }
146
147 static inline void data_hi(void)
148 {
149     /* input with pull up */
150     IBMPC_DATA_DDR  &= ~(1<<IBMPC_DATA_BIT);
151     IBMPC_DATA_PORT |=  (1<<IBMPC_DATA_BIT);
152 }
153
154 static inline bool data_in(void)
155 {
156     IBMPC_DATA_DDR  &= ~(1<<IBMPC_DATA_BIT);
157     IBMPC_DATA_PORT |=  (1<<IBMPC_DATA_BIT);
158     wait_us(1);
159     return IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
160 }
161 #endif
162
163
164 static inline uint16_t wait_clock_lo(uint16_t us)
165 {
166     while (clock_in()  && us) { asm(""); wait_us(1); us--; }
167     return us;
168 }
169 static inline uint16_t wait_clock_hi(uint16_t us)
170 {
171     while (!clock_in() && us) { asm(""); wait_us(1); us--; }
172     return us;
173 }
174 static inline uint16_t wait_data_lo(uint16_t us)
175 {
176     while (data_in() && us)  { asm(""); wait_us(1); us--; }
177     return us;
178 }
179 static inline uint16_t wait_data_hi(uint16_t us)
180 {
181     while (!data_in() && us)  { asm(""); wait_us(1); us--; }
182     return us;
183 }
184
185 /* idle state that device can send */
186 static inline void idle(void)
187 {
188     clock_hi();
189     data_hi();
190 }
191
192 /* inhibit device to send */
193 static inline void inhibit(void)
194 {
195     clock_lo();
196     data_hi();
197 }
198
199 #endif