tabby/avr/recv.c
2016-06-07 17:05:49 -05:00

102 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 uint16_t offset_header = 0,
offset_body = 0,
size_body = 0;
/*
* SPI byte receipt interrupt vector
*/
ISR(SPI_STC_vect) {
uint8_t in = SPDR,
out = 0;
uart_putchar(in, NULL);
switch (offset_header) {
case 0: {
if (in == TABBY_PRINTER_SYNC_1) {
offset_header++;
offset_body = 0;
}
break;
}
case 1: {
if (in == TABBY_PRINTER_SYNC_2) {
offset_header++;
} else {
offset_header = 0;
}
break;
}
case 2:
case 3: {
offset_header++;
break;
}
case 4: {
size_body = in;
offset_header++;
break;
}
case 5: {
size_body |= in << 8;
if (size_body > TABBY_PRINTER_PACKET_MAX_SIZE) {
offset_header = 0;
} else {
offset_header++;
}
break;
}
default: {
if (offset_body <= size_body) {
offset_body++;
} else if (offset_body == size_body + 1) {
offset_body++;
out = TABBY_PRINTER_DEVICE_ID;
} else if (offset_body == size_body + 2) {
offset_body++;
out = TABBY_PRINTER_OK;
} else if (offset_body == size_body + 3) {
offset_header = 0;
}
}
}
SPDR = out;
}
int main() {
uart_init();
tabby_avr_link_init_slave();
sei();
while (1) {
sleep_mode();
}
return 0;
}