]> git.gir.st - tmk_keyboard.git/blob - protocol/ps2.h
Merge pull request #81 from bgould/master
[tmk_keyboard.git] / protocol / ps2.h
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 #ifndef PS2_H
39 #define PS2_H
40
41 #include <stdbool.h>
42 #include <util/delay.h>
43 #include <avr/io.h>
44
45 /*
46 * Primitive PS/2 Library for AVR
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 PS2_ACK 0xFA
70 #define PS2_RESEND 0xFE
71 #define PS2_SET_LED 0xED
72
73 // TODO: error numbers
74 #define PS2_ERR_NONE 0
75 #define PS2_ERR_STARTBIT1 1
76 #define PS2_ERR_STARTBIT2 2
77 #define PS2_ERR_STARTBIT3 3
78 #define PS2_ERR_PARITY 0x10
79 #define PS2_ERR_NODATA 0x20
80
81 #define PS2_LED_SCROLL_LOCK 0
82 #define PS2_LED_NUM_LOCK 1
83 #define PS2_LED_CAPS_LOCK 2
84
85
86 extern uint8_t ps2_error;
87
88 void ps2_host_init(void);
89 uint8_t ps2_host_send(uint8_t data);
90 uint8_t ps2_host_recv_response(void);
91 uint8_t ps2_host_recv(void);
92 void ps2_host_set_led(uint8_t usb_led);
93
94
95 /* Check port settings for clock and data line */
96 #if !(defined(PS2_CLOCK_PORT) && \
97 defined(PS2_CLOCK_PIN) && \
98 defined(PS2_CLOCK_DDR) && \
99 defined(PS2_CLOCK_BIT))
100 # error "PS/2 clock port setting is required in config.h"
101 #endif
102
103 #if !(defined(PS2_DATA_PORT) && \
104 defined(PS2_DATA_PIN) && \
105 defined(PS2_DATA_DDR) && \
106 defined(PS2_DATA_BIT))
107 # error "PS/2 data port setting is required in config.h"
108 #endif
109
110 /*--------------------------------------------------------------------
111 * static functions
112 *------------------------------------------------------------------*/
113 static inline void clock_lo(void)
114 {
115 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
116 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
117 }
118 static inline void clock_hi(void)
119 {
120 /* input with pull up */
121 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
122 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
123 }
124 static inline bool clock_in(void)
125 {
126 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
127 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
128 _delay_us(1);
129 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
130 }
131 static inline void data_lo(void)
132 {
133 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
134 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
135 }
136 static inline void data_hi(void)
137 {
138 /* input with pull up */
139 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
140 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
141 }
142 static inline bool data_in(void)
143 {
144 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
145 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
146 _delay_us(1);
147 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
148 }
149
150 static inline uint16_t wait_clock_lo(uint16_t us)
151 {
152 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
153 return us;
154 }
155 static inline uint16_t wait_clock_hi(uint16_t us)
156 {
157 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
158 return us;
159 }
160 static inline uint16_t wait_data_lo(uint16_t us)
161 {
162 while (data_in() && us) { asm(""); _delay_us(1); us--; }
163 return us;
164 }
165 static inline uint16_t wait_data_hi(uint16_t us)
166 {
167 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
168 return us;
169 }
170
171 /* idle state that device can send */
172 static inline void idle(void)
173 {
174 clock_hi();
175 data_hi();
176 }
177
178 /* inhibit device to send */
179 static inline void inhibit(void)
180 {
181 clock_lo();
182 data_hi();
183 }
184
185 #endif
Imprint / Impressum