#include <stdbool.h>
#include <avr/interrupt.h>
-#include "ringbuf.h"
+#include <util/atomic.h>
#include "ibmpc.h"
#include "debug.h"
#include "timer.h"
} while (0)
-#define BUF_SIZE 16
-static uint8_t buf[BUF_SIZE];
-static ringbuf_t rb = {
- .buffer = buf,
- .head = 0,
- .tail = 0,
- .size_mask = BUF_SIZE - 1
-};
-
volatile uint8_t ibmpc_protocol = IBMPC_PROTOCOL_AT;
volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
+/* 2-byte buffer for data received from keyhboard
+ * buffer states:
+ * FFFF: empty
+ * FFss: one data
+ * sstt: two data(full)
+ * 0xFF can not be stored as data in buffer because it means empty or no data.
+ */
+static volatile uint16_t recv_data = 0xFFFF;
+/* internal state of receiving data */
+static volatile uint16_t isr_data = 0x8000;
+
void ibmpc_host_init(void)
{
- // initialize reset pin
- IBMPC_RST_PORT |= (1<<IBMPC_RST_BIT1);
- IBMPC_RST_DDR |= (1<<IBMPC_RST_BIT1);
- IBMPC_RST_PORT |= (1<<IBMPC_RST_BIT2);
- IBMPC_RST_DDR |= (1<<IBMPC_RST_BIT2);
+ // initialize reset pin to HiZ
+ IBMPC_RST_HIZ();
inhibit();
IBMPC_INT_INIT();
IBMPC_INT_OFF();
void ibmpc_host_disable(void)
{
- inhibit();
IBMPC_INT_OFF();
+ inhibit();
}
int16_t ibmpc_host_send(uint8_t data)
WAIT(clock_hi, 50, 8);
WAIT(data_hi, 50, 9);
+ // clear buffer to get response correctly
+ recv_data = 0xFFFF;
+ ibmpc_host_isr_clear();
+
idle();
IBMPC_INT_ON();
return ibmpc_host_recv_response();
return -1;
}
+/*
+ * Receive data from keyboard
+ */
+int16_t ibmpc_host_recv(void)
+{
+ uint16_t data = 0;
+ uint8_t ret = 0xFF;
+
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ data = recv_data;
+ if ((data&0xFF00) != 0xFF00) { // recv_data:sstt -> recv_data:FFtt, ret:ss
+ ret = (data>>8)&0x00FF;
+ recv_data = data | 0xFF00;
+ } else if (data != 0xFFFF) { // recv_data:FFss -> recv_data:FFFF, ret:ss
+ ret = data&0x00FF;
+ recv_data = data | 0x00FF;
+ }
+ }
+
+ if (ret != 0xFF) dprintf("r%02X ", ret);
+ if (recv_data != 0xFFFF) dprintf("b%04X ", recv_data);
+ return ((ret != 0xFF) ? ret : -1);
+
+}
+
int16_t ibmpc_host_recv_response(void)
{
// Command may take 25ms/20ms at most([5]p.46, [3]p.21)
uint8_t retry = 25;
- while (retry-- && ringbuf_is_empty(&rb)) {
+ int16_t data = -1;
+ while (retry-- && (data = ibmpc_host_recv()) == -1) {
wait_ms(1);
}
- int16_t data = ringbuf_get(&rb);
- if (data != -1) dprintf("r%02X ", data);
return data;
}
-/* get data received by interrupt */
-int16_t ibmpc_host_recv(void)
+void ibmpc_host_isr_clear(void)
{
- int16_t data = ringbuf_get(&rb);
- if (data != -1) dprintf("r%02X ", data);
- return data;
+ isr_data = 0x8000;
+ recv_data = 0xFFFF;
}
+// NOTE: With this ISR data line can be read within 2us after clock falling edge.
+// To read data line early as possible:
+// write naked ISR with asembly code to read the line and call C func to do other job?
ISR(IBMPC_INT_VECT)
{
- static uint16_t last_time = 0;
- static enum {
- START,
- BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
- PARITY,
- STOP,
- } state = START;
- static uint8_t data = 0;
- static uint8_t parity = 1;
-
- uint8_t dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
-
- // Reset state when taking more than 1ms
- if (last_time && timer_elapsed(last_time) > 10) {
- ibmpc_error = IBMPC_ERR_TIMEOUT | IBMPC_ERR_RECV | state;
- state = START;
- data = 0;
- parity = 1;
- }
- last_time = timer_read();
-
- switch (state) {
- case START:
- if (ibmpc_protocol == IBMPC_PROTOCOL_XT) {
- // ignore start(0) bit
- if (!dbit) return;
- } else {
- if (dbit)
- goto ERROR;
- }
+ uint8_t dbit;
+ dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
+PINB|=0x01;
+ isr_data = isr_data>>1;
+ if (dbit) isr_data |= 0x8000;
+
+ // isr_data: state of receiving data from keyboard
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+ // -----------------------------------------------------
+ // Initial: *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 MSB sentinel
+ // XT IBM: b7 b6 b5 b4 b3 b2 b1 b0 | s1 s0 *1 0 0 0 0 0 after receiving **
+ // XT Clone: b7 b6 b5 b4 b3 b2 b1 b0 | s1 *1 0 0 0 0 0 0 after receiving
+ // AT: st pr b7 b6 b5 b4 b3 b2 | b1 b0 s0 *1 0 0 0 0 after receiving
+ // AT**: pr b7 b6 b5 b4 b3 b2 b1 | b0 s0 *1 0 0 0 0 0 before stop bit **
+ //
+ // x x x x x x x x | 0 0 0 0 0 0 0 0 midway(0-7 bits received)
+ // x x x x x x x x | 1 0 0 0 0 0 0 0 midway(8 bits received)
+ // x x x x x x x x | 0 1 0 0 0 0 0 0 XT IBM-midway or AT-midway
+ // x x x x x x x x | 1 1 0 0 0 0 0 0 XT Clone-done
+ // x x x x x x x x | 0 0 1 0 0 0 0 0 AT-midway
+ // x x x x x x x x | 1 0 1 0 0 0 0 0 XT IBM-done or AT-midway **
+ // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
+ // x x x x x x x x | x x 0 1 0 0 0 0 AT-done
+ // x x x x x x x x | x x 1 1 0 0 0 0 illegal
+ // other states than avobe illegal
+ //
+ // **: AT can take same as end sate of XT IBM(1010 000) when b0 is 1,
+ // to discriminate between them we will have to wait a while for stop bit.
+ //
+ // mask for isr_data:
+ // 0x00A0(1010 0000) when XT IBM
+ // 0x00C0(1100 0000) when XT Clone
+ // 0x0010(xx01 0000) when AT
+ //
+ switch (isr_data & 0xFF) {
+ case 0b00000000:
+ case 0b10000000:
+ case 0b01000000:
+ case 0b00100000:
+ // midway
+ return;
break;
- case BIT0:
- case BIT1:
- case BIT2:
- case BIT3:
- case BIT4:
- case BIT5:
- case BIT6:
- case BIT7:
- data >>= 1;
- if (dbit) {
- data |= 0x80;
- parity++;
- }
- if (state == BIT7 && ibmpc_protocol == IBMPC_PROTOCOL_XT) {
- if (!ringbuf_put(&rb, data)) {
- ibmpc_error = IBMPC_ERR_FULL;
- goto ERROR;
- }
- ibmpc_error = IBMPC_ERR_NONE;
- goto DONE;
- }
+ case 0b11000000:
+ // XT Clone-done
+ recv_data = recv_data<<8;
+ recv_data |= (isr_data>>8) & 0xFF;
+ goto DONE;
break;
- case PARITY:
- if (dbit) {
- if (!(parity & 0x01))
- goto ERROR;
- } else {
- if (parity & 0x01)
- goto ERROR;
- }
+ case 0b10100000:
+ {
+ uint8_t us = 100;
+ // wait for rising and falling edge of AT stop bit
+ while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
+ while ( IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT) && us) { wait_us(1); us--; }
+
+ if (us) {
+ // found stop bit: return immediately and process the stop bit in ISR
+ // AT-midway
+ return;
+ } else {
+ // no stop bit
+ // XT-IBM-done
+ recv_data = recv_data<<8;
+ recv_data |= (isr_data>>8) & 0xFF;
+ goto DONE;
+ }
+ }
break;
- case STOP:
- if (!dbit)
- goto ERROR;
- if (!ringbuf_put(&rb, data)) {
- ibmpc_error = IBMPC_ERR_FULL;
- goto ERROR;
- }
- ibmpc_error = IBMPC_ERR_NONE;
+ case 0b00010000:
+ case 0b10010000:
+ case 0b01010000:
+ case 0b11010000:
+ // AT-done
+ recv_data = recv_data<<8;
+ recv_data |= (isr_data>>6) & 0xFF;
goto DONE;
break;
- default:
- goto ERROR;
+ case 0b01100000:
+ case 0b11100000:
+ case 0b00110000:
+ case 0b10110000:
+ case 0b01110000:
+ case 0b11110000:
+ default: // xxxx_oooo(any 1 in low nibble)
+ recv_data = isr_data;
+ break;
}
- goto NEXT;
-
-ERROR:
- ibmpc_error |= state;
- ibmpc_error |= IBMPC_ERR_RECV;
- ringbuf_reset(&rb);
DONE:
- last_time = 0;
- state = START;
- data = 0;
- parity = 1;
- return;
-NEXT:
- state++;
+ // TODO: check protocol change to support keyboard howswap
+ // not correct if there is clock edge within short time like 100us after receving data
+ // TODO: process error code: 0x00(AT), 0xFF(XT) in particular
+ isr_data = 0x8000; // clear to next data
return;
}