From 7a5f9e94ad787c7a1d10bba3af2d4c79bc39e54c Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 9 Apr 2014 23:30:34 +0100 Subject: [PATCH] stmhal: Make /src/main.py just /main.py; allow SD to be used as root device. --- stmhal/main.c | 78 +++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/stmhal/main.c b/stmhal/main.c index 64d9022aef..29c704b10e 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -124,9 +124,9 @@ static const char fresh_boot_py[] = "# can run arbitrary Python, but best to keep it minimal\n" "\n" "import pyb\n" -"#pyb.source_dir('/src')\n" -"#pyb.main('main.py')\n" -"#pyb.usb_mode('CDC+MSC') # one of: 'CDC+MSC', 'CDC+HID'\n" +"#pyb.main('main.py') # main script to run after this one\n" +"#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device\n" +"#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse\n" ; static const char fresh_main_py[] = @@ -191,16 +191,15 @@ int main(void) { switch_init0(); int first_soft_reset = true; - uint reset_mode; soft_reset: // check if user switch held to select the reset mode - reset_mode = 1; led_state(1, 0); led_state(2, 1); led_state(3, 0); led_state(4, 0); + uint reset_mode = 1; #if MICROPY_HW_HAS_SWITCH if (switch_get()) { @@ -264,11 +263,10 @@ soft_reset: // Micro Python init qstr_init(); mp_init(); - mp_obj_t def_path[3]; + mp_obj_t def_path[2]; def_path[0] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_); - def_path[1] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_src); - def_path[2] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib); - mp_sys_path = mp_obj_new_list(3, def_path); + def_path[1] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib); + mp_sys_path = mp_obj_new_list(2, def_path); readline_init(); @@ -304,13 +302,9 @@ soft_reset: __fatal_error("could not create LFS"); } - // create src directory - res = f_mkdir("0:/src"); - // ignore result from mkdir - // create empty main.py FIL fp; - f_open(&fp, "0:/src/main.py", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "0:/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes @@ -331,7 +325,7 @@ soft_reset: } } - // make sure we have a /boot.py + // make sure we have a 0:/boot.py { FILINFO fno; #if _USE_LFN @@ -367,17 +361,8 @@ soft_reset: } } - // run /boot.py - if (reset_mode == 1) { - if (!pyexec_file("0:/boot.py")) { - flash_error(4); - } - } - - // turn boot-up LEDs off - led_state(2, 0); - led_state(3, 0); - led_state(4, 0); + // root device defaults to internal flash filesystem + uint root_device = 0; #if defined(USE_DEVICE_MODE) usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH; @@ -390,6 +375,9 @@ soft_reset: if (res != FR_OK) { printf("[SD] could not mount SD card\n"); } else { + // use SD card as root device + root_device = 1; + if (first_soft_reset) { // use SD card as medium for the USB MSD #if defined(USE_DEVICE_MODE) @@ -403,6 +391,27 @@ soft_reset: (void)first_soft_reset; #endif + // run :/boot.py, if it exists + if (reset_mode == 1) { + const char *boot_file; + if (root_device == 0) { + boot_file = "0:/boot.py"; + } else { + boot_file = "1:/boot.py"; + } + FRESULT res = f_stat(boot_file, NULL); + if (res == FR_OK) { + if (!pyexec_file(boot_file)) { + flash_error(4); + } + } + } + + // turn boot-up LEDs off + led_state(2, 0); + led_state(3, 0); + led_state(4, 0); + #if defined(USE_HOST_MODE) // USB host pyb_usb_host_init(); @@ -449,23 +458,20 @@ soft_reset: dac_init(); #endif - // run main script + // now that everything is initialised, run main script if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { vstr_t *vstr = vstr_new(); - vstr_add_str(vstr, "0:/"); - if (pyb_config_source_dir == MP_OBJ_NULL) { - vstr_add_str(vstr, "src"); - } else { - vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_source_dir)); - } - vstr_add_char(vstr, '/'); + 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)); } - if (!pyexec_file(vstr_str(vstr))) { - flash_error(3); + FRESULT res = f_stat(vstr_str(vstr), NULL); + if (res == FR_OK) { + if (!pyexec_file(vstr_str(vstr))) { + flash_error(3); + } } vstr_free(vstr); }