Merge pull request #30 from logicog/crc16

CRC-16 Calculation and verification
This commit is contained in:
René van Dorst
2025-10-26 21:59:50 +00:00
committed by GitHub
6 changed files with 407 additions and 46 deletions
+23 -5
View File
@@ -49,6 +49,10 @@ __xdata uint16_t short_parsed;
#define TSTATE_CLOSED 3
#define TSTATE_POST 4
extern __xdata uint16_t crc_value;
__xdata uint16_t crc_final;
void crc16(__xdata uint8_t *v) __naked;
inline uint8_t is_separator(uint8_t c)
{
@@ -184,13 +188,15 @@ __xdata uint8_t *scan_header(__xdata uint8_t *p)
content_type += 30;
uint8_t i = 0;
while (content_type[i] != '\r' && content_type[i] != '\n') {
boundary[i + 2] = content_type[i];
boundary[i + 4] = content_type[i];
i++;
}
// The boundary between parts is "--" + the boundary given in the header
boundary[0] = '-';
boundary[1] = '-';
boundary[i + 2] = 0;
// The boundary between parts is "\r\n--" + the boundary given in the header
boundary[0] = '\r';
boundary[1] = '\n';
boundary[2] = '-';
boundary[3] = '-';
boundary[i + 4] = 0;
}
return p;
}
@@ -224,6 +230,12 @@ uint8_t stream_upload(uint16_t bptr)
uptr += write_len;
write_len = 0;
// TODO: This is a bit premature, what about a nice web-page saying the device will reset???
print_string("CRC16: "); print_short(crc_final); write_char('\n');
if (crc_final == 0xb001) {
print_string("Checksum OK.");
} else {
print_string("Checksum incorrect!");
}
print_string("Upload to flash done, will reset!\n");
reset_chip();
if (bptr >= uip_len)
@@ -231,6 +243,9 @@ uint8_t stream_upload(uint16_t bptr)
return 1;
}
if (p[bptr] == boundary[bindex]) {
if (!bindex)
crc_final = crc_value;
crc16(p + bptr);
bptr++;
bindex++;
} else {
@@ -239,9 +254,11 @@ uint8_t stream_upload(uint16_t bptr)
write_len += bindex;
bindex = 0;
}
crc16(p + bptr);
flash_buf[write_len++] = p[bptr++];
if (write_len >= FLASHMEM_PAGE_SIZE) {
print_string("len: "); print_short(write_len); write_char(' ');
print_string("CRC16: "); print_short(crc_value); write_char('\n');
flash_region.addr = uptr;
flash_region.len = FLASHMEM_PAGE_SIZE;
flash_write_bytes(flash_buf);
@@ -312,6 +329,7 @@ void handle_post(void)
p += 4; // Skip \r\n\r\n sequence at end of preamble of part
uptr = FIRMWARE_UPLOAD_START;
crc_value = 0;
bindex = 0;
write_len = 0;
stream_upload(p - uip_appdata);