Skip to content

Fix inconsistency between mbed crc and psoc6 crc implementations. #12324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion targets/TARGET_Cypress/TARGET_PSOC6/cy_crc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ static cyhal_crc_t cy_crc;
static crc_algorithm_t cy_crc_cfg;
static bool cy_crc_initialized = false;

// Reverses width least significant bits of 32 bit input. Any bits more
// significant than width are dropped.
static uint32_t reverse(uint8_t width, uint32_t in)
{
MBED_ASSERT(width <= 32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I wouldn't bother with the assert - this is internal, and you've already cleared the HAL_CRC_IS_SUPPORTED check, and there are a squillion uint32_ts around here. 32-bit or less is kind of assumed.

Actually though, having this after HAL_CRC_IS_SUPPORTED means the compiler should hopefully skip putting in the assert anyway. So maybe I shouldn't be worrying about code size.

return __RBIT(in) >> (32 - width);
}

void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
{
if (!cy_crc_initialized) {
Expand All @@ -46,9 +54,17 @@ void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
cy_crc_cfg.polynomial = config->polynomial;
cy_crc_cfg.lfsrInitState = config->initial_xor;
cy_crc_cfg.dataXor = 0;
cy_crc_cfg.remXor = config->final_xor;
cy_crc_cfg.dataReverse = config->reflect_in ? 1 : 0;
cy_crc_cfg.remReverse = config->reflect_out ? 1 : 0;

// There is an incongruity between what MBeds CRC spec expects and what
// PSoC6 hardware actually performs when it comes to the final XOR and
// remainder reversal: MBed expects the final remainder to be reversed then
// XORed with remXor while PSoC6s CRC, however, performs the final XOR with
// remXor then reverses the resulting value. Since Rev(A) XOR B == Rev( A
// XOR Rev(B) ), a simple fix is to reverse remXor if reflect_out is true.
cy_crc_cfg.remXor = config->reflect_out ? reverse(config->width, config->final_xor) : config->final_xor;

if (CY_RSLT_SUCCESS != cyhal_crc_start(&cy_crc, &cy_crc_cfg)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_crc_start");
}
Expand Down