#include #include #include #include #include #include #include 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 }; /* * 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; sum_calc = 0; response.status = TABBY_PRINTER_OK; } break; } case 1: { if (in == TABBY_PRINTER_SYNC_2) { offset_header++; } else { offset_header = 0; } break; } case 2: case 3: { offset_header++; sum_calc += in; break; } case 4: { size_body = in; sum_calc += in; offset_header++; break; } case 5: { size_body |= in << 8; if (size_body > TABBY_PRINTER_PACKET_MAX_SIZE) { offset_header = 0; } else { offset_header++; } 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++; out = response.status; } 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; }