tabby/avr/recv.c

145 lines
2.5 KiB
C
Raw Normal View History

2016-05-27 12:23:00 -05:00
#include <avr/io.h>
#include <avr/interrupt.h>
2016-05-25 23:28:06 -05:00
#include <avr/sleep.h>
#include <tabby/printer.h>
#include <tabby/avr/uart.h>
2016-06-01 00:03:26 -05:00
static volatile tabby_printer_packet header = {
.type = 0,
.compression = 0,
.size = 0
};
static volatile uint16_t i = 0,
b = 0;
2016-05-25 23:28:06 -05:00
/*
* SPI byte receipt interrupt vector
2016-05-25 23:28:06 -05:00
*/
ISR(SPI_STC_vect) {
2016-06-01 20:33:47 -05:00
uint8_t in = SPDR,
out = 0x00;
2016-05-25 23:28:06 -05:00
2016-06-01 20:33:47 -05:00
uart_putchar(in, NULL);
switch (i) {
case 0: {
2016-06-01 20:33:47 -05:00
if (in == 0x88) {
header.data[0] = in;
i++;
2016-05-25 23:28:06 -05:00
b = 0;
}
break;
}
2016-05-25 23:28:06 -05:00
case 1: {
2016-06-01 20:33:47 -05:00
if (in == 0x33) {
header.data[1] = in;
i++;
} else {
i = 0;
}
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 2: {
2016-06-01 20:33:47 -05:00
header.type = in;
i++;
break;
}
2016-05-25 23:28:06 -05:00
case 3: {
2016-06-01 20:33:47 -05:00
header.compression = in;
i++;
break;
}
case 4: {
2016-06-01 20:33:47 -05:00
header.size = in;
i++;
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 5: {
2016-06-01 20:33:47 -05:00
header.size |= in << 8;
i++;
2016-05-25 23:28:06 -05:00
break;
}
default: {
2016-06-01 20:33:47 -05:00
if (b <= header.size) {
b++;
} else if (b == header.size + 1) {
b++;
2016-06-01 20:33:47 -05:00
out = 0x81;
} else if (b == header.size + 2) {
b++;
} else if (b == header.size + 3) {
i = 0;
}
}
}
2016-06-01 20:33:47 -05:00
SPDR = out;
}
static void setup_clock_external() {
/*
* Configure MISO as output
*/
2016-05-29 01:12:51 -05:00
DDRB |= (1 << DDB4);
/*
2016-05-29 01:12:51 -05:00
* Configure SS as input
*/
2016-05-29 01:12:51 -05:00
DDRB &= ~(1 << DDB2);
/*
* Set SPI slave mode, and shift in/out most significant bit first
*/
SPCR &= ~((1 << MSTR) | (1 << DORD));
/*
* Enable SPI in Mode 3 with interrupts
*/
SPCR |= (1 << CPOL) | (1 << CPHA) | (1 << SPIE) | (1 << SPE);
/*
* Initialize the SPI Data Register and serial buffer
*/
SPDR = 0;
}
int main() {
/*
* Best turn on the serial UART!
*/
uart_init();
2016-05-25 23:28:06 -05:00
/*
* By default, the Game Boy link port is configured to monitor for external
* clock pulses, so we'll go ahead and do that in this case.
*/
setup_clock_external();
2016-05-25 23:28:06 -05:00
/*
* We do actually want to globally enable interrupts here, so here's that
* one assembly instruction to do so.
*/
sei();
while (1) {
sleep_mode();
2016-05-25 23:28:06 -05:00
}
return 0;
}