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
6566static mbed_error_ctx * const report_error_ctx = (mbed_error_ctx * )(ERROR_CONTEXT_LOCATION );
6667static 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