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>
This commit is contained in:
IhorNehrutsa 2023-02-28 23:15:31 +02:00 committed by Damien George
parent b0e03b3e07
commit 00930b213e
2 changed files with 22 additions and 2 deletions

View File

@ -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

View File

@ -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) {