Add chech CRC-16
This commit is contained in:
parent
f1dd814d86
commit
cbab11f918
1 changed files with 17 additions and 7 deletions
|
@ -116,11 +116,21 @@ uint8_t flash_read_word(uint32_t address){
|
|||
}
|
||||
// Wait if flash
|
||||
|
||||
|
||||
bool validate_crc(FLASH_RECORD* crc){
|
||||
if(crc->crc == 6933)
|
||||
return true;
|
||||
return false;
|
||||
bool validate_crc(FLASH_RECORD* ptr, uint32_t length) {
|
||||
uint16_t crc = 0xFFFF; // Начальное значение
|
||||
uint16_t crc_value = ptr->crc;
|
||||
uint8_t* data = reinterpret_cast<uint8_t*>(&crc_value);
|
||||
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 */
|
||||
void flash_read(uint32_t addr,FLASH_RECORD* ptr){
|
||||
|
@ -136,7 +146,7 @@ void compact_page(){
|
|||
FLASH_RECORD rec;
|
||||
flash_read(i,&rec);
|
||||
|
||||
if (validate_crc(&rec)){
|
||||
if (validate_crc(&rec,2)){
|
||||
latest[rec.data_id] = rec;
|
||||
}
|
||||
}
|
||||
|
@ -186,7 +196,7 @@ FLASH_RECORD* load_params(){
|
|||
FLASH_RECORD res;
|
||||
for(uint32_t addr = SECTOR_6;addr < SECTOR_6_END;addr +=FLASH_RECORD_SIZE) {
|
||||
flash_read(addr,&res);
|
||||
if (!validate_crc(&res))
|
||||
if (!validate_crc(&res,2))
|
||||
break;
|
||||
else{
|
||||
latest[res.data_id] = res;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue