main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 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. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <p33Fxxxx.h>
  30. #include "py/compile.h"
  31. #include "py/runtime.h"
  32. #include "py/gc.h"
  33. #include "py/mphal.h"
  34. #include "py/mperrno.h"
  35. #include "lib/utils/pyexec.h"
  36. #include "lib/mp-readline/readline.h"
  37. #include "board.h"
  38. #include "modpyb.h"
  39. _FGS(GWRP_OFF & GCP_OFF);
  40. _FOSCSEL(FNOSC_FRC);
  41. _FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE);
  42. _FWDT(FWDTEN_OFF);
  43. // maximum heap for device with 8k RAM
  44. static char heap[4600];
  45. int main(int argc, char **argv) {
  46. // init the CPU and the peripherals
  47. cpu_init();
  48. led_init();
  49. switch_init();
  50. uart_init();
  51. soft_reset:
  52. // flash green led for 150ms to indicate boot
  53. led_state(1, 0);
  54. led_state(2, 0);
  55. led_state(3, 1);
  56. mp_hal_delay_ms(150);
  57. led_state(3, 0);
  58. // init MicroPython runtime
  59. int stack_dummy;
  60. MP_STATE_THREAD(stack_top) = (char*)&stack_dummy;
  61. gc_init(heap, heap + sizeof(heap));
  62. mp_init();
  63. mp_hal_init();
  64. readline_init0();
  65. // REPL loop
  66. for (;;) {
  67. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  68. if (pyexec_raw_repl() != 0) {
  69. break;
  70. }
  71. } else {
  72. if (pyexec_friendly_repl() != 0) {
  73. break;
  74. }
  75. }
  76. }
  77. printf("PYB: soft reboot\n");
  78. mp_deinit();
  79. goto soft_reset;
  80. }
  81. void gc_collect(void) {
  82. // TODO possibly need to trace registers
  83. void *dummy;
  84. gc_collect_start();
  85. // Node: stack is ascending
  86. gc_collect_root(&dummy, ((mp_uint_t)&dummy - (mp_uint_t)MP_STATE_THREAD(stack_top)) / sizeof(mp_uint_t));
  87. gc_collect_end();
  88. }
  89. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  90. mp_raise_OSError(MP_ENOENT);
  91. }
  92. mp_import_stat_t mp_import_stat(const char *path) {
  93. return MP_IMPORT_STAT_NO_EXIST;
  94. }
  95. mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
  96. return mp_const_none;
  97. }
  98. MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
  99. void nlr_jump_fail(void *val) {
  100. while (1);
  101. }
  102. void NORETURN __fatal_error(const char *msg) {
  103. while (1);
  104. }
  105. #ifndef NDEBUG
  106. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  107. printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  108. __fatal_error("Assertion failed");
  109. }
  110. #endif