unleashed-firmware/firmware/targets/f7/furi_hal/furi_hal_crc.c
hedger 7ce305fca3
[FL-2269] Core2 OTA (#1144)
* C2OTA: wip
* Update Cube to 1.13.3
* Fixed prio
* Functional Core2 updater
* Removed hardware CRC usage; code cleanup & linter fixes
* Moved hardcoded stack params to copro.mk
* Fixing CI bundling of core2 fw
* Removed last traces of hardcoded radio stack
* OB processing draft
* Python scripts cleanup
* Support for comments in ob data
* Sacrificed SD card icon in favor of faster update. Waiting for Storage fix
* Additional handling for OB mismatched values
* Description for new furi_hal apis; spelling fixes
* Rework of OB write, WIP
* Properly restarting OB verification loop
* Split update_task_workers.c
* Checking OBs after enabling post-update mode
* Moved OB verification before flashing
* Removed ob.data for custom stacks
* Fixed progress calculation for OB
* Removed unnecessary OB mask cast

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-04-27 18:53:48 +03:00

94 lines
3.1 KiB
C

#include <furi_hal.h>
#include <stm32wbxx_ll_crc.h>
typedef enum {
CRC_State_Reset,
CRC_State_Ready,
CRC_State_Busy,
} CRC_State;
typedef struct {
CRC_State state;
osMutexId_t mtx;
} HAL_CRC_Control;
static volatile HAL_CRC_Control hal_crc_control = {
.state = CRC_State_Reset,
.mtx = NULL,
};
void furi_hal_crc_init(bool synchronize) {
/* initialize peripheral with default generating polynomial */
LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_BYTE);
LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_BIT);
LL_CRC_SetPolynomialCoef(CRC, LL_CRC_DEFAULT_CRC32_POLY);
LL_CRC_SetPolynomialSize(CRC, LL_CRC_POLYLENGTH_32B);
LL_CRC_SetInitialData(CRC, LL_CRC_DEFAULT_CRC_INITVALUE);
if(synchronize) {
hal_crc_control.mtx = osMutexNew(NULL);
}
hal_crc_control.state = CRC_State_Ready;
}
void furi_hal_crc_reset() {
furi_check(hal_crc_control.state == CRC_State_Ready);
if(hal_crc_control.mtx) {
furi_check(osMutexGetOwner(hal_crc_control.mtx) == osThreadGetId());
osMutexRelease(hal_crc_control.mtx);
}
LL_CRC_ResetCRCCalculationUnit(CRC);
}
static uint32_t furi_hal_crc_handle_8(uint8_t pBuffer[], uint32_t BufferLength) {
uint32_t i; /* input data buffer index */
hal_crc_control.state = CRC_State_Busy;
/* Processing time optimization: 4 bytes are entered in a row with a single word write,
* last bytes must be carefully fed to the CRC calculator to ensure a correct type
* handling by the peripheral */
for(i = 0U; i < (BufferLength / 4U); i++) {
LL_CRC_FeedData32(
CRC,
((uint32_t)pBuffer[4U * i] << 24U) | ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) |
((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | (uint32_t)pBuffer[(4U * i) + 3U]);
}
/* last bytes specific handling */
if((BufferLength % 4U) != 0U) {
if((BufferLength % 4U) == 1U) {
LL_CRC_FeedData8(CRC, pBuffer[4U * i]);
} else if((BufferLength % 4U) == 2U) {
LL_CRC_FeedData16(
CRC, ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]);
} else if((BufferLength % 4U) == 3U) {
LL_CRC_FeedData16(
CRC, ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]);
LL_CRC_FeedData8(CRC, pBuffer[(4U * i) + 2U]);
}
}
hal_crc_control.state = CRC_State_Ready;
/* Return the CRC computed value */
return LL_CRC_ReadData32(CRC);
}
static uint32_t furi_hal_crc_accumulate(uint32_t pBuffer[], uint32_t BufferLength) {
furi_check(hal_crc_control.state == CRC_State_Ready);
if(hal_crc_control.mtx) {
furi_check(osMutexGetOwner(hal_crc_control.mtx) != NULL);
}
return furi_hal_crc_handle_8((uint8_t*)pBuffer, BufferLength);
}
uint32_t furi_hal_crc_feed(void* data, uint16_t length) {
return ~furi_hal_crc_accumulate(data, length);
}
bool furi_hal_crc_acquire(uint32_t timeout) {
furi_assert(hal_crc_control.mtx);
if(osMutexAcquire(hal_crc_control.mtx, timeout) == osOK) {
LL_CRC_ResetCRCCalculationUnit(CRC);
return true;
}
return false;
}