Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 1 | /* |
| 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 George | fe7d542 | 2015-01-01 21:16:58 +0000 | [diff] [blame] | 30 | #include "py/nlr.h" |
Damien George | fe7d542 | 2015-01-01 21:16:58 +0000 | [diff] [blame] | 31 | #include "py/compile.h" |
| 32 | #include "py/runtime0.h" |
| 33 | #include "py/runtime.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 34 | #include "py/stackctrl.h" |
Damien George | 52f8f56 | 2017-03-14 11:30:05 +1100 | [diff] [blame] | 35 | #include "py/mperrno.h" |
Damien George | 731f359 | 2015-10-30 23:03:58 +0000 | [diff] [blame] | 36 | #include "py/mphal.h" |
Damien George | fe7d542 | 2015-01-01 21:16:58 +0000 | [diff] [blame] | 37 | #include "py/gc.h" |
Robert HH | eb7637b | 2016-06-26 09:01:18 +0200 | [diff] [blame] | 38 | #include "lib/mp-readline/readline.h" |
Damien George | 40274fe | 2015-11-09 13:13:09 +0000 | [diff] [blame] | 39 | #include "lib/utils/pyexec.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 40 | #include "gccollect.h" |
Josef Gajdusek | 967f323 | 2015-05-17 11:22:26 +0200 | [diff] [blame] | 41 | #include "user_interface.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 42 | |
Paul Sokolovsky | a6c9060 | 2016-11-02 00:22:43 +0300 | [diff] [blame] | 43 | STATIC char heap[36 * 1024]; |
Paul Sokolovsky | c6b8750 | 2015-01-15 00:21:57 +0200 | [diff] [blame] | 44 | |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 45 | STATIC void mp_reset(void) { |
Paul Sokolovsky | db984b7 | 2016-02-13 15:44:53 +0200 | [diff] [blame] | 46 | mp_stack_set_top((void*)0x40000000); |
| 47 | mp_stack_set_limit(8192); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 48 | mp_hal_init(); |
Paul Sokolovsky | c6b8750 | 2015-01-15 00:21:57 +0200 | [diff] [blame] | 49 | gc_init(heap, heap + sizeof(heap)); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 50 | mp_init(); |
| 51 | mp_obj_list_init(mp_sys_path, 0); |
Paul Sokolovsky | fb5017f | 2016-05-03 18:21:50 +0300 | [diff] [blame] | 52 | mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) |
Damien George | e62235f | 2017-03-10 17:14:21 +1100 | [diff] [blame] | 53 | mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); |
| 54 | mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_)); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 55 | mp_obj_list_init(mp_sys_argv, 0); |
Damien George | 4b597a1 | 2016-03-31 19:55:42 +0300 | [diff] [blame] | 56 | MP_STATE_PORT(term_obj) = MP_OBJ_NULL; |
Paul Sokolovsky | e07ef8f | 2016-07-04 17:40:26 +0300 | [diff] [blame] | 57 | MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; |
Damien George | 3a4ebf5 | 2016-12-09 17:05:51 +1100 | [diff] [blame] | 58 | #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA |
Damien George | 45a6156 | 2016-12-09 16:47:47 +1100 | [diff] [blame] | 59 | extern void esp_native_code_init(void); |
| 60 | esp_native_code_init(); |
| 61 | #endif |
Damien George | 674bf1b | 2016-04-14 11:15:43 +0100 | [diff] [blame] | 62 | pin_init0(); |
Robert HH | eb7637b | 2016-06-26 09:01:18 +0200 | [diff] [blame] | 63 | readline_init0(); |
Paul Sokolovsky | 77f0cd8 | 2016-07-23 00:05:38 +0300 | [diff] [blame] | 64 | dupterm_task_init(); |
Josef Gajdusek | bda7041 | 2015-05-06 00:19:26 +0200 | [diff] [blame] | 65 | #if MICROPY_MODULE_FROZEN |
Paul Sokolovsky | cb7693b | 2016-05-22 04:08:22 +0300 | [diff] [blame] | 66 | pyexec_frozen_module("_boot.py"); |
Damien George | 6e87aeb | 2016-04-10 14:12:52 +0300 | [diff] [blame] | 67 | pyexec_file("boot.py"); |
Damien George | 4f29b31 | 2017-03-06 17:41:34 +1100 | [diff] [blame] | 68 | if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { |
| 69 | pyexec_file("main.py"); |
| 70 | } |
Josef Gajdusek | bda7041 | 2015-05-06 00:19:26 +0200 | [diff] [blame] | 71 | #endif |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 72 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 73 | |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 74 | void soft_reset(void) { |
Damien George | 1febaf3 | 2015-12-22 14:28:02 +0000 | [diff] [blame] | 75 | mp_hal_stdout_tx_str("PYB: soft reboot\r\n"); |
Paul Sokolovsky | 5699fc9 | 2015-10-29 02:06:13 +0300 | [diff] [blame] | 76 | mp_hal_delay_us(10000); // allow UART to flush output |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 77 | mp_reset(); |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 78 | #if MICROPY_REPL_EVENT_DRIVEN |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 79 | pyexec_event_repl_init(); |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 80 | #endif |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 81 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 82 | |
Josef Gajdusek | 967f323 | 2015-05-17 11:22:26 +0200 | [diff] [blame] | 83 | void init_done(void) { |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 84 | #if MICROPY_REPL_EVENT_DRIVEN |
Paul Sokolovsky | 53302f1 | 2016-03-27 14:33:17 +0300 | [diff] [blame] | 85 | uart_task_init(); |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 86 | #endif |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 87 | mp_reset(); |
| 88 | mp_hal_stdout_tx_str("\r\n"); |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 89 | #if MICROPY_REPL_EVENT_DRIVEN |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 90 | pyexec_event_repl_init(); |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 91 | #endif |
Paul Sokolovsky | 785cf9a | 2016-04-01 14:02:36 +0300 | [diff] [blame] | 92 | |
| 93 | #if !MICROPY_REPL_EVENT_DRIVEN |
| 94 | soft_reset: |
| 95 | for (;;) { |
| 96 | if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { |
| 97 | if (pyexec_raw_repl() != 0) { |
| 98 | break; |
| 99 | } |
| 100 | } else { |
| 101 | if (pyexec_friendly_repl() != 0) { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | soft_reset(); |
| 107 | goto soft_reset; |
| 108 | #endif |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Josef Gajdusek | 967f323 | 2015-05-17 11:22:26 +0200 | [diff] [blame] | 111 | void user_init(void) { |
| 112 | system_init_done_cb(init_done); |
| 113 | } |
| 114 | |
Damien George | f9ecd48 | 2017-01-27 15:16:08 +1100 | [diff] [blame] | 115 | #if !MICROPY_VFS |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 116 | mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
Damien George | 52f8f56 | 2017-03-14 11:30:05 +1100 | [diff] [blame] | 117 | mp_raise_OSError(MP_ENOENT); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | mp_import_stat_t mp_import_stat(const char *path) { |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 121 | (void)path; |
| 122 | return MP_IMPORT_STAT_NO_EXIST; |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Alexander Steffen | ebb9396 | 2017-06-27 17:13:09 +0200 | [diff] [blame] | 125 | mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 126 | return mp_const_none; |
| 127 | } |
| 128 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); |
| 129 | |
Damien George | f9ecd48 | 2017-01-27 15:16:08 +1100 | [diff] [blame] | 130 | #endif |
| 131 | |
Paul Sokolovsky | 2042226 | 2016-10-19 00:20:34 +0300 | [diff] [blame] | 132 | void MP_FASTCODE(nlr_jump_fail)(void *val) { |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 133 | printf("NLR jump failed\n"); |
| 134 | for (;;) { |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | //void __assert(const char *file, int line, const char *func, const char *expr) { |
| 139 | void __assert(const char *file, int line, const char *expr) { |
| 140 | printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); |
| 141 | for (;;) { |
| 142 | } |
| 143 | } |