From b6fac581f7b11f1f59ed34c9f11d180bbc7c90c5 Mon Sep 17 00:00:00 2001 From: Jacob Pease Date: Mon, 22 Jul 2024 01:19:10 -0500 Subject: [PATCH] Corrected the CRC7 code with the right sequence of instructions. --- fpga/zsbl/sd.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fpga/zsbl/sd.c b/fpga/zsbl/sd.c index 4ffb53d67..8781dd5c3 100644 --- a/fpga/zsbl/sd.c +++ b/fpga/zsbl/sd.c @@ -1,12 +1,15 @@ #include "sd.h" #include "spi.h" +// Parallel byte update CRC7-CCITT algorithm. +// The result is the CRC7 result, left shifted over by 1 +// which is perfect, since we append a 1 at the end anyway uint8_t crc7(uint8_t prev, uint8_t in) { // CRC polynomial 0x89 - uint8_t remainder = prev & in; + uint8_t remainder = prev ^ in; remainder ^= (remainder >> 4) ^ (remainder >> 7); - remainder ^= remainder << 4; - return remainder & 0x7f; + remainder = (remainder << 1) ^ (remainder << 4); + return remainder & 0xff; } uint16_t crc16(uint16_t crc, uint8_t data) {