blob: 9883f9f96d10d3cd613a008fe526380400ef08b0 [file] [log] [blame]
Damien George075d5972014-11-27 20:30:33 +00001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include <string.h>
29
Damien Georgefe7d5422015-01-01 21:16:58 +000030#include "py/nlr.h"
Damien Georgefe7d5422015-01-01 21:16:58 +000031#include "py/compile.h"
32#include "py/runtime0.h"
33#include "py/runtime.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000034#include "py/stackctrl.h"
Damien George731f3592015-10-30 23:03:58 +000035#include "py/mphal.h"
Damien Georgefe7d5422015-01-01 21:16:58 +000036#include "py/gc.h"
Robert HHeb7637b2016-06-26 09:01:18 +020037#include "lib/mp-readline/readline.h"
Damien George40274fe2015-11-09 13:13:09 +000038#include "lib/utils/pyexec.h"
Damien George075d5972014-11-27 20:30:33 +000039#include "gccollect.h"
Josef Gajdusek967f3232015-05-17 11:22:26 +020040#include "user_interface.h"
Damien George075d5972014-11-27 20:30:33 +000041
Paul Sokolovskya6c90602016-11-02 00:22:43 +030042STATIC char heap[36 * 1024];
Paul Sokolovskyc6b87502015-01-15 00:21:57 +020043
Damien Georgec98c1282015-05-06 00:02:58 +010044STATIC void mp_reset(void) {
Paul Sokolovskydb984b72016-02-13 15:44:53 +020045 mp_stack_set_top((void*)0x40000000);
46 mp_stack_set_limit(8192);
Damien George075d5972014-11-27 20:30:33 +000047 mp_hal_init();
Paul Sokolovskyc6b87502015-01-15 00:21:57 +020048 gc_init(heap, heap + sizeof(heap));
Damien George075d5972014-11-27 20:30:33 +000049 mp_init();
50 mp_obj_list_init(mp_sys_path, 0);
Paul Sokolovskyfb5017f2016-05-03 18:21:50 +030051 mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
Paul Sokolovskyfb5017f2016-05-03 18:21:50 +030052 mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
Paul Sokolovsky161e9f42016-10-05 00:02:51 +030053 mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
Damien George075d5972014-11-27 20:30:33 +000054 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +030055 #if MICROPY_VFS_FAT
Damien George71d40f12016-03-28 13:28:41 +030056 memset(MP_STATE_PORT(fs_user_mount), 0, sizeof(MP_STATE_PORT(fs_user_mount)));
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +030057 #endif
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +020058 MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
Damien George4b597a12016-03-31 19:55:42 +030059 MP_STATE_PORT(term_obj) = MP_OBJ_NULL;
Paul Sokolovskye07ef8f2016-07-04 17:40:26 +030060 MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL;
Damien George3a4ebf52016-12-09 17:05:51 +110061 #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
Damien George45a61562016-12-09 16:47:47 +110062 extern void esp_native_code_init(void);
63 esp_native_code_init();
64 #endif
Damien George674bf1b2016-04-14 11:15:43 +010065 pin_init0();
Robert HHeb7637b2016-06-26 09:01:18 +020066 readline_init0();
Paul Sokolovsky77f0cd82016-07-23 00:05:38 +030067 dupterm_task_init();
Josef Gajdusekbda70412015-05-06 00:19:26 +020068#if MICROPY_MODULE_FROZEN
Paul Sokolovskycb7693b2016-05-22 04:08:22 +030069 pyexec_frozen_module("_boot.py");
Damien George6e87aeb2016-04-10 14:12:52 +030070 pyexec_file("boot.py");
71 pyexec_file("main.py");
Josef Gajdusekbda70412015-05-06 00:19:26 +020072#endif
Damien Georgec98c1282015-05-06 00:02:58 +010073}
Damien George075d5972014-11-27 20:30:33 +000074
Damien Georgec98c1282015-05-06 00:02:58 +010075void soft_reset(void) {
Damien George1febaf32015-12-22 14:28:02 +000076 mp_hal_stdout_tx_str("PYB: soft reboot\r\n");
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030077 mp_hal_delay_us(10000); // allow UART to flush output
Damien Georgec98c1282015-05-06 00:02:58 +010078 mp_reset();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030079 #if MICROPY_REPL_EVENT_DRIVEN
Damien Georgec98c1282015-05-06 00:02:58 +010080 pyexec_event_repl_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030081 #endif
Damien Georgec98c1282015-05-06 00:02:58 +010082}
Damien George075d5972014-11-27 20:30:33 +000083
Josef Gajdusek967f3232015-05-17 11:22:26 +020084void init_done(void) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030085 #if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovsky53302f12016-03-27 14:33:17 +030086 uart_task_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030087 #endif
Damien Georgec98c1282015-05-06 00:02:58 +010088 mp_reset();
89 mp_hal_stdout_tx_str("\r\n");
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030090 #if MICROPY_REPL_EVENT_DRIVEN
Damien Georgec98c1282015-05-06 00:02:58 +010091 pyexec_event_repl_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030092 #endif
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030093
94 #if !MICROPY_REPL_EVENT_DRIVEN
95soft_reset:
96 for (;;) {
97 if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
98 if (pyexec_raw_repl() != 0) {
99 break;
100 }
101 } else {
102 if (pyexec_friendly_repl() != 0) {
103 break;
104 }
105 }
106 }
107 soft_reset();
108 goto soft_reset;
109 #endif
Damien George075d5972014-11-27 20:30:33 +0000110}
111
Josef Gajdusek967f3232015-05-17 11:22:26 +0200112void user_init(void) {
113 system_init_done_cb(init_done);
114}
115
Paul Sokolovsky2f023022016-03-28 18:38:25 +0300116mp_import_stat_t fat_vfs_import_stat(const char *path);
117
Damien Georgee5ef15a2016-11-16 16:25:06 +1100118#if !MICROPY_VFS_FAT
Damien George075d5972014-11-27 20:30:33 +0000119mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300120 return NULL;
Damien George075d5972014-11-27 20:30:33 +0000121}
Damien Georgee5ef15a2016-11-16 16:25:06 +1100122#endif
Damien George075d5972014-11-27 20:30:33 +0000123
124mp_import_stat_t mp_import_stat(const char *path) {
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300125 #if MICROPY_VFS_FAT
Paul Sokolovsky2f023022016-03-28 18:38:25 +0300126 return fat_vfs_import_stat(path);
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300127 #else
128 (void)path;
129 return MP_IMPORT_STAT_NO_EXIST;
130 #endif
Damien George075d5972014-11-27 20:30:33 +0000131}
132
Paul Sokolovskyc734de42016-04-10 16:53:28 +0300133mp_obj_t vfs_proxy_call(qstr method_name, mp_uint_t n_args, const mp_obj_t *args);
Damien George075d5972014-11-27 20:30:33 +0000134mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Paul Sokolovskyc734de42016-04-10 16:53:28 +0300135 #if MICROPY_VFS_FAT
136 // TODO: Handle kwargs!
137 return vfs_proxy_call(MP_QSTR_open, n_args, args);
138 #else
Damien George075d5972014-11-27 20:30:33 +0000139 return mp_const_none;
Paul Sokolovskyc734de42016-04-10 16:53:28 +0300140 #endif
Damien George075d5972014-11-27 20:30:33 +0000141}
142MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
143
Paul Sokolovsky20422262016-10-19 00:20:34 +0300144void MP_FASTCODE(nlr_jump_fail)(void *val) {
Damien George075d5972014-11-27 20:30:33 +0000145 printf("NLR jump failed\n");
146 for (;;) {
147 }
148}
149
150//void __assert(const char *file, int line, const char *func, const char *expr) {
151void __assert(const char *file, int line, const char *expr) {
152 printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
153 for (;;) {
154 }
155}