Refactor print receiver to calculate checksums and raise CRC error when necessary
This commit is contained in:
parent
eb8ac6acb6
commit
5b27cf9c79
1 changed files with 28 additions and 4 deletions
32
avr/recv.c
32
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue