Skip to content

Commit 2a2d5b7

Browse files
HalfSweetaescolar
authored andcommitted
drivers: crc: sf32lb: Fix crc calculation error
When the number of input bits changes, the value in the `CR_DATASIZE` register should be modified accordingly. The CRC32_C algorithm does not require the result to be XOR Signed-off-by: Haoran Jiang <[email protected]>
1 parent 9bdafbf commit 2a2d5b7

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

drivers/crc/crc_sf32lb.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ static int crc_sf32lb_prepare_config(const struct crc_ctx *ctx, uint8_t *polysiz
8585
*width = 16U;
8686
*xor_out = 0U;
8787
break;
88-
case CRC32_C:
89-
__fallthrough;
9088
case CRC32_IEEE:
9189
*polysize = CRC_POLYSIZE_32;
9290
*width = 32U;
9391
*xor_out = 0xFFFFFFFFU;
9492
break;
93+
case CRC32_C:
94+
__fallthrough;
9595
case CRC32_K_4_2:
9696
*polysize = CRC_POLYSIZE_32;
9797
*width = 32U;
@@ -151,8 +151,7 @@ static int crc_sf32lb_begin(const struct device *dev, struct crc_ctx *ctx)
151151
data->xor_out = xor_out;
152152
mask = crc_sf32lb_mask(width);
153153

154-
cr = FIELD_PREP(CRC_CR_DATASIZE_Msk, CRC_DATASIZE_8) |
155-
FIELD_PREP(CRC_CR_POLYSIZE_Msk, polysize);
154+
cr = FIELD_PREP(CRC_CR_POLYSIZE_Msk, polysize);
156155

157156
if ((ctx->reversed & CRC_FLAG_REVERSE_INPUT) != 0U) {
158157
cr |= FIELD_PREP(CRC_CR_REV_IN_Msk, CRC_REV_IN_BYTE);
@@ -196,6 +195,7 @@ static int crc_sf32lb_update(const struct device *dev, struct crc_ctx *ctx, cons
196195
size_t idx = 0U;
197196

198197
for (; idx < aligned_len; idx += sizeof(uint32_t)) {
198+
sys_set_bits(config->base + CRC_CR_OFFSET, CRC_CR_DATASIZE_Msk);
199199
uint32_t data = sys_get_le32(&data_buf[idx]);
200200

201201
sys_write32(data, config->base + CRC_DR_OFFSET);
@@ -213,6 +213,10 @@ static int crc_sf32lb_update(const struct device *dev, struct crc_ctx *ctx, cons
213213
uint32_t rem = 0U;
214214
size_t rem_bytes = bufsize - idx;
215215

216+
sys_clear_bits(config->base + CRC_CR_OFFSET, CRC_CR_DATASIZE_Msk);
217+
sys_set_bits(config->base + CRC_CR_OFFSET,
218+
FIELD_PREP(CRC_CR_DATASIZE_Msk, (rem_bytes - 1U)));
219+
216220
sys_get_le(&rem, &data_buf[idx], rem_bytes);
217221

218222
sys_write32(rem, config->base + CRC_DR_OFFSET);

0 commit comments

Comments
 (0)