main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2016 Damien P. George
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include "freertos/FreeRTOS.h"
  32. #include "freertos/task.h"
  33. #include "esp_system.h"
  34. #include "nvs_flash.h"
  35. #include "esp_task.h"
  36. #include "soc/cpu.h"
  37. #include "esp_log.h"
  38. #include "py/stackctrl.h"
  39. #include "py/nlr.h"
  40. #include "py/compile.h"
  41. #include "py/runtime.h"
  42. #include "py/repl.h"
  43. #include "py/gc.h"
  44. #include "py/mphal.h"
  45. #include "lib/mp-readline/readline.h"
  46. #include "lib/utils/pyexec.h"
  47. #include "uart.h"
  48. #include "modmachine.h"
  49. #include "modnetwork.h"
  50. #include "mpthreadport.h"
  51. // MicroPython runs as a task under FreeRTOS
  52. #define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1)
  53. #define MP_TASK_STACK_SIZE (16 * 1024)
  54. #define MP_TASK_STACK_LEN (MP_TASK_STACK_SIZE / sizeof(StackType_t))
  55. STATIC StaticTask_t mp_task_tcb;
  56. STATIC StackType_t mp_task_stack[MP_TASK_STACK_LEN] __attribute__((aligned (8)));
  57. int vprintf_null(const char *format, va_list ap) {
  58. // do nothing: this is used as a log target during raw repl mode
  59. return 0;
  60. }
  61. void mp_task(void *pvParameter) {
  62. volatile uint32_t sp = (uint32_t)get_sp();
  63. #if MICROPY_PY_THREAD
  64. mp_thread_init(&mp_task_stack[0], MP_TASK_STACK_LEN);
  65. #endif
  66. uart_init();
  67. // Allocate the uPy heap using malloc and get the largest available region
  68. size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
  69. void *mp_task_heap = malloc(mp_task_heap_size);
  70. soft_reset:
  71. // initialise the stack pointer for the main thread
  72. mp_stack_set_top((void *)sp);
  73. mp_stack_set_limit(MP_TASK_STACK_SIZE - 1024);
  74. gc_init(mp_task_heap, mp_task_heap + mp_task_heap_size);
  75. mp_init();
  76. mp_obj_list_init(mp_sys_path, 0);
  77. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
  78. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
  79. mp_obj_list_init(mp_sys_argv, 0);
  80. readline_init0();
  81. // initialise peripherals
  82. machine_pins_init();
  83. // run boot-up scripts
  84. pyexec_frozen_module("_boot.py");
  85. pyexec_file("boot.py");
  86. if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
  87. pyexec_file("main.py");
  88. }
  89. for (;;) {
  90. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  91. vprintf_like_t vprintf_log = esp_log_set_vprintf(vprintf_null);
  92. if (pyexec_raw_repl() != 0) {
  93. break;
  94. }
  95. esp_log_set_vprintf(vprintf_log);
  96. } else {
  97. if (pyexec_friendly_repl() != 0) {
  98. break;
  99. }
  100. }
  101. }
  102. #if MICROPY_PY_THREAD
  103. mp_thread_deinit();
  104. #endif
  105. gc_sweep_all();
  106. mp_hal_stdout_tx_str("PYB: soft reboot\r\n");
  107. // deinitialise peripherals
  108. machine_pins_deinit();
  109. usocket_events_deinit();
  110. mp_deinit();
  111. fflush(stdout);
  112. goto soft_reset;
  113. }
  114. void app_main(void) {
  115. nvs_flash_init();
  116. mp_main_task_handle = xTaskCreateStaticPinnedToCore(mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY,
  117. &mp_task_stack[0], &mp_task_tcb, 0);
  118. }
  119. void nlr_jump_fail(void *val) {
  120. printf("NLR jump failed, val=%p\n", val);
  121. esp_restart();
  122. }
  123. // modussl_mbedtls uses this function but it's not enabled in ESP IDF
  124. void mbedtls_debug_set_threshold(int threshold) {
  125. (void)threshold;
  126. }