aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/ps2_busywait.c
diff options
context:
space:
mode:
authortmk <hasu@tmk-kbd.com>2015-04-10 01:32:04 +0900
committertmk <hasu@tmk-kbd.com>2015-04-10 01:32:04 +0900
commit1a02ebcc612e9a9c0d87e02295c7258de3a70ccc (patch)
treee517f3c70bb2d542797e57d13e9023c84af230fb /tmk_core/protocol/ps2_busywait.c
parent6746e37088ce8ba03529c1226bd216705edb2b1f (diff)
parenta074364c3731d66b56d988c8a6c960a83ea0e0a1 (diff)
downloadqmk_firmware-1a02ebcc612e9a9c0d87e02295c7258de3a70ccc.tar.gz
qmk_firmware-1a02ebcc612e9a9c0d87e02295c7258de3a70ccc.zip
Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core'
Diffstat (limited to 'tmk_core/protocol/ps2_busywait.c')
-rw-r--r--tmk_core/protocol/ps2_busywait.c189
1 files changed, 189 insertions, 0 deletions
diff --git a/tmk_core/protocol/ps2_busywait.c b/tmk_core/protocol/ps2_busywait.c
new file mode 100644
index 000000000..a64933219
--- /dev/null
+++ b/tmk_core/protocol/ps2_busywait.c
@@ -0,0 +1,189 @@
1/*
2Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3
4This software is licensed with a Modified BSD License.
5All of this is supposed to be Free Software, Open Source, DFSG-free,
6GPL-compatible, and OK to use in both free and proprietary applications.
7Additions and corrections to this file are welcome.
8
9
10Redistribution and use in source and binary forms, with or without
11modification, 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
25THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35POSSIBILITY OF SUCH DAMAGE.
36*/
37
38/*
39 * PS/2 protocol busywait version
40 */
41
42#include <stdbool.h>
43#include "wait.h"
44#include "ps2.h"
45#include "ps2_io.h"
46#include "debug.h"
47
48
49#define WAIT(stat, us, err) do { \
50 if (!wait_##stat(us)) { \
51 ps2_error = err; \
52 goto ERROR; \
53 } \
54} while (0)
55
56
57uint8_t ps2_error = PS2_ERR_NONE;
58
59
60void ps2_host_init(void)
61{
62 clock_init();
63 data_init();
64
65 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
66 wait_ms(2500);
67
68 inhibit();
69}
70
71uint8_t ps2_host_send(uint8_t data)
72{
73 bool parity = true;
74 ps2_error = PS2_ERR_NONE;
75
76 /* terminate a transmission if we have */
77 inhibit();
78 wait_us(100); // 100us [4]p.13, [5]p.50
79
80 /* 'Request to Send' and Start bit */
81 data_lo();
82 clock_hi();
83 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
84
85 /* Data bit */
86 for (uint8_t i = 0; i < 8; i++) {
87 wait_us(15);
88 if (data&(1<<i)) {
89 parity = !parity;
90 data_hi();
91 } else {
92 data_lo();
93 }
94 WAIT(clock_hi, 50, 2);
95 WAIT(clock_lo, 50, 3);
96 }
97
98 /* Parity bit */
99 wait_us(15);
100 if (parity) { data_hi(); } else { data_lo(); }
101 WAIT(clock_hi, 50, 4);
102 WAIT(clock_lo, 50, 5);
103
104 /* Stop bit */
105 wait_us(15);
106 data_hi();
107
108 /* Ack */
109 WAIT(data_lo, 50, 6);
110 WAIT(clock_lo, 50, 7);
111
112 /* wait for idle state */
113 WAIT(clock_hi, 50, 8);
114 WAIT(data_hi, 50, 9);
115
116 inhibit();
117 return ps2_host_recv_response();
118ERROR:
119 inhibit();
120 return 0;
121}
122
123/* receive data when host want else inhibit communication */
124uint8_t ps2_host_recv_response(void)
125{
126 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
127 // 250 * 100us(wait for start bit in ps2_host_recv)
128 uint8_t data = 0;
129 uint8_t try = 250;
130 do {
131 data = ps2_host_recv();
132 } while (try-- && ps2_error);
133 return data;
134}
135
136/* called after start bit comes */
137uint8_t ps2_host_recv(void)
138{
139 uint8_t data = 0;
140 bool parity = true;
141 ps2_error = PS2_ERR_NONE;
142
143 /* release lines(idle state) */
144 idle();
145
146 /* start bit [1] */
147 WAIT(clock_lo, 100, 1); // TODO: this is enough?
148 WAIT(data_lo, 1, 2);
149 WAIT(clock_hi, 50, 3);
150
151 /* data [2-9] */
152 for (uint8_t i = 0; i < 8; i++) {
153 WAIT(clock_lo, 50, 4);
154 if (data_in()) {
155 parity = !parity;
156 data |= (1<<i);
157 }
158 WAIT(clock_hi, 50, 5);
159 }
160
161 /* parity [10] */
162 WAIT(clock_lo, 50, 6);
163 if (data_in() != parity) {
164 ps2_error = PS2_ERR_PARITY;
165 goto ERROR;
166 }
167 WAIT(clock_hi, 50, 7);
168
169 /* stop bit [11] */
170 WAIT(clock_lo, 50, 8);
171 WAIT(data_hi, 1, 9);
172 WAIT(clock_hi, 50, 10);
173
174 inhibit();
175 return data;
176ERROR:
177 if (ps2_error > PS2_ERR_STARTBIT3) {
178 xprintf("x%02X\n", ps2_error);
179 }
180 inhibit();
181 return 0;
182}
183
184/* send LED state to keyboard */
185void ps2_host_set_led(uint8_t led)
186{
187 ps2_host_send(0xED);
188 ps2_host_send(led);
189}