2015-02-06 14:35:48 +00:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-02-06 14:35:48 +00:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/mpconfig.h"
|
2016-05-31 17:31:28 +01:00
|
|
|
#include "py/stackctrl.h"
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/gc.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2017-03-31 12:29:39 +01:00
|
|
|
#include "lib/mp-readline/readline.h"
|
2017-01-29 04:15:16 +00:00
|
|
|
#include "lib/oofatfs/ff.h"
|
|
|
|
#include "lib/oofatfs/diskio.h"
|
|
|
|
#include "extmod/vfs.h"
|
2017-01-29 08:20:27 +00:00
|
|
|
#include "extmod/vfs_fat.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "inc/hw_memmap.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
#include "inc/hw_types.h"
|
2015-03-12 19:59:15 +00:00
|
|
|
#include "inc/hw_ints.h"
|
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "rom_map.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
#include "pin.h"
|
2015-03-12 19:59:15 +00:00
|
|
|
#include "prcm.h"
|
2015-03-26 09:25:28 +00:00
|
|
|
#include "interrupt.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "pybuart.h"
|
2015-02-09 18:38:02 +00:00
|
|
|
#include "pybpin.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "pybrtc.h"
|
2015-11-09 13:13:09 +00:00
|
|
|
#include "lib/utils/pyexec.h"
|
2019-02-12 03:41:48 +00:00
|
|
|
#include "lib/utils/gchelper.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "gccollect.h"
|
|
|
|
#include "mperror.h"
|
|
|
|
#include "simplelink.h"
|
|
|
|
#include "modnetwork.h"
|
2015-05-22 18:53:33 +01:00
|
|
|
#include "modusocket.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "modwlan.h"
|
|
|
|
#include "serverstask.h"
|
|
|
|
#include "telnet.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "sflash_diskio.h"
|
|
|
|
#include "random.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
#include "pybi2c.h"
|
|
|
|
#include "pins.h"
|
2017-01-29 04:15:16 +00:00
|
|
|
#include "mods/pybflash.h"
|
2015-02-27 15:50:06 +00:00
|
|
|
#include "pybsleep.h"
|
2015-05-09 16:46:16 +01:00
|
|
|
#include "pybtimer.h"
|
2015-05-19 23:23:13 +01:00
|
|
|
#include "cryptohash.h"
|
2015-09-22 22:20:29 +01:00
|
|
|
#include "mpirq.h"
|
2015-07-14 20:39:07 +01:00
|
|
|
#include "updater.h"
|
2015-09-16 13:09:51 +01:00
|
|
|
#include "moduos.h"
|
2015-09-26 21:55:24 +01:00
|
|
|
#include "antenna.h"
|
2016-05-31 17:27:21 +01:00
|
|
|
#include "task.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE CONSTANTS
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|
2015-03-02 13:11:02 +00:00
|
|
|
STATIC void mptask_pre_init (void);
|
|
|
|
STATIC void mptask_init_sflash_filesystem (void);
|
|
|
|
STATIC void mptask_enter_ap_mode (void);
|
|
|
|
STATIC void mptask_create_main_py (void);
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PUBLIC DATA
|
|
|
|
******************************************************************************/
|
|
|
|
#ifdef DEBUG
|
|
|
|
OsiTaskHandle svTaskHandle;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2017-01-29 04:15:16 +00:00
|
|
|
static fs_user_mount_t *sflash_vfs_fat;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
static const char fresh_main_py[] = "# main.py -- put your code here!\r\n";
|
|
|
|
static const char fresh_boot_py[] = "# boot.py -- run on boot-up\r\n"
|
2016-12-26 22:00:12 +00:00
|
|
|
"# can run arbitrary Python, but best to keep it minimal\r\n"
|
|
|
|
#if MICROPY_STDIO_UART
|
|
|
|
"import os, machine\r\n"
|
|
|
|
"os.dupterm(machine.UART(0, " MP_STRINGIFY(MICROPY_STDIO_UART_BAUD) "))\r\n"
|
|
|
|
#endif
|
|
|
|
;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PUBLIC FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
|
2017-07-15 10:39:05 +01:00
|
|
|
void TASK_MicroPython (void *pvParameters) {
|
2016-05-31 17:31:28 +01:00
|
|
|
// get the top of the stack to initialize the garbage collector
|
2015-02-06 14:35:48 +00:00
|
|
|
uint32_t sp = gc_helper_get_sp();
|
2015-08-09 17:54:29 +01:00
|
|
|
|
2015-08-09 18:17:48 +01:00
|
|
|
bool safeboot = false;
|
|
|
|
mptask_pre_init();
|
|
|
|
|
2015-08-09 17:54:29 +01:00
|
|
|
#ifndef DEBUG
|
2015-08-09 18:17:48 +01:00
|
|
|
safeboot = PRCMGetSpecialBit(PRCM_SAFE_BOOT_BIT);
|
2015-08-09 17:54:29 +01:00
|
|
|
#endif
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
soft_reset:
|
|
|
|
|
2016-05-27 17:35:46 +01:00
|
|
|
// Thread init
|
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
mp_thread_init();
|
|
|
|
#endif
|
|
|
|
|
2016-05-31 17:31:28 +01:00
|
|
|
// initialise the stack pointer for the main thread (must be done after mp_thread_init)
|
|
|
|
mp_stack_set_top((void*)sp);
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
// GC init
|
2015-03-02 13:11:02 +00:00
|
|
|
gc_init(&_boot, &_eheap);
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-03-25 10:21:44 +00:00
|
|
|
// MicroPython init
|
2015-02-06 14:35:48 +00:00
|
|
|
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_)); // current dir (or base dir of the script)
|
|
|
|
|
2015-03-04 12:52:39 +00:00
|
|
|
// execute all basic initializations
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_irq_init0();
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_init0();
|
2015-09-03 10:25:44 +01:00
|
|
|
pin_init0();
|
2015-03-25 13:14:25 +00:00
|
|
|
mperror_init0();
|
2015-02-06 14:35:48 +00:00
|
|
|
uart_init0();
|
2015-05-09 16:46:16 +01:00
|
|
|
timer_init0();
|
2015-03-04 12:52:39 +00:00
|
|
|
readline_init0();
|
|
|
|
mod_network_init0();
|
|
|
|
rng_init0();
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-09-27 12:45:48 +01:00
|
|
|
pybsleep_reset_cause_t rstcause = pyb_sleep_get_reset_cause();
|
2015-03-25 10:21:44 +00:00
|
|
|
if (rstcause < PYB_SLP_SOFT_RESET) {
|
|
|
|
if (rstcause == PYB_SLP_HIB_RESET) {
|
|
|
|
// when waking up from hibernate we just want
|
|
|
|
// to enable simplelink and leave it as is
|
|
|
|
wlan_first_start();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// only if not comming out of hibernate or a soft reset
|
|
|
|
mptask_enter_ap_mode();
|
|
|
|
}
|
2015-02-26 14:03:52 +00:00
|
|
|
|
2015-03-25 10:21:44 +00:00
|
|
|
// enable telnet and ftp
|
|
|
|
servers_start();
|
2015-03-12 19:59:15 +00:00
|
|
|
}
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
// initialize the serial flash file system
|
2015-03-02 13:11:02 +00:00
|
|
|
mptask_init_sflash_filesystem();
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-04-10 20:02:07 +01:00
|
|
|
// append the flash paths to the system path
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
// reset config variables; they should be set by boot.py
|
2015-09-27 12:45:48 +01:00
|
|
|
MP_STATE_PORT(machine_config_main) = MP_OBJ_NULL;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-02-25 21:38:33 +00:00
|
|
|
if (!safeboot) {
|
2015-05-24 17:05:02 +01:00
|
|
|
// run boot.py
|
2019-04-26 06:22:14 +01:00
|
|
|
int ret = pyexec_file_if_exists("boot.py");
|
2015-05-24 17:05:02 +01:00
|
|
|
if (ret & PYEXEC_FORCED_EXIT) {
|
|
|
|
goto soft_reset_exit;
|
|
|
|
}
|
|
|
|
if (!ret) {
|
|
|
|
// flash the system led
|
|
|
|
mperror_signal_error();
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:21:44 +00:00
|
|
|
// now we initialise sub-systems that need configuration from boot.py,
|
2015-02-06 14:35:48 +00:00
|
|
|
// or whose initialisation can be safely deferred until after running
|
|
|
|
// boot.py.
|
|
|
|
|
2015-03-25 10:21:44 +00:00
|
|
|
// at this point everything is fully configured and initialised.
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-02-25 21:38:33 +00:00
|
|
|
if (!safeboot) {
|
2015-03-25 10:21:44 +00:00
|
|
|
// run the main script from the current directory.
|
2015-02-25 21:38:33 +00:00
|
|
|
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
|
|
|
|
const char *main_py;
|
2015-09-27 12:45:48 +01:00
|
|
|
if (MP_STATE_PORT(machine_config_main) == MP_OBJ_NULL) {
|
2015-04-10 20:02:07 +01:00
|
|
|
main_py = "main.py";
|
2015-02-25 21:38:33 +00:00
|
|
|
} else {
|
2015-09-27 12:45:48 +01:00
|
|
|
main_py = mp_obj_str_get_str(MP_STATE_PORT(machine_config_main));
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2019-04-26 06:22:14 +01:00
|
|
|
int ret = pyexec_file_if_exists(main_py);
|
2015-05-24 17:05:02 +01:00
|
|
|
if (ret & PYEXEC_FORCED_EXIT) {
|
|
|
|
goto soft_reset_exit;
|
|
|
|
}
|
|
|
|
if (!ret) {
|
|
|
|
// flash the system led
|
|
|
|
mperror_signal_error();
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:21:44 +00:00
|
|
|
// main script is finished, so now go into REPL mode.
|
|
|
|
// the REPL mode can change, or it can request a soft reset.
|
2015-03-20 12:39:43 +00:00
|
|
|
for ( ; ; ) {
|
2015-02-06 14:35:48 +00:00
|
|
|
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
|
|
|
if (pyexec_raw_repl() != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pyexec_friendly_repl() != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
soft_reset_exit:
|
|
|
|
|
|
|
|
// soft reset
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_signal_soft_reset();
|
2017-12-03 11:42:50 +00:00
|
|
|
mp_printf(&mp_plat_print, "MPY: soft reboot\n");
|
2015-03-25 10:21:44 +00:00
|
|
|
|
2015-09-07 08:23:46 +01:00
|
|
|
// disable all callbacks to avoid undefined behaviour
|
|
|
|
// when coming out of a soft reset
|
2015-09-22 22:20:29 +01:00
|
|
|
mp_irq_disable_all();
|
|
|
|
|
|
|
|
// cancel the RTC alarm which might be running independent of the irq state
|
|
|
|
pyb_rtc_disable_alarm();
|
2015-05-09 16:46:16 +01:00
|
|
|
|
|
|
|
// flush the serial flash buffer
|
2015-02-06 14:35:48 +00:00
|
|
|
sflash_disk_flush();
|
|
|
|
|
2015-05-22 18:53:33 +01:00
|
|
|
// clean-up the user socket space
|
|
|
|
modusocket_close_all_user_sockets();
|
|
|
|
|
2015-11-16 19:37:58 +00:00
|
|
|
// unmount all user file systems
|
|
|
|
osmount_unmount_all();
|
|
|
|
|
2015-05-09 16:46:16 +01:00
|
|
|
// wait for pending transactions to complete
|
2015-10-29 17:38:44 +00:00
|
|
|
mp_hal_delay_ms(20);
|
2015-05-09 16:46:16 +01:00
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
goto soft_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|
2015-03-02 13:11:02 +00:00
|
|
|
__attribute__ ((section (".boot")))
|
|
|
|
STATIC void mptask_pre_init (void) {
|
2015-09-16 13:09:51 +01:00
|
|
|
// this one only makes sense after a poweron reset
|
|
|
|
pyb_rtc_pre_init();
|
2015-03-02 13:11:02 +00:00
|
|
|
|
|
|
|
// Create the simple link spawn task
|
|
|
|
ASSERT (OSI_OK == VStartSimpleLinkSpawnTask(SIMPLELINK_SPAWN_TASK_PRIORITY));
|
|
|
|
|
|
|
|
// Allocate memory for the flash file system
|
2017-01-29 04:15:16 +00:00
|
|
|
ASSERT ((sflash_vfs_fat = mem_Malloc(sizeof(*sflash_vfs_fat))) != NULL);
|
2015-03-02 13:11:02 +00:00
|
|
|
|
2015-03-14 08:59:47 +00:00
|
|
|
// this one allocates memory for the nvic vault
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_pre_init();
|
2015-03-14 08:59:47 +00:00
|
|
|
|
2015-05-22 18:53:33 +01:00
|
|
|
// this one allocates memory for the WLAN semaphore
|
2015-05-01 22:09:29 +01:00
|
|
|
wlan_pre_init();
|
2015-04-29 15:01:51 +01:00
|
|
|
|
2015-07-14 20:39:07 +01:00
|
|
|
// this one allocates memory for the updater semaphore
|
|
|
|
updater_pre_init();
|
|
|
|
|
2015-08-11 15:06:43 +01:00
|
|
|
// this one allocates memory for the socket semaphore
|
2015-05-22 18:53:33 +01:00
|
|
|
modusocket_pre_init();
|
|
|
|
|
2015-09-27 17:04:11 +01:00
|
|
|
//CRYPTOHASH_Init();
|
2015-05-19 23:23:13 +01:00
|
|
|
|
2016-05-31 17:27:21 +01:00
|
|
|
#ifndef DEBUG
|
|
|
|
OsiTaskHandle svTaskHandle;
|
2015-03-02 13:11:02 +00:00
|
|
|
#endif
|
2016-05-31 17:27:21 +01:00
|
|
|
svTaskHandle = xTaskCreateStatic(TASK_Servers, "Servers",
|
|
|
|
SERVERS_STACK_LEN, NULL, SERVERS_PRIORITY, svTaskStack, &svTaskTCB);
|
|
|
|
ASSERT(svTaskHandle != NULL);
|
2015-03-02 13:11:02 +00:00
|
|
|
}
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-03-02 13:11:02 +00:00
|
|
|
STATIC void mptask_init_sflash_filesystem (void) {
|
2015-04-10 20:02:07 +01:00
|
|
|
FILINFO fno;
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
// Initialise the local flash filesystem.
|
2017-01-29 04:15:16 +00:00
|
|
|
// init the vfs object
|
|
|
|
fs_user_mount_t *vfs_fat = sflash_vfs_fat;
|
|
|
|
vfs_fat->flags = 0;
|
|
|
|
pyb_flash_init_vfs(vfs_fat);
|
|
|
|
|
2015-04-10 20:02:07 +01:00
|
|
|
// Create it if needed, and mount in on /flash.
|
2017-01-29 04:15:16 +00:00
|
|
|
FRESULT res = f_mount(&vfs_fat->fatfs);
|
2015-02-06 14:35:48 +00:00
|
|
|
if (res == FR_NO_FILESYSTEM) {
|
|
|
|
// no filesystem, so create a fresh one
|
2019-02-25 12:46:17 +00:00
|
|
|
uint8_t working_buf[FF_MAX_SS];
|
2017-01-29 04:15:16 +00:00
|
|
|
res = f_mkfs(&vfs_fat->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
|
2015-02-06 14:35:48 +00:00
|
|
|
if (res == FR_OK) {
|
|
|
|
// success creating fresh LFS
|
|
|
|
} else {
|
2015-04-10 20:02:07 +01:00
|
|
|
__fatal_error("failed to create /flash");
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
// create empty main.py
|
2015-03-02 13:11:02 +00:00
|
|
|
mptask_create_main_py();
|
2015-02-06 14:35:48 +00:00
|
|
|
} else if (res == FR_OK) {
|
|
|
|
// mount sucessful
|
2017-01-29 04:15:16 +00:00
|
|
|
if (FR_OK != f_stat(&vfs_fat->fatfs, "/main.py", &fno)) {
|
2015-02-06 14:35:48 +00:00
|
|
|
// create empty main.py
|
2015-03-02 13:11:02 +00:00
|
|
|
mptask_create_main_py();
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-10 08:09:19 +00:00
|
|
|
fail:
|
2015-04-10 20:02:07 +01:00
|
|
|
__fatal_error("failed to create /flash");
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 04:15:16 +00:00
|
|
|
// mount the flash device (there should be no other devices mounted at this point)
|
2017-03-10 08:09:19 +00:00
|
|
|
// we allocate this structure on the heap because vfs->next is a root pointer
|
|
|
|
mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-01-29 04:15:16 +00:00
|
|
|
vfs->str = "/flash";
|
|
|
|
vfs->len = 6;
|
|
|
|
vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
|
|
|
|
vfs->next = NULL;
|
|
|
|
MP_STATE_VM(vfs_mount_table) = vfs;
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
// The current directory is used as the boot up directory.
|
|
|
|
// It is set to the internal flash filesystem by default.
|
2017-01-29 04:15:16 +00:00
|
|
|
MP_STATE_PORT(vfs_cur) = vfs;
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-07-15 11:06:16 +01:00
|
|
|
// create /flash/sys, /flash/lib and /flash/cert if they don't exist
|
2017-01-29 04:15:16 +00:00
|
|
|
if (FR_OK != f_chdir(&vfs_fat->fatfs, "/sys")) {
|
|
|
|
f_mkdir(&vfs_fat->fatfs, "/sys");
|
2015-07-05 22:01:23 +01:00
|
|
|
}
|
2017-01-29 04:15:16 +00:00
|
|
|
if (FR_OK != f_chdir(&vfs_fat->fatfs, "/lib")) {
|
|
|
|
f_mkdir(&vfs_fat->fatfs, "/lib");
|
2015-07-05 22:01:23 +01:00
|
|
|
}
|
2017-01-29 04:15:16 +00:00
|
|
|
if (FR_OK != f_chdir(&vfs_fat->fatfs, "/cert")) {
|
|
|
|
f_mkdir(&vfs_fat->fatfs, "/cert");
|
2015-07-15 11:06:16 +01:00
|
|
|
}
|
2015-07-05 22:01:23 +01:00
|
|
|
|
2017-01-29 04:15:16 +00:00
|
|
|
f_chdir(&vfs_fat->fatfs, "/");
|
2015-07-05 22:01:23 +01:00
|
|
|
|
2015-07-15 11:06:16 +01:00
|
|
|
// make sure we have a /flash/boot.py. Create it if needed.
|
2017-01-29 04:15:16 +00:00
|
|
|
res = f_stat(&vfs_fat->fatfs, "/boot.py", &fno);
|
2015-02-06 14:35:48 +00:00
|
|
|
if (res == FR_OK) {
|
|
|
|
if (fno.fattrib & AM_DIR) {
|
|
|
|
// exists as a directory
|
|
|
|
// TODO handle this case
|
|
|
|
// see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
|
|
|
|
} else {
|
|
|
|
// exists as a file, good!
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// doesn't exist, create fresh file
|
|
|
|
FIL fp;
|
2017-01-29 04:15:16 +00:00
|
|
|
f_open(&vfs_fat->fatfs, &fp, "/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
|
2015-02-06 14:35:48 +00:00
|
|
|
UINT n;
|
|
|
|
f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
|
|
|
|
// TODO check we could write n bytes
|
|
|
|
f_close(&fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 13:11:02 +00:00
|
|
|
STATIC void mptask_enter_ap_mode (void) {
|
2015-08-09 18:17:48 +01:00
|
|
|
// append the mac only if it's not the first boot
|
2015-09-26 21:55:24 +01:00
|
|
|
bool add_mac = !PRCMGetSpecialBit(PRCM_FIRST_BOOT_BIT);
|
2015-07-28 22:50:22 +01:00
|
|
|
// enable simplelink in ap mode (use the MAC address to make the ssid unique)
|
2015-09-26 21:55:24 +01:00
|
|
|
wlan_sl_init (ROLE_AP, MICROPY_PORT_WLAN_AP_SSID, strlen(MICROPY_PORT_WLAN_AP_SSID),
|
|
|
|
MICROPY_PORT_WLAN_AP_SECURITY, MICROPY_PORT_WLAN_AP_KEY, strlen(MICROPY_PORT_WLAN_AP_KEY),
|
|
|
|
MICROPY_PORT_WLAN_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, add_mac);
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 13:11:02 +00:00
|
|
|
STATIC void mptask_create_main_py (void) {
|
2015-02-06 14:35:48 +00:00
|
|
|
// create empty main.py
|
|
|
|
FIL fp;
|
2017-01-29 04:15:16 +00:00
|
|
|
f_open(&sflash_vfs_fat->fatfs, &fp, "/main.py", FA_WRITE | FA_CREATE_ALWAYS);
|
2015-02-06 14:35:48 +00:00
|
|
|
UINT n;
|
|
|
|
f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n);
|
|
|
|
f_close(&fp);
|
|
|
|
}
|