Skip to content

Fix for HW CRC calculations on Silicon Labs targets #8026

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 2 commits into from
Sep 24, 2018
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
29 changes: 22 additions & 7 deletions targets/TARGET_Silicon_Labs/TARGET_EFM32/crc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "em_gpcrc.h"

static bool revOutput = false;
static bool enableWordInput = false;
static uint32_t final_xor;

bool hal_crc_is_supported(const crc_mbed_config_t *config)
Expand Down Expand Up @@ -75,21 +76,24 @@ void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
// defined by the mbed API. Emlib does the reversal on the poly, but
// not on the initial value.
if (config->width == 16) {
enableWordInput = false;
crc_init.initValue = __RBIT(config->initial_xor) >> 16;
} else {
enableWordInput = true;
crc_init.initValue = __RBIT(config->initial_xor);
}

// GPCRC operates on bit-reversed inputs and outputs vs the standard
// defined by the mbed API, so reflect_in/out needs to be negated.
if (config->reflect_in) {
crc_init.reverseByteOrder = false;
crc_init.reverseBits = false;
} else {
crc_init.reverseByteOrder = true;
crc_init.reverseBits = true;
}

// Input is little-endian
crc_init.reverseByteOrder = false;

// Disable byte mode to be able to run a faster U32 input version
crc_init.enableByteMode = false;

Expand All @@ -109,19 +113,30 @@ void hal_crc_compute_partial(const uint8_t *data, const size_t size)
return;
}

if (((uint32_t)data & 0x3) != 0 || size < 4) {
// Unaligned or very small input, run a bytewise CRC
if (!enableWordInput || size < sizeof(uint32_t)) {
// Input to a non-word-sized poly, or too small data size for a word input
for (size_t i = 0; i < size; i++) {
GPCRC_InputU8(GPCRC, data[i]);
}
} else {
// Aligned input, run 32-bit inputs as long as possible to make go faster.
size_t i = 0;
for (; i < (size & (~0x3)); i+=4) {

// If input is unaligned, take off as many bytes as needed to align
while (((uint32_t)(data + i) & 0x3) != 0) {
GPCRC_InputU8(GPCRC, data[i]);
i++;
}

// If enough input remaining to do word-sized writes, do so
while ((size - i) >= sizeof(uint32_t)) {
GPCRC_InputU32(GPCRC, *((uint32_t*)(&data[i])));
i += sizeof(uint32_t);
}
for (; i < size; i++) {

// Do byte input to pick off the last remaining bytes
while (i < size) {
GPCRC_InputU8(GPCRC, data[i]);
i++;
}
}
}
Expand Down