aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/ibm4704.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/protocol/ibm4704.c')
-rw-r--r--tmk_core/protocol/ibm4704.c185
1 files changed, 0 insertions, 185 deletions
diff --git a/tmk_core/protocol/ibm4704.c b/tmk_core/protocol/ibm4704.c
deleted file mode 100644
index a19443976..000000000
--- a/tmk_core/protocol/ibm4704.c
+++ /dev/null
@@ -1,185 +0,0 @@
1/*
2Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3*/
4#include <stdbool.h>
5#include <util/delay.h>
6#include "debug.h"
7#include "ring_buffer.h"
8#include "ibm4704.h"
9
10#define WAIT(stat, us, err) \
11 do { \
12 if (!wait_##stat(us)) { \
13 ibm4704_error = err; \
14 goto ERROR; \
15 } \
16 } while (0)
17
18uint8_t ibm4704_error = 0;
19
20void ibm4704_init(void) {
21 inhibit(); // keep keyboard from sending
22 IBM4704_INT_INIT();
23 IBM4704_INT_ON();
24 idle(); // allow keyboard sending
25}
26
27/*
28Host to Keyboard
29----------------
30Data bits are LSB first and Parity is odd. Clock has around 60us high and 30us low part.
31
32 ____ __ __ __ __ __ __ __ __ __ ________
33Clock \______/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
34 ^ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ___
35Data ____|__/ X____X____X____X____X____X____X____X____X____X \___
36 | Start 0 1 2 3 4 5 6 7 P Stop
37 Request by host
38
39Start bit: can be long as 300-350us.
40Request: Host pulls Clock line down to request to send a command.
41Timing: After Request keyboard pull up Data and down Clock line to low for start bit.
42 After request host release Clock line once Data line becomes hi.
43 Host writes a bit while Clock is hi and Keyboard reads while low.
44Stop bit: Host releases or pulls up Data line to hi after 9th clock and waits for keyboard pull down the line to lo.
45*/
46uint8_t ibm4704_send(uint8_t data) {
47 bool parity = true; // odd parity
48 ibm4704_error = 0;
49
50 IBM4704_INT_OFF();
51
52 /* Request to send */
53 idle();
54 clock_lo();
55
56 /* wait for Start bit(Clock:lo/Data:hi) */
57 WAIT(data_hi, 300, 0x30);
58
59 /* Data bit */
60 for (uint8_t i = 0; i < 8; i++) {
61 WAIT(clock_hi, 100, 0x40 + i);
62 if (data & (1 << i)) {
63 parity = !parity;
64 data_hi();
65 } else {
66 data_lo();
67 }
68 WAIT(clock_lo, 100, 0x48 + i);
69 }
70
71 /* Parity bit */
72 WAIT(clock_hi, 100, 0x34);
73 if (parity) {
74 data_hi();
75 } else {
76 data_lo();
77 }
78 WAIT(clock_lo, 100, 0x35);
79
80 /* Stop bit */
81 WAIT(clock_hi, 100, 0x34);
82 data_hi();
83
84 /* End */
85 WAIT(data_lo, 100, 0x36);
86
87 idle();
88 IBM4704_INT_ON();
89 return 0;
90ERROR:
91 idle();
92 if (ibm4704_error > 0x30) {
93 xprintf("S:%02X ", ibm4704_error);
94 }
95 IBM4704_INT_ON();
96 return -1;
97}
98
99/* wait forever to receive data */
100uint8_t ibm4704_recv_response(void) {
101 while (!rbuf_has_data()) {
102 _delay_ms(1);
103 }
104 return rbuf_dequeue();
105}
106
107uint8_t ibm4704_recv(void) {
108 if (rbuf_has_data()) {
109 return rbuf_dequeue();
110 } else {
111 return -1;
112 }
113}
114
115/*
116Keyboard to Host
117----------------
118Data bits are LSB first and Parity is odd. Clock has around 60us high and 30us low part.
119
120 ____ __ __ __ __ __ __ __ __ __ _______
121Clock \_____/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
122 ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
123Data ____/ X____X____X____X____X____X____X____X____X____X________
124 Start 0 1 2 3 4 5 6 7 P Stop
125
126Start bit: can be long as 300-350us.
127Inhibit: Pull Data line down to inhibit keyboard to send.
128Timing: Host reads bit while Clock is hi.(rising edge)
129Stop bit: Keyboard pulls down Data line to lo after 9th clock.
130*/
131ISR(IBM4704_INT_VECT) {
132 static enum { BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, PARITY, STOP } state = BIT0;
133 // LSB first
134 static uint8_t data = 0;
135 // Odd parity
136 static uint8_t parity = false;
137
138 ibm4704_error = 0;
139
140 switch (state) {
141 case BIT0:
142 case BIT1:
143 case BIT2:
144 case BIT3:
145 case BIT4:
146 case BIT5:
147 case BIT6:
148 case BIT7:
149 data >>= 1;
150 if (data_in()) {
151 data |= 0x80;
152 parity = !parity;
153 }
154 break;
155 case PARITY:
156 if (data_in()) {
157 parity = !parity;
158 }
159 if (!parity) goto ERROR;
160 break;
161 case STOP:
162 // Data:Low
163 WAIT(data_lo, 100, state);
164 if (!rbuf_enqueue(data)) {
165 print("rbuf: full\n");
166 }
167 ibm4704_error = IBM4704_ERR_NONE;
168 goto DONE;
169 break;
170 default:
171 goto ERROR;
172 }
173 state++;
174 goto RETURN;
175ERROR:
176 ibm4704_error = state;
177 while (ibm4704_send(0xFE)) _delay_ms(1); // resend
178 xprintf("R:%02X%02X\n", state, data);
179DONE:
180 state = BIT0;
181 data = 0;
182 parity = false;
183RETURN:
184 return;
185}