Skip to content

Commit ec7ed60

Browse files
committed
Fixes soft reset crash
I don't know if this is going to work or not. Hopefully it will fix the soft reset crash that occurs after files get uploaded to the ESP32. Fingers crossed on this...
1 parent 37bee5a commit ec7ed60

File tree

15 files changed

+494
-167
lines changed

15 files changed

+494
-167
lines changed

builder/esp32.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,28 @@ def update_mphalport():
11011101

11021102
def update_main():
11031103
data = read_file('esp32', MAIN_PATH)
1104+
1105+
rep_data = [
1106+
'#if SOC_LCD_I80_SUPPORTED',
1107+
'#include "../../../../ext_mod/lcd_bus/esp32_include/i80_bus.h"',
1108+
'#endif',
1109+
'',
1110+
'#if SOC_LCD_RGB_SUPPORTED',
1111+
'#include "../../../../ext_mod/lcd_bus/esp32_include/rgb_bus.h"',
1112+
'#endif',
1113+
'',
1114+
'#include "../../../../ext_mod/lcd_bus/esp32_include/spi_bus.h"',
1115+
'#include "../../../../ext_mod/lcd_bus/esp32_include/i2c_bus.h"',
1116+
'#include "../../../../micropy_updates/common/mp_spi_common.h"'
1117+
'',
1118+
'#if MICROPY_BLUETOOTH_NIMBLE'
1119+
]
1120+
1121+
data = data.replace(
1122+
'#if MICROPY_BLUETOOTH_NIMBLE',
1123+
'\n'.join(rep_data),
1124+
1
1125+
)
11041126
data = data.replace(
11051127
'#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG',
11061128
'#if MP_USB_SERIAL_JTAG'
@@ -1110,6 +1132,29 @@ def update_main():
11101132
'#elif MP_USB_OTG'
11111133
)
11121134

1135+
rep_data = [
1136+
'soft_reset_exit:',
1137+
' ',
1138+
'#if SOC_LCD_I80_SUPPORTED',
1139+
' mp_lcd_i80_bus_deinit_all();',
1140+
'#endif',
1141+
' ',
1142+
'#if SOC_LCD_RGB_SUPPORTED',
1143+
' mp_lcd_rgb_bus_deinit_all();',
1144+
'#endif',
1145+
' ',
1146+
' mp_lcd_spi_bus_deinit_all();',
1147+
' ',
1148+
' mp_lcd_i2c_bus_deinit_all();',
1149+
' ',
1150+
' machine_hw_spi_bus_deinit_all();'
1151+
]
1152+
1153+
data = data.replace(
1154+
'soft_reset_exit:',
1155+
'\n'.join(rep_data)
1156+
)
1157+
11131158
write_file(MAIN_PATH, data)
11141159

11151160

@@ -1208,6 +1253,7 @@ def build_sdkconfig(*args):
12081253
f.write('\n'.join(base_config))
12091254

12101255

1256+
12111257
def compile(*args): # NOQA
12121258
global PORT
12131259
global flash_size

ext_mod/lcd_bus/esp32_include/i2c_bus.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
mp_obj_t callback;
2020

21-
void *buf1;
22-
void *buf2;
21+
mp_obj_array_t *view1;
22+
mp_obj_array_t *view2;
23+
2324
uint32_t buffer_flags;
2425

2526
bool trans_done;
@@ -35,4 +36,7 @@
3536
} mp_lcd_i2c_bus_obj_t;
3637

3738
extern const mp_obj_type_t mp_lcd_i2c_bus_type;
39+
40+
extern void mp_lcd_i2c_bus_deinit_all(void);
41+
3842
#endif /* _ESP32_I2C_BUS_H_ */

ext_mod/lcd_bus/esp32_include/i80_bus.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222

2323
mp_obj_t callback;
2424

25-
void *buf1;
26-
void *buf2;
25+
mp_obj_array_t *view1;
26+
mp_obj_array_t *view2;
27+
2728
uint32_t buffer_flags;
2829

2930
bool trans_done;
@@ -38,6 +39,8 @@
3839

3940
extern const mp_obj_type_t mp_lcd_i80_bus_type;
4041

42+
extern void mp_lcd_i80_bus_deinit_all(void);
43+
4144
#else
4245
#include "../common_include/i80_bus.h"
4346

ext_mod/lcd_bus/esp32_include/rgb_bus.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@
140140

141141
extern const mp_obj_type_t mp_lcd_rgb_bus_type;
142142

143+
extern void mp_lcd_rgb_bus_deinit_all(void);
144+
145+
143146
#endif /* _ESP32_RGB_BUS_H_ */
144147
#else
145148
#include "../common_include/rgb_bus.h"

ext_mod/lcd_bus/esp32_include/spi_bus.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222

2323
mp_obj_t callback;
2424

25-
void *buf1;
26-
void *buf2;
25+
mp_obj_array_t *view1;
26+
mp_obj_array_t *view2;
27+
2728
uint32_t buffer_flags;
2829

2930
bool trans_done;
@@ -39,5 +40,9 @@
3940
} mp_lcd_spi_bus_obj_t;
4041

4142
extern const mp_obj_type_t mp_lcd_spi_bus_type;
43+
44+
extern void mp_lcd_spi_bus_deinit_all(void);
45+
46+
4247
#endif /* _ESP32_SPI_BUS_H_ */
4348

ext_mod/lcd_bus/esp32_src/i2c_bus.c

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ mp_lcd_err_t i2c_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
2323
mp_lcd_err_t i2c_get_lane_count(mp_obj_t obj, uint8_t *lane_count);
2424

2525

26+
static uint8_t i2c_bus_count = 0;
27+
static mp_lcd_i2c_bus_obj_t **i2c_bus_objs;
28+
29+
30+
void mp_lcd_i2c_bus_deinit_all(void)
31+
{
32+
// we need to copy the existing array to a new one so the order doesn't
33+
// get all mucked up when objects get removed.
34+
mp_lcd_i2c_bus_obj_t *objs[i2c_bus_count];
35+
36+
for (uint8_t i=0;i<i2c_bus_count;i++) {
37+
objs[i] = i2c_bus_objs[i];
38+
}
39+
40+
for (uint8_t i=0;i<i2c_bus_count;i++) {
41+
spi_del(MP_OBJ_FROM_PTR(objs[i]));
42+
}
43+
}
44+
45+
2646
static mp_obj_t mp_lcd_i2c_bus_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args)
2747
{
2848
enum {
@@ -100,16 +120,57 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
100120
{
101121
mp_lcd_i2c_bus_obj_t *self = (mp_lcd_i2c_bus_obj_t *)obj;
102122

103-
mp_lcd_err_t ret = esp_lcd_panel_io_del(self->panel_io_handle.panel_io);
104-
if (ret != 0) {
105-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_panel_io_del)"), ret);
106-
}
107-
ret = i2c_driver_delete(self->host);
108-
if (ret != 0) {
109-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(i2c_driver_delete)"), ret);
123+
if (self->panel_io_handle.panel_io != NULL) {
124+
125+
mp_lcd_err_t ret = esp_lcd_panel_io_del(self->panel_io_handle.panel_io);
126+
if (ret != 0) {
127+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_panel_io_del)"), ret);
128+
return ret;
129+
}
130+
131+
ret = i2c_driver_delete(self->host);
132+
if (ret != 0) {
133+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(i2c_driver_delete)"), ret);
134+
return ret;
135+
}
136+
137+
self->panel_io_handle.panel_io = NULL;
138+
139+
if (self->view1 != NULL) {
140+
heap_caps_free(self->view1->items);
141+
self->view1->items = NULL;
142+
self->view1->len = 0
143+
self->view1 = NULL;
144+
LCD_DEBUG_PRINT("i2c_free_framebuffer(self, buf=1)\n")
145+
}
146+
147+
if (self->view2 != NULL) {
148+
heap_caps_free(self->view2->items);
149+
self->view2->items = NULL;
150+
self->view2->len = 0
151+
self->view2 = NULL;
152+
LCD_DEBUG_PRINT("i2c_free_framebuffer(self, buf=1)\n")
153+
}
154+
155+
uint8_t i= 0;
156+
for (;i<i2c_bus_count;i++) {
157+
if (i2c_bus_objs[i] == self) {
158+
i2c_bus_objs[i] = NULL;
159+
break;
160+
}
161+
}
162+
163+
for (uint8_t j=i + 1;j<i2c_bus_count;j++) {
164+
i2c_bus_objs[j - i + 1] = i2c_bus_objs[j];
165+
}
166+
167+
i2c_bus_count--;
168+
i2c_bus_objs = m_realloc(i2c_bus_objs, i2c_bus_count * sizeof(mp_lcd_i2c_bus_obj_t *));
169+
170+
return ret;
171+
} else {
172+
return LCD_FAIL;
110173
}
111-
112-
return ret;
113174
}
114175

115176

@@ -129,19 +190,27 @@ mp_lcd_err_t i2c_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
129190
mp_lcd_err_t ret = i2c_param_config(self->host, &self->bus_config);
130191
if (ret != 0) {
131192
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(i2c_param_config)"), ret);
193+
return ret;
132194
}
133195

134196
ret = i2c_driver_install(self->host, I2C_MODE_MASTER, 0, 0, 0);
135197
if (ret != 0) {
136198
mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("%d(i2c_driver_install)"), ret);
199+
return ret;
137200
}
138201

139202
ret = esp_lcd_new_panel_io_i2c(self->bus_handle , &self->panel_io_config, &self->panel_io_handle.panel_io);
140203

141204
if (ret != 0) {
142205
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_new_panel_io_i2c)"), ret);
206+
return ret;
143207
}
144208

209+
// add the new bus ONLY after successfull initilization of the bus
210+
i2c_bus_count++;
211+
m_realloc(i2c_bus_objs, i2c_bus_count * sizeof(mp_lcd_i2c_bus_obj_t *));
212+
spi_bus_objs[i2c_bus_count - 1] = self;
213+
145214
return ret;
146215
}
147216

ext_mod/lcd_bus/esp32_src/i80_bus.c

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@
2525
mp_lcd_err_t i80_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp, uint32_t buffer_size, bool rgb565_byte_swap, uint8_t cmd_bits, uint8_t param_bits);
2626
mp_lcd_err_t i80_get_lane_count(mp_obj_t obj, uint8_t *lane_count);
2727

28+
29+
static uint8_t i80_bus_count = 0;
30+
static mp_lcd_i80_bus_obj_t **i80_bus_objs;
31+
32+
33+
void mp_lcd_i80_bus_deinit_all(void)
34+
{
35+
// we need to copy the existing array to a new one so the order doesn't
36+
// get all mucked up when objects get removed.
37+
mp_lcd_i80_bus_obj_t *objs[i80_bus_count];
38+
39+
for (uint8_t i=0;i<i80_bus_count;i++) {
40+
objs[i] = i80_bus_objs[i];
41+
}
42+
43+
for (uint8_t i=0;i<i80_bus_count;i++) {
44+
i80_del(MP_OBJ_FROM_PTR(objs[i]));
45+
}
46+
}
47+
48+
2849
static mp_obj_t mp_lcd_i80_bus_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args)
2950
{
3051

@@ -194,6 +215,11 @@
194215
LCD_DEBUG_PRINT("i80_init(self, width=%i, height=%i, bpp=%i, buffer_size=%lu, rgb565_byte_swap=%i, cmd_bits=%i, param_bits=%i)\n", width, height, bpp, buffer_size, (uint8_t)rgb565_byte_swap, cmd_bits, param_bits)
195216

196217
mp_lcd_i80_bus_obj_t *self = (mp_lcd_i80_bus_obj_t *)obj;
218+
219+
if (self->panel_io_handle.panel_io != NULL) {
220+
return LCD_FAIL;
221+
}
222+
197223
self->rgb565_byte_swap = false;
198224

199225
if (rgb565_byte_swap && bpp == 16) {
@@ -220,6 +246,11 @@
220246
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_new_panel_io_i80)"), ret);
221247
}
222248

249+
// add the new bus ONLY after successfull initilization of the bus
250+
i80_bus_count++;
251+
m_realloc(i80_bus_objs, i80_bus_count * sizeof(mp_lcd_i80_bus_obj_t *));
252+
i80_bus_objs[i80_bus_count - 1] = self;
253+
223254
return ret;
224255
}
225256

@@ -230,17 +261,55 @@
230261

231262
mp_lcd_i80_bus_obj_t *self = (mp_lcd_i80_bus_obj_t *)obj;
232263

233-
mp_lcd_err_t ret = esp_lcd_panel_io_del(self->panel_io_handle.panel_io);
234-
if (ret != 0) {
235-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_panel_io_del)"), ret);
236-
}
264+
if (self->panel_io_handle.panel_io != NULL) {
265+
mp_lcd_err_t ret = esp_lcd_panel_io_del(self->panel_io_handle.panel_io);
266+
if (ret != 0) {
267+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_panel_io_del)"), ret);
268+
return ret;
269+
}
237270

238-
ret = esp_lcd_del_i80_bus(self->bus_handle);
239-
if (ret != 0) {
240-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_del_i80_bus)"), ret);
241-
}
271+
ret = esp_lcd_del_i80_bus(self->bus_handle);
272+
if (ret != 0) {
273+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_del_i80_bus)"), ret);
274+
return ret;
275+
}
242276

243-
return ret;
277+
self->panel_io_handle.panel_io = NULL
278+
279+
if (self->view1 != NULL) {
280+
heap_caps_free(self->view1->items);
281+
self->view1->items = NULL;
282+
self->view1->len = 0
283+
self->view1 = NULL;
284+
LCD_DEBUG_PRINT("i80_free_framebuffer(self, buf=1)\n")
285+
}
286+
287+
if (self->view2 != NULL) {
288+
heap_caps_free(self->view2->items);
289+
self->view2->items = NULL;
290+
self->view2->len = 0
291+
self->view2 = NULL;
292+
LCD_DEBUG_PRINT("i80_free_framebuffer(self, buf=1)\n")
293+
}
294+
295+
uint8_t i= 0;
296+
for (;i<i80_bus_count;i++) {
297+
if (i80_bus_objs[i] == self) {
298+
i80_bus_objs[i] = NULL;
299+
break;
300+
}
301+
}
302+
303+
for (uint8_t j=i + 1;j<i80_bus_count;j++) {
304+
i80_bus_objs[j - i + 1] = i80_bus_objs[j];
305+
}
306+
307+
i80_bus_count--;
308+
i80_bus_objs = m_realloc(i80_bus_objs, i80_bus_count * sizeof(mp_lcd_i80_bus_obj_t *));
309+
return ret;
310+
} else {
311+
return LCD_FAIL;
312+
}
244313
}
245314

246315
mp_lcd_err_t i80_get_lane_count(mp_obj_t obj, uint8_t *lane_count)

0 commit comments

Comments
 (0)