stmhal: Make /src/main.py just /main.py; allow SD to be used as root device.

This commit is contained in:
Damien George 2014-04-09 23:30:34 +01:00
parent 038fd52faa
commit 7a5f9e94ad

View File

@ -124,9 +124,9 @@ static const char fresh_boot_py[] =
"# can run arbitrary Python, but best to keep it minimal\n" "# can run arbitrary Python, but best to keep it minimal\n"
"\n" "\n"
"import pyb\n" "import pyb\n"
"#pyb.source_dir('/src')\n" "#pyb.main('main.py') # main script to run after this one\n"
"#pyb.main('main.py')\n" "#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device\n"
"#pyb.usb_mode('CDC+MSC') # one of: 'CDC+MSC', 'CDC+HID'\n" "#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse\n"
; ;
static const char fresh_main_py[] = static const char fresh_main_py[] =
@ -191,16 +191,15 @@ int main(void) {
switch_init0(); switch_init0();
int first_soft_reset = true; int first_soft_reset = true;
uint reset_mode;
soft_reset: soft_reset:
// check if user switch held to select the reset mode // check if user switch held to select the reset mode
reset_mode = 1;
led_state(1, 0); led_state(1, 0);
led_state(2, 1); led_state(2, 1);
led_state(3, 0); led_state(3, 0);
led_state(4, 0); led_state(4, 0);
uint reset_mode = 1;
#if MICROPY_HW_HAS_SWITCH #if MICROPY_HW_HAS_SWITCH
if (switch_get()) { if (switch_get()) {
@ -264,11 +263,10 @@ soft_reset:
// Micro Python init // Micro Python init
qstr_init(); qstr_init();
mp_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[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[1] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib);
def_path[2] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib); mp_sys_path = mp_obj_new_list(2, def_path);
mp_sys_path = mp_obj_new_list(3, def_path);
readline_init(); readline_init();
@ -304,13 +302,9 @@ soft_reset:
__fatal_error("could not create LFS"); __fatal_error("could not create LFS");
} }
// create src directory
res = f_mkdir("0:/src");
// ignore result from mkdir
// create empty main.py // create empty main.py
FIL fp; 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; UINT n;
f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &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 // 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; FILINFO fno;
#if _USE_LFN #if _USE_LFN
@ -367,17 +361,8 @@ soft_reset:
} }
} }
// run /boot.py // root device defaults to internal flash filesystem
if (reset_mode == 1) { uint root_device = 0;
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);
#if defined(USE_DEVICE_MODE) #if defined(USE_DEVICE_MODE)
usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH; usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH;
@ -390,6 +375,9 @@ soft_reset:
if (res != FR_OK) { if (res != FR_OK) {
printf("[SD] could not mount SD card\n"); printf("[SD] could not mount SD card\n");
} else { } else {
// use SD card as root device
root_device = 1;
if (first_soft_reset) { if (first_soft_reset) {
// use SD card as medium for the USB MSD // use SD card as medium for the USB MSD
#if defined(USE_DEVICE_MODE) #if defined(USE_DEVICE_MODE)
@ -403,6 +391,27 @@ soft_reset:
(void)first_soft_reset; (void)first_soft_reset;
#endif #endif
// run <root>:/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) #if defined(USE_HOST_MODE)
// USB host // USB host
pyb_usb_host_init(); pyb_usb_host_init();
@ -449,24 +458,21 @@ soft_reset:
dac_init(); dac_init();
#endif #endif
// run main script // now that everything is initialised, run main script
if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
vstr_t *vstr = vstr_new(); vstr_t *vstr = vstr_new();
vstr_add_str(vstr, "0:/"); vstr_printf(vstr, "%d:/", root_device);
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, '/');
if (pyb_config_main == MP_OBJ_NULL) { if (pyb_config_main == MP_OBJ_NULL) {
vstr_add_str(vstr, "main.py"); vstr_add_str(vstr, "main.py");
} else { } else {
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
} }
FRESULT res = f_stat(vstr_str(vstr), NULL);
if (res == FR_OK) {
if (!pyexec_file(vstr_str(vstr))) { if (!pyexec_file(vstr_str(vstr))) {
flash_error(3); flash_error(3);
} }
}
vstr_free(vstr); vstr_free(vstr);
} }