Refactor print receiver to calculate checksums and raise CRC error when necessary

This commit is contained in:
XANTRONIX Development 2016-06-07 17:59:23 -05:00
parent eb8ac6acb6
commit 5b27cf9c79

View file

@ -9,7 +9,14 @@
static volatile uint16_t offset_header = 0, static volatile uint16_t offset_header = 0,
offset_body = 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 * SPI byte receipt interrupt vector
@ -25,6 +32,9 @@ ISR(SPI_STC_vect) {
if (in == TABBY_PRINTER_SYNC_1) { if (in == TABBY_PRINTER_SYNC_1) {
offset_header++; offset_header++;
offset_body = 0; offset_body = 0;
sum_calc = 0;
response.status = TABBY_PRINTER_OK;
} }
break; break;
@ -43,6 +53,7 @@ ISR(SPI_STC_vect) {
case 2: case 2:
case 3: { case 3: {
offset_header++; offset_header++;
sum_calc += in;
break; break;
} }
@ -50,6 +61,7 @@ ISR(SPI_STC_vect) {
case 4: { case 4: {
size_body = in; size_body = in;
sum_calc += in;
offset_header++; offset_header++;
break; break;
@ -64,20 +76,32 @@ ISR(SPI_STC_vect) {
offset_header++; offset_header++;
} }
sum_calc += in;
break; break;
} }
default: { default: {
if (offset_body <= size_body) { if (offset_body < size_body) {
offset_body++; offset_body++;
sum_calc += in;
} else if (offset_body == size_body) {
offset_body++;
sum_footer = in;
} else if (offset_body == size_body + 1) { } else if (offset_body == size_body + 1) {
offset_body++; 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) { } else if (offset_body == size_body + 2) {
offset_body++; offset_body++;
out = TABBY_PRINTER_OK; out = response.status;
} else if (offset_body == size_body + 3) { } else if (offset_body == size_body + 3) {
offset_header = 0; offset_header = 0;
} }