mp_flipper_runtime.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const char* mp_flipper_root_module_path;
  11. void* mp_flipper_context;
  12. void mp_flipper_set_root_module_path(const char* path) {
  13. mp_flipper_root_module_path = path;
  14. }
  15. void mp_flipper_init(void* heap, size_t heap_size, size_t stack_size, void* stack_top) {
  16. mp_flipper_context = mp_flipper_context_alloc();
  17. mp_stack_set_top(stack_top);
  18. mp_stack_set_limit(stack_size);
  19. #if MICROPY_ENABLE_GC
  20. gc_init(heap, (uint8_t*)heap + heap_size);
  21. #endif
  22. mp_init();
  23. }
  24. void mp_flipper_deinit() {
  25. gc_sweep_all();
  26. mp_deinit();
  27. mp_flipper_context_free(mp_flipper_context);
  28. }
  29. // Called if an exception is raised outside all C exception-catching handlers.
  30. void nlr_jump_fail(void* val) {
  31. mp_flipper_nlr_jump_fail(val);
  32. }
  33. #if MICROPY_ENABLE_GC
  34. // Run a garbage collection cycle.
  35. void gc_collect(void) {
  36. gc_collect_start();
  37. gc_helper_collect_regs_and_stack();
  38. gc_collect_end();
  39. }
  40. #endif
  41. #ifndef NDEBUG
  42. // Used when debugging is enabled.
  43. void __assert_func(const char* file, int line, const char* func, const char* expr) {
  44. mp_flipper_assert(file, line, func, expr);
  45. }
  46. void NORETURN __fatal_error(const char* msg) {
  47. mp_flipper_fatal_error(msg);
  48. }
  49. #endif
  50. void mp_flipper_raise_os_error(int error) {
  51. mp_raise_OSError(error);
  52. }
  53. void mp_flipper_raise_os_error_with_filename(int error, const char* filename) {
  54. mp_raise_OSError_with_filename(error, filename);
  55. }