Skip to content

Commit 8811972

Browse files
committed
Adjust code for MbedCRC changes
* Make mbed_error use bitwise MbedCRC call rather than local implementation. * Remove use of POLY_32BIT_REV_ANSI from LittleFS. * Move some MbedCRC instances closer to use - construction cost is trivial, and visibility aids compiler optimisation.
1 parent 3939c99 commit 8811972

File tree

7 files changed

+34
-51
lines changed

7 files changed

+34
-51
lines changed

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
252252
#if MBED_CONF_SD_CRC_ENABLED
253253
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
254254
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
255-
_init_ref_count(0), _crc_on(crc_on), _crc16(0, 0, false, false)
255+
_init_ref_count(0), _crc_on(crc_on)
256256
#else
257257
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
258258
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
@@ -678,10 +678,11 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg)
678678
cmdPacket[4] = (arg >> 0);
679679

680680
#if MBED_CONF_SD_CRC_ENABLED
681-
uint32_t crc;
682681
if (_crc_on) {
683-
_crc7.compute((void *)cmdPacket, 5, &crc);
684-
cmdPacket[5] = (char)(crc | 0x01);
682+
MbedCRC<POLY_7BIT_SD, 7> crc7;
683+
uint32_t crc;
684+
crc7.compute(cmdPacket, 5, &crc);
685+
cmdPacket[5] = ((uint8_t) crc << 1) | 0x01;
685686
} else
686687
#endif
687688
{
@@ -899,12 +900,13 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
899900

900901
#if MBED_CONF_SD_CRC_ENABLED
901902
if (_crc_on) {
903+
mbed::MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
902904
uint32_t crc_result;
903905
// Compute and verify checksum
904-
_crc16.compute((void *)buffer, length, &crc_result);
905-
if ((uint16_t)crc_result != crc) {
906-
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
907-
crc, (uint16_t)crc_result);
906+
crc16.compute(buffer, length, &crc_result);
907+
if (crc_result != crc) {
908+
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx32 "\n",
909+
crc, crc_result);
908910
_deselect();
909911
return SD_BLOCK_DEVICE_ERROR_CRC;
910912
}
@@ -934,9 +936,10 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
934936

935937
#if MBED_CONF_SD_CRC_ENABLED
936938
if (_crc_on) {
939+
mbed::MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
937940
uint32_t crc_result;
938941
// Compute and verify checksum
939-
_crc16.compute((void *)buffer, length, &crc_result);
942+
crc16.compute((void *)buffer, length, &crc_result);
940943
if ((uint16_t)crc_result != crc) {
941944
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
942945
crc, (uint16_t)crc_result);
@@ -962,8 +965,9 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len
962965

963966
#if MBED_CONF_SD_CRC_ENABLED
964967
if (_crc_on) {
968+
mbed::MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
965969
// Compute CRC
966-
_crc16.compute((void *)buffer, length, &crc);
970+
crc16.compute(buffer, length, &crc);
967971
}
968972
#endif
969973

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ class SDBlockDevice : public mbed::BlockDevice {
256256

257257
#if MBED_CONF_SD_CRC_ENABLED
258258
bool _crc_on;
259-
mbed::MbedCRC<POLY_7BIT_SD, 7> _crc7;
260-
mbed::MbedCRC<POLY_16BIT_CCITT, 16> _crc16;
261259
#endif
262260
};
263261

drivers/source/MbedCRC.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,11 @@ const uint32_t MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE>::_crc_table[MBED_CRC
146146
#endif // MBED_CRC_TABLE_SIZE > 0
147147

148148
} // namespace mbed
149+
150+
extern "C" uint32_t mbed_tiny_compute_crc32(const void *data, int datalen)
151+
{
152+
mbed::MbedCRC<POLY_32BIT_ANSI, 32, mbed::CrcMode::BITWISE> crc32(0, 0, false, false);
153+
uint32_t result;
154+
crc32.compute(data, datalen, &result);
155+
return result;
156+
}

features/storage/filesystem/littlefs/LittleFileSystem.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ namespace mbed {
2525
extern "C" void lfs_crc(uint32_t *crc, const void *buffer, size_t size)
2626
{
2727
uint32_t initial_xor = *crc;
28-
MbedCRC<POLY_32BIT_REV_ANSI, 32> ct(initial_xor, 0x0, true, false);
29-
ct.compute((void *)buffer, size, (uint32_t *) crc);
28+
// lfs_cache_crc calls lfs_crc for every byte individually, so can't afford
29+
// start-up overhead for hardware acceleration. Limit to table-based.
30+
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> ct(initial_xor, 0x0, true, true);
31+
ct.compute(buffer, size, crc);
3032
}
3133

3234
////// Conversion functions //////

features/storage/kvstore/direct_access_devicekey/DirectAccessDevicekey.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data
309309
{
310310
uint32_t crc;
311311
MbedCRC<POLY_32BIT_ANSI, 32> ct(init_crc, 0x0, true, false);
312-
ct.compute(const_cast<void *>(data_buf), data_size, &crc);
312+
ct.compute(data_buf, data_size, &crc);
313313
return crc;
314314
}
315315
#endif // DEVICE_FLASH

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data
117117
{
118118
uint32_t crc;
119119
MbedCRC<POLY_32BIT_ANSI, 32> ct(init_crc, 0x0, true, false);
120-
ct.compute(const_cast<void *>(data_buf), data_size, &crc);
120+
ct.compute(data_buf, data_size, &crc);
121121
return crc;
122122
}
123123

platform/source/mbed_error.c

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "platform/mbed_power_mgmt.h"
2828
#include "platform/mbed_stats.h"
2929
#include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h"
30+
#include "drivers/MbedCRC.h"
3031
#include "mbed_rtx.h"
3132
#ifdef MBED_CONF_RTOS_PRESENT
3233
#include "rtx_os.h"
@@ -64,36 +65,6 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
6465
//Global for populating the context in exception handler
6566
static mbed_error_ctx *const report_error_ctx = (mbed_error_ctx *)(ERROR_CONTEXT_LOCATION);
6667
static bool is_reboot_error_valid = false;
67-
68-
//Helper function to calculate CRC
69-
//NOTE: It would have been better to use MbedCRC implementation. But
70-
//MbedCRC uses table based calculation and we dont want to keep that table memory
71-
//used up for this purpose. Also we cannot force bitwise calculation in MbedCRC
72-
//and it also requires a new wrapper to be called from C implementation. Since
73-
//we dont have many uses cases to create a C wrapper for MbedCRC and the data
74-
//we calculate CRC on in this context is very less we will use a local
75-
//implementation here.
76-
static unsigned int compute_crc32(const void *data, int datalen)
77-
{
78-
const unsigned int polynomial = 0x04C11DB7; /* divisor is 32bit */
79-
unsigned int crc = 0; /* CRC value is 32bit */
80-
unsigned char *buf = (unsigned char *)data;//use a temp variable to make code readable and to avoid typecasting issues.
81-
82-
for (; datalen > 0; datalen--) {
83-
unsigned char b = *buf++;
84-
crc ^= (unsigned int)(b << 24); /* move byte into upper 8bit */
85-
for (int i = 0; i < 8; i++) {
86-
/* is MSB 1 */
87-
if ((crc & 0x80000000) != 0) {
88-
crc = (unsigned int)((crc << 1) ^ polynomial);
89-
} else {
90-
crc <<= 1;
91-
}
92-
}
93-
}
94-
95-
return crc;
96-
}
9768
#endif
9869

9970
//Helper function to halt the system
@@ -246,7 +217,7 @@ mbed_error_status_t mbed_error_initialize(void)
246217

247218
//Just check if we have valid value for error_status, if error_status is positive(which is not valid), no need to check crc
248219
if (report_error_ctx->error_status < 0) {
249-
crc_val = compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
220+
crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
250221
//Read report_error_ctx and check if CRC is correct, and with valid status code
251222
if ((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0)) {
252223
is_reboot_error_valid = true;
@@ -258,7 +229,7 @@ mbed_error_status_t mbed_error_initialize(void)
258229
if (report_error_ctx->error_reboot_count > 0) {
259230

260231
report_error_ctx->is_error_processed = 1;//Set the flag that we already processed this error
261-
crc_val = compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
232+
crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
262233
report_error_ctx->crc_error_ctx = crc_val;
263234

264235
//Enforce max-reboot only if auto reboot is enabled
@@ -321,7 +292,7 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat
321292

322293
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
323294
uint32_t crc_val = 0;
324-
crc_val = compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
295+
crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
325296
//Read report_error_ctx and check if CRC is correct for report_error_ctx
326297
if (report_error_ctx->crc_error_ctx == crc_val) {
327298
uint32_t current_reboot_count = report_error_ctx->error_reboot_count;
@@ -331,7 +302,7 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat
331302
}
332303
last_error_ctx.is_error_processed = 0;//Set the flag that this is a new error
333304
//Update the struct with crc
334-
last_error_ctx.crc_error_ctx = compute_crc32(&last_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
305+
last_error_ctx.crc_error_ctx = mbed_tiny_compute_crc32(&last_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
335306
//Protect report_error_ctx while we update it
336307
core_util_critical_section_enter();
337308
memcpy(report_error_ctx, &last_error_ctx, sizeof(mbed_error_ctx));
@@ -384,7 +355,7 @@ mbed_error_status_t mbed_reset_reboot_count()
384355
core_util_critical_section_enter();
385356
report_error_ctx->error_reboot_count = 0;//Set reboot count to 0
386357
//Update CRC
387-
crc_val = compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
358+
crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx));
388359
report_error_ctx->crc_error_ctx = crc_val;
389360
core_util_critical_section_exit();
390361
return MBED_SUCCESS;

0 commit comments

Comments
 (0)