nuclear@2: #include nuclear@2: #include nuclear@2: #include nuclear@2: #include "ps2kbd.h" nuclear@2: #include "defs.h" nuclear@2: nuclear@2: #define BUF_SZ 16 nuclear@2: #define BUF_IDX_MASK (BUF_SZ - 1) nuclear@2: #define NEXT_IDX(x) (((x) + 1) & BUF_IDX_MASK) nuclear@2: static volatile unsigned char keybuf[BUF_SZ]; nuclear@2: static volatile unsigned char key_rd, key_wr; nuclear@2: nuclear@2: static volatile int sending; nuclear@2: nuclear@2: void ps2write(unsigned char c) nuclear@2: { nuclear@2: #if 0 nuclear@2: int i; nuclear@2: unsigned short out = (unsigned short)c | ((unsigned short)(parity_even_bit(c) & 1) << 8); nuclear@2: nuclear@2: /* pull clock low for >60us */ nuclear@2: PS2LOW(BIT_CLK); nuclear@2: _delay_us(60); nuclear@2: nuclear@2: /* then pull data low and release clock */ nuclear@2: PS2LOW(BIT_DATA); nuclear@2: PS2REL(BIT_CLK); nuclear@2: nuclear@2: for(i=0; i<9; i++) { nuclear@2: while(PINB & BIT_CLK); /* wait for kbd to drive the clock low */ nuclear@2: PORTB |= out & 1; nuclear@2: out >>= 1; nuclear@2: while(!(PINB & BIT_CLK)); /* wait for kbd to drive the clock high */ nuclear@2: } nuclear@2: nuclear@2: PS2REL(BIT_DATA); nuclear@2: /* wait for ack */ nuclear@2: while(PINB & BIT_DATA); nuclear@2: while(!(PINB & BIT_DATA)); nuclear@2: #endif nuclear@2: } nuclear@2: nuclear@2: unsigned char ps2read(void) nuclear@2: { nuclear@2: unsigned char key; nuclear@2: nuclear@2: while(key_rd == key_wr) { nuclear@2: } nuclear@2: nuclear@2: cli(); nuclear@2: key = keybuf[key_rd]; nuclear@2: key_rd = NEXT_IDX(key_rd); nuclear@2: sei(); nuclear@2: nuclear@2: return key; nuclear@2: } nuclear@2: nuclear@2: int ps2pending(void) nuclear@2: { nuclear@2: return key_rd != key_wr; nuclear@2: } nuclear@2: nuclear@2: ISR(INT0_vect) nuclear@2: { nuclear@2: static unsigned char value, parity; nuclear@2: /*static unsigned char valp;*/ nuclear@2: static int nbits; nuclear@2: nuclear@2: if(sending) { nuclear@2: } else { nuclear@2: if(nbits > 0 && nbits < 9) { nuclear@2: value >>= 1; nuclear@2: if(PIND & PDATA_BIT) { nuclear@2: value |= 0x80; nuclear@2: parity ^= 1; nuclear@2: } nuclear@2: }/* else if(nbits == 9) { nuclear@2: valp = (PIND >> PDATA) & 1; nuclear@2: }*/ nuclear@2: if(++nbits >= 11) { nuclear@2: nbits = 0; nuclear@2: nuclear@2: /* check parity */ nuclear@2: /*if((parity & 1) == (valp & 1)) {}*/ nuclear@2: keybuf[key_wr] = (unsigned char)value; nuclear@2: key_wr = NEXT_IDX(key_wr); nuclear@2: nuclear@2: value = 0; nuclear@2: parity = 0; nuclear@2: } nuclear@2: } nuclear@2: }