2012-12-06 11:48:17 +01:00
|
|
|
#include <avr/io.h>
|
2012-12-06 15:27:07 +01:00
|
|
|
#include <avr/interrupt.h>
|
2012-12-06 11:48:17 +01:00
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2012-12-06 15:27:07 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
2012-12-06 20:50:13 +01:00
|
|
|
static volatile int8_t kbd_state = 0;
|
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
static volatile uint8_t kbd_input = 0;
|
2012-12-06 20:50:13 +01:00
|
|
|
static volatile uint8_t kbd_output = 0;
|
2012-12-06 15:27:07 +01:00
|
|
|
static volatile uint8_t kbd_flags = 0;
|
|
|
|
|
|
|
|
|
2012-12-06 19:17:46 +01:00
|
|
|
static volatile uint8_t ts = 10;
|
|
|
|
|
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
#define KBD_FLAG_ERROR (_BV(0))
|
|
|
|
#define KBD_FLAG_BREAK (_BV(1))
|
|
|
|
#define KBD_FLAG_EXT (_BV(2))
|
|
|
|
|
|
|
|
#define KBD_CODE_UP 0xe075
|
|
|
|
#define KBD_CODE_LEFT 0xe06b
|
|
|
|
#define KBD_CODE_DOWN 0xe072
|
|
|
|
#define KBD_CODE_RIGHT 0xe074
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline bool kbd_data() {
|
|
|
|
return (PINC & (1 << 0));
|
|
|
|
}
|
2012-12-06 11:48:17 +01:00
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
static inline bool kbd_clock() {
|
|
|
|
return (PINC & (1 << 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-10 16:44:50 +01:00
|
|
|
static inline void kbd_clock_down() {
|
|
|
|
DDRC |= 0x02;
|
|
|
|
PORTC &= ~0x02;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kbd_clock_in() {
|
|
|
|
DDRC &= ~0x02;
|
|
|
|
PORTC |= 0x02;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kbd_data_set(bool state) {
|
|
|
|
if (state)
|
|
|
|
PORTC |= 0x01;
|
|
|
|
else
|
|
|
|
PORTC &= ~0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kbd_data_out() {
|
|
|
|
DDRC |= 0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kbd_data_in() {
|
|
|
|
DDRC &= ~0x01;
|
|
|
|
PORTC |= 0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void kbd_wait() {
|
|
|
|
while (kbd_state) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
ISR(PCINT1_vect) {
|
|
|
|
if (kbd_clock())
|
|
|
|
return;
|
|
|
|
|
2012-12-06 20:50:13 +01:00
|
|
|
if (kbd_state < 0) {
|
|
|
|
if (kbd_state >= -8) {
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_data_set(kbd_output & _BV(-1-kbd_state));
|
2012-12-06 20:50:13 +01:00
|
|
|
}
|
2012-12-10 16:44:50 +01:00
|
|
|
else if (kbd_state == -9) {
|
|
|
|
kbd_data_set(!(__builtin_popcount(kbd_output) & 1));
|
2012-12-06 20:50:13 +01:00
|
|
|
}
|
2012-12-10 16:44:50 +01:00
|
|
|
else if (kbd_state == -10) {
|
|
|
|
kbd_data_in();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kbd_state = 0;
|
2012-12-06 20:50:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_state--;
|
2012-12-06 20:50:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
bool data = kbd_data();
|
|
|
|
|
|
|
|
if (kbd_state == 0) {
|
|
|
|
if (!data) { /* start bit */
|
|
|
|
kbd_input = 0;
|
|
|
|
kbd_state++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kbd_state <= 8) {
|
2012-12-10 16:44:50 +01:00
|
|
|
if (data)
|
|
|
|
kbd_input |= _BV(kbd_state-1);
|
2012-12-06 15:27:07 +01:00
|
|
|
kbd_state++;
|
|
|
|
return;
|
|
|
|
}
|
2012-12-06 11:48:17 +01:00
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
if (kbd_state == 9) {
|
|
|
|
if ((__builtin_popcount(kbd_input) & 1) == data)
|
|
|
|
kbd_flags |= KBD_FLAG_ERROR;
|
2012-12-06 11:48:17 +01:00
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
kbd_state++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kbd_state = 0;
|
|
|
|
|
|
|
|
if (kbd_flags & KBD_FLAG_ERROR) {
|
|
|
|
/* Retry */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kbd_input == 0xe0) {
|
|
|
|
kbd_flags |= KBD_FLAG_EXT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kbd_input == 0xf0) {
|
|
|
|
kbd_flags |= KBD_FLAG_BREAK;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t code = kbd_input;
|
|
|
|
if (kbd_flags & KBD_FLAG_EXT)
|
|
|
|
code |= 0xe000;
|
|
|
|
|
|
|
|
if (!(kbd_flags & KBD_FLAG_BREAK)) {
|
|
|
|
switch (code) {
|
|
|
|
case KBD_CODE_UP:
|
2012-12-06 19:17:46 +01:00
|
|
|
if (ts > 0)
|
|
|
|
ts--;
|
2012-12-06 15:27:07 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KBD_CODE_DOWN:
|
2012-12-06 19:17:46 +01:00
|
|
|
if (ts < 31)
|
|
|
|
ts++;
|
2012-12-06 15:27:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kbd_flags = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:50:13 +01:00
|
|
|
|
|
|
|
void kbd_send(uint8_t command) {
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_wait();
|
2012-12-06 20:50:13 +01:00
|
|
|
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_clock_down();
|
2012-12-06 20:50:13 +01:00
|
|
|
_delay_us(100);
|
|
|
|
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_data_out();
|
|
|
|
kbd_data_set(false);
|
2012-12-06 20:50:13 +01:00
|
|
|
_delay_us(10);
|
|
|
|
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_clock_in();
|
2012-12-06 20:50:13 +01:00
|
|
|
|
|
|
|
kbd_output = command;
|
|
|
|
kbd_state = -1;
|
|
|
|
|
2012-12-10 16:44:50 +01:00
|
|
|
kbd_wait();
|
|
|
|
|
|
|
|
/* wait for ack */
|
2012-12-06 20:50:13 +01:00
|
|
|
while (!kbd_state) {}
|
2012-12-10 16:44:50 +01:00
|
|
|
|
|
|
|
kbd_wait();
|
2012-12-06 20:50:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
int main(void) {
|
|
|
|
DDRB = 0xff;
|
2012-12-06 11:48:17 +01:00
|
|
|
PORTB = 0;
|
|
|
|
|
2012-12-06 15:27:07 +01:00
|
|
|
DDRC = 0x00;
|
|
|
|
PORTC = 0x03;
|
|
|
|
|
|
|
|
PCMSK1 = (1 << PCINT9);
|
|
|
|
PCICR = (1 << PCIE1);
|
|
|
|
|
|
|
|
sei();
|
|
|
|
|
2012-12-06 20:50:13 +01:00
|
|
|
kbd_send(0xff);
|
|
|
|
|
|
|
|
uint8_t l = 0;
|
|
|
|
|
2012-12-06 11:48:17 +01:00
|
|
|
while(true) {
|
2012-12-06 20:50:13 +01:00
|
|
|
uint8_t state = PORTB ^ 0x20;
|
|
|
|
PORTB = state;
|
|
|
|
|
|
|
|
if (state & 0x20) {
|
|
|
|
l = (l+1)%3;
|
|
|
|
|
|
|
|
kbd_send(0xed);
|
|
|
|
kbd_send(1 << l);
|
|
|
|
}
|
2012-12-06 19:17:46 +01:00
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < (1 << ts); i++)
|
|
|
|
_delay_ms(1);
|
2012-12-06 11:48:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|