From 0aeab2864091fefed408b7950004ed8c7cc8008b Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 26 Aug 2014 20:32:43 +0200 Subject: [PATCH] Update MP Modules * Update all modules for new API * Move modules to built-in --- src/Makefile | 26 ++++--- src/micropython | 2 +- src/omv/main.c | 171 ++++++++++++++++------------------------- src/omv/py/mlx90620.c | 2 +- src/omv/py/mp.h | 1 + src/omv/py/py_gpio.c | 4 +- src/omv/py/py_led.c | 4 +- src/omv/py/py_sensor.c | 4 +- src/omv/py/py_socket.c | 4 +- src/omv/py/py_spi.c | 8 +- src/omv/py/py_time.c | 4 +- src/omv/py/py_wlan.c | 2 +- 12 files changed, 100 insertions(+), 132 deletions(-) diff --git a/src/Makefile b/src/Makefile index fa14a37c7..4f55a5cb2 100755 --- a/src/Makefile +++ b/src/Makefile @@ -131,7 +131,8 @@ OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/img/,\ point.o \ ppm.o \ rectangle.o \ - surf.o \ + fast.o \ + freak.o \ template.o \ ) @@ -230,8 +231,20 @@ export TOP_DIR export BUILD export TARGET ################################################### +all: $(BUILD)/$(BIN).elf -all: | $(BUILD) +$(BUILD): + $(MKDIR) -p $@ + +objs: | $(BUILD) + $(MAKE) -C $(CMSIS_DIR) BUILD=$(BUILD)/$(CMSIS_DIR) + $(MAKE) -C $(STHAL_DIR) BUILD=$(BUILD)/$(STHAL_DIR) + $(MAKE) -C $(FATFS_DIR) BUILD=$(BUILD)/$(FATFS_DIR) + $(MAKE) -C $(CC3K_DIR) BUILD=$(BUILD)/$(CC3K_DIR) + $(MAKE) -C $(MICROPY_DIR)/stmhal BUILD=$(BUILD)/$(MICROPY_DIR) BOARD=$(TARGET) + $(MAKE) -C $(OMV_DIR) BUILD=$(BUILD)/$(OMV_DIR) + +$(BUILD)/$(BIN).elf: objs $(CCP) -P -E -D$(TARGET) $(OMV_DIR)/stm32f4xx.ld.S > $(BUILD)/stm32f4xx.lds $(CC) $(LDFLAGS) $(OBJ) $(LIB) -o $(BUILD)/$(BIN).elf $(OBJCOPY) -Obinary $(BUILD)/$(BIN).elf $(BUILD)/$(BIN).bin @@ -240,15 +253,6 @@ all: | $(BUILD) size: $(SIZE) $(BUILD)/$(BIN).elf -$(BUILD): - $(MKDIR) -p $@ - $(MAKE) -C $(CMSIS_DIR) BUILD=$(BUILD)/$(CMSIS_DIR) - $(MAKE) -C $(STHAL_DIR) BUILD=$(BUILD)/$(STHAL_DIR) - $(MAKE) -C $(FATFS_DIR) BUILD=$(BUILD)/$(FATFS_DIR) - $(MAKE) -C $(CC3K_DIR) BUILD=$(BUILD)/$(CC3K_DIR) - $(MAKE) -C $(MICROPY_DIR)/stmhal BUILD=$(BUILD)/$(MICROPY_DIR) BOARD=$(TARGET) - $(MAKE) -C $(OMV_DIR) BUILD=$(BUILD)/$(OMV_DIR) - clean: $(RM) -fr $(BUILD) diff --git a/src/micropython b/src/micropython index a7b367bc3..686ad5b15 160000 --- a/src/micropython +++ b/src/micropython @@ -1 +1 @@ -Subproject commit a7b367bc39e6bef9c5e26e99f859264675fb3417 +Subproject commit 686ad5b151b1a50bb98b716fb5f7ad6c4c1f3e1b diff --git a/src/omv/main.c b/src/omv/main.c index df18ca3d0..bd183fd0f 100644 --- a/src/omv/main.c +++ b/src/omv/main.c @@ -1,10 +1,10 @@ #include #include #include +#include "mpconfig.h" #include "misc.h" #include "systick.h" #include "pendsv.h" -#include "mpconfig.h" #include "qstr.h" #include "misc.h" #include "nlr.h" @@ -14,11 +14,12 @@ #include "objmodule.h" #include "runtime.h" #include "gc.h" +#include "stackctrl.h" #include "gccollect.h" +#include "uart.h" #include "pybstdio.h" #include "readline.h" #include "pyexec.h" -#include "uart.h" #include "timer.h" #include "pin.h" #include "extint.h" @@ -51,6 +52,7 @@ int errno; static FATFS fatfs0; static FATFS fatfs1; +extern char _stack_size; void flash_error(int n) { for (int i = 0; i < n; i++) { @@ -127,18 +129,21 @@ STATIC mp_obj_t py_random(mp_obj_t min, mp_obj_t max) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_random_obj, py_random); -STATIC mp_obj_t py_gc_collect() { - gc_collect(); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_gc_collect_obj, py_gc_collect); - extern uint32_t SystemCoreClock; STATIC mp_obj_t py_cpu_freq(void ) { return mp_obj_new_int(SystemCoreClock); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_cpu_freq_obj, py_cpu_freq); +static NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) { + int rc = 0; + if (n_args > 0) { + rc = mp_obj_get_int(args[0]); + } + nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc))); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit); + static const char fresh_main_py[] = "# main.py -- put your code here!\n" "import led, time\n" @@ -173,26 +178,30 @@ static const char fresh_readme_txt[] = ; typedef struct { - qstr name; const mp_obj_module_t *(*init)(void); } module_t; -static const module_t exported_modules[] ={ - {MP_QSTR_sensor, py_sensor_init}, - {MP_QSTR_led, py_led_init}, - {MP_QSTR_time, py_time_init}, -// {MP_QSTR_wlan, py_wlan_init}, -// {MP_QSTR_socket, py_socket_init}, -// {MP_QSTR_select, py_select_init}, - {MP_QSTR_spi, py_spi_init}, - {MP_QSTR_gpio, py_gpio_init}, +static const module_t init_modules[] ={ + {py_sensor_init}, + {py_led_init}, + {py_time_init}, +// {py_wlan_init}, +// {py_socket_init}, +// {py_select_init}, + {py_spi_init}, + {py_gpio_init}, #ifdef OPENMV2 - {MP_QSTR_mlx, py_mlx90620_init}, + {py_mlx90620_init}, #endif - {0, NULL} + {NULL} }; -int main(void) { +int main(void) +{ + // Stack limit should be less than real stack size, so we + // had chance to recover from limit hit. + mp_stack_set_limit((mp_uint_t) (&_stack_size - 512)); + /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec @@ -211,6 +220,7 @@ int main(void) { } #endif #endif + // basic sub-system init pendsv_init(); timer_tim3_init(); @@ -242,81 +252,77 @@ soft_reset: // GC init gc_init(&_heap_start, &_heap_end); - xalloc_init(); - -#if 0 - // Change #if 0 to #if 1 if you want REPL on UART_6 (or another uart) - // as well as on USB VCP - mp_obj_t args[2] = { - MP_OBJ_NEW_SMALL_INT(PYB_UART_6), - MP_OBJ_NEW_SMALL_INT(115200), - }; - pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, - ARRAY_SIZE(args), - 0, args); -#else - pyb_uart_global_debug = NULL; -#endif // Micro Python init - qstr_init(); mp_init(); mp_obj_list_init(mp_sys_path, 0); + mp_obj_list_init(mp_sys_argv, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib)); - mp_obj_list_init(mp_sys_argv, 0); - readline_init(); + readline_init0(); + pin_init0(); + extint_init0(); + timer_init0(); + pyb_usb_init0(); - pin_init(); - extint_init(); + xalloc_init(); + rng_init(); + usbdbg_init(); + + /* init built-in modules */ + for (const module_t *p = init_modules; p->init; p++) { + const mp_obj_module_t *module = p->init(); + if (module == NULL) { + __fatal_error("failed to init module"); + } + } + + /* Export functions to the global python namespace */ + mp_store_global(qstr_from_str("vcp_is_connected"), (mp_obj_t)&py_vcp_is_connected_obj); + mp_store_global(qstr_from_str("Image"), (mp_obj_t)&py_image_load_image); + mp_store_global(qstr_from_str("HaarCascade"), (mp_obj_t)&py_image_load_cascade); + mp_store_global(qstr_from_str("cpu_freq"), (mp_obj_t)&py_cpu_freq); + mp_store_global(qstr_from_str("random"), (mp_obj_t)&py_random); + + pyb_stdio_uart = NULL; - // local filesystem init // try to mount the flash FRESULT res = f_mount(&fatfs0, "0:", 1); if (reset_mode == 3 || res == FR_NO_FILESYSTEM) { // no filesystem, or asked to reset it, so create a fresh one - // LED on to indicate creation of LFS led_state(LED_RED, 1); - res = f_mkfs("0:", 0, 0); - if (res == FR_OK) { - // success creating fresh LFS - } else { + if (f_mkfs("0:", 0, 0) != FR_OK) { __fatal_error("could not create LFS"); } // create empty main.py FIL fp; - f_open(&fp, "0:/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; + f_open(&fp, "main.py", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); - // TODO check we could write n bytes f_close(&fp); // create .inf driver file - f_open(&fp, "0:/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n); f_close(&fp); // create readme file - f_open(&fp, "0:/README.txt", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "README.txt", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n); f_close(&fp); - // keep LED on for at least 200ms led_state(LED_RED, 0); } else if (res != FR_OK) { __fatal_error("could not access LFS"); } - // root device defaults to internal flash filesystem - uint root_device = 0; - -#if defined(USE_DEVICE_MODE) + // Set CWD and USB medium to flash + f_chdrive("0:"); usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH; -#endif // if an SD card is present then mount it on 1:/ if (reset_mode == 1 && sdcard_is_present()) { @@ -325,7 +331,7 @@ soft_reset: printf("[SD] could not mount SD card\n"); } else { // use SD card as root device - root_device = 1; + f_chdrive("1:"); if (first_soft_reset) { // use SD card as medium for the USB MSD @@ -360,51 +366,17 @@ soft_reset: } #endif - timer_init0(); - - rng_init(); - usbdbg_init(); - - /* Add functions to the global python namespace */ - mp_store_global(qstr_from_str("vcp_is_connected"), mp_make_function_n(0, py_vcp_is_connected)); - mp_store_global(qstr_from_str("Image"), mp_make_function_n(1, py_image_load_image)); - mp_store_global(qstr_from_str("HaarCascade"), mp_make_function_n(1, py_image_load_cascade)); - mp_store_global(qstr_from_str("cpu_freq"), mp_make_function_n(0, py_cpu_freq)); - mp_store_global(qstr_from_str("random"), mp_make_function_n(2, py_random)); - mp_store_global(qstr_from_str("gc_collect"), mp_make_function_n(0, py_gc_collect)); - //mp_store_global(qstr_from_str("info"), mp_make_function_var(0, py_info)); - - /* Export Python modules to the global python namespace */ - for (const module_t *p = exported_modules; p->name; p++) { - const mp_obj_module_t *module = p->init(); - if (module == NULL) { - __fatal_error("failed to init module"); - } else { - mp_module_register(p->name, (mp_obj_t)module); - } - } - - - // now that everything is initialised, run main script + // Run the main script from the current directory. if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { - vstr_t *vstr = vstr_new(); - vstr_printf(vstr, "%d:/", root_device); - if (pyb_config_main == MP_OBJ_NULL) { - vstr_add_str(vstr, "main.py"); - } else { - vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); - } - FRESULT res = f_stat(vstr_str(vstr), NULL); + FRESULT res = f_stat("main.py", NULL); if (res == FR_OK) { - if (!pyexec_file(vstr_str(vstr))) { + if (!pyexec_file("main.py")) { flash_error(3); } } - vstr_free(vstr); } // Enter REPL - // REPL mode can change, or it can request a soft reset nlr_buf_t nlr; for (;;) { if (nlr_push(&nlr) == 0) { @@ -429,12 +401,3 @@ soft_reset: first_soft_reset = false; goto soft_reset; } - -static NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) { - int rc = 0; - if (n_args > 0) { - rc = mp_obj_get_int(args[0]); - } - nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc))); -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit); diff --git a/src/omv/py/mlx90620.c b/src/omv/py/mlx90620.c index 8894bd693..7967f2825 100644 --- a/src/omv/py/mlx90620.c +++ b/src/omv/py/mlx90620.c @@ -233,7 +233,7 @@ static const mp_map_elem_t globals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -static const mp_obj_module_t mlx_module = { +const mp_obj_module_t mlx_module = { .base = { &mp_type_module }, .name = MP_QSTR_mlx, .globals = (mp_obj_t)&globals_dict, diff --git a/src/omv/py/mp.h b/src/omv/py/mp.h index 5b6ac63ab..8904b478a 100644 --- a/src/omv/py/mp.h +++ b/src/omv/py/mp.h @@ -18,6 +18,7 @@ #include "stream.h" #include "gc.h" #include "gccollect.h" +#include "uart.h" #include "pybstdio.h" #include "readline.h" #include "pyexec.h" diff --git a/src/omv/py/py_gpio.c b/src/omv/py/py_gpio.c index 415611a4d..7da442d52 100644 --- a/src/omv/py/py_gpio.c +++ b/src/omv/py/py_gpio.c @@ -78,7 +78,7 @@ static const mp_map_elem_t globals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -const mp_obj_module_t py_gpio_module = { +const mp_obj_module_t gpio_module = { .base = { &mp_type_module }, .name = MP_QSTR_gpio, .globals = (mp_obj_t)&globals_dict, @@ -87,5 +87,5 @@ const mp_obj_module_t py_gpio_module = { const mp_obj_module_t *py_gpio_init() { /* no init required */ - return &py_gpio_module; + return &gpio_module; } diff --git a/src/omv/py/py_led.c b/src/omv/py/py_led.c index f05aed9e9..ab40bdaf3 100644 --- a/src/omv/py/py_led.c +++ b/src/omv/py/py_led.c @@ -61,7 +61,7 @@ static const mp_map_elem_t globals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -static const mp_obj_module_t py_led_module = { +const mp_obj_module_t led_module = { .base = { &mp_type_module }, .name = MP_QSTR_led, .globals = (mp_obj_t)&globals_dict, @@ -71,5 +71,5 @@ const mp_obj_module_t *py_led_init() { /* Init LED */ led_init(LED_BLUE); - return &py_led_module; + return &led_module; } diff --git a/src/omv/py/py_sensor.c b/src/omv/py/py_sensor.c index e8d551ae8..36b137441 100644 --- a/src/omv/py/py_sensor.c +++ b/src/omv/py/py_sensor.c @@ -188,7 +188,7 @@ STATIC const mp_map_elem_t globals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -static const mp_obj_module_t py_sensor_module = { +const mp_obj_module_t sensor_module = { .base = { &mp_type_module }, .name = MP_QSTR_sensor, .globals = (mp_obj_t)&globals_dict, @@ -212,5 +212,5 @@ const mp_obj_module_t *py_sensor_init() sensor_set_contrast(0); sensor_set_brightness(0); - return &py_sensor_module; + return &sensor_module; } diff --git a/src/omv/py/py_socket.c b/src/omv/py/py_socket.c index 4c6a4e097..708f79131 100644 --- a/src/omv/py/py_socket.c +++ b/src/omv/py/py_socket.c @@ -28,7 +28,7 @@ void socket_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ printf("<%s %p>", mp_obj_get_type_str(self_in), self_in); } -static machine_int_t socket_send(mp_obj_t self_in, const void *buf, machine_uint_t size, int *errcode) +static mp_uint_t socket_send(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { int bytes = 0; socket_t *self = self_in; @@ -54,7 +54,7 @@ static machine_int_t socket_send(mp_obj_t self_in, const void *buf, machine_uint return bytes; } -static machine_int_t socket_recv(mp_obj_t self_in, void *buf, machine_uint_t size, int *errcode) +static mp_uint_t socket_recv(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { int bytes = 0; socket_t *self = self_in; diff --git a/src/omv/py/py_spi.c b/src/omv/py/py_spi.c index 48386a9aa..9984c0a43 100644 --- a/src/omv/py/py_spi.c +++ b/src/omv/py/py_spi.c @@ -20,7 +20,7 @@ SPI_HandleTypeDef SPIHandle; static DMA_HandleTypeDef hdma_tx; static DMA_HandleTypeDef hdma_rx; -static machine_int_t py_spi_read(mp_obj_t obj) +static mp_int_t py_spi_read(mp_obj_t obj) { mp_buffer_info_t bufinfo; @@ -43,7 +43,7 @@ static machine_int_t py_spi_read(mp_obj_t obj) return bufinfo.len; } -static machine_int_t py_spi_write(mp_obj_t obj) +static mp_int_t py_spi_write(mp_obj_t obj) { byte buf[1]; mp_buffer_info_t bufinfo; @@ -85,7 +85,7 @@ static const mp_map_elem_t globals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -static const mp_obj_module_t py_spi_module = { +const mp_obj_module_t spi_module = { .base = { &mp_type_module }, .name = MP_QSTR_spi, .globals = (mp_obj_t)&globals_dict, @@ -152,5 +152,5 @@ const mp_obj_module_t *py_spi_init() /* dummy read */ HAL_SPI_Receive(&SPIHandle, buf, sizeof(buf), SPI_TIMEOUT); - return &py_spi_module; + return &spi_module; } diff --git a/src/omv/py/py_time.c b/src/omv/py/py_time.c index 4425d9551..37403a71a 100644 --- a/src/omv/py/py_time.c +++ b/src/omv/py/py_time.c @@ -32,7 +32,7 @@ static const mp_map_elem_t globals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -static const mp_obj_module_t py_time_module = { +const mp_obj_module_t time_module = { .base = { &mp_type_module }, .name = MP_QSTR_time, .globals = (mp_obj_t)&globals_dict, @@ -40,5 +40,5 @@ static const mp_obj_module_t py_time_module = { const mp_obj_module_t *py_time_init() { - return &py_time_module; + return &time_module; } diff --git a/src/omv/py/py_wlan.c b/src/omv/py/py_wlan.c index 7bad29f66..a0a6fd9bf 100644 --- a/src/omv/py/py_wlan.c +++ b/src/omv/py/py_wlan.c @@ -188,7 +188,7 @@ static const mp_map_elem_t globals_dict_table[] = { static MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); -static const mp_obj_module_t wlan_module = { +const mp_obj_module_t wlan_module = { .base = { &mp_type_module }, .name = MP_QSTR_wlan, .globals = (mp_obj_t)&globals_dict,