modrtthread.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Armink (armink.ztl@gmail.com)
  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 "py/mpconfig.h"
  27. #if MICROPY_PY_RTTHREAD
  28. #include <rtthread.h>
  29. #include "py/runtime.h"
  30. rt_bool_t rt_is_preempt_thread(void) {
  31. if (rt_interrupt_get_nest() || rt_critical_level()) {
  32. return RT_FALSE;
  33. } else {
  34. return RT_TRUE;
  35. }
  36. }
  37. STATIC mp_obj_t mod_is_preempt_thread(void) {
  38. return mp_obj_new_bool(rt_is_preempt_thread());
  39. }
  40. STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_is_preempt_thread_obj, mod_is_preempt_thread);
  41. STATIC mp_obj_t mod_current_tid(void) {
  42. return MP_OBJ_NEW_SMALL_INT(rt_thread_self());
  43. }
  44. STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_current_tid_obj, mod_current_tid);
  45. STATIC mp_obj_t mod_stacks_analyze(void) {
  46. extern long list_thread(void);
  47. list_thread();
  48. return mp_const_none;
  49. }
  50. STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_stacks_analyze_obj, mod_stacks_analyze);
  51. STATIC mp_obj_t os_sync(void) {
  52. //TODO
  53. MP_RTT_NOT_IMPL_PRINT;
  54. return mp_const_none;
  55. }
  56. MP_DEFINE_CONST_FUN_OBJ_0(mod_os_sync_obj, os_sync);
  57. STATIC mp_obj_t mp_os_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  58. //TODO
  59. rt_kprintf("NOT implement, Please add for your board!\n");
  60. return mp_const_none;
  61. }
  62. MP_DEFINE_CONST_FUN_OBJ_KW(mp_os_mount_obj, 2, mp_os_mount);
  63. STATIC const mp_rom_map_elem_t mp_module_rtthread_globals_table[] = {
  64. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rtthread) },
  65. { MP_ROM_QSTR(MP_QSTR_is_preempt_thread), MP_ROM_PTR(&mod_is_preempt_thread_obj) },
  66. { MP_ROM_QSTR(MP_QSTR_current_tid), MP_ROM_PTR(&mod_current_tid_obj) },
  67. { MP_ROM_QSTR(MP_QSTR_stacks_analyze), MP_ROM_PTR(&mod_stacks_analyze_obj) },
  68. };
  69. STATIC MP_DEFINE_CONST_DICT(mp_module_rtthread_globals, mp_module_rtthread_globals_table);
  70. const mp_obj_module_t mp_module_rtthread = {
  71. .base = { &mp_type_module },
  72. .globals = (mp_obj_dict_t*)&mp_module_rtthread_globals,
  73. };
  74. #endif // MICROPY_PY_RTTHREAD