tabby/avr/recv.c

112 lines
1.9 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/avr/printer.h>
#include <tabby/avr/uart.h>
#include <tabby/avr/link.h>
2016-06-05 12:37:05 -05:00
#include <tabby/avr.h>
2016-06-05 16:45:04 -05:00
static volatile tabby_printer_packet header;
static volatile uint16_t i, b;
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,
2016-06-05 12:37:05 -05:00
out = 0;
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-05 12:37:05 -05:00
if (in == TABBY_PRINTER_SYNC_1) {
2016-06-06 19:16:05 -05:00
header.preamble[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-05 12:37:05 -05:00
if (in == TABBY_PRINTER_SYNC_2) {
2016-06-06 19:16:05 -05:00
header.preamble[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;
2016-05-25 23:28:06 -05:00
2016-06-06 20:49:43 -05:00
if (header.size > TABBY_PRINTER_PACKET_MAX_SIZE) {
2016-06-06 19:16:05 -05:00
i = 0;
2016-06-06 20:46:25 -05:00
} else {
i++;
2016-06-06 19:16:05 -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-05 12:37:05 -05:00
out = TABBY_PRINTER_DEVICE_ID;
} else if (b == header.size + 2) {
b++;
2016-06-06 19:16:05 -05:00
out = TABBY_PRINTER_OK;
} else if (b == header.size + 3) {
i = 0;
}
}
}
2016-06-01 20:33:47 -05:00
SPDR = out;
}
int main() {
uart_init();
tabby_avr_link_init_slave();
2016-05-25 23:28:06 -05:00
sei();
while (1) {
sleep_mode();
2016-05-25 23:28:06 -05:00
}
return 0;
}