Skip to content

Commit b40a579

Browse files
committed
esp32s2: espidf: Consistent error checking of esp-idf calls
By surrounding most ESP-IDF API calls with this new macro, their failure will result in a CircuitPython exception.
1 parent 05ba143 commit b40a579

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

ports/esp32s2/bindings/espidf/__init__.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,64 @@ const mp_obj_module_t espidf_module = {
127127
.base = { &mp_type_module },
128128
.globals = (mp_obj_dict_t*)&espidf_module_globals,
129129
};
130+
131+
void raise_esp_error(esp_err_t err) {
132+
const compressed_string_t *msg = NULL;
133+
const mp_obj_type_t * exception_type = &mp_type_espidf_IDFError;
134+
switch(err) {
135+
case ESP_FAIL:
136+
msg = translate("Generic Failure");
137+
break;
138+
case ESP_ERR_NO_MEM:
139+
exception_type = &mp_type_espidf_MemoryError;
140+
msg = translate("Out of memory");
141+
break;
142+
case ESP_ERR_INVALID_ARG:
143+
msg = translate("Invalid argument");
144+
break;
145+
case ESP_ERR_INVALID_STATE:
146+
msg = translate("Invalid state");
147+
break;
148+
case ESP_ERR_INVALID_SIZE:
149+
msg = translate("Invalid size");
150+
break;
151+
case ESP_ERR_NOT_FOUND:
152+
msg = translate("Requested resource not found");
153+
break;
154+
case ESP_ERR_NOT_SUPPORTED:
155+
msg = translate("Operation or feature not supported");
156+
break;
157+
case ESP_ERR_TIMEOUT:
158+
msg = translate("Operation timed out");
159+
break;
160+
case ESP_ERR_INVALID_RESPONSE:
161+
msg = translate("Received response was invalid");
162+
break;
163+
case ESP_ERR_INVALID_CRC:
164+
msg = translate("CRC or checksum was invalid");
165+
break;
166+
case ESP_ERR_INVALID_VERSION:
167+
msg = translate("Version was invalid");
168+
break;
169+
case ESP_ERR_INVALID_MAC:
170+
msg = translate("MAC address was invalid");
171+
break;
172+
}
173+
if (msg) {
174+
mp_raise_msg(exception_type, msg);
175+
}
176+
177+
const char *group = "ESP-IDF";
178+
179+
// tests must be in descending order
180+
MP_STATIC_ASSERT( ESP_ERR_FLASH_BASE > ESP_ERR_MESH_BASE );
181+
MP_STATIC_ASSERT( ESP_ERR_MESH_BASE > ESP_ERR_WIFI_BASE );
182+
if(err >= ESP_ERR_FLASH_BASE) {
183+
group = "Flash";
184+
} else if (err >= ESP_ERR_MESH_BASE) {
185+
group = "Mesh";
186+
} else if (err >= ESP_ERR_WIFI_BASE) {
187+
group = "WiFi";
188+
}
189+
mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err);
190+
}

ports/esp32s2/bindings/espidf/__init__.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@
2727
#ifndef MICROPY_INCLUDED_ESP32S2_BINDINGS_ESPIDF___INIT___H
2828
#define MICROPY_INCLUDED_ESP32S2_BINDINGS_ESPIDF___INIT___H
2929

30+
#include "esp_err.h"
31+
#include "py/mpconfig.h"
32+
#include "py/obj.h"
33+
3034
extern const mp_obj_type_t mp_type_espidf_IDFError;
3135
extern const mp_obj_type_t mp_type_espidf_MemoryError;
3236

3337
NORETURN void mp_raise_espidf_MemoryError(void);
3438

39+
void raise_esp_error(esp_err_t err) NORETURN;
40+
#define ESP_CALL_RAISE(x) do { int res = (x); if(res != ESP_OK) raise_esp_error(res); } while(0)
41+
3542
#endif // MICROPY_INCLUDED_ESP32S2_BINDINGS_ESPIDF___INIT___H

ports/esp32s2/esp_error.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "esp_error.h"
28+
#include "py/runtime.h"
29+
30+
#include "bindings/espidf/__init__.h"
31+
32+
void raise_esp_error(esp_err_t err) {
33+
const compressed_string_t *msg = NULL;
34+
const mp_obj_type_t * exception_type = &mp_type_espidf_IDFError;
35+
switch(err) {
36+
case ESP_FAIL:
37+
msg = translate("Generic Failure");
38+
break;
39+
case ESP_ERR_NO_MEM:
40+
exception_type = &mp_type_espidf_MemoryError;
41+
msg = translate("Out of memory");
42+
break;
43+
case ESP_ERR_INVALID_ARG:
44+
msg = translate("Invalid argument");
45+
break;
46+
case ESP_ERR_INVALID_STATE:
47+
msg = translate("Invalid state");
48+
break;
49+
case ESP_ERR_INVALID_SIZE:
50+
msg = translate("Invalid size");
51+
break;
52+
case ESP_ERR_NOT_FOUND:
53+
msg = translate("Requested resource not found");
54+
break;
55+
case ESP_ERR_NOT_SUPPORTED:
56+
msg = translate("Operation or feature not supported");
57+
break;
58+
case ESP_ERR_TIMEOUT:
59+
msg = translate("Operation timed out");
60+
break;
61+
case ESP_ERR_INVALID_RESPONSE:
62+
msg = translate("Received response was invalid");
63+
break;
64+
case ESP_ERR_INVALID_CRC:
65+
msg = translate("CRC or checksum was invalid");
66+
break;
67+
case ESP_ERR_INVALID_VERSION:
68+
msg = translate("Version was invalid");
69+
break;
70+
case ESP_ERR_INVALID_MAC:
71+
msg = translate("MAC address was invalid");
72+
break;
73+
}
74+
if (msg) {
75+
mp_raise_msg(exception_type, msg);
76+
}
77+
78+
const char *group = "ESP-IDF";
79+
80+
// tests must be in descending order
81+
MP_STATIC_ASSERT( ESP_ERR_FLASH_BASE > ESP_ERR_MESH_BASE );
82+
MP_STATIC_ASSERT( ESP_ERR_MESH_BASE > ESP_ERR_WIFI_BASE );
83+
if(err >= ESP_ERR_FLASH_BASE) {
84+
group = "Flash";
85+
} else if (err >= ESP_ERR_MESH_BASE) {
86+
group = "Mesh";
87+
} else if (err >= ESP_ERR_WIFI_BASE) {
88+
group = "WiFi";
89+
}
90+
mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err);
91+
}

0 commit comments

Comments
 (0)