Skip to content

Commit ee6a07e

Browse files
committed
K64F CRC driver: Fix handling of CRC final XOR value
According to the test results final XOR was incorrectly handled by the CRC driver. This patch fixes this issue.
1 parent da557a5 commit ee6a07e

File tree

1 file changed

+31
-15
lines changed
  • targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F

1 file changed

+31
-15
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/mbed_crc_api.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ void hal_crc_compute_partial_start(const crc_mbed_config_t* config)
5050
platform_config.seed = config->initial_xor;
5151
platform_config.reflectIn = config->reflect_in;
5252
platform_config.reflectOut = config->reflect_out;
53-
platform_config.complementChecksum = (config->final_xor == 0xFFFFFFFFU);
53+
if ((width == kCrcBits16 && config->final_xor == 0xFFFFU) ||
54+
(width == kCrcBits32 && config->final_xor == 0xFFFFFFFFU)) {
55+
platform_config.complementChecksum = true;
56+
} else {
57+
platform_config.complementChecksum = false;
58+
}
5459
platform_config.crcBits = width;
5560
platform_config.crcResult = kCrcFinalChecksum;
5661

@@ -72,20 +77,31 @@ void hal_crc_compute_partial(const uint8_t *data, const size_t size)
7277

7378
uint32_t hal_crc_get_result(void)
7479
{
75-
if ((final_xor != 0x00000000U) && (final_xor != 0xFFFFFFFFU)) {
76-
CRC_WriteData(CRC0, (uint8_t*)&final_xor, sizeof(final_xor));
77-
}
78-
79-
switch (width)
80-
{
81-
case kCrcBits16:
82-
return CRC_Get16bitResult(CRC0);
83-
case kCrcBits32:
84-
return CRC_Get32bitResult(CRC0);
85-
default:
86-
MBED_ASSERT("Unhandled switch case");
87-
return 0;
88-
}
80+
uint32_t result;
81+
82+
const bool manual_final_xor = ((final_xor != 0x00000000U) &&
83+
((final_xor != 0xFFFFFFFFU && width == kCrcBits32) ||
84+
(final_xor != 0xFFFFU && width == kCrcBits16)));
85+
86+
switch (width)
87+
{
88+
case kCrcBits16:
89+
result = CRC_Get16bitResult(CRC0);
90+
if (manual_final_xor) {
91+
result ^= final_xor;
92+
result &= 0xFFFF;
93+
}
94+
return result;
95+
case kCrcBits32:
96+
result = CRC_Get32bitResult(CRC0);
97+
if (manual_final_xor) {
98+
result ^= final_xor;
99+
}
100+
return result;
101+
default:
102+
MBED_ASSERT("Unhandled switch case");
103+
return 0;
104+
}
89105
}
90106

91107
#endif // DEVICE_CRC

0 commit comments

Comments
 (0)