Add chech CRC-16

This commit is contained in:
lulko 2025-04-14 17:31:11 +03:00
parent f1dd814d86
commit cbab11f918

View file

@ -116,11 +116,21 @@ uint8_t flash_read_word(uint32_t address){
} }
// Wait if flash // Wait if flash
bool validate_crc(FLASH_RECORD* ptr, uint32_t length) {
bool validate_crc(FLASH_RECORD* crc){ uint16_t crc = 0xFFFF; // Начальное значение
if(crc->crc == 6933) uint16_t crc_value = ptr->crc;
return true; uint8_t* data = reinterpret_cast<uint8_t*>(&crc_value);
return false; while (length--) {
crc ^= *data++; // Обрабатываем LSB первым
for (uint8_t i = 0; i < 8; i++) {
if (crc & 0x0001) { // Проверяем младший бит
crc = (crc >> 1) ^ 0xA001; // Полином 0x8005 (reverse)
} else {
crc >>= 1;
}
}
}
return crc == 0? true : false;
} }
/* read struct from FLASH */ /* read struct from FLASH */
void flash_read(uint32_t addr,FLASH_RECORD* ptr){ void flash_read(uint32_t addr,FLASH_RECORD* ptr){
@ -136,7 +146,7 @@ void compact_page(){
FLASH_RECORD rec; FLASH_RECORD rec;
flash_read(i,&rec); flash_read(i,&rec);
if (validate_crc(&rec)){ if (validate_crc(&rec,2)){
latest[rec.data_id] = rec; latest[rec.data_id] = rec;
} }
} }
@ -186,7 +196,7 @@ FLASH_RECORD* load_params(){
FLASH_RECORD res; FLASH_RECORD res;
for(uint32_t addr = SECTOR_6;addr < SECTOR_6_END;addr +=FLASH_RECORD_SIZE) { for(uint32_t addr = SECTOR_6;addr < SECTOR_6_END;addr +=FLASH_RECORD_SIZE) {
flash_read(addr,&res); flash_read(addr,&res);
if (!validate_crc(&res)) if (!validate_crc(&res,2))
break; break;
else{ else{
latest[res.data_id] = res; latest[res.data_id] = res;