2016-11-16 15:38:25 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2016-12-13 14:27:26 -05:00
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2016-11-16 15:38:25 -05:00
|
|
|
* Copyright (c) 2015 Glenn Ruben Bakke
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/lexer.h"
|
|
|
|
#include "py/parse.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stackctrl.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/compile.h"
|
|
|
|
#include "lib/utils/pyexec.h"
|
|
|
|
#include "readline.h"
|
|
|
|
#include "gccollect.h"
|
2016-12-13 14:27:26 -05:00
|
|
|
#include "modmachine.h"
|
2016-11-16 15:38:25 -05:00
|
|
|
#include "led.h"
|
|
|
|
#include "uart.h"
|
|
|
|
#include "nrf.h"
|
2016-12-13 14:27:26 -05:00
|
|
|
#include "pin.h"
|
|
|
|
#include "spi.h"
|
2017-01-29 10:38:48 -05:00
|
|
|
#include "i2c.h"
|
2017-03-05 18:57:33 -05:00
|
|
|
#include "rtc.h"
|
2017-01-08 10:25:10 -05:00
|
|
|
#if MICROPY_PY_MACHINE_PWM
|
2016-12-19 11:10:48 -05:00
|
|
|
#include "pwm.h"
|
2017-01-08 10:25:10 -05:00
|
|
|
#endif
|
2016-12-26 10:15:26 -05:00
|
|
|
#include "timer.h"
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2017-02-21 17:38:35 -05:00
|
|
|
#if (MICROPY_PY_BLE_NUS)
|
|
|
|
#include "ble_uart.h"
|
2016-12-13 14:27:26 -05:00
|
|
|
#endif
|
2016-11-16 15:38:25 -05:00
|
|
|
|
|
|
|
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
|
|
|
|
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
|
|
|
|
if (lex == NULL) {
|
|
|
|
printf("MemoryError: lexer could not allocate memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
qstr source_name = lex->source_name;
|
|
|
|
mp_parse_tree_t pn = mp_parse(lex, input_kind);
|
|
|
|
mp_obj_t module_fun = mp_compile(&pn, source_name, MP_EMIT_OPT_NONE, true);
|
|
|
|
mp_call_function_0(module_fun);
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
// uncaught exception
|
|
|
|
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern uint32_t _heap_start;
|
|
|
|
extern uint32_t _heap_end;
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
2017-02-25 19:29:13 -05:00
|
|
|
mp_stack_set_top(&_ram_end);
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
// Stack limit should be less than real stack size, so we have a chance
|
|
|
|
// to recover from limit hit. (Limit is measured in bytes.)
|
2016-12-13 14:27:26 -05:00
|
|
|
mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 400);
|
2016-11-16 15:38:25 -05:00
|
|
|
|
|
|
|
led_init();
|
2016-12-13 14:27:26 -05:00
|
|
|
machine_init();
|
2016-11-16 15:38:25 -05:00
|
|
|
|
|
|
|
gc_init(&_heap_start, &_heap_end);
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
mp_init();
|
|
|
|
mp_obj_list_init(mp_sys_path, 0);
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
|
|
|
|
mp_obj_list_init(mp_sys_argv, 0);
|
|
|
|
|
2017-03-02 18:40:44 -05:00
|
|
|
pyb_set_repl_info(MP_OBJ_NEW_SMALL_INT(0));
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
readline_init0();
|
|
|
|
|
2017-01-26 15:29:24 -05:00
|
|
|
#if MICROPY_PY_MACHINE_HW_SPI
|
2016-12-13 14:27:26 -05:00
|
|
|
spi_init0();
|
|
|
|
#endif
|
|
|
|
|
2017-02-02 12:41:44 -05:00
|
|
|
#if MICROPY_PY_MACHINE_I2C
|
2017-01-29 10:38:48 -05:00
|
|
|
i2c_init0();
|
|
|
|
#endif
|
|
|
|
|
2016-12-19 11:10:48 -05:00
|
|
|
#if MICROPY_PY_MACHINE_PWM
|
|
|
|
pwm_init0();
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 18:57:33 -05:00
|
|
|
#if MICROPY_PY_MACHINE_RTC
|
|
|
|
rtc_init0();
|
|
|
|
#endif
|
|
|
|
|
2016-12-26 10:15:26 -05:00
|
|
|
#if MICROPY_PY_MACHINE_TIMER
|
|
|
|
timer_init0();
|
|
|
|
#endif
|
|
|
|
|
2016-12-13 14:27:26 -05:00
|
|
|
/*
|
|
|
|
extint_init0();
|
|
|
|
timer_init0();
|
|
|
|
*/
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
uart_init0();
|
2017-02-21 17:38:35 -05:00
|
|
|
|
|
|
|
#if (MICROPY_PY_BLE_NUS == 0)
|
2016-11-16 15:38:25 -05:00
|
|
|
{
|
|
|
|
mp_obj_t args[2] = {
|
2017-03-10 16:21:19 -05:00
|
|
|
MP_OBJ_NEW_SMALL_INT(0),
|
2016-11-16 15:38:25 -05:00
|
|
|
MP_OBJ_NEW_SMALL_INT(115200),
|
|
|
|
};
|
2017-03-10 16:21:19 -05:00
|
|
|
MP_STATE_PORT(pyb_stdio_uart) = machine_hard_uart_type.make_new((mp_obj_t)&machine_hard_uart_type, MP_ARRAY_SIZE(args), 0, args);
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
2016-12-13 14:27:26 -05:00
|
|
|
#endif
|
|
|
|
|
2017-04-11 07:55:01 -04:00
|
|
|
pin_init0();
|
|
|
|
|
2016-12-13 14:27:26 -05:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
|
|
|
// if an SD card is present then mount it on /sd/
|
|
|
|
if (sdcard_is_present()) {
|
|
|
|
// create vfs object
|
|
|
|
fs_user_mount_t *vfs = m_new_obj_maybe(fs_user_mount_t);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
goto no_mem_for_sd;
|
|
|
|
}
|
|
|
|
vfs->str = "/sd";
|
|
|
|
vfs->len = 3;
|
|
|
|
vfs->flags = FSUSER_FREE_OBJ;
|
|
|
|
sdcard_init_vfs(vfs);
|
|
|
|
|
|
|
|
// put the sd device in slot 1 (it will be unused at this point)
|
|
|
|
MP_STATE_PORT(fs_user_mount)[1] = vfs;
|
|
|
|
|
|
|
|
FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
|
|
|
|
if (res != FR_OK) {
|
|
|
|
printf("PYB: can't mount SD card\n");
|
|
|
|
MP_STATE_PORT(fs_user_mount)[1] = NULL;
|
|
|
|
m_del_obj(fs_user_mount_t, vfs);
|
|
|
|
} else {
|
|
|
|
// TODO these should go before the /flash entries in the path
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
|
|
|
|
|
|
|
|
// use SD card as current directory
|
|
|
|
f_chdrive("/sd");
|
|
|
|
}
|
|
|
|
no_mem_for_sd:;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
#if MICROPY_HW_LED_TRICOLOR
|
|
|
|
do_str("import pyb\r\n" \
|
|
|
|
"pyb.LED(1).on()",
|
|
|
|
MP_PARSE_FILE_INPUT);
|
2017-04-29 13:50:50 -04:00
|
|
|
#elif (MICROPY_HW_LED_COUNT == 2)
|
|
|
|
do_str("import pyb\r\n" \
|
|
|
|
"pyb.LED(1).on()",
|
|
|
|
MP_PARSE_FILE_INPUT);
|
2016-11-16 15:38:25 -05:00
|
|
|
#else
|
|
|
|
do_str("import pyb\r\n" \
|
|
|
|
"pyb.LED(1).on()\r\n" \
|
|
|
|
"pyb.LED(3).on()",
|
|
|
|
MP_PARSE_FILE_INPUT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Main script is finished, so now go into REPL mode.
|
|
|
|
// The REPL mode can change, or it can request a soft reset.
|
2016-12-29 10:11:59 -05:00
|
|
|
int ret_code = 0;
|
|
|
|
|
2017-02-21 17:38:35 -05:00
|
|
|
#if MICROPY_PY_BLE_NUS
|
|
|
|
ble_uart_init0();
|
2017-04-04 16:31:17 -04:00
|
|
|
while (!ble_uart_enabled()) {
|
|
|
|
;
|
|
|
|
}
|
2017-02-21 17:38:35 -05:00
|
|
|
#endif
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
for (;;) {
|
2016-12-29 10:11:59 -05:00
|
|
|
ret_code = pyexec_friendly_repl();
|
|
|
|
if (ret_code != 0) {
|
2016-11-16 15:38:25 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_deinit();
|
|
|
|
|
2016-12-29 10:11:59 -05:00
|
|
|
if (ret_code == PYEXEC_FORCED_EXIT) {
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardFault_Handler(void)
|
|
|
|
{
|
|
|
|
#if NRF52
|
|
|
|
static volatile uint32_t reg;
|
|
|
|
static volatile uint32_t reg2;
|
|
|
|
static volatile uint32_t bfar;
|
|
|
|
reg = SCB->HFSR;
|
|
|
|
reg2 = SCB->CFSR;
|
|
|
|
bfar = SCB->BFAR;
|
|
|
|
for (int i = 0; i < 0; i++)
|
|
|
|
{
|
|
|
|
(void)reg;
|
|
|
|
(void)reg2;
|
|
|
|
(void)bfar;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN __fatal_error(const char *msg) {
|
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
2017-03-07 11:53:10 -05:00
|
|
|
void nlr_jump_fail(void *val) {
|
|
|
|
printf("FATAL: uncaught exception %p\n", val);
|
|
|
|
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)val);
|
|
|
|
__fatal_error("");
|
|
|
|
}
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
|
|
|
|
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
|
|
|
|
__fatal_error("Assertion failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void _start(void) {main(0, NULL);}
|