2 Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
5 #include <util/delay.h>
7 #include "ring_buffer.h"
11 #define WAIT(stat, us, err) do { \
12 if (!wait_##stat(us)) { \
13 ibm4704_error = err; \
19 uint8_t ibm4704_error = 0;
22 void ibm4704_init(void)
24 inhibit(); // keep keyboard from sending
27 idle(); // allow keyboard sending
33 Data bits are LSB first and Parity is odd. Clock has around 60us high and 30us low part.
35 ____ __ __ __ __ __ __ __ __ __ ________
36 Clock \______/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
37 ^ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ___
38 Data ____|__/ X____X____X____X____X____X____X____X____X____X \___
39 | Start 0 1 2 3 4 5 6 7 P Stop
42 Start bit: can be long as 300-350us.
43 Request: Host pulls Clock line down to request to send a command.
44 Timing: After Request keyboard pull up Data and down Clock line to low for start bit.
45 After request host release Clock line once Data line becomes hi.
46 Host writes a bit while Clock is hi and Keyboard reads while low.
47 Stop bit: Host releases or pulls up Data line to hi after 9th clock and waits for keyboard pull down the line to lo.
49 uint8_t ibm4704_send(uint8_t data)
51 bool parity = true; // odd parity
60 /* wait for Start bit(Clock:lo/Data:hi) */
61 WAIT(data_hi, 300, 0x30);
64 for (uint8_t i = 0; i < 8; i++) {
65 WAIT(clock_hi, 100, 0x40+i);
72 WAIT(clock_lo, 100, 0x48+i);
76 WAIT(clock_hi, 100, 0x34);
77 if (parity) { data_hi(); } else { data_lo(); }
78 WAIT(clock_lo, 100, 0x35);
81 WAIT(clock_hi, 100, 0x34);
85 WAIT(data_lo, 100, 0x36);
92 if (ibm4704_error > 0x30) {
93 xprintf("S:%02X ", ibm4704_error);
99 /* wait forever to receive data */
100 uint8_t ibm4704_recv_response(void)
102 while (!rbuf_has_data()) {
105 return rbuf_dequeue();
108 uint8_t ibm4704_recv(void)
110 if (rbuf_has_data()) {
111 return rbuf_dequeue();
120 Data bits are LSB first and Parity is odd. Clock has around 60us high and 30us low part.
122 ____ __ __ __ __ __ __ __ __ __ _______
123 Clock \_____/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
124 ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
125 Data ____/ X____X____X____X____X____X____X____X____X____X________
126 Start 0 1 2 3 4 5 6 7 P Stop
128 Start bit: can be long as 300-350us.
129 Inhibit: Pull Data line down to inhibit keyboard to send.
130 Timing: Host reads bit while Clock is hi.(rising edge)
131 Stop bit: Keyboard pulls down Data line to lo after 9th clock.
133 ISR(IBM4704_INT_VECT)
136 BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, PARITY, STOP
139 static uint8_t data = 0;
141 static uint8_t parity = false;
169 WAIT(data_lo, 100, state);
171 ibm4704_error = IBM4704_ERR_NONE;
180 ibm4704_error = state;
181 while (ibm4704_send(0xFE)) _delay_ms(1); // resend
182 xprintf("R:%02X%02X\n", state, data);