a500kbd
diff src/ps2kbd.c @ 2:a4fd9c5a6655
first working version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 17 Oct 2017 15:25:33 +0300 |
parents | |
children | 31a1f0b53d98 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ps2kbd.c Tue Oct 17 15:25:33 2017 +0300 1.3 @@ -0,0 +1,92 @@ 1.4 +#include <avr/io.h> 1.5 +#include <avr/interrupt.h> 1.6 +#include <util/delay.h> 1.7 +#include "ps2kbd.h" 1.8 +#include "defs.h" 1.9 + 1.10 +#define BUF_SZ 16 1.11 +#define BUF_IDX_MASK (BUF_SZ - 1) 1.12 +#define NEXT_IDX(x) (((x) + 1) & BUF_IDX_MASK) 1.13 +static volatile unsigned char keybuf[BUF_SZ]; 1.14 +static volatile unsigned char key_rd, key_wr; 1.15 + 1.16 +static volatile int sending; 1.17 + 1.18 +void ps2write(unsigned char c) 1.19 +{ 1.20 +#if 0 1.21 + int i; 1.22 + unsigned short out = (unsigned short)c | ((unsigned short)(parity_even_bit(c) & 1) << 8); 1.23 + 1.24 + /* pull clock low for >60us */ 1.25 + PS2LOW(BIT_CLK); 1.26 + _delay_us(60); 1.27 + 1.28 + /* then pull data low and release clock */ 1.29 + PS2LOW(BIT_DATA); 1.30 + PS2REL(BIT_CLK); 1.31 + 1.32 + for(i=0; i<9; i++) { 1.33 + while(PINB & BIT_CLK); /* wait for kbd to drive the clock low */ 1.34 + PORTB |= out & 1; 1.35 + out >>= 1; 1.36 + while(!(PINB & BIT_CLK)); /* wait for kbd to drive the clock high */ 1.37 + } 1.38 + 1.39 + PS2REL(BIT_DATA); 1.40 + /* wait for ack */ 1.41 + while(PINB & BIT_DATA); 1.42 + while(!(PINB & BIT_DATA)); 1.43 +#endif 1.44 +} 1.45 + 1.46 +unsigned char ps2read(void) 1.47 +{ 1.48 + unsigned char key; 1.49 + 1.50 + while(key_rd == key_wr) { 1.51 + } 1.52 + 1.53 + cli(); 1.54 + key = keybuf[key_rd]; 1.55 + key_rd = NEXT_IDX(key_rd); 1.56 + sei(); 1.57 + 1.58 + return key; 1.59 +} 1.60 + 1.61 +int ps2pending(void) 1.62 +{ 1.63 + return key_rd != key_wr; 1.64 +} 1.65 + 1.66 +ISR(INT0_vect) 1.67 +{ 1.68 + static unsigned char value, parity; 1.69 + /*static unsigned char valp;*/ 1.70 + static int nbits; 1.71 + 1.72 + if(sending) { 1.73 + } else { 1.74 + if(nbits > 0 && nbits < 9) { 1.75 + value >>= 1; 1.76 + if(PIND & PDATA_BIT) { 1.77 + value |= 0x80; 1.78 + parity ^= 1; 1.79 + } 1.80 + }/* else if(nbits == 9) { 1.81 + valp = (PIND >> PDATA) & 1; 1.82 + }*/ 1.83 + if(++nbits >= 11) { 1.84 + nbits = 0; 1.85 + 1.86 + /* check parity */ 1.87 + /*if((parity & 1) == (valp & 1)) {}*/ 1.88 + keybuf[key_wr] = (unsigned char)value; 1.89 + key_wr = NEXT_IDX(key_wr); 1.90 + 1.91 + value = 0; 1.92 + parity = 0; 1.93 + } 1.94 + } 1.95 +}