tabby/avr/capture.c

127 lines
2.6 KiB
C
Raw Permalink 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>
static volatile uint16_t offset_header = 0,
offset_body = 0,
size_body = 0,
sum_calc = 0,
sum_footer = 0;
static volatile tabby_printer_response response = {
.device = TABBY_PRINTER_DEVICE_ID,
.status = TABBY_PRINTER_OK
};
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 (offset_header) {
case 0: {
2016-06-05 12:37:05 -05:00
if (in == TABBY_PRINTER_SYNC_1) {
offset_header++;
offset_body = 0;
sum_calc = 0;
response.status = TABBY_PRINTER_OK;
}
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) {
offset_header++;
} else {
offset_header = 0;
}
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 2:
case 3: {
offset_header++;
sum_calc += in;
break;
}
case 4: {
size_body = in;
sum_calc += in;
offset_header++;
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 5: {
size_body |= in << 8;
2016-05-25 23:28:06 -05:00
if (size_body > TABBY_PRINTER_PACKET_MAX_SIZE) {
offset_header = 0;
2016-06-06 20:46:25 -05:00
} else {
offset_header++;
2016-06-06 19:16:05 -05:00
}
sum_calc += in;
break;
}
default: {
if (offset_body < size_body) {
offset_body++;
sum_calc += in;
} else if (offset_body == size_body) {
offset_body++;
sum_footer = in;
} else if (offset_body == size_body + 1) {
offset_body++;
sum_footer |= in << 8;
if (sum_footer != sum_calc) {
response.status |= TABBY_PRINTER_SUM;
}
out = response.device;
} else if (offset_body == size_body + 2) {
offset_body++;
2016-06-06 19:16:05 -05:00
out = response.status;
} else if (offset_body == size_body + 3) {
offset_header = 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;
}