summaryrefslogtreecommitdiff
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.c62
1 files changed, 28 insertions, 34 deletions
diff --git a/tmk_core/protocol/ibm4704.c b/tmk_core/protocol/ibm4704.c
index 6a03cd4419..fd8fc4dbd5 100644
--- a/tmk_core/protocol/ibm4704.c
+++ b/tmk_core/protocol/ibm4704.c
@@ -7,24 +7,21 @@ Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
#include "ring_buffer.h"
#include "ibm4704.h"
-
-#define WAIT(stat, us, err) do { \
- if (!wait_##stat(us)) { \
- ibm4704_error = err; \
- goto ERROR; \
- } \
-} while (0)
-
+#define WAIT(stat, us, err) \
+ do { \
+ if (!wait_##stat(us)) { \
+ ibm4704_error = err; \
+ goto ERROR; \
+ } \
+ } while (0)
uint8_t ibm4704_error = 0;
-
-void ibm4704_init(void)
-{
+void ibm4704_init(void) {
inhibit(); // keep keyboard from sending
IBM4704_INT_INIT();
IBM4704_INT_ON();
- idle(); // allow keyboard sending
+ idle(); // allow keyboard sending
}
/*
@@ -46,9 +43,8 @@ Timing: After Request keyboard pull up Data and down Clock line to low for s
Host writes a bit while Clock is hi and Keyboard reads while low.
Stop bit: Host releases or pulls up Data line to hi after 9th clock and waits for keyboard pull down the line to lo.
*/
-uint8_t ibm4704_send(uint8_t data)
-{
- bool parity = true; // odd parity
+uint8_t ibm4704_send(uint8_t data) {
+ bool parity = true; // odd parity
ibm4704_error = 0;
IBM4704_INT_OFF();
@@ -62,19 +58,23 @@ uint8_t ibm4704_send(uint8_t data)
/* Data bit */
for (uint8_t i = 0; i < 8; i++) {
- WAIT(clock_hi, 100, 0x40+i);
- if (data&(1<<i)) {
+ WAIT(clock_hi, 100, 0x40 + i);
+ if (data & (1 << i)) {
parity = !parity;
data_hi();
} else {
data_lo();
}
- WAIT(clock_lo, 100, 0x48+i);
+ WAIT(clock_lo, 100, 0x48 + i);
}
/* Parity bit */
WAIT(clock_hi, 100, 0x34);
- if (parity) { data_hi(); } else { data_lo(); }
+ if (parity) {
+ data_hi();
+ } else {
+ data_lo();
+ }
WAIT(clock_lo, 100, 0x35);
/* Stop bit */
@@ -97,16 +97,14 @@ ERROR:
}
/* wait forever to receive data */
-uint8_t ibm4704_recv_response(void)
-{
+uint8_t ibm4704_recv_response(void) {
while (!rbuf_has_data()) {
_delay_ms(1);
}
return rbuf_dequeue();
}
-uint8_t ibm4704_recv(void)
-{
+uint8_t ibm4704_recv(void) {
if (rbuf_has_data()) {
return rbuf_dequeue();
} else {
@@ -121,7 +119,7 @@ Data bits are LSB first and Parity is odd. Clock has around 60us high and 30us l
____ __ __ __ __ __ __ __ __ __ _______
Clock \_____/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
- ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
+ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
Data ____/ X____X____X____X____X____X____X____X____X____X________
Start 0 1 2 3 4 5 6 7 P Stop
@@ -130,11 +128,8 @@ Inhibit: Pull Data line down to inhibit keyboard to send.
Timing: Host reads bit while Clock is hi.(rising edge)
Stop bit: Keyboard pulls down Data line to lo after 9th clock.
*/
-ISR(IBM4704_INT_VECT)
-{
- static enum {
- BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, PARITY, STOP
- } state = BIT0;
+ISR(IBM4704_INT_VECT) {
+ static enum { BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, PARITY, STOP } state = BIT0;
// LSB first
static uint8_t data = 0;
// Odd parity
@@ -161,8 +156,7 @@ ISR(IBM4704_INT_VECT)
if (data_in()) {
parity = !parity;
}
- if (!parity)
- goto ERROR;
+ if (!parity) goto ERROR;
break;
case STOP:
// Data:Low
@@ -178,11 +172,11 @@ ISR(IBM4704_INT_VECT)
goto RETURN;
ERROR:
ibm4704_error = state;
- while (ibm4704_send(0xFE)) _delay_ms(1); // resend
+ while (ibm4704_send(0xFE)) _delay_ms(1); // resend
xprintf("R:%02X%02X\n", state, data);
DONE:
- state = BIT0;
- data = 0;
+ state = BIT0;
+ data = 0;
parity = false;
RETURN:
return;