main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2016-2017 Linaro Limited
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <zephyr.h>
  31. #ifdef CONFIG_NETWORKING
  32. #include <net/net_context.h>
  33. #endif
  34. #include "py/compile.h"
  35. #include "py/runtime.h"
  36. #include "py/repl.h"
  37. #include "py/gc.h"
  38. #include "py/stackctrl.h"
  39. #include "lib/utils/pyexec.h"
  40. #include "lib/mp-readline/readline.h"
  41. #ifdef TEST
  42. #include "lib/upytesthelper/upytesthelper.h"
  43. #include "lib/tinytest/tinytest.c"
  44. #include "lib/upytesthelper/upytesthelper.c"
  45. #include TEST
  46. #endif
  47. static char *stack_top;
  48. static char heap[MICROPY_HEAP_SIZE];
  49. void init_zephyr(void) {
  50. // We now rely on CONFIG_NET_APP_SETTINGS to set up bootstrap
  51. // network addresses.
  52. #if 0
  53. #ifdef CONFIG_NETWORKING
  54. if (net_if_get_default() == NULL) {
  55. // If there's no default networking interface,
  56. // there's nothing to configure.
  57. return;
  58. }
  59. #endif
  60. #ifdef CONFIG_NET_IPV4
  61. static struct in_addr in4addr_my = {{{192, 0, 2, 1}}};
  62. net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0);
  63. static struct in_addr in4netmask_my = {{{255, 255, 255, 0}}};
  64. net_if_ipv4_set_netmask(net_if_get_default(), &in4netmask_my);
  65. static struct in_addr in4gw_my = {{{192, 0, 2, 2}}};
  66. net_if_ipv4_set_gw(net_if_get_default(), &in4gw_my);
  67. #endif
  68. #ifdef CONFIG_NET_IPV6
  69. // 2001:db8::1
  70. static struct in6_addr in6addr_my = {{{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}};
  71. net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0);
  72. #endif
  73. #endif
  74. }
  75. int real_main(void) {
  76. int stack_dummy;
  77. stack_top = (char*)&stack_dummy;
  78. mp_stack_set_top(stack_top);
  79. // Make MicroPython's stack limit somewhat smaller than full stack available
  80. mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512);
  81. init_zephyr();
  82. #ifdef TEST
  83. static const char *argv[] = {"test"};
  84. upytest_set_heap(heap, heap + sizeof(heap));
  85. int r = tinytest_main(1, argv, groups);
  86. printf("status: %d\n", r);
  87. #endif
  88. soft_reset:
  89. #if MICROPY_ENABLE_GC
  90. gc_init(heap, heap + sizeof(heap));
  91. #endif
  92. mp_init();
  93. mp_obj_list_init(mp_sys_path, 0);
  94. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  95. mp_obj_list_init(mp_sys_argv, 0);
  96. #if MICROPY_MODULE_FROZEN
  97. pyexec_frozen_module("main.py");
  98. #endif
  99. for (;;) {
  100. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  101. if (pyexec_raw_repl() != 0) {
  102. break;
  103. }
  104. } else {
  105. if (pyexec_friendly_repl() != 0) {
  106. break;
  107. }
  108. }
  109. }
  110. printf("soft reboot\n");
  111. goto soft_reset;
  112. return 0;
  113. }
  114. void gc_collect(void) {
  115. // WARNING: This gc_collect implementation doesn't try to get root
  116. // pointers from CPU registers, and thus may function incorrectly.
  117. void *dummy;
  118. gc_collect_start();
  119. gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
  120. gc_collect_end();
  121. //gc_dump_info();
  122. }
  123. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  124. mp_raise_OSError(ENOENT);
  125. }
  126. mp_import_stat_t mp_import_stat(const char *path) {
  127. return MP_IMPORT_STAT_NO_EXIST;
  128. }
  129. mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
  130. return mp_const_none;
  131. }
  132. MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
  133. NORETURN void nlr_jump_fail(void *val) {
  134. while (1);
  135. }
  136. #ifndef NDEBUG
  137. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  138. printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  139. __fatal_error("Assertion failed");
  140. }
  141. #endif