From ba9baf49c516c5d5ededa407efb0cbf521d8e2ef Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Fri, 3 May 2019 08:58:26 +0200 Subject: [PATCH] Fix error in EEPROM CRC calculation --- src/eeprom.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/eeprom.cpp b/src/eeprom.cpp index d61cf53..ea1718f 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -1,3 +1,18 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include "pcb.h" #include "thingset.h" @@ -37,7 +52,14 @@ uint32_t _calc_crc(uint8_t *buf, size_t len) CRC->CR |= CRC_CR_RESET; for (size_t i = 0; i < len; i += 4) { //printf("CRC buf: %.8x, CRC->DR: %.8x\n", *((uint32_t*)&buf[i]), CRC->DR); - CRC->DR = *((uint32_t*)&buf[i]); + size_t remaining_bytes = len - i; + if (remaining_bytes >= 4) { + CRC->DR = *((uint32_t*)&buf[i]); + } + else { + // ignore bytes >= len if len is not a multiple of 4 + CRC->DR = *((uint32_t*)&buf[i]) & (0xFFFFFFFFU >> (32 - remaining_bytes * 8)); + } } RCC->AHBENR &= ~(RCC_AHBENR_CRCEN);