From 5b27cf9c79fa22544e3aa98c702f0496a7e2a6e7 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Tue, 7 Jun 2016 17:59:23 -0500 Subject: [PATCH] Refactor print receiver to calculate checksums and raise CRC error when necessary --- avr/recv.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/avr/recv.c b/avr/recv.c index da9fc95..924d03d 100644 --- a/avr/recv.c +++ b/avr/recv.c @@ -9,7 +9,14 @@ static volatile uint16_t offset_header = 0, offset_body = 0, - size_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 @@ -25,6 +32,9 @@ ISR(SPI_STC_vect) { if (in == TABBY_PRINTER_SYNC_1) { offset_header++; offset_body = 0; + sum_calc = 0; + + response.status = TABBY_PRINTER_OK; } break; @@ -43,6 +53,7 @@ ISR(SPI_STC_vect) { case 2: case 3: { offset_header++; + sum_calc += in; break; } @@ -50,6 +61,7 @@ ISR(SPI_STC_vect) { case 4: { size_body = in; + sum_calc += in; offset_header++; break; @@ -64,20 +76,32 @@ ISR(SPI_STC_vect) { offset_header++; } + sum_calc += in; + break; } default: { - if (offset_body <= size_body) { + 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++; - out = TABBY_PRINTER_DEVICE_ID; + 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 = TABBY_PRINTER_OK; + out = response.status; } else if (offset_body == size_body + 3) { offset_header = 0; }