2016-05-27 12:23:00 -05:00
|
|
|
#include <avr/io.h>
|
2016-05-25 21:48:39 -05:00
|
|
|
#include <avr/interrupt.h>
|
2016-05-25 23:28:06 -05:00
|
|
|
#include <avr/sleep.h>
|
2016-05-25 21:48:39 -05:00
|
|
|
|
2016-06-06 20:28:19 -05:00
|
|
|
#include <tabby/avr/printer.h>
|
2016-05-25 23:44:21 -05:00
|
|
|
#include <tabby/avr/uart.h>
|
2016-06-06 19:42:15 -05:00
|
|
|
#include <tabby/avr/link.h>
|
2016-06-05 12:37:05 -05:00
|
|
|
#include <tabby/avr.h>
|
2016-05-25 21:48:39 -05:00
|
|
|
|
2016-06-07 17:05:49 -05:00
|
|
|
static volatile uint16_t offset_header = 0,
|
|
|
|
offset_body = 0,
|
2016-06-07 17:59:23 -05:00
|
|
|
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-31 18:15:37 -05:00
|
|
|
|
2016-05-25 23:28:06 -05:00
|
|
|
/*
|
2016-05-31 18:15:37 -05:00
|
|
|
* SPI byte receipt interrupt vector
|
2016-05-25 23:28:06 -05:00
|
|
|
*/
|
2016-05-31 18:15:37 -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);
|
2016-05-31 19:32:20 -05:00
|
|
|
|
2016-06-07 17:05:49 -05:00
|
|
|
switch (offset_header) {
|
2016-05-31 18:15:37 -05:00
|
|
|
case 0: {
|
2016-06-05 12:37:05 -05:00
|
|
|
if (in == TABBY_PRINTER_SYNC_1) {
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_header++;
|
|
|
|
offset_body = 0;
|
2016-06-07 17:59:23 -05:00
|
|
|
sum_calc = 0;
|
|
|
|
|
|
|
|
response.status = TABBY_PRINTER_OK;
|
2016-05-31 18:15:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-05-25 23:28:06 -05:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
case 1: {
|
2016-06-05 12:37:05 -05:00
|
|
|
if (in == TABBY_PRINTER_SYNC_2) {
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_header++;
|
2016-05-31 18:15:37 -05:00
|
|
|
} else {
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_header = 0;
|
2016-05-31 18:15:37 -05:00
|
|
|
}
|
2016-05-25 23:28:06 -05:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
break;
|
2016-05-26 23:58:39 +00:00
|
|
|
}
|
2016-05-25 23:28:06 -05:00
|
|
|
|
2016-06-07 17:05:49 -05:00
|
|
|
case 2:
|
2016-05-31 18:15:37 -05:00
|
|
|
case 3: {
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_header++;
|
2016-06-07 17:59:23 -05:00
|
|
|
sum_calc += in;
|
2016-05-26 23:58:39 +00:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-05-26 23:58:39 +00:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
case 4: {
|
2016-06-07 17:05:49 -05:00
|
|
|
size_body = in;
|
|
|
|
|
2016-06-07 17:59:23 -05:00
|
|
|
sum_calc += in;
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_header++;
|
2016-05-25 23:28:06 -05:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-05-25 23:28:06 -05:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
case 5: {
|
2016-06-07 17:05:49 -05:00
|
|
|
size_body |= in << 8;
|
2016-05-25 23:28:06 -05:00
|
|
|
|
2016-06-07 17:05:49 -05:00
|
|
|
if (size_body > TABBY_PRINTER_PACKET_MAX_SIZE) {
|
|
|
|
offset_header = 0;
|
2016-06-06 20:46:25 -05:00
|
|
|
} else {
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_header++;
|
2016-06-06 19:16:05 -05:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:59:23 -05:00
|
|
|
sum_calc += in;
|
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-05-25 21:48:39 -05:00
|
|
|
|
2016-05-31 18:15:37 -05:00
|
|
|
default: {
|
2016-06-07 17:59:23 -05:00
|
|
|
if (offset_body < size_body) {
|
2016-06-07 17:05:49 -05:00
|
|
|
offset_body++;
|
2016-06-07 17:59:23 -05:00
|
|
|
sum_calc += in;
|
|
|
|
} else if (offset_body == size_body) {
|
|
|
|
offset_body++;
|
|
|
|
sum_footer = in;
|
2016-06-07 17:05:49 -05:00
|
|
|
} else if (offset_body == size_body + 1) {
|
|
|
|
offset_body++;
|
2016-05-31 18:15:37 -05:00
|
|
|
|
2016-06-07 17:59:23 -05:00
|
|
|
sum_footer |= in << 8;
|
|
|
|
|
|
|
|
if (sum_footer != sum_calc) {
|
|
|
|
response.status |= TABBY_PRINTER_SUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = response.device;
|
2016-06-07 17:05:49 -05:00
|
|
|
} else if (offset_body == size_body + 2) {
|
|
|
|
offset_body++;
|
2016-06-06 19:16:05 -05:00
|
|
|
|
2016-06-07 17:59:23 -05:00
|
|
|
out = response.status;
|
2016-06-07 17:05:49 -05:00
|
|
|
} else if (offset_body == size_body + 3) {
|
|
|
|
offset_header = 0;
|
2016-05-31 18:15:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 20:33:47 -05:00
|
|
|
|
|
|
|
SPDR = out;
|
2016-05-25 21:48:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2016-05-25 23:44:21 -05:00
|
|
|
uart_init();
|
2016-06-06 19:42:15 -05:00
|
|
|
|
|
|
|
tabby_avr_link_init_slave();
|
2016-05-25 23:44:21 -05:00
|
|
|
|
2016-05-25 23:28:06 -05:00
|
|
|
sei();
|
|
|
|
|
|
|
|
while (1) {
|
2016-05-31 18:15:37 -05:00
|
|
|
sleep_mode();
|
2016-05-25 23:28:06 -05:00
|
|
|
}
|
|
|
|
|
2016-05-25 21:48:39 -05:00
|
|
|
return 0;
|
|
|
|
}
|