tabby/avr/recv.c
XANTRONIX Development c18eb40c46 Whoops
2016-06-06 20:46:25 -05:00

111 lines
1.9 KiB
C

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <tabby/avr/printer.h>
#include <tabby/avr/uart.h>
#include <tabby/avr/link.h>
#include <tabby/avr.h>
static volatile tabby_printer_packet header;
static volatile uint16_t i, b;
/*
* SPI byte receipt interrupt vector
*/
ISR(SPI_STC_vect) {
uint8_t in = SPDR,
out = 0;
uart_putchar(in, NULL);
switch (i) {
case 0: {
if (in == TABBY_PRINTER_SYNC_1) {
header.preamble[0] = in;
i++;
b = 0;
}
break;
}
case 1: {
if (in == TABBY_PRINTER_SYNC_2) {
header.preamble[1] = in;
i++;
} else {
i = 0;
}
break;
}
case 2: {
header.type = in;
i++;
break;
}
case 3: {
header.compression = in;
i++;
break;
}
case 4: {
header.size = in;
i++;
break;
}
case 5: {
header.size |= in << 8;
if (tabby_avr_printer_packet_toolarge(header.type, header.size)) {
i = 0;
} else {
i++;
}
break;
}
default: {
if (b <= header.size) {
b++;
} else if (b == header.size + 1) {
b++;
out = TABBY_PRINTER_DEVICE_ID;
} else if (b == header.size + 2) {
b++;
out = TABBY_PRINTER_OK;
} else if (b == header.size + 3) {
i = 0;
}
}
}
SPDR = out;
}
int main() {
uart_init();
tabby_avr_link_init_slave();
sei();
while (1) {
sleep_mode();
}
return 0;
}