blob: 1701ef69f9b54cf7179a079123eecc55c89ee378 [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"
Damien George40274fe2015-11-09 13:13:09 +000037#include "lib/utils/pyexec.h"
Damien George075d5972014-11-27 20:30:33 +000038#include "gccollect.h"
Josef Gajdusek967f3232015-05-17 11:22:26 +020039#include "user_interface.h"
Damien George075d5972014-11-27 20:30:33 +000040
Paul Sokolovsky69256ac2016-04-03 19:55:45 +030041STATIC char heap[24 * 1024];
Paul Sokolovskyc6b87502015-01-15 00:21:57 +020042
Damien Georgec98c1282015-05-06 00:02:58 +010043STATIC void mp_reset(void) {
Paul Sokolovskydb984b72016-02-13 15:44:53 +020044 mp_stack_set_top((void*)0x40000000);
45 mp_stack_set_limit(8192);
Damien George075d5972014-11-27 20:30:33 +000046 mp_hal_init();
Paul Sokolovskyc6b87502015-01-15 00:21:57 +020047 gc_init(heap, heap + sizeof(heap));
Damien George075d5972014-11-27 20:30:33 +000048 mp_init();
49 mp_obj_list_init(mp_sys_path, 0);
50 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +030051 #if MICROPY_VFS_FAT
Damien George71d40f12016-03-28 13:28:41 +030052 memset(MP_STATE_PORT(fs_user_mount), 0, sizeof(MP_STATE_PORT(fs_user_mount)));
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +030053 #endif
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +020054 MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
Damien George4b597a12016-03-31 19:55:42 +030055 MP_STATE_PORT(term_obj) = MP_OBJ_NULL;
Josef Gajdusekbda70412015-05-06 00:19:26 +020056#if MICROPY_MODULE_FROZEN
Paul Sokolovskya0cd1182016-02-20 20:45:23 +020057 pyexec_frozen_module("boot");
Josef Gajdusekbda70412015-05-06 00:19:26 +020058#endif
Damien Georgec98c1282015-05-06 00:02:58 +010059}
Damien George075d5972014-11-27 20:30:33 +000060
Damien Georgec98c1282015-05-06 00:02:58 +010061void soft_reset(void) {
Damien George1febaf32015-12-22 14:28:02 +000062 mp_hal_stdout_tx_str("PYB: soft reboot\r\n");
Paul Sokolovsky5699fc92015-10-29 02:06:13 +030063 mp_hal_delay_us(10000); // allow UART to flush output
Damien Georgec98c1282015-05-06 00:02:58 +010064 mp_reset();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030065 #if MICROPY_REPL_EVENT_DRIVEN
Damien Georgec98c1282015-05-06 00:02:58 +010066 pyexec_event_repl_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030067 #endif
Damien Georgec98c1282015-05-06 00:02:58 +010068}
Damien George075d5972014-11-27 20:30:33 +000069
Josef Gajdusek967f3232015-05-17 11:22:26 +020070void init_done(void) {
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030071 #if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovsky53302f12016-03-27 14:33:17 +030072 uart_task_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030073 #endif
Damien Georgec98c1282015-05-06 00:02:58 +010074 mp_reset();
75 mp_hal_stdout_tx_str("\r\n");
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030076 #if MICROPY_REPL_EVENT_DRIVEN
Damien Georgec98c1282015-05-06 00:02:58 +010077 pyexec_event_repl_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030078 #endif
Paul Sokolovsky98af8912016-03-31 19:49:55 +030079 dupterm_task_init();
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +030080
81 #if !MICROPY_REPL_EVENT_DRIVEN
82soft_reset:
83 for (;;) {
84 if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
85 if (pyexec_raw_repl() != 0) {
86 break;
87 }
88 } else {
89 if (pyexec_friendly_repl() != 0) {
90 break;
91 }
92 }
93 }
94 soft_reset();
95 goto soft_reset;
96 #endif
Damien George075d5972014-11-27 20:30:33 +000097}
98
Josef Gajdusek967f3232015-05-17 11:22:26 +020099void user_init(void) {
100 system_init_done_cb(init_done);
101}
102
Paul Sokolovsky2f023022016-03-28 18:38:25 +0300103mp_lexer_t *fat_vfs_lexer_new_from_file(const char *filename);
104mp_import_stat_t fat_vfs_import_stat(const char *path);
105
Damien George075d5972014-11-27 20:30:33 +0000106mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300107 #if MICROPY_VFS_FAT
Paul Sokolovsky2f023022016-03-28 18:38:25 +0300108 return fat_vfs_lexer_new_from_file(filename);
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300109 #else
110 (void)filename;
111 return NULL;
112 #endif
Damien George075d5972014-11-27 20:30:33 +0000113}
114
115mp_import_stat_t mp_import_stat(const char *path) {
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300116 #if MICROPY_VFS_FAT
Paul Sokolovsky2f023022016-03-28 18:38:25 +0300117 return fat_vfs_import_stat(path);
Paul Sokolovskyb4070ee2016-03-28 21:35:41 +0300118 #else
119 (void)path;
120 return MP_IMPORT_STAT_NO_EXIST;
121 #endif
Damien George075d5972014-11-27 20:30:33 +0000122}
123
124mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
125 return mp_const_none;
126}
127MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
128
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200129void mp_keyboard_interrupt(void) {
130 MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(mp_kbd_exception);
131}
132
Damien George075d5972014-11-27 20:30:33 +0000133void nlr_jump_fail(void *val) {
134 printf("NLR jump failed\n");
135 for (;;) {
136 }
137}
138
139//void __assert(const char *file, int line, const char *func, const char *expr) {
140void __assert(const char *file, int line, const char *expr) {
141 printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
142 for (;;) {
143 }
144}