*/ | */ | ||||
#define SERIAL_BAUD 19200 | #define SERIAL_BAUD 19200 | ||||
#define SERIAL_PARITY_ODD | #define SERIAL_PARITY_ODD | ||||
#define SERIAL_BIT_ORDER_MSB | |||||
#define SERIAL_BIT_ORDER_LSB | |||||
#define SERIAL_RXD_DDR DDRD | #define SERIAL_RXD_DDR DDRD | ||||
#define SERIAL_RXD_PORT PORTD | #define SERIAL_RXD_PORT PORTD | ||||
SERIAL_RXD_DDR &= ~(1<<SERIAL_RXD_BIT); \ | SERIAL_RXD_DDR &= ~(1<<SERIAL_RXD_BIT); \ | ||||
SERIAL_RXD_PORT |= (1<<SERIAL_RXD_BIT); \ | SERIAL_RXD_PORT |= (1<<SERIAL_RXD_BIT); \ | ||||
/* enable interrupt: INT2(rising edge) */ \ | /* enable interrupt: INT2(rising edge) */ \ | ||||
EICRA |= ((1<<ISC21)|(1<<ISC20)); \ | |||||
/*EICRA |= ((1<<ISC21)|(1<<ISC20));*/ \ | |||||
/* enable interrupt: INT2(falling edge) */ \ | |||||
EICRA |= ((1<<ISC21)|(0<<ISC20)); \ | |||||
EIMSK |= (1<<INT2); \ | EIMSK |= (1<<INT2); \ | ||||
} while (0) | } while (0) | ||||
#define SERIAL_RXD_INT_ENTER() | #define SERIAL_RXD_INT_ENTER() | ||||
/* clear interrupt flag */ \ | /* clear interrupt flag */ \ | ||||
EIFR = (1<<INTF2); \ | EIFR = (1<<INTF2); \ | ||||
} while (0) | } while (0) | ||||
#define SERIAL_RXD_READ() (~SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT)) | |||||
//#define SERIAL_RXD_READ() (~SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT)) | |||||
#define SERIAL_RXD_READ() ((SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT))) | |||||
#define SERIAL_TXD_DDR DDRD | #define SERIAL_TXD_DDR DDRD | ||||
#define SERIAL_TXD_PORT PORTD | #define SERIAL_TXD_PORT PORTD |
PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||||
PC98_RTY_PORT |= (1<<PC98_RTY_BIT); | PC98_RTY_PORT |= (1<<PC98_RTY_BIT); | ||||
DDRD |= 1<<7; | |||||
serial_init(); | serial_init(); | ||||
_delay_us(13); | _delay_us(13); | ||||
PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||||
// PC98 ready | |||||
PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | |||||
// initialize matrix state: all keys off | // initialize matrix state: all keys off | ||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; | for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; | ||||
uint8_t code; | uint8_t code; | ||||
PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||||
_delay_us(30); | |||||
code = serial_recv(); | code = serial_recv(); | ||||
PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||||
if (!code) return 0; | if (!code) return 0; |
#define WAIT_US (1000000/SERIAL_BAUD) | #define WAIT_US (1000000/SERIAL_BAUD) | ||||
#if 1 | |||||
#define WAIT_TICK (1000000/SERIAL_BAUD) | |||||
#define WAIT4(tick) _delay_us(tick) | |||||
#else | |||||
#define WAIT_TICK ((16000000/SERIAL_BAUD)/4 - 5) | |||||
static inline void WAIT4(uint8_t tick) | |||||
{ | |||||
__asm__ __volatile__ ( | |||||
"1: dec %0" "\n\t" | |||||
"nop" "\n\t" | |||||
"brne 1b" | |||||
: | |||||
: "r" (tick) | |||||
); | |||||
} | |||||
#endif | |||||
void serial_init(void) | void serial_init(void) | ||||
{ | { | ||||
SERIAL_RXD_INIT(); | SERIAL_RXD_INIT(); | ||||
static uint8_t rbuf_head = 0; | static uint8_t rbuf_head = 0; | ||||
static uint8_t rbuf_tail = 0; | static uint8_t rbuf_tail = 0; | ||||
uint8_t serial_recv(void) | uint8_t serial_recv(void) | ||||
{ | { | ||||
uint8_t data = 0; | uint8_t data = 0; | ||||
/* detect edge of start bit */ | /* detect edge of start bit */ | ||||
ISR(SERIAL_RXD_VECT) | ISR(SERIAL_RXD_VECT) | ||||
{ | { | ||||
PORTD ^= 1<<7; | |||||
SERIAL_RXD_INT_ENTER() | SERIAL_RXD_INT_ENTER() | ||||
uint8_t data = 0; | uint8_t data = 0; | ||||
#endif | #endif | ||||
/* to center of start bit */ | /* to center of start bit */ | ||||
_delay_us(WAIT_US/2); | |||||
//_delay_us(WAIT_US/2); | |||||
WAIT4(WAIT_TICK/2); | |||||
PORTD ^= 1<<7; | |||||
do { | do { | ||||
/* to center of next bit */ | /* to center of next bit */ | ||||
_delay_us(WAIT_US); | |||||
//_delay_us(WAIT_US); | |||||
WAIT4(WAIT_TICK); | |||||
PORTD ^= 1<<7; | |||||
if (SERIAL_RXD_READ()) { | if (SERIAL_RXD_READ()) { | ||||
data |= mask; | data |= mask; | ||||
parity ^= 1; | parity ^= 1; | ||||
} while (mask); | } while (mask); | ||||
/* to center of parity bit */ | /* to center of parity bit */ | ||||
_delay_us(WAIT_US); | |||||
parity ^= SERIAL_RXD_READ(); | |||||
//_delay_us(WAIT_US); | |||||
WAIT4(WAIT_TICK); | |||||
if (SERIAL_RXD_READ()) { parity ^= 1; } | |||||
PORTD ^= 1<<7; | |||||
/* to center of stop bit */ | /* to center of stop bit */ | ||||
_delay_us(WAIT_US); | |||||
_delay_us(WAIT_US/2); | |||||
//_delay_us(WAIT_US); | |||||
WAIT4(WAIT_TICK); | |||||
parity = 1; | |||||
uint8_t next = (rbuf_head + 1) % RBUF_SIZE; | uint8_t next = (rbuf_head + 1) % RBUF_SIZE; | ||||
if (parity && next != rbuf_tail) { | |||||
//if (parity && next != rbuf_tail) { | |||||
if (next != rbuf_tail) { | |||||
rbuf[rbuf_head] = data; | rbuf[rbuf_head] = data; | ||||
rbuf_head = next; | rbuf_head = next; | ||||
} | } | ||||
SERIAL_RXD_INT_EXIT(); | SERIAL_RXD_INT_EXIT(); | ||||
PORTD ^= 1<<7; | |||||
} | } |