2013-10-21 09:56:56 +01:00
|
|
|
#include <stdio.h>
|
2014-01-21 21:40:13 +00:00
|
|
|
#include <string.h>
|
2013-10-13 00:42:20 +01:00
|
|
|
#include <stm32f4xx.h>
|
|
|
|
#include <stm32f4xx_rcc.h>
|
2013-12-11 00:40:15 +00:00
|
|
|
#include <stm32f4xx_syscfg.h>
|
2013-10-19 14:40:54 +01:00
|
|
|
#include <stm32f4xx_gpio.h>
|
2013-12-11 00:40:15 +00:00
|
|
|
#include <stm32f4xx_exti.h>
|
2013-10-23 20:39:20 +01:00
|
|
|
#include <stm32f4xx_tim.h>
|
2013-10-26 02:29:02 +01:00
|
|
|
#include <stm32f4xx_pwr.h>
|
2013-10-26 02:06:37 +01:00
|
|
|
#include <stm32f4xx_rtc.h>
|
2013-10-26 18:01:48 +01:00
|
|
|
#include <stm32f4xx_usart.h>
|
2013-11-09 20:15:48 +00:00
|
|
|
#include <stm32f4xx_rng.h>
|
2014-02-13 23:21:02 +00:00
|
|
|
#include <usbd_storage_msd.h>
|
2013-10-19 14:40:54 +01:00
|
|
|
#include <stm_misc.h>
|
2013-10-13 00:42:20 +01:00
|
|
|
#include "std.h"
|
|
|
|
|
2013-10-18 23:44:05 +01:00
|
|
|
#include "misc.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "ff.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "lexer.h"
|
2014-01-07 17:49:42 +00:00
|
|
|
#include "lexerfatfs.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include "obj.h"
|
2014-02-15 16:10:44 +00:00
|
|
|
#include "parsehelper.h"
|
2014-01-03 14:22:03 +00:00
|
|
|
#include "compile.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "runtime0.h"
|
|
|
|
#include "runtime.h"
|
2013-10-21 23:46:04 +01:00
|
|
|
#include "gc.h"
|
2014-01-25 00:44:31 +00:00
|
|
|
#include "gccollect.h"
|
2013-10-19 14:40:54 +01:00
|
|
|
#include "systick.h"
|
2014-02-01 16:04:34 +00:00
|
|
|
#include "pendsv.h"
|
2014-02-16 21:36:03 +00:00
|
|
|
#include "pyexec.h"
|
2013-10-18 23:44:05 +01:00
|
|
|
#include "led.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "servo.h"
|
2013-10-19 14:40:54 +01:00
|
|
|
#include "lcd.h"
|
2013-10-18 23:44:05 +01:00
|
|
|
#include "storage.h"
|
2014-02-08 16:00:15 +00:00
|
|
|
#include "sdcard.h"
|
2014-02-16 21:41:57 +00:00
|
|
|
#include "accel.h"
|
2013-10-19 15:37:09 +01:00
|
|
|
#include "usb.h"
|
2013-11-06 23:04:33 +00:00
|
|
|
#include "timer.h"
|
2013-12-05 20:38:23 +00:00
|
|
|
#include "pybwlan.h"
|
2014-01-05 12:52:37 +00:00
|
|
|
#include "usrsw.h"
|
2014-01-23 11:49:06 +00:00
|
|
|
#include "rtc.h"
|
2014-01-24 15:14:13 +00:00
|
|
|
#include "file.h"
|
2014-02-18 01:57:13 +00:00
|
|
|
#include "pin.h"
|
2014-02-20 06:28:36 +00:00
|
|
|
#include "exti.h"
|
2014-03-08 16:40:08 +00:00
|
|
|
#include "pybmodule.h"
|
2013-12-05 20:38:23 +00:00
|
|
|
|
|
|
|
int errno;
|
2013-10-13 00:42:20 +01:00
|
|
|
|
2013-11-30 15:06:24 +00:00
|
|
|
static FATFS fatfs0;
|
2014-02-17 07:14:49 +00:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
2014-02-08 16:00:15 +00:00
|
|
|
static FATFS fatfs1;
|
2014-02-17 07:14:49 +00:00
|
|
|
#endif
|
2013-11-30 15:06:24 +00:00
|
|
|
|
2013-10-23 00:02:06 +01:00
|
|
|
void flash_error(int n) {
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
led_state(PYB_LED_R1, 1);
|
|
|
|
led_state(PYB_LED_R2, 0);
|
|
|
|
sys_tick_delay_ms(250);
|
|
|
|
led_state(PYB_LED_R1, 0);
|
|
|
|
led_state(PYB_LED_R2, 1);
|
|
|
|
sys_tick_delay_ms(250);
|
|
|
|
}
|
|
|
|
led_state(PYB_LED_R2, 0);
|
|
|
|
}
|
|
|
|
|
2013-10-13 00:42:20 +01:00
|
|
|
void __fatal_error(const char *msg) {
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_HAS_LCD
|
2013-10-13 00:42:20 +01:00
|
|
|
lcd_print_strn("\nFATAL ERROR:\n", 14);
|
|
|
|
lcd_print_strn(msg, strlen(msg));
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-10-13 00:42:20 +01:00
|
|
|
for (;;) {
|
2013-10-23 00:02:06 +01:00
|
|
|
flash_error(1);
|
2013-10-13 00:42:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
STATIC mp_obj_t pyb_config_source_dir = MP_OBJ_NULL;
|
|
|
|
STATIC mp_obj_t pyb_config_main = MP_OBJ_NULL;
|
2013-10-23 00:02:06 +01:00
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
STATIC mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
|
2014-01-22 14:35:10 +00:00
|
|
|
if (MP_OBJ_IS_STR(source_dir)) {
|
|
|
|
pyb_config_source_dir = source_dir;
|
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
return mp_const_none;
|
2013-10-22 23:09:25 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_source_dir_obj, pyb_source_dir);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_main(mp_obj_t main) {
|
2014-01-22 14:35:10 +00:00
|
|
|
if (MP_OBJ_IS_STR(main)) {
|
|
|
|
pyb_config_main = main;
|
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
return mp_const_none;
|
2013-10-23 00:02:06 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_main_obj, pyb_main);
|
2014-03-08 15:17:10 +00:00
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
void fatality(void) {
|
2013-10-19 14:40:54 +01:00
|
|
|
led_state(PYB_LED_R1, 1);
|
|
|
|
led_state(PYB_LED_G1, 1);
|
|
|
|
led_state(PYB_LED_R2, 1);
|
|
|
|
led_state(PYB_LED_G2, 1);
|
|
|
|
}
|
|
|
|
|
2013-10-19 18:13:48 +01:00
|
|
|
static const char fresh_boot_py[] =
|
2013-10-19 14:40:54 +01:00
|
|
|
"# boot.py -- run on boot-up\n"
|
|
|
|
"# can run arbitrary Python, but best to keep it minimal\n"
|
|
|
|
"\n"
|
|
|
|
"pyb.source_dir('/src')\n"
|
|
|
|
"pyb.main('main.py')\n"
|
|
|
|
"#pyb.usb_usr('VCP')\n"
|
|
|
|
"#pyb.usb_msd(True, 'dual partition')\n"
|
|
|
|
"#pyb.flush_cache(False)\n"
|
|
|
|
"#pyb.error_log('error.txt')\n"
|
|
|
|
;
|
|
|
|
|
2013-12-17 18:36:19 +00:00
|
|
|
static const char fresh_main_py[] =
|
|
|
|
"# main.py -- put your code here!\n"
|
|
|
|
;
|
|
|
|
|
|
|
|
static const char *help_text =
|
|
|
|
"Welcome to Micro Python!\n\n"
|
|
|
|
"This is a *very* early version of Micro Python and has minimal functionality.\n\n"
|
|
|
|
"Specific commands for the board:\n"
|
|
|
|
" pyb.info() -- print some general information\n"
|
|
|
|
" pyb.gc() -- run the garbage collector\n"
|
2014-01-29 20:33:20 +00:00
|
|
|
" pyb.repl_info(<val>) -- enable/disable printing of info after each command\n"
|
2013-12-17 18:36:19 +00:00
|
|
|
" pyb.delay(<n>) -- wait for n milliseconds\n"
|
2014-03-08 16:40:08 +00:00
|
|
|
" pyb.udelay(<n>) -- wait for n microseconds\n"
|
2013-12-17 18:36:19 +00:00
|
|
|
" pyb.Led(<n>) -- create Led object for LED n (n=1,2)\n"
|
|
|
|
" Led methods: on(), off()\n"
|
|
|
|
" pyb.Servo(<n>) -- create Servo object for servo n (n=1,2,3,4)\n"
|
|
|
|
" Servo methods: angle(<x>)\n"
|
|
|
|
" pyb.switch() -- return True/False if switch pressed or not\n"
|
|
|
|
" pyb.accel() -- get accelerometer values\n"
|
|
|
|
" pyb.rand() -- get a 16-bit random number\n"
|
2014-01-25 16:30:47 +00:00
|
|
|
" pyb.gpio(<port>) -- get port value (port='A4' for example)\n"
|
2014-01-21 23:28:03 +00:00
|
|
|
" pyb.gpio(<port>, <val>) -- set port value, True or False, 1 or 0\n"
|
2014-01-25 16:30:47 +00:00
|
|
|
" pyb.ADC(<port>) -- make an analog port object (port='C0' for example)\n"
|
|
|
|
" ADC methods: read()\n"
|
2013-12-17 18:36:19 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
// get some help about available functions
|
2013-12-21 18:17:45 +00:00
|
|
|
static mp_obj_t pyb_help(void) {
|
2013-12-17 18:36:19 +00:00
|
|
|
printf("%s", help_text);
|
2013-12-21 18:17:45 +00:00
|
|
|
return mp_const_none;
|
2013-12-17 18:36:19 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
int main(void) {
|
2013-10-18 23:44:05 +01:00
|
|
|
// TODO disable JTAG
|
2013-10-13 00:42:20 +01:00
|
|
|
|
2013-12-06 00:41:33 +00:00
|
|
|
// update the SystemCoreClock variable
|
|
|
|
SystemCoreClockUpdate();
|
|
|
|
|
2013-10-19 18:13:48 +01:00
|
|
|
// set interrupt priority config to use all 4 bits for pre-empting
|
|
|
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
|
|
|
|
|
|
|
|
// enable the CCM RAM and the GPIO's
|
2014-01-26 17:41:01 +00:00
|
|
|
RCC->AHB1ENR |= RCC_AHB1ENR_CCMDATARAMEN | RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN;
|
2013-10-19 18:13:48 +01:00
|
|
|
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
2013-10-24 00:22:27 +01:00
|
|
|
{
|
2014-01-23 18:40:57 +00:00
|
|
|
// configure SDIO pins to be high to start with (apparently makes it more robust)
|
|
|
|
// FIXME this is not making them high, it just makes them outputs...
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
// Configure PD.02 CMD line
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
|
|
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
2013-10-24 00:22:27 +01:00
|
|
|
}
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2014-02-04 05:11:48 +00:00
|
|
|
#if defined(NETDUINO_PLUS_2)
|
|
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
|
|
|
// Turn on the power enable for the sdcard (PB1)
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Turn on the power for the 5V on the expansion header (PB2)
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-24 00:22:27 +01:00
|
|
|
|
2013-10-19 14:40:54 +01:00
|
|
|
// basic sub-system init
|
|
|
|
sys_tick_init();
|
2014-02-01 16:04:34 +00:00
|
|
|
pendsv_init();
|
2013-10-13 00:42:20 +01:00
|
|
|
led_init();
|
2014-01-23 11:16:06 +00:00
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_RTC
|
2013-10-26 02:06:37 +01:00
|
|
|
rtc_init();
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-10-19 14:40:54 +01:00
|
|
|
|
|
|
|
// turn on LED to indicate bootup
|
|
|
|
led_state(PYB_LED_G1, 1);
|
|
|
|
|
|
|
|
// more sub-system init
|
2014-02-08 16:00:15 +00:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
|
|
|
sdcard_init();
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-10-18 23:44:05 +01:00
|
|
|
storage_init();
|
2013-12-05 20:38:23 +00:00
|
|
|
|
2014-01-13 00:20:06 +00:00
|
|
|
// uncomment these 2 lines if you want REPL on USART_6 (or another usart) as well as on USB VCP
|
2014-02-23 00:31:11 +00:00
|
|
|
//pyb_usart_global_debug = PYB_USART_YA;
|
2014-01-13 00:20:06 +00:00
|
|
|
//usart_init(pyb_usart_global_debug, 115200);
|
2013-10-13 00:42:20 +01:00
|
|
|
|
2013-11-06 23:04:33 +00:00
|
|
|
int first_soft_reset = true;
|
|
|
|
|
2013-10-22 23:09:25 +01:00
|
|
|
soft_reset:
|
|
|
|
|
2013-10-21 23:46:04 +01:00
|
|
|
// GC init
|
2014-01-29 10:51:38 +00:00
|
|
|
gc_init(&_heap_start, &_heap_end);
|
2013-10-21 23:46:04 +01:00
|
|
|
|
2013-10-22 23:09:25 +01:00
|
|
|
// Micro Python init
|
2013-10-19 18:13:48 +01:00
|
|
|
qstr_init();
|
|
|
|
rt_init();
|
2014-02-17 22:06:37 +00:00
|
|
|
mp_obj_t def_path[3];
|
|
|
|
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);
|
|
|
|
sys_path = mp_obj_new_list(3, def_path);
|
2013-10-19 14:40:54 +01:00
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
exti_init();
|
|
|
|
|
2014-02-20 06:28:36 +00:00
|
|
|
#if MICROPY_HW_HAS_SWITCH
|
|
|
|
switch_init();
|
|
|
|
#endif
|
|
|
|
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_HAS_LCD
|
2014-01-26 17:41:01 +00:00
|
|
|
// LCD init (just creates class, init hardware by calling LCD())
|
2014-01-23 18:10:42 +00:00
|
|
|
lcd_init();
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-11-09 20:15:48 +00:00
|
|
|
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_ENABLE_SERVO
|
2013-10-23 20:39:20 +01:00
|
|
|
// servo
|
|
|
|
servo_init();
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-10-23 20:39:20 +01:00
|
|
|
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_ENABLE_TIMER
|
2013-11-06 23:04:33 +00:00
|
|
|
// timer
|
|
|
|
timer_init();
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-11-06 23:04:33 +00:00
|
|
|
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_ENABLE_RNG
|
2013-11-09 20:15:48 +00:00
|
|
|
// RNG
|
2014-01-23 11:16:06 +00:00
|
|
|
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
|
|
|
|
RNG_Cmd(ENABLE);
|
|
|
|
#endif
|
2013-11-09 20:15:48 +00:00
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
pin_map_init();
|
2014-03-08 15:14:53 +00:00
|
|
|
|
2014-03-08 16:40:08 +00:00
|
|
|
// add some functions to the builtin Python namespace
|
|
|
|
rt_store_name(MP_QSTR_help, rt_make_function_n(0, pyb_help));
|
|
|
|
rt_store_name(MP_QSTR_open, rt_make_function_n(2, pyb_io_open));
|
2014-01-21 21:40:13 +00:00
|
|
|
|
2014-03-24 22:25:27 +00:00
|
|
|
// load the pyb module
|
|
|
|
mp_obj_module_register(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
|
2013-10-21 10:42:06 +01:00
|
|
|
|
2013-12-06 00:41:33 +00:00
|
|
|
// check if user switch held (initiates reset of filesystem)
|
|
|
|
bool reset_filesystem = false;
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_HAS_SWITCH
|
2014-01-05 14:04:55 +00:00
|
|
|
if (switch_get()) {
|
2013-12-06 00:41:33 +00:00
|
|
|
reset_filesystem = true;
|
|
|
|
for (int i = 0; i < 50; i++) {
|
2014-01-05 14:04:55 +00:00
|
|
|
if (!switch_get()) {
|
2013-12-06 00:41:33 +00:00
|
|
|
reset_filesystem = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sys_tick_delay_ms(10);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 11:16:06 +00:00
|
|
|
#endif
|
2013-10-19 14:40:54 +01:00
|
|
|
// local filesystem init
|
|
|
|
{
|
|
|
|
// try to mount the flash
|
|
|
|
FRESULT res = f_mount(&fatfs0, "0:", 1);
|
2013-12-06 00:41:33 +00:00
|
|
|
if (!reset_filesystem && res == FR_OK) {
|
2013-10-19 14:40:54 +01:00
|
|
|
// mount sucessful
|
2013-12-06 00:41:33 +00:00
|
|
|
} else if (reset_filesystem || res == FR_NO_FILESYSTEM) {
|
2013-10-19 14:40:54 +01:00
|
|
|
// no filesystem, so create a fresh one
|
2013-12-06 00:41:33 +00:00
|
|
|
// TODO doesn't seem to work correctly when reset_filesystem is true...
|
2013-10-19 14:40:54 +01:00
|
|
|
|
|
|
|
// LED on to indicate creation of LFS
|
|
|
|
led_state(PYB_LED_R2, 1);
|
|
|
|
uint32_t stc = sys_tick_counter;
|
|
|
|
|
|
|
|
res = f_mkfs("0:", 0, 0);
|
|
|
|
if (res == FR_OK) {
|
|
|
|
// success creating fresh LFS
|
|
|
|
} else {
|
|
|
|
__fatal_error("could not create LFS");
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:36:19 +00:00
|
|
|
// 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);
|
|
|
|
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
|
|
|
|
f_close(&fp);
|
|
|
|
|
2013-10-19 18:13:48 +01:00
|
|
|
// keep LED on for at least 200ms
|
|
|
|
sys_tick_wait_at_least(stc, 200);
|
2013-10-19 14:40:54 +01:00
|
|
|
led_state(PYB_LED_R2, 0);
|
|
|
|
} else {
|
|
|
|
__fatal_error("could not access LFS");
|
|
|
|
}
|
2013-10-13 00:42:20 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 14:40:54 +01:00
|
|
|
// make sure we have a /boot.py
|
|
|
|
{
|
|
|
|
FILINFO fno;
|
|
|
|
FRESULT res = f_stat("0:/boot.py", &fno);
|
|
|
|
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
|
|
|
|
|
|
|
|
// LED on to indicate creation of boot.py
|
|
|
|
led_state(PYB_LED_R2, 1);
|
|
|
|
uint32_t stc = sys_tick_counter;
|
|
|
|
|
|
|
|
FIL fp;
|
|
|
|
f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
|
|
|
|
UINT n;
|
2013-10-22 23:09:25 +01:00
|
|
|
f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
|
2013-10-19 14:40:54 +01:00
|
|
|
// TODO check we could write n bytes
|
|
|
|
f_close(&fp);
|
|
|
|
|
2013-10-19 18:13:48 +01:00
|
|
|
// keep LED on for at least 200ms
|
|
|
|
sys_tick_wait_at_least(stc, 200);
|
2013-10-19 14:40:54 +01:00
|
|
|
led_state(PYB_LED_R2, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 00:02:06 +01:00
|
|
|
// run /boot.py
|
2014-02-16 21:36:03 +00:00
|
|
|
if (!pyexec_file("0:/boot.py")) {
|
2013-10-23 00:02:06 +01:00
|
|
|
flash_error(4);
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:04:33 +00:00
|
|
|
if (first_soft_reset) {
|
2014-01-22 18:34:02 +00:00
|
|
|
#if MICROPY_HW_HAS_MMA7660
|
2014-02-16 21:41:57 +00:00
|
|
|
// MMA accel: init and reset address to zero
|
|
|
|
accel_init();
|
2014-01-22 02:06:28 +00:00
|
|
|
#endif
|
2013-10-23 22:17:26 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 00:02:06 +01:00
|
|
|
// turn boot-up LED off
|
|
|
|
led_state(PYB_LED_G1, 0);
|
2013-10-22 21:13:36 +01:00
|
|
|
|
2014-02-08 16:00:15 +00:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
|
|
|
// if an SD card is present then mount it on 1:/
|
|
|
|
if (sdcard_is_present()) {
|
|
|
|
FRESULT res = f_mount(&fatfs1, "1:", 1);
|
|
|
|
if (res != FR_OK) {
|
|
|
|
printf("[SD] could not mount SD card\n");
|
2014-02-13 23:21:02 +00:00
|
|
|
} else {
|
|
|
|
if (first_soft_reset) {
|
|
|
|
// use SD card as medium for the USB MSD
|
|
|
|
usbd_storage_select_medium(USBD_STORAGE_MEDIUM_SDCARD);
|
|
|
|
}
|
2014-02-08 16:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-13 23:21:02 +00:00
|
|
|
#ifdef USE_HOST_MODE
|
|
|
|
// USB host
|
|
|
|
pyb_usb_host_init();
|
|
|
|
#elif defined(USE_DEVICE_MODE)
|
|
|
|
// USB device
|
2014-02-24 01:12:04 +00:00
|
|
|
pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC);
|
2014-02-13 23:21:02 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-23 00:02:06 +01:00
|
|
|
// run main script
|
|
|
|
{
|
|
|
|
vstr_t *vstr = vstr_new();
|
|
|
|
vstr_add_str(vstr, "0:/");
|
2014-01-22 14:35:10 +00:00
|
|
|
if (pyb_config_source_dir == MP_OBJ_NULL) {
|
2013-10-23 00:02:06 +01:00
|
|
|
vstr_add_str(vstr, "src");
|
|
|
|
} else {
|
2014-01-22 14:35:10 +00:00
|
|
|
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_source_dir));
|
2013-10-23 00:02:06 +01:00
|
|
|
}
|
|
|
|
vstr_add_char(vstr, '/');
|
2014-01-22 14:35:10 +00:00
|
|
|
if (pyb_config_main == MP_OBJ_NULL) {
|
2013-10-23 00:02:06 +01:00
|
|
|
vstr_add_str(vstr, "main.py");
|
|
|
|
} else {
|
2014-01-22 14:35:10 +00:00
|
|
|
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
|
2013-10-23 00:02:06 +01:00
|
|
|
}
|
2014-02-16 21:36:03 +00:00
|
|
|
if (!pyexec_file(vstr_str(vstr))) {
|
2013-10-23 00:02:06 +01:00
|
|
|
flash_error(3);
|
2013-10-22 21:13:36 +01:00
|
|
|
}
|
2013-10-23 00:02:06 +01:00
|
|
|
vstr_free(vstr);
|
2013-10-19 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-13 00:42:20 +01:00
|
|
|
|
2014-01-22 18:34:02 +00:00
|
|
|
#if MICROPY_HW_HAS_MMA7660
|
2013-10-25 20:53:54 +01:00
|
|
|
// HID example
|
|
|
|
if (0) {
|
|
|
|
uint8_t data[4];
|
|
|
|
data[0] = 0;
|
|
|
|
data[1] = 1;
|
|
|
|
data[2] = -2;
|
|
|
|
data[3] = 0;
|
|
|
|
for (;;) {
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_HAS_SWITCH
|
2014-01-05 14:04:55 +00:00
|
|
|
if (switch_get()) {
|
2013-10-25 20:53:54 +01:00
|
|
|
data[0] = 0x01; // 0x04 is middle, 0x02 is right
|
|
|
|
} else {
|
|
|
|
data[0] = 0x00;
|
|
|
|
}
|
2014-01-23 11:16:06 +00:00
|
|
|
#else
|
|
|
|
data[0] = 0x00;
|
|
|
|
#endif
|
2014-02-16 21:41:57 +00:00
|
|
|
accel_start(0x4c /* ACCEL_ADDR */, 1);
|
|
|
|
accel_send_byte(0);
|
|
|
|
accel_restart(0x4c /* ACCEL_ADDR */, 0);
|
2013-10-25 20:53:54 +01:00
|
|
|
for (int i = 0; i <= 1; i++) {
|
2014-02-16 21:41:57 +00:00
|
|
|
int v = accel_read_ack() & 0x3f;
|
2013-10-25 20:53:54 +01:00
|
|
|
if (v & 0x20) {
|
|
|
|
v |= ~0x1f;
|
|
|
|
}
|
|
|
|
data[1 + i] = v;
|
|
|
|
}
|
2014-02-16 21:41:57 +00:00
|
|
|
accel_read_nack();
|
2013-10-25 20:53:54 +01:00
|
|
|
usb_hid_send_report(data);
|
|
|
|
sys_tick_delay_ms(15);
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 02:06:28 +00:00
|
|
|
#endif
|
2013-10-25 20:53:54 +01:00
|
|
|
|
2014-01-23 11:16:06 +00:00
|
|
|
#if MICROPY_HW_HAS_WLAN
|
2013-12-05 20:38:23 +00:00
|
|
|
// wifi
|
2014-01-23 11:16:06 +00:00
|
|
|
pyb_wlan_init();
|
|
|
|
pyb_wlan_start();
|
|
|
|
#endif
|
2013-12-05 20:38:23 +00:00
|
|
|
|
2014-02-16 21:36:03 +00:00
|
|
|
pyexec_repl();
|
2013-10-21 09:56:56 +01:00
|
|
|
|
2013-10-23 00:02:06 +01:00
|
|
|
printf("PYB: sync filesystems\n");
|
2014-03-08 16:40:08 +00:00
|
|
|
storage_flush();
|
2013-10-23 00:02:06 +01:00
|
|
|
|
|
|
|
printf("PYB: soft reboot\n");
|
2013-11-06 23:04:33 +00:00
|
|
|
|
|
|
|
first_soft_reset = false;
|
2013-10-22 23:09:25 +01:00
|
|
|
goto soft_reset;
|
2013-10-13 00:42:20 +01:00
|
|
|
}
|