From 00930b213e55f4396c1e255092f09bdb8dd8c16f Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Tue, 28 Feb 2023 23:15:31 +0200 Subject: [PATCH] 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 "", line 8, in 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 "", line 1, in 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 --- ports/esp32/mphalport.c | 16 +++++++++++++++- ports/esp32/mphalport.h | 8 +++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index d7003a1437..538b3e405c 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -53,7 +53,12 @@ STATIC uint8_t stdin_ringbuf_array[260]; ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0}; // Check the ESP-IDF error code and raise an OSError if it's not ESP_OK. -void check_esp_err(esp_err_t code) { +#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_NORMAL +void check_esp_err_(esp_err_t code) +#else +void check_esp_err_(esp_err_t code, const char *func, const int line, const char *file) +#endif +{ if (code != ESP_OK) { // map esp-idf error code to posix error code uint32_t pcode = -code; @@ -75,7 +80,16 @@ void check_esp_err(esp_err_t code) { return; } o_str->base.type = &mp_type_str; + #if MICROPY_ERROR_REPORTING > MICROPY_ERROR_REPORTING_NORMAL + char err_msg[64]; + esp_err_to_name_r(code, err_msg, sizeof(err_msg)); + vstr_t vstr; + vstr_init(&vstr, 80); + vstr_printf(&vstr, "0x%04X %s in function '%s' at line %d in file '%s'", code, err_msg, func, line, file); + o_str->data = (const byte *)vstr_null_terminated_str(&vstr); + #else o_str->data = (const byte *)esp_err_to_name(code); // esp_err_to_name ret's ptr to const str + #endif o_str->len = strlen((char *)o_str->data); o_str->hash = qstr_compute_hash(o_str->data, o_str->len); // raise diff --git a/ports/esp32/mphalport.h b/ports/esp32/mphalport.h index 566d6609f1..5e54c24bfe 100644 --- a/ports/esp32/mphalport.h +++ b/ports/esp32/mphalport.h @@ -55,7 +55,13 @@ extern TaskHandle_t mp_main_task_handle; extern ringbuf_t stdin_ringbuf; // Check the ESP-IDF error code and raise an OSError if it's not ESP_OK. -void check_esp_err(esp_err_t code); +#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_NORMAL +#define check_esp_err(code) check_esp_err_(code) +void check_esp_err_(esp_err_t code); +#else +#define check_esp_err(code) check_esp_err_(code, __FUNCTION__, __LINE__, __FILE__) +void check_esp_err_(esp_err_t code, const char *func, const int line, const char *file); +#endif uint32_t mp_hal_ticks_us(void); __attribute__((always_inline)) static inline uint32_t mp_hal_ticks_cpu(void) {