Fix error in EEPROM CRC calculation

This commit is contained in:
Martin Jaeger
2019-05-03 08:58:26 +02:00
parent 3fbb6df468
commit ba9baf49c5
+23 -1
View File
@@ -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);