工具/软件:
您好、
我相信我已经修复了这个问题,并在 CRC32.c :CRC32_calc ()中发现了一个错误;
该选项在通过内部闪存运行时不执行正确的 CRC。 传感器上 ToAD 过程的第一部分会检查内部闪存的 CRC、以确保其与发送到 ToAD 标头中的 CRC 相匹配。 此时我的代码失败。
这是适用于我的修改后的代码。 我将"offset"更改为不包含在 CRC32_memcpy ()中,但在 解析数据以创建 CRC 时包含在其中。
我可以重写它,这样它将接受 CRC32_memcpy ()的偏移量,否则,如果偏移量大于 CRC 缓冲区中读取的值,它可能会失败。 现在、我添加了一项检查、如果偏移量过大、该检查会返回误差。 功能比较全面。
uint32_t CRC32_calc(uint8_t page, uint32_t pageSize, uint16_t offset, uint32_t len, bool useExtFl) { uint8_t pageIdx; uint8_t pageBeg = page; uint8_t pageEnd; uint16_t numBytesInLastPg; uint32_t temp1 = pageSize, temp2, crc = 0; uint16_t oset = 0; uint8_t bufNum; /* Check for invalid length */ if((len == 0) || (len == 0xFFFFFFFF) || (useExtFl == true && len > EFL_FLASH_SIZE) || (useExtFl == false && len > (MAX_ONCHIP_FLASH_PAGES*INTFLASH_PAGE_SIZE)) || (offset > (CRC32_BUF_SZ - 1 - IMG_DATA_OFFSET))) { return crc; } /* Read first page of the image into the buffer. */ if(!useExtFl) { CRC32_memCpy(crcBuf, (uint8_t *)(page * pageSize) /* + offset */, CRC32_BUF_SZ); } else { readFlashPg(page, 0, crcBuf, CRC32_BUF_SZ); } pageEnd = ((len - 1) / (pageSize) + pageBeg); /* Determine the number of bytes in the last page */ numBytesInLastPg = ((len - 1) % pageSize) + 1; crc = 0xFFFFFFFF; /* Read over image pages. */ for (pageIdx = pageBeg; pageIdx <= pageEnd; pageIdx++) { uint8_t numBufInCurPg; /* Find the number of buffers within this page */ if(pageIdx == pageEnd) { /* Number of bytes divided by buf_sz is the number of buffers */ numBufInCurPg = numBytesInLastPg / CRC32_BUF_SZ; /* Round up a buffer if a partial buffer must be used */ if(numBytesInLastPg % CRC32_BUF_SZ != 0) { numBufInCurPg++; } } else { /* Note this requires that pageSize is an integer multiple of CRC32_BUF_SZ */ numBufInCurPg = pageSize / CRC32_BUF_SZ; } /* Read over buffers within each page */ for(bufNum = 0; bufNum < numBufInCurPg; bufNum++) { /* Find ending offset in bytes last buffer. */ uint16_t osetEnd; /* Calculate the ending offset for this buffer */ if(bufNum == (numBufInCurPg - 1) && pageIdx == pageEnd) { if(numBytesInLastPg % CRC32_BUF_SZ != 0) { osetEnd = (numBytesInLastPg % CRC32_BUF_SZ ); } else { osetEnd = CRC32_BUF_SZ; } } else { osetEnd = CRC32_BUF_SZ; } /* Read over all flash words in a buffer, excluding the CRC section * of the first page and all bytes after the remainder bytes in the * last buffer */ for (oset = ((pageIdx == pageBeg && bufNum == 0) ? (IMG_DATA_OFFSET + offset): 0); oset < osetEnd; oset++) { temp1 = (crc >> 8) & 0x00FFFFFFL; temp2 = CRC32_value(((uint32_t)crc ^ crcBuf[oset]) & 0xFF); crc = temp1 ^ temp2; } /* Read data into the next buffer */ if(!useExtFl) { CRC32_memCpy(crcBuf, (uint8_t *)((pageIdx*pageSize) + /*offset + */ ((bufNum + 1)*CRC32_BUF_SZ)), CRC32_BUF_SZ); } else { /* Check to see if the next buffer is on the next page */ if(bufNum == (numBufInCurPg - 1)) { readFlashPg((pageIdx + 1), 0, crcBuf, CRC32_BUF_SZ); } else { readFlashPg(pageIdx, ((bufNum + 1)*CRC32_BUF_SZ), crcBuf, CRC32_BUF_SZ); } } } /* for(uint8_t bufNum = 0; bufNum < numBufInCurPg; bufNum++) */ } /* for (uint8_t pageIdx = pageBeg; pageIdx <= pageEnd; pageIdx++) */ /* XOR CRC with all bits on */ crc = crc ^ 0xFFFFFFFF; return(crc); }