mp_flipper_runtime.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <string.h>
  2. #include "py/compile.h"
  3. #include "py/runtime.h"
  4. #include "py/persistentcode.h"
  5. #include "py/gc.h"
  6. #include "py/stackctrl.h"
  7. #include "shared/runtime/gchelper.h"
  8. #include "mp_flipper_runtime.h"
  9. #include "mp_flipper_halport.h"
  10. #include "mp_flipper_logging.h"
  11. const char* mp_flipper_root_module_path;
  12. void* mp_flipper_context;
  13. void mp_flipper_set_root_module_path(const char* path) {
  14. mp_flipper_root_module_path = path;
  15. }
  16. void mp_flipper_init(void* heap, size_t heap_size, size_t stack_size, void* stack_top) {
  17. mp_flipper_context = mp_flipper_context_alloc();
  18. mp_flipper_log_level = MP_FLIPPER_LOG_LEVEL_INFO;
  19. mp_stack_set_top(stack_top);
  20. mp_stack_set_limit(stack_size);
  21. #if MICROPY_ENABLE_GC
  22. gc_init(heap, (uint8_t*)heap + heap_size);
  23. #endif
  24. mp_init();
  25. }
  26. void mp_flipper_deinit() {
  27. gc_sweep_all();
  28. mp_deinit();
  29. mp_flipper_context_free(mp_flipper_context);
  30. }
  31. // Called if an exception is raised outside all C exception-catching handlers.
  32. void nlr_jump_fail(void* val) {
  33. mp_flipper_nlr_jump_fail(val);
  34. }
  35. #if MICROPY_ENABLE_GC
  36. // Run a garbage collection cycle.
  37. void gc_collect(void) {
  38. gc_collect_start();
  39. gc_helper_collect_regs_and_stack();
  40. gc_collect_end();
  41. }
  42. #endif
  43. #ifndef NDEBUG
  44. // Used when debugging is enabled.
  45. void __assert_func(const char* file, int line, const char* func, const char* expr) {
  46. mp_flipper_assert(file, line, func, expr);
  47. }
  48. void NORETURN __fatal_error(const char* msg) {
  49. mp_flipper_fatal_error(msg);
  50. }
  51. #endif
  52. void mp_flipper_raise_os_error(int error) {
  53. mp_raise_OSError(error);
  54. }
  55. void mp_flipper_raise_os_error_with_filename(int error, const char* filename) {
  56. mp_raise_OSError_with_filename(error, filename);
  57. }