Skip to content

Commit

Permalink
esp32/mphalport: Add function/line/file info to check_esp_err exception.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
IhorNehrutsa authored and dpgeorge committed Sep 18, 2023
1 parent b0e03b3 commit 00930b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion ports/esp32/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 00930b2

Please sign in to comment.