Skip to content

Commit d34dcaa

Browse files
committed
feat: update
1 parent 9896440 commit d34dcaa

File tree

3 files changed

+50
-103
lines changed

3 files changed

+50
-103
lines changed

ports/esp32/main_esp32s3_box3/esp32_common.cmake

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# This is the common ESP-IDF "main component" CMakeLists.txt contents for MicroPython.
2-
#
3-
# This file is included directly from a main_${IDF_TARGET}/CMakeLists.txt file
4-
# (or included from an out-of-tree main component CMakeLists.txt for out-of-tree
5-
# builds.)
6-
71
# Set location of base MicroPython directory.
82
if(NOT MICROPY_DIR)
93
get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../.. ABSOLUTE)
@@ -42,7 +36,6 @@ list(APPEND MICROPY_SOURCE_SHARED
4236
${MICROPY_DIR}/shared/netutils/netutils.c
4337
${MICROPY_DIR}/shared/timeutils/timeutils.c
4438
${MICROPY_DIR}/shared/runtime/interrupt_char.c
45-
${MICROPY_DIR}/shared/runtime/mpirq.c
4639
${MICROPY_DIR}/shared/runtime/stdout_helpers.c
4740
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
4841
${MICROPY_DIR}/shared/runtime/pyexec.c
@@ -63,37 +56,6 @@ list(APPEND MICROPY_SOURCE_DRIVERS
6356
${MICROPY_DIR}/drivers/dht/dht.c
6457
)
6558

66-
string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/tinyusb)
67-
if(MICROPY_PY_TINYUSB)
68-
set(TINYUSB_SRC "${MICROPY_DIR}/lib/tinyusb/src")
69-
string(TOUPPER OPT_MCU_${IDF_TARGET} tusb_mcu)
70-
71-
list(APPEND MICROPY_DEF_TINYUSB
72-
CFG_TUSB_MCU=${tusb_mcu}
73-
)
74-
75-
list(APPEND MICROPY_SOURCE_TINYUSB
76-
${TINYUSB_SRC}/tusb.c
77-
${TINYUSB_SRC}/common/tusb_fifo.c
78-
${TINYUSB_SRC}/device/usbd.c
79-
${TINYUSB_SRC}/device/usbd_control.c
80-
${TINYUSB_SRC}/class/cdc/cdc_device.c
81-
${TINYUSB_SRC}/portable/synopsys/dwc2/dcd_dwc2.c
82-
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
83-
${MICROPY_DIR}/shared/tinyusb/mp_usbd_cdc.c
84-
${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
85-
)
86-
87-
list(APPEND MICROPY_INC_TINYUSB
88-
${TINYUSB_SRC}
89-
${MICROPY_DIR}/shared/tinyusb/
90-
)
91-
92-
list(APPEND MICROPY_LINK_TINYUSB
93-
-Wl,--wrap=dcd_event_handler
94-
)
95-
endif()
96-
9759
list(APPEND MICROPY_SOURCE_PORT
9860
panichandler.c
9961
adc.c
@@ -117,7 +79,6 @@ list(APPEND MICROPY_SOURCE_PORT
11779
network_wlan.c
11880
mpnimbleport.c
11981
modsocket.c
120-
lwip_patch.c
12182
modesp.c
12283
esp32_nvs.c
12384
esp32_partition.c
@@ -183,7 +144,6 @@ list(APPEND IDF_COMPONENTS
183144
soc
184145
spi_flash
185146
ulp
186-
usb
187147
vfs
188148
)
189149

@@ -197,12 +157,10 @@ idf_component_register(
197157
${MICROPY_SOURCE_DRIVERS}
198158
${MICROPY_SOURCE_PORT}
199159
${MICROPY_SOURCE_BOARD}
200-
${MICROPY_SOURCE_TINYUSB}
201160
${MAIN_SOURCE_PORT}
202161
INCLUDE_DIRS
203162
${MICROPY_INC_CORE}
204163
${MICROPY_INC_USERMOD}
205-
${MICROPY_INC_TINYUSB}
206164
${MICROPY_PORT_DIR}
207165
${MICROPY_BOARD_DIR}
208166
${CMAKE_BINARY_DIR}
@@ -216,15 +174,14 @@ idf_component_register(
216174
set(MICROPY_TARGET ${COMPONENT_TARGET})
217175

218176
# Define mpy-cross flags, for use with frozen code.
219-
if(CONFIG_IDF_TARGET_ARCH STREQUAL "xtensa")
177+
if(NOT IDF_TARGET STREQUAL "esp32c3")
220178
set(MICROPY_CROSS_FLAGS -march=xtensawin)
221179
endif()
222180

223181
# Set compile options for this port.
224182
target_compile_definitions(${MICROPY_TARGET} PUBLIC
225183
${MICROPY_DEF_CORE}
226184
${MICROPY_DEF_BOARD}
227-
${MICROPY_DEF_TINYUSB}
228185
MICROPY_ESP_IDF_4=1
229186
MICROPY_VFS_FAT=1
230187
MICROPY_VFS_LFS2=1
@@ -240,10 +197,6 @@ target_compile_options(${MICROPY_TARGET} PUBLIC
240197
-Wno-missing-field-initializers
241198
)
242199

243-
target_link_options(${MICROPY_TARGET} PUBLIC
244-
${MICROPY_LINK_TINYUSB}
245-
)
246-
247200
# Additional include directories needed for private NimBLE headers.
248201
target_include_directories(${MICROPY_TARGET} PUBLIC
249202
${IDF_PATH}/components/bt/host/nimble/nimble
@@ -253,13 +206,6 @@ target_include_directories(${MICROPY_TARGET} PUBLIC
253206
target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
254207
target_link_libraries(${MICROPY_TARGET} usermod)
255208

256-
# Enable the panic handler wrapper
257-
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_panic_handler" APPEND)
258-
259-
# Patch LWIP memory pool allocators (see lwip_patch.c)
260-
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=memp_malloc" APPEND)
261-
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=memp_free" APPEND)
262-
263209
# Collect all of the include directories and compile definitions for the IDF components,
264210
# including those added by the IDF Component Manager via idf_components.yaml.
265211
foreach(comp ${__COMPONENT_NAMES_RESOLVED})

ports/esp32/main_esp32s3_box3/lvgl_port.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#include <sys/lock.h>
33
#include <sys/param.h>
44
#include "esp_log.h"
5+
#include "esp_timer.h"
56
#include "freertos/FreeRTOS.h"
67
#include "freertos/task.h"
7-
#include "esp_timer.h"
8+
#include "py/mpconfig.h"
9+
#include "py/obj.h"
10+
#include "py/runtime.h"
811
#include "lvgl/lvgl.h"
912
#include "lvgl_port.h"
1013

@@ -95,36 +98,38 @@ static void increase_lvgl_tick(void *arg)
9598
lv_tick_inc(LVGL_TICK_PERIOD_MS);
9699
}
97100

101+
static mp_obj_t mp_lv_task_handler(mp_obj_t arg)
102+
{
103+
lv_timer_handler();
104+
return mp_const_none;
105+
}
106+
static MP_DEFINE_CONST_FUN_OBJ_1(mp_lv_task_handler_obj, mp_lv_task_handler);
107+
98108
static void lvgl_port_task(void *arg)
99109
{
100110
printf("Starting LVGL task\n");
101-
uint32_t time_till_next_ms = 0;
102-
uint32_t time_threshold_ms = 1000 / CONFIG_FREERTOS_HZ;
111+
uint32_t time_till_next_ms = 5;
112+
// uint32_t time_threshold_ms = 1000 / CONFIG_FREERTOS_HZ;
103113
while (1) {
104-
_lock_acquire(&lvgl_api_lock);
105-
time_till_next_ms = lv_timer_handler();
106-
_lock_release(&lvgl_api_lock);
114+
mp_sched_schedule((mp_obj_t)&mp_lv_task_handler_obj, mp_const_none);
107115
// in case of triggering a task watch dog time out
108-
time_till_next_ms = MAX(time_till_next_ms, time_threshold_ms);
116+
// time_till_next_ms = MAX(time_till_next_ms, time_threshold_ms);
109117
usleep(1000 * time_till_next_ms);
110118
}
111119
}
112120

113121
lv_display_t *lvgl_port_init(int h_res, int v_res, esp_lcd_panel_handle_t panel_handle, esp_lcd_touch_handle_t tp_handle)
114122
{
115-
vTaskDelay(pdMS_TO_TICKS(1000));
116123
printf("Initialize LVGL library\n");
117124
lv_init();
118125

119-
vTaskDelay(pdMS_TO_TICKS(1000));
120126
// create a lvgl display
121127
lv_display_t *display = lv_display_create(h_res, v_res);
122128

123129
// alloc draw buffers used by LVGL
124130
// it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
125131
size_t draw_buffer_sz = h_res * LVGL_DRAW_BUF_LINES * sizeof(lv_color16_t);
126132

127-
vTaskDelay(pdMS_TO_TICKS(1000));
128133
void *buf1 = heap_caps_aligned_alloc(4, draw_buffer_sz, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
129134
assert(buf1);
130135
void *buf2 = heap_caps_aligned_alloc(4, draw_buffer_sz, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
@@ -138,7 +143,6 @@ lv_display_t *lvgl_port_init(int h_res, int v_res, esp_lcd_panel_handle_t panel_
138143
// set the callback which can copy the rendered image to an area of the display
139144
lv_display_set_flush_cb(display, lvgl_flush_cb);
140145

141-
vTaskDelay(pdMS_TO_TICKS(1000));
142146
printf("Install LVGL tick timer\n");
143147
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
144148
const esp_timer_create_args_t lvgl_tick_timer_args = {
@@ -151,7 +155,6 @@ lv_display_t *lvgl_port_init(int h_res, int v_res, esp_lcd_panel_handle_t panel_
151155

152156
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
153157

154-
vTaskDelay(pdMS_TO_TICKS(1000));
155158
if (tp_handle) {
156159
static lv_indev_t *indev;
157160
indev = lv_indev_create(); // Input device driver (Touch)
@@ -161,7 +164,6 @@ lv_display_t *lvgl_port_init(int h_res, int v_res, esp_lcd_panel_handle_t panel_
161164
lv_indev_set_read_cb(indev, lvgl_touch_cb);
162165
}
163166

164-
vTaskDelay(pdMS_TO_TICKS(1000));
165167
printf("Create LVGL task\n");
166168
xTaskCreate(lvgl_port_task, "LVGL", LVGL_TASK_STACK_SIZE, NULL, LVGL_TASK_PRIORITY, NULL);
167169

ports/esp32/main_esp32s3_box3/main.c

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include "esp_log.h"
4242
#include "esp_psram.h"
4343

44-
#include "py/cstack.h"
44+
#include "py/stackctrl.h"
4545
#include "py/nlr.h"
4646
#include "py/compile.h"
4747
#include "py/runtime.h"
@@ -52,7 +52,6 @@
5252
#include "shared/readline/readline.h"
5353
#include "shared/runtime/pyexec.h"
5454
#include "shared/timeutils/timeutils.h"
55-
#include "shared/tinyusb/mp_usbd.h"
5655
#include "mbedtls/platform_time.h"
5756

5857
#include "uart.h"
@@ -75,18 +74,15 @@
7574
#include "lvgl_port.h"
7675

7776
// MicroPython runs as a task under FreeRTOS
78-
#define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 2)
77+
#define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1)
7978

80-
typedef struct _native_code_node_t {
81-
struct _native_code_node_t *next;
82-
uint32_t data[];
83-
} native_code_node_t;
84-
85-
static native_code_node_t *native_code_head = NULL;
86-
87-
static void esp_native_code_free_all(void);
79+
// Set the margin for detecting stack overflow, depending on the CPU architecture.
80+
#if CONFIG_IDF_TARGET_ESP32C3
81+
#define MP_TASK_STACK_LIMIT_MARGIN (2048)
82+
#else
83+
#define MP_TASK_STACK_LIMIT_MARGIN (1024)
84+
#endif
8885

89-
static const char *TAG = "main";
9086
static void init_lvgl_port();
9187

9288
int vprintf_null(const char *format, va_list ap) {
@@ -108,9 +104,9 @@ void mp_task(void *pvParameter) {
108104
#if MICROPY_PY_THREAD
109105
mp_thread_init(pxTaskGetStackStart(NULL), MICROPY_TASK_STACK_SIZE / sizeof(uintptr_t));
110106
#endif
111-
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
112-
usb_serial_jtag_init();
113-
#elif MICROPY_HW_ENABLE_USBDEV
107+
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
108+
// usb_serial_jtag_init();
109+
#elif CONFIG_USB_OTG_SUPPORTED
114110
// usb_init();
115111
#endif
116112
#if MICROPY_HW_ENABLE_UART_REPL
@@ -134,12 +130,15 @@ void mp_task(void *pvParameter) {
134130

135131
soft_reset:
136132
// initialise the stack pointer for the main thread
137-
mp_cstack_init_with_top((void *)sp, MICROPY_TASK_STACK_SIZE);
133+
mp_stack_set_top((void *)sp);
134+
mp_stack_set_limit(MICROPY_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN);
138135
gc_init(mp_task_heap, mp_task_heap + MICROPY_GC_INITIAL_HEAP_SIZE);
139136
mp_init();
140137
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
141138
readline_init0();
142139

140+
MP_STATE_PORT(native_code_pointers) = MP_OBJ_NULL;
141+
143142
// initialise peripherals
144143
machine_pins_init();
145144
#if MICROPY_PY_MACHINE_I2S
@@ -192,10 +191,17 @@ void mp_task(void *pvParameter) {
192191
mp_thread_deinit();
193192
#endif
194193

195-
gc_sweep_all();
196-
197194
// Free any native code pointers that point to iRAM.
198-
esp_native_code_free_all();
195+
if (MP_STATE_PORT(native_code_pointers) != MP_OBJ_NULL) {
196+
size_t len;
197+
mp_obj_t *items;
198+
mp_obj_list_get(MP_STATE_PORT(native_code_pointers), &len, &items);
199+
for (size_t i = 0; i < len; ++i) {
200+
heap_caps_free(MP_OBJ_TO_PTR(items[i]));
201+
}
202+
}
203+
204+
gc_sweep_all();
199205

200206
mp_hal_stdout_tx_str("MPY: soft reboot\r\n");
201207

@@ -235,31 +241,25 @@ void nlr_jump_fail(void *val) {
235241
esp_restart();
236242
}
237243

238-
static void esp_native_code_free_all(void) {
239-
while (native_code_head != NULL) {
240-
native_code_node_t *next = native_code_head->next;
241-
heap_caps_free(native_code_head);
242-
native_code_head = next;
243-
}
244-
}
245-
246244
void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
247245
len = (len + 3) & ~3;
248-
size_t len_node = sizeof(native_code_node_t) + len;
249-
native_code_node_t *node = heap_caps_malloc(len_node, MALLOC_CAP_EXEC);
250-
if (node == NULL) {
251-
m_malloc_fail(len_node);
246+
uint32_t *p = heap_caps_malloc(len, MALLOC_CAP_EXEC);
247+
if (p == NULL) {
248+
m_malloc_fail(len);
249+
}
250+
if (MP_STATE_PORT(native_code_pointers) == MP_OBJ_NULL) {
251+
MP_STATE_PORT(native_code_pointers) = mp_obj_new_list(0, NULL);
252252
}
253-
node->next = native_code_head;
254-
native_code_head = node;
255-
void *p = node->data;
253+
mp_obj_list_append(MP_STATE_PORT(native_code_pointers), MP_OBJ_TO_PTR(p));
256254
if (reloc) {
257255
mp_native_relocate(reloc, buf, (uintptr_t)p);
258256
}
259257
memcpy(p, buf, len);
260258
return p;
261259
}
262260

261+
MP_REGISTER_ROOT_POINTER(mp_obj_t native_code_pointers);
262+
263263
static void init_lvgl_port()
264264
{
265265
printf("Turn off LCD backlight\n");
@@ -285,7 +285,6 @@ static void init_lvgl_port()
285285
esp_lcd_touch_handle_t tp_handle = NULL;
286286
bsp_touch_new(&touch_cfg, &tp_handle);
287287

288-
vTaskDelay(pdMS_TO_TICKS(2000));
289288
lv_display_t *disp = lvgl_port_init(BSP_LCD_H_RES, BSP_LCD_V_RES, panel_handle, tp_handle);
290289

291290
printf("Register io panel event callback for LVGL flush ready notification\n");

0 commit comments

Comments
 (0)