ibmpc: Change ISR code

It reads data line within 3us
This commit is contained in:
tmk 2020-01-23 23:36:52 +09:00
parent e7d6d24c17
commit c2e8c0d43e

View file

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <stdbool.h> #include <stdbool.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/atomic.h>
#include "ringbuf.h" #include "ringbuf.h"
#include "ibmpc.h" #include "ibmpc.h"
#include "debug.h" #include "debug.h"
@ -88,9 +89,8 @@ void ibmpc_host_enable(void)
void ibmpc_host_disable(void) void ibmpc_host_disable(void)
{ {
// TODO: test order? uneeded interrupt happens by making clock lo
inhibit();
IBMPC_INT_OFF(); IBMPC_INT_OFF();
inhibit();
} }
int16_t ibmpc_host_send(uint8_t data) int16_t ibmpc_host_send(uint8_t data)
@ -158,128 +158,123 @@ ERROR:
return -1; return -1;
} }
/*
* Receive data from keyboard with ISR
*/
static volatile int16_t recv_data = -1;
static volatile uint16_t isr_data = 0x8000;
void ibmpc_host_isr_clear(void)
{
isr_data = 0x8000;
}
int16_t ibmpc_host_recv(void)
{
int16_t data = 0;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
data = recv_data;
recv_data = -1;
}
if (data != -1) {
dprintf("r%04X ", data);
}
return data;
}
int16_t ibmpc_host_recv_response(void) int16_t ibmpc_host_recv_response(void)
{ {
// Command may take 25ms/20ms at most([5]p.46, [3]p.21) // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
uint8_t retry = 25; uint8_t retry = 25;
while (retry-- && ringbuf_is_empty(&rb)) { int16_t data = -1;
while (retry-- && (data = ibmpc_host_recv()) == -1) {
wait_ms(1); wait_ms(1);
} }
int16_t data = ringbuf_get(&rb);
if (data != -1) dprintf("r%02X ", data);
return data; return data;
} }
/* get data received by interrupt */
int16_t ibmpc_host_recv(void)
{
int16_t data = ringbuf_get(&rb);
if (data != -1) dprintf("r%02X ", data);
return data;
}
/*
* Receive data from keyboard with ISR
*/
static enum {
START,
BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
PARITY,
STOP, } isr_state = START;
static uint8_t isr_data = 0;
static uint8_t isr_parity = 1;
static uint16_t isr_time = 0;
void ibmpc_host_isr_clear(void)
{
isr_state = START;
isr_data = 0;
isr_parity = 1;
isr_time = 0;
}
ISR(IBMPC_INT_VECT) ISR(IBMPC_INT_VECT)
{ {
uint8_t dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT); uint8_t dbit;
dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
isr_data = isr_data>>1;
if (dbit) isr_data |= 0x8000;
// Reset state when taking more than 1ms // isr_data:
if (isr_time && timer_elapsed(isr_time) > 1) { // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ibmpc_error = IBMPC_ERR_TIMEOUT | IBMPC_ERR_RECV | isr_state; // -----------------------------------------------------
isr_state = START; // Initial: *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0
isr_data = 0; // XT IBM: b7 b6 b5 b4 b3 b2 b1 b0 | s1 s0 *1 0 0 0 0 0 after receiving **
isr_parity = 1; // 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
isr_time = timer_read(); // AT**: pr b7 b6 b5 b4 b3 b2 b1 | b0 s0 *1 0 0 0 0 0 before stop bit **
//
switch (isr_state) { // x x x x x x x x | 0 0 0 0 0 0 0 0 midway(0-7 bits received)
case START: // x x x x x x x x | 1 0 0 0 0 0 0 0 midway(8 bits received)
if (ibmpc_protocol == IBMPC_PROTOCOL_XT) { // x x x x x x x x | 0 1 0 0 0 0 0 0 XT IBM-midway or AT-midway
// ignore start(0) bit // x x x x x x x x | 1 1 0 0 0 0 0 0 XT Clone-done
if (!dbit) return; // x x x x x x x x | 0 0 1 0 0 0 0 0 AT-midway
} else { // x x x x x x x x | 1 0 1 0 0 0 0 0 XT IBM-done or AT-midway **
if (dbit) // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
goto ERROR; // 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; break;
case BIT0: case 0b11000000:
case BIT1: // XT Clone-done
case BIT2: recv_data = (isr_data>>8) & 0xFF;
case BIT3: goto DONE;
case BIT4: break;
case BIT5: case 0b10100000:
case BIT6: // XT IBM-done or AT-midway
case BIT7: // wait and check for clock of AT stop bit
isr_data >>= 1; if (wait_clock_hi(100) && wait_clock_lo(100)) { // FIXME this makes ISR prologe long
if (dbit) { // AT-midway
isr_data |= 0x80; return;
isr_parity++; } else {
} // XT-IBM-done
if (isr_state == BIT7 && ibmpc_protocol == IBMPC_PROTOCOL_XT) { recv_data = (isr_data>>8) & 0xFF;
if (!ringbuf_put(&rb, isr_data)) {
ibmpc_error = IBMPC_ERR_FULL;
goto ERROR;
}
ibmpc_error = IBMPC_ERR_NONE;
goto DONE; goto DONE;
} }
break; break;
case PARITY: case 0b00010000:
if (dbit) { case 0b10010000:
if (!(isr_parity & 0x01)) case 0b01010000:
goto ERROR; case 0b11010000:
} else { // AT-done
if (isr_parity & 0x01) recv_data = (isr_data>>6) & 0xFF;
goto ERROR;
}
break;
case STOP:
if (!dbit)
goto ERROR;
if (!ringbuf_put(&rb, isr_data)) {
ibmpc_error = IBMPC_ERR_FULL;
goto ERROR;
}
ibmpc_error = IBMPC_ERR_NONE;
goto DONE; goto DONE;
break; break;
default: case 0b01100000:
goto ERROR; 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 |= isr_state;
ibmpc_error |= IBMPC_ERR_RECV;
ringbuf_reset(&rb);
DONE: DONE:
isr_state = START; // TODO: buffer for recv_data
isr_data = 0; isr_data = 0x8000; // clear to next data
isr_parity = 1;
isr_time = 0;
return;
NEXT:
isr_state++;
return; return;
} }