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 | 731f359 | 2015-10-30 23:03:58 +0000 | [diff] [blame] | 35 | #include "py/mphal.h" |
Damien George | fe7d542 | 2015-01-01 21:16:58 +0000 | [diff] [blame] | 36 | #include "py/gc.h" |
Damien George | 40274fe | 2015-11-09 13:13:09 +0000 | [diff] [blame] | 37 | #include "lib/utils/pyexec.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 38 | #include "gccollect.h" |
Josef Gajdusek | 967f323 | 2015-05-17 11:22:26 +0200 | [diff] [blame] | 39 | #include "user_interface.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 40 | |
Paul Sokolovsky | d88250c | 2016-03-29 21:14:41 +0300 | [diff] [blame] | 41 | STATIC char heap[15360]; |
Paul Sokolovsky | c6b8750 | 2015-01-15 00:21:57 +0200 | [diff] [blame] | 42 | |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 43 | STATIC void mp_reset(void) { |
Paul Sokolovsky | db984b7 | 2016-02-13 15:44:53 +0200 | [diff] [blame] | 44 | mp_stack_set_top((void*)0x40000000); |
| 45 | mp_stack_set_limit(8192); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 46 | mp_hal_init(); |
Paul Sokolovsky | c6b8750 | 2015-01-15 00:21:57 +0200 | [diff] [blame] | 47 | gc_init(heap, heap + sizeof(heap)); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 48 | mp_init(); |
| 49 | mp_obj_list_init(mp_sys_path, 0); |
| 50 | mp_obj_list_init(mp_sys_argv, 0); |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 51 | #if MICROPY_VFS_FAT |
Damien George | 71d40f1 | 2016-03-28 13:28:41 +0300 | [diff] [blame] | 52 | memset(MP_STATE_PORT(fs_user_mount), 0, sizeof(MP_STATE_PORT(fs_user_mount))); |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 53 | #endif |
Paul Sokolovsky | d3a4d39 | 2015-12-20 13:58:58 +0200 | [diff] [blame] | 54 | MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); |
Damien George | 4b597a1 | 2016-03-31 19:55:42 +0300 | [diff] [blame^] | 55 | MP_STATE_PORT(term_obj) = MP_OBJ_NULL; |
Josef Gajdusek | bda7041 | 2015-05-06 00:19:26 +0200 | [diff] [blame] | 56 | #if MICROPY_MODULE_FROZEN |
Paul Sokolovsky | a0cd118 | 2016-02-20 20:45:23 +0200 | [diff] [blame] | 57 | pyexec_frozen_module("boot"); |
Josef Gajdusek | bda7041 | 2015-05-06 00:19:26 +0200 | [diff] [blame] | 58 | #endif |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 59 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 60 | |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 61 | void soft_reset(void) { |
Damien George | 1febaf3 | 2015-12-22 14:28:02 +0000 | [diff] [blame] | 62 | mp_hal_stdout_tx_str("PYB: soft reboot\r\n"); |
Paul Sokolovsky | 5699fc9 | 2015-10-29 02:06:13 +0300 | [diff] [blame] | 63 | mp_hal_delay_us(10000); // allow UART to flush output |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 64 | mp_reset(); |
| 65 | pyexec_event_repl_init(); |
| 66 | } |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 67 | |
Josef Gajdusek | 967f323 | 2015-05-17 11:22:26 +0200 | [diff] [blame] | 68 | void init_done(void) { |
Paul Sokolovsky | 53302f1 | 2016-03-27 14:33:17 +0300 | [diff] [blame] | 69 | uart_task_init(); |
Damien George | c98c128 | 2015-05-06 00:02:58 +0100 | [diff] [blame] | 70 | mp_reset(); |
| 71 | mp_hal_stdout_tx_str("\r\n"); |
| 72 | pyexec_event_repl_init(); |
Paul Sokolovsky | 98af891 | 2016-03-31 19:49:55 +0300 | [diff] [blame] | 73 | dupterm_task_init(); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Josef Gajdusek | 967f323 | 2015-05-17 11:22:26 +0200 | [diff] [blame] | 76 | void user_init(void) { |
| 77 | system_init_done_cb(init_done); |
| 78 | } |
| 79 | |
Paul Sokolovsky | 2f02302 | 2016-03-28 18:38:25 +0300 | [diff] [blame] | 80 | mp_lexer_t *fat_vfs_lexer_new_from_file(const char *filename); |
| 81 | mp_import_stat_t fat_vfs_import_stat(const char *path); |
| 82 | |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 83 | mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 84 | #if MICROPY_VFS_FAT |
Paul Sokolovsky | 2f02302 | 2016-03-28 18:38:25 +0300 | [diff] [blame] | 85 | return fat_vfs_lexer_new_from_file(filename); |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 86 | #else |
| 87 | (void)filename; |
| 88 | return NULL; |
| 89 | #endif |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | mp_import_stat_t mp_import_stat(const char *path) { |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 93 | #if MICROPY_VFS_FAT |
Paul Sokolovsky | 2f02302 | 2016-03-28 18:38:25 +0300 | [diff] [blame] | 94 | return fat_vfs_import_stat(path); |
Paul Sokolovsky | b4070ee | 2016-03-28 21:35:41 +0300 | [diff] [blame] | 95 | #else |
| 96 | (void)path; |
| 97 | return MP_IMPORT_STAT_NO_EXIST; |
| 98 | #endif |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
| 102 | return mp_const_none; |
| 103 | } |
| 104 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); |
| 105 | |
Paul Sokolovsky | d3a4d39 | 2015-12-20 13:58:58 +0200 | [diff] [blame] | 106 | void mp_keyboard_interrupt(void) { |
| 107 | MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(mp_kbd_exception); |
| 108 | } |
| 109 | |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 110 | void nlr_jump_fail(void *val) { |
| 111 | printf("NLR jump failed\n"); |
| 112 | for (;;) { |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | //void __assert(const char *file, int line, const char *func, const char *expr) { |
| 117 | void __assert(const char *file, int line, const char *expr) { |
| 118 | printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); |
| 119 | for (;;) { |
| 120 | } |
| 121 | } |