Skip to content

Commit 2d900bd

Browse files
committed
Changed variable names for registers to avoid namespace conflicts, build fixes, macros and other fixes
1 parent 24edf54 commit 2d900bd

File tree

7 files changed

+377
-280
lines changed

7 files changed

+377
-280
lines changed

TESTS/mbed_platform/error_handling/main.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ void test_error_logging()
274274
SET_WARNING(ERROR_INVALID_DATA_DETECTED, "Invalid data", 4 );
275275
SET_WARNING(ERROR_INVALID_OPERATION, "Invalid operation", 5 );
276276

277-
status = get_error_log_info( 3, &error_ctx );
277+
status = get_error_log_info( 2, &error_ctx );
278278
TEST_ASSERT_EQUAL_UINT(ERROR_INVALID_DATA_DETECTED, error_ctx.error_status);
279279
TEST_ASSERT_EQUAL_UINT(4, error_ctx.error_value);
280280

281-
status = get_error_log_info( 4, &error_ctx );
281+
status = get_error_log_info( 3, &error_ctx );
282282
TEST_ASSERT_EQUAL_UINT(ERROR_INVALID_OPERATION, error_ctx.error_status);
283283
TEST_ASSERT_EQUAL_UINT(5, error_ctx.error_value);
284284

@@ -288,12 +288,11 @@ void test_error_logging()
288288
SET_WARNING(ERROR_INVALID_FORMAT, "Invalid format error", 8 );
289289
SET_WARNING(ERROR_NOT_READY, "Not ready error", 9 );
290290

291-
//Last 5 entries
291+
//Last 4 entries
292292
SET_WARNING(ERROR_TIME_OUT, "Timeout error", 10 );
293293
SET_WARNING(ERROR_ALREADY_IN_USE, "Already in use error", 11 );
294294
SET_WARNING(ERROR_UNSUPPORTED, "Not supported error", 12 );
295295
SET_WARNING(ERROR_ACCESS_DENIED, "Access denied error", 13 );
296-
SET_WARNING(ERROR_ITEM_NOT_FOUND, "Not found error", 14 );
297296

298297
status = get_error_log_info( 0, &error_ctx );
299298
TEST_ASSERT_EQUAL_UINT(ERROR_TIME_OUT, error_ctx.error_status);
@@ -311,10 +310,6 @@ void test_error_logging()
311310
TEST_ASSERT_EQUAL_UINT(ERROR_ACCESS_DENIED, error_ctx.error_status);
312311
TEST_ASSERT_EQUAL_UINT(13, error_ctx.error_value);
313312

314-
status = get_error_log_info( 4, &error_ctx );
315-
TEST_ASSERT_EQUAL_UINT(ERROR_ITEM_NOT_FOUND, error_ctx.error_status);
316-
TEST_ASSERT_EQUAL_UINT(14, error_ctx.error_value);
317-
318313
//Try an index which is invalid, we should get ERROR_INVALID_ARGUMENT back
319314
status = get_error_log_info( 99, &error_ctx );
320315
TEST_ASSERT_EQUAL_UINT(ERROR_INVALID_ARGUMENT, status);

platform/mbed_error.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,17 @@ MbedErrorStatus clear_all_errors(void)
236236
{
237237
MbedErrorStatus status = ERROR_SUCCESS;
238238

239+
//Make sure we dont multiple clients resetting
240+
core_util_critical_section_enter();
239241
//Clear the error and context capturing buffer
240242
memset(&last_error_ctx, sizeof(mbed_error_ctx), 0);
241243
//reset error count to 0
242244
error_count = 0;
243245
#ifndef MBED_CONF_ERROR_LOG_DISABLED
244246
status = mbed_log_reset();
245-
#endif
247+
#endif
248+
core_util_critical_section_exit();
249+
246250
return status;
247251
}
248252

platform/mbed_error.h

Lines changed: 208 additions & 208 deletions
Large diffs are not rendered by default.

platform/mbed_error_log.h

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define MBED_ERROR_LOG_H
1818

1919
#ifndef MBED_CONF_ERROR_LOG_SIZE
20-
#define MBED_CONF_ERROR_LOG_SIZE 5
20+
#define MBED_CONF_ERROR_LOG_SIZE 4
2121
#else
2222
#if MBED_CONF_ERROR_LOG_SIZE == 0
2323
#define MBED_CONF_ERROR_LOG_SIZE 1
@@ -27,12 +27,84 @@
2727
#ifdef __cplusplus
2828
extern "C" {
2929
#endif
30+
/*
31+
* Puts/Adds an error entry into the error list
32+
*
33+
* @param error_ctx pointer to the mbed_error_ctx struct with the error context
34+
* @return 0 or ERROR_SUCCESS on success.
35+
* ERROR_WRITE_FAILED if writing to file failed
36+
* ERROR_INVALID_ARGUMENT if path is not valid
37+
*
38+
*
39+
*/
3040
MbedErrorStatus mbed_log_put_error(mbed_error_ctx *error_ctx);
41+
42+
/*
43+
* Reads the error entry from the error list with the specified index
44+
*
45+
* @param index Index of the error context to be retrieved. It starts from 0 and 0 is the oldest.
46+
* @param error_ctx pointer to the mbed_error_ctx struct where the error context will be filled, this should be allocated by the caller
47+
* @return 0 or ERROR_SUCCESS on success.
48+
* ERROR_WRITE_FAILED if writing to file failed
49+
* ERROR_INVALID_ARGUMENT if path is not valid
50+
*
51+
*
52+
*/
3153
MbedErrorStatus mbed_log_get_error(int index, mbed_error_ctx *error_ctx);
54+
55+
/*
56+
* Gets a reference to the next error entry in the error log where in the error ctx can be filled in.
57+
* Its like reserving the next error entry to fill in the error info
58+
*
59+
* @return Returns the pointer to the next error ctx entry
60+
*
61+
*
62+
*/
3263
mbed_error_ctx *mbed_log_get_entry(void);
64+
65+
/*
66+
* Reads the last(latest) error entry from the error list
67+
*
68+
* @param error_ctx pointer to the mbed_error_ctx struct where the error context will be filled, this should be allocated by the caller
69+
* @return 0 or ERROR_SUCCESS on success.
70+
* ERROR_WRITE_FAILED if writing to file failed
71+
* ERROR_INVALID_ARGUMENT if path is not valid
72+
*
73+
*
74+
*/
3375
MbedErrorStatus mbed_log_get_last_error(mbed_error_ctx *error_ctx);
76+
77+
/*
78+
* Returns the number of error entries in the error list
79+
*
80+
* @return Number of entries in the list
81+
*
82+
*
83+
*/
3484
int mbed_log_get_error_log_count(void);
85+
86+
/*
87+
* Resets the error log by resetting the number of errors to 0 and clears all previous errors in the log
88+
*
89+
* @return 0 or ERROR_SUCCESS on success.
90+
* ERROR_WRITE_FAILED if writing to file failed
91+
* ERROR_INVALID_ARGUMENT if path is not valid
92+
*
93+
*
94+
*/
3595
MbedErrorStatus mbed_log_reset(void);
96+
97+
/*
98+
* Saves the error log information to a file
99+
*
100+
* @param path path to the file in the filesystem
101+
* @return 0 or ERROR_SUCCESS on success.
102+
* ERROR_WRITE_FAILED if writing to file failed
103+
* ERROR_INVALID_ARGUMENT if path is not valid
104+
*
105+
* @note Filesystem support is required in order for this function to work.
106+
*
107+
*/
36108
MbedErrorStatus mbed_save_error_log(const char *path);
37109

38110
#ifdef __cplusplus

platform/mbed_error_report.c

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ extern int stdio_uart_inited;
2525
extern serial_t stdio_uart;
2626
#endif
2727

28+
//Helper macro to get the current SP
29+
#define GET_CURRENT_SP(sp) \
30+
{ \
31+
/*If in Handler mode we are always using MSP*/ \
32+
if( __get_IPSR() != 0U ) { \
33+
sp = __get_MSP(); \
34+
} else { \
35+
/*Look into CONTROL.SPSEL value*/ \
36+
if ((__get_CONTROL() & 2U) == 0U) { \
37+
sp = __get_MSP();/*Read MSP*/ \
38+
} else { \
39+
sp = __get_PSP();/*Read PSP*/ \
40+
} \
41+
} \
42+
}
43+
2844
/* Converts a uint32 to hex char string */
2945
static void value_to_hex_str(uint32_t value, char *hex_str)
3046
{
@@ -54,22 +70,6 @@ static void value_to_dec_str(uint32_t value, char *dec_str)
5470
}
5571
}
5672

57-
//Helper function to get the current SP
58-
static unsigned int get_current_sp()
59-
{
60-
//If in Handler mode we are always using MSP
61-
if( __get_IPSR() != 0U ) {
62-
return __get_MSP();
63-
} else {
64-
//Look into CONTROL.SPSEL value
65-
if ((__get_CONTROL() & 2U) == 0U) {
66-
return __get_PSP();//Read PSP
67-
} else {
68-
return __get_MSP();//Read MSP
69-
}
70-
}
71-
}
72-
7373
void mbed_error_init(void)
7474
{
7575
#if DEVICE_SERIAL && (MBED_CONF_ERROR_REPORT_INTERFACE==DEVICE_SERIAL)
@@ -106,7 +106,7 @@ and prints it in hex format.
106106
*/
107107
void mbed_error_print(char *fmtstr, uint32_t *values)
108108
{
109-
#if DEVICE_SERIAL
109+
#if DEVICE_SERIAL || DEVICE_ITM
110110
int i = 0;
111111
int idx = 0;
112112
int vidx = 0;
@@ -119,8 +119,7 @@ void mbed_error_print(char *fmtstr, uint32_t *values)
119119
while(fmtstr[i] != '\0') {
120120
if(fmtstr[i]=='%') {
121121
i++;
122-
if(fmtstr[i]=='x') {
123-
memset(num_str, '0', sizeof(num_str));
122+
if(fmtstr[i]=='x' || fmtstr[i]=='d') {
124123
//print the number in hex format
125124
value_to_hex_str(values[vidx++],num_str);
126125
for(idx=7; idx>=0; idx--) {
@@ -131,7 +130,7 @@ void mbed_error_print(char *fmtstr, uint32_t *values)
131130
memset(num_str, '0', sizeof(num_str));
132131
//print the number in dec format
133132
value_to_dec_str(values[vidx++],num_str);
134-
idx=7;
133+
idx=5;//Start from 5 as we dont have big decimal numbers
135134
while(num_str[idx--]=='0' && idx > 0);//Dont print zeros at front
136135
for(idx++;idx>=0; idx--) {
137136
mbed_error_putc(num_str[idx]);
@@ -146,9 +145,7 @@ void mbed_error_print(char *fmtstr, uint32_t *values)
146145
}
147146
str = NULL;
148147
} else {
149-
//print the % and char without formatting and keep going
150-
mbed_error_putc('%');
151-
mbed_error_putc(fmtstr[i]);
148+
//Do not handle any other % formatting and keep going
152149
}
153150
} else {
154151
//handle carriage returns
@@ -182,67 +179,69 @@ void print_thread(osRtxThread_t *thread)
182179
data[2]=thread->stack_size;
183180
data[3]=(uint32_t)thread->stack_mem;
184181
data[4]=thread->sp;
185-
mbed_error_print("\nState: 0x%x EntryFn: 0x%x Stack Size: 0x%x Mem: 0x%x SP: 0x%x", data);
182+
mbed_error_print("\nState: 0x%x Entry: 0x%x Stack Size: 0x%x Mem: 0x%x SP: 0x%x", data);
186183
}
187184
#endif
188185

186+
/* Prints the error information */
189187
void mbed_report_error(mbed_error_ctx *error_ctx, char *error_msg)
190188
{
191-
int error_code = GET_MBED_ERROR_CODE(error_ctx->error_status);
192-
int error_entity = GET_MBED_ERROR_MODULE(error_ctx->error_status);
189+
uint32_t error_vals[3] = {0};
190+
error_vals[0] = error_ctx->error_status;
191+
error_vals[1] = GET_MBED_ERROR_CODE(error_ctx->error_status);
192+
error_vals[2] = GET_MBED_ERROR_MODULE(error_ctx->error_status);
193193

194-
mbed_error_print("\n\n++ MbedOS Error Info ++\nError Status: 0x%x", (uint32_t *)&error_ctx->error_status);
195-
mbed_error_print("\nError Code: %d", (uint32_t *)&error_code);
196-
mbed_error_print("\nError Entity: %d\nError Message: ", (uint32_t *)&error_entity);
194+
mbed_error_print("\n\n++ MbedOS Error Info ++\nError Status: 0x%x Code: %d Entity: %d\nError Message: ", error_vals);
197195

198-
//Report error info based on error code, some errors require different info
199-
if(error_code == ERROR_CODE_HARDFAULT_EXCEPTION ||
200-
error_code == ERROR_CODE_MEMMANAGE_EXCEPTION ||
201-
error_code == ERROR_CODE_BUSFAULT_EXCEPTION ||
202-
error_code == ERROR_CODE_USAGEFAULT_EXCEPTION ) {
196+
//Report error info based on error code, some errors require different
197+
//error_vals[1] contains the error code
198+
if(error_vals[1] == ERROR_CODE_HARDFAULT_EXCEPTION ||
199+
error_vals[1] == ERROR_CODE_MEMMANAGE_EXCEPTION ||
200+
error_vals[1] == ERROR_CODE_BUSFAULT_EXCEPTION ||
201+
error_vals[1] == ERROR_CODE_USAGEFAULT_EXCEPTION ) {
203202
mbed_error_print(error_msg, NULL);
204-
mbed_error_print("\nError Location: 0x%x\n", (uint32_t *)&error_ctx->error_value);
203+
mbed_error_print("\nLocation: 0x%x\n", (uint32_t *)&error_ctx->error_value);
205204
} else {
206-
switch (error_code) {
205+
switch (error_vals[1]) {
207206
//These are errors reported by kernel handled from mbed_rtx_handlers
208207
case ERROR_CODE_RTOS_EVENT:
209208
mbed_error_print("Kernel Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
210209
break;
211210

212211
case ERROR_CODE_RTOS_THREAD_EVENT:
213-
mbed_error_print("Thread Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
212+
mbed_error_print("Thread: 0x%x, ", (uint32_t *)&error_ctx->error_value);
214213
break;
215214

216215
case ERROR_CODE_RTOS_MUTEX_EVENT:
217-
mbed_error_print("Mutex Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
216+
mbed_error_print("Mutex: 0x%x, ", (uint32_t *)&error_ctx->error_value);
218217
break;
219218

220219
case ERROR_CODE_RTOS_SEMAPHORE_EVENT:
221-
mbed_error_print("Semaphore Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
220+
mbed_error_print("Semaphore: 0x%x, ", (uint32_t *)&error_ctx->error_value);
222221
break;
223222

224223
case ERROR_CODE_RTOS_MEMORY_POOL_EVENT:
225-
mbed_error_print("MemoryPool Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
224+
mbed_error_print("MemoryPool: 0x%x, ", (uint32_t *)&error_ctx->error_value);
226225
break;
227226

228227
case ERROR_CODE_RTOS_EVENT_FLAGS_EVENT:
229-
mbed_error_print("EventFlags Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
228+
mbed_error_print("EventFlags: 0x%x, ", (uint32_t *)&error_ctx->error_value);
230229
break;
231230

232231
case ERROR_CODE_RTOS_TIMER_EVENT:
233-
mbed_error_print("Timer Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
232+
mbed_error_print("Timer: 0x%x, ", (uint32_t *)&error_ctx->error_value);
234233
break;
235234

236235
case ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT:
237-
mbed_error_print("MessageQueue Error: 0x%x, ", (uint32_t *)&error_ctx->error_value);
236+
mbed_error_print("MessageQueue: 0x%x, ", (uint32_t *)&error_ctx->error_value);
238237
break;
239238

240239
default:
241240
//Nothing to do here, just print the error info down
242241
break;
243242
}
244243
mbed_error_print(error_msg, NULL);
245-
mbed_error_print("\nError Location: 0x%x", (uint32_t *)&error_ctx->error_address);
244+
mbed_error_print("\nLocation: 0x%x", (uint32_t *)&error_ctx->error_address);
246245
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
247246
if(NULL != error_ctx->error_filename) {
248247
//for string, we must pass address of a ptr which has the address of the string
@@ -259,12 +258,20 @@ void mbed_report_error(mbed_error_ctx *error_ctx, char *error_msg)
259258
error_ctx->thread_entry_address = (uint32_t)current_thread->thread_addr;
260259
error_ctx->thread_stack_size = current_thread->stack_size;
261260
error_ctx->thread_stack_mem = (uint32_t)current_thread->stack_mem;
262-
error_ctx->thread_current_sp = get_current_sp();
261+
#ifdef TARGET_CORTEX_M
262+
GET_CURRENT_SP(error_ctx->thread_current_sp);
263263

264264
//Take advantage of the fact that the thread info in context struct is consecutively placed
265-
mbed_error_print("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x EntryFn: 0x%x StackSize: 0x%x StackMem: 0x%x SP: 0x%x ",
265+
mbed_error_print("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x Entry: 0x%x StackSize: 0x%x StackMem: 0x%x SP: 0x%x ",
266266
(uint32_t *)&error_ctx->error_value);
267-
#endif
267+
#else
268+
//For Cortex-A targets we dont have support to capture the current SP
269+
//Take advantage of the fact that the thread info in context struct is consecutively placed
270+
mbed_error_print("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x Entry: 0x%x StackSize: 0x%x StackMem: 0x%x ",
271+
(uint32_t *)&error_ctx->error_value);
272+
#endif //TARGET_CORTEX_M
273+
274+
#endif //MBED_CONF_RTOS_PRESENT
268275
}
269276

270277
mbed_error_print("\n-- MbedOS Error Info --", NULL);

0 commit comments

Comments
 (0)