27
27
#include "platform/mbed_power_mgmt.h"
28
28
#include "platform/mbed_stats.h"
29
29
#include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h"
30
+ #include "drivers/MbedCRC.h"
30
31
#include "mbed_rtx.h"
31
32
#ifdef MBED_CONF_RTOS_PRESENT
32
33
#include "rtx_os.h"
@@ -64,36 +65,6 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
64
65
//Global for populating the context in exception handler
65
66
static mbed_error_ctx * const report_error_ctx = (mbed_error_ctx * )(ERROR_CONTEXT_LOCATION );
66
67
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
- }
97
68
#endif
98
69
99
70
//Helper function to halt the system
@@ -246,7 +217,7 @@ mbed_error_status_t mbed_error_initialize(void)
246
217
247
218
//Just check if we have valid value for error_status, if error_status is positive(which is not valid), no need to check crc
248
219
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 ));
250
221
//Read report_error_ctx and check if CRC is correct, and with valid status code
251
222
if ((report_error_ctx -> crc_error_ctx == crc_val ) && (report_error_ctx -> is_error_processed == 0 )) {
252
223
is_reboot_error_valid = true;
@@ -258,7 +229,7 @@ mbed_error_status_t mbed_error_initialize(void)
258
229
if (report_error_ctx -> error_reboot_count > 0 ) {
259
230
260
231
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 ));
262
233
report_error_ctx -> crc_error_ctx = crc_val ;
263
234
264
235
//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
321
292
322
293
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
323
294
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 ));
325
296
//Read report_error_ctx and check if CRC is correct for report_error_ctx
326
297
if (report_error_ctx -> crc_error_ctx == crc_val ) {
327
298
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
331
302
}
332
303
last_error_ctx .is_error_processed = 0 ;//Set the flag that this is a new error
333
304
//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 ));
335
306
//Protect report_error_ctx while we update it
336
307
core_util_critical_section_enter ();
337
308
memcpy (report_error_ctx , & last_error_ctx , sizeof (mbed_error_ctx ));
@@ -384,7 +355,7 @@ mbed_error_status_t mbed_reset_reboot_count()
384
355
core_util_critical_section_enter ();
385
356
report_error_ctx -> error_reboot_count = 0 ;//Set reboot count to 0
386
357
//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 ));
388
359
report_error_ctx -> crc_error_ctx = crc_val ;
389
360
core_util_critical_section_exit ();
390
361
return MBED_SUCCESS ;
0 commit comments