Skip to content

Commit 00930b2

Browse files
IhorNehrutsadpgeorge
authored andcommitted
esp32/mphalport: Add function/line/file info to check_esp_err exception.
Currently, check_esp_err() raises an exception without a location in the source code, eg: Traceback (most recent call last): File "<stdin>", line 8, in <module> OSError: (-258, 'ESP_ERR_INVALID_ARG') This commit allows additional error reporting (function, line and file) to be enabled via detailed exceptions. Change the error reporting config to #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED) and then exception messages from IDF errors look like: Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: (-258, "0x0102 ESP_ERR_INVALID_ARG in function 'set_duty_u16' at line 342 in file './machine_pwm.c'") Signed-off-by: Ihor Nehrutsa <IhorNehrutsa@gmail.com>
1 parent b0e03b3 commit 00930b2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

ports/esp32/mphalport.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ STATIC uint8_t stdin_ringbuf_array[260];
5353
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
5454

5555
// Check the ESP-IDF error code and raise an OSError if it's not ESP_OK.
56-
void check_esp_err(esp_err_t code) {
56+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_NORMAL
57+
void check_esp_err_(esp_err_t code)
58+
#else
59+
void check_esp_err_(esp_err_t code, const char *func, const int line, const char *file)
60+
#endif
61+
{
5762
if (code != ESP_OK) {
5863
// map esp-idf error code to posix error code
5964
uint32_t pcode = -code;
@@ -75,7 +80,16 @@ void check_esp_err(esp_err_t code) {
7580
return;
7681
}
7782
o_str->base.type = &mp_type_str;
83+
#if MICROPY_ERROR_REPORTING > MICROPY_ERROR_REPORTING_NORMAL
84+
char err_msg[64];
85+
esp_err_to_name_r(code, err_msg, sizeof(err_msg));
86+
vstr_t vstr;
87+
vstr_init(&vstr, 80);
88+
vstr_printf(&vstr, "0x%04X %s in function '%s' at line %d in file '%s'", code, err_msg, func, line, file);
89+
o_str->data = (const byte *)vstr_null_terminated_str(&vstr);
90+
#else
7891
o_str->data = (const byte *)esp_err_to_name(code); // esp_err_to_name ret's ptr to const str
92+
#endif
7993
o_str->len = strlen((char *)o_str->data);
8094
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
8195
// raise

ports/esp32/mphalport.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ extern TaskHandle_t mp_main_task_handle;
5555
extern ringbuf_t stdin_ringbuf;
5656

5757
// Check the ESP-IDF error code and raise an OSError if it's not ESP_OK.
58-
void check_esp_err(esp_err_t code);
58+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_NORMAL
59+
#define check_esp_err(code) check_esp_err_(code)
60+
void check_esp_err_(esp_err_t code);
61+
#else
62+
#define check_esp_err(code) check_esp_err_(code, __FUNCTION__, __LINE__, __FILE__)
63+
void check_esp_err_(esp_err_t code, const char *func, const int line, const char *file);
64+
#endif
5965

6066
uint32_t mp_hal_ticks_us(void);
6167
__attribute__((always_inline)) static inline uint32_t mp_hal_ticks_cpu(void) {

0 commit comments

Comments
 (0)