Rewrote send and receive routines using coroutines

This commit is contained in:
Dreaded_X 2021-01-18 17:58:30 +01:00
parent befc326c7f
commit 3b0fb96364

View File

@ -1,4 +1,5 @@
#include <avr/io.h> #include <avr/io.h>
#include <stdint.h>
#include <util/delay.h> #include <util/delay.h>
#include "keyboard.h" #include "keyboard.h"
@ -10,8 +11,7 @@ volatile struct {
struct FIFO buffer; struct FIFO buffer;
uint8_t sending : 1; uint8_t sending : 1;
uint16_t value; uint8_t cmd;
uint8_t counter;
struct { struct {
uint8_t shift : 1; uint8_t shift : 1;
@ -22,7 +22,7 @@ volatile struct {
uint8_t released : 1; uint8_t released : 1;
} keyboard = {0}; } keyboard = {0};
// Send a command to the keyboard (BLOCKS) // Send a command to the keyboard (THIS BLOCKS)
void send_keyboard_cmd(uint8_t value) { void send_keyboard_cmd(uint8_t value) {
// Pull clock low // Pull clock low
DDRD |= (1<<KEYBOARD_CLK); DDRD |= (1<<KEYBOARD_CLK);
@ -38,7 +38,7 @@ void send_keyboard_cmd(uint8_t value) {
DDRD &= ~(1<<KEYBOARD_CLK); DDRD &= ~(1<<KEYBOARD_CLK);
keyboard.sending = 1; keyboard.sending = 1;
keyboard.value = value; keyboard.cmd = value;
while (keyboard.sending) {}; while (keyboard.sending) {};
} }
@ -64,41 +64,7 @@ uint8_t parity(uint8_t value) {
return (~value) & 1; return (~value) & 1;
} }
void receive_scancode_bit(void (*callback)(uint8_t)) { void process_scancode(uint8_t value, void (*callback)(uint8_t)) {
keyboard.value |= ((PIND >> KEYBOARD_DATA) & 1) << keyboard.counter;
keyboard.counter++;
// Check that the first bit is 0
if (keyboard.counter == 1 && keyboard.value & 1) {
keyboard.counter = 0;
keyboard.value = 0;
return;
}
// Check the parity
if (keyboard.counter == 10 && parity(keyboard.value >> 1) != ((keyboard.value >> 9) & 1)) {
keyboard.counter = 0;
keyboard.value = 0;
return;
}
// Check that the last bit is 1
if (keyboard.counter == 11 && !((keyboard.value >> 10) & 1)) {
keyboard.counter = 0;
keyboard.value = 0;
return;
}
// Process the scancode
if (keyboard.counter == 11) {
uint8_t value = keyboard.value >> 1;
keyboard.counter = 0;
keyboard.value = 0;
switch (value) { switch (value) {
// Left and right shift // Left and right shift
case 18: case 18:
@ -184,41 +150,80 @@ void receive_scancode_bit(void (*callback)(uint8_t)) {
break; break;
} }
} }
void receive_scancode_bit(void (*callback)(uint8_t)) {
uint8_t in = (PIND >> KEYBOARD_DATA) & 1;
CO_BEGIN;
// Check that the first bit is 0
if (in != 0) {
CO_BREAK;
}
CO_YIELD;
static uint8_t value;
value = 0;
static int i = 0;
for (i = 0; i < 8; ++i) {
value |= in << i;
CO_YIELD;
}
// Check the parity
if (in != parity(value)) {
CO_BREAK;
}
CO_YIELD;
// Check that the last bit is 1
if (in != 1) {
CO_BREAK;
}
process_scancode(value, callback);
CO_END;
} }
void send_cmd_bit() { void send_cmd_bit() {
if (keyboard.counter == 8) { CO_BEGIN;
static int i;
for (i = 0; i < 8; ++i) {
// Send bit
if ((keyboard.cmd >> i) & 1) {
PORTD |= (1<<KEYBOARD_DATA);
} else {
PORTD &= ~(1<<KEYBOARD_DATA);
}
CO_YIELD;
}
// Send parity // Send parity
if (parity(keyboard.value)) { if (parity(keyboard.cmd)) {
PORTD |= (1<<KEYBOARD_DATA); PORTD |= (1<<KEYBOARD_DATA);
} else { } else {
PORTD &= ~(1<<KEYBOARD_DATA); PORTD &= ~(1<<KEYBOARD_DATA);
} }
++keyboard.counter; CO_YIELD;
} else if (keyboard.counter == 9) {
// Release data line // Release data line
PORTD &= ~(1<<KEYBOARD_DATA); PORTD &= ~(1<<KEYBOARD_DATA);
DDRD &= ~(1<<KEYBOARD_DATA); DDRD &= ~(1<<KEYBOARD_DATA);
++keyboard.counter; CO_YIELD;
} else if (keyboard.counter == 10) {
// Wait for data to go low as ack // Wait for data to go low as ack
if (!((PIND >> KEYBOARD_DATA) & 1)) { if (!((PIND >> KEYBOARD_DATA) & 1)) {
keyboard.counter = 0;
keyboard.value = 0;
keyboard.sending = 0; keyboard.sending = 0;
} }
} else {
// Send bit
if ((keyboard.value >> keyboard.counter) & 1) {
PORTD |= (1<<KEYBOARD_DATA);
} else {
PORTD &= ~(1<<KEYBOARD_DATA);
}
++keyboard.counter; CO_END;
}
} }
void keyboard_interrupt(void (*callback)(uint8_t)) { void keyboard_interrupt(void (*callback)(uint8_t)) {