modmachine.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <stdint.h>
  27. #include <stdio.h>
  28. #include "py/obj.h"
  29. #include "py/runtime.h"
  30. #include "py/gc.h"
  31. #include "lib/utils/pyexec.h"
  32. #include "extmod/machine_mem.h"
  33. #include "extmod/machine_signal.h"
  34. #include "extmod/machine_pulse.h"
  35. #include "extmod/machine_i2c.h"
  36. #include "extmod/machine_spi.h"
  37. #include "modmachine.h"
  38. #include "machine_uart.h"
  39. #include <rthw.h>
  40. #if MICROPY_PY_MACHINE
  41. STATIC mp_obj_t machine_info(uint n_args, const mp_obj_t *args) {
  42. #ifdef RT_USING_FINSH
  43. extern long list_thread(void);
  44. #endif
  45. // RT-Thread info
  46. {
  47. rt_kprintf("---------------------------------------------\n");
  48. rt_kprintf("RT-Thread\n");
  49. rt_kprintf("---------------------------------------------\n");
  50. #ifdef RT_USING_FINSH
  51. extern void list_mem(void);
  52. extern void list_memheap(void);
  53. #ifdef RT_USING_MEMHEAP_AS_HEAP
  54. list_memheap();
  55. #else
  56. list_mem();
  57. #endif
  58. list_thread();
  59. #endif
  60. rt_kprintf("---------------------------------------------\n");
  61. }
  62. // qstr info
  63. {
  64. mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
  65. qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
  66. rt_kprintf("qstr:\n n_pool=" UINT_FMT "\n n_qstr=" UINT_FMT "\n n_str_data_bytes=" UINT_FMT "\n n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
  67. }
  68. rt_kprintf("---------------------------------------------\n");
  69. // GC info
  70. {
  71. gc_info_t info;
  72. gc_info(&info);
  73. rt_kprintf("GC:\n");
  74. rt_kprintf(" " UINT_FMT " total\n", info.total);
  75. rt_kprintf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
  76. rt_kprintf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
  77. }
  78. // free space on flash
  79. {
  80. //TODO
  81. }
  82. if (n_args == 1) {
  83. // arg given means dump gc allocation table
  84. gc_dump_alloc_table();
  85. }
  86. return mp_const_none;
  87. }
  88. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info);
  89. STATIC mp_obj_t machine_unique_id(void) {
  90. //TODO
  91. MP_RTT_NOT_IMPL_PRINT;
  92. return 0;
  93. }
  94. MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
  95. STATIC mp_obj_t machine_reset(void) {
  96. //TODO
  97. MP_RTT_NOT_IMPL_PRINT;
  98. return mp_const_none;
  99. }
  100. MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
  101. STATIC mp_obj_t machine_soft_reset(void) {
  102. pyexec_system_exit = PYEXEC_FORCED_EXIT;
  103. nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
  104. }
  105. MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
  106. STATIC mp_obj_t machine_freq(void) {
  107. //TODO
  108. MP_RTT_NOT_IMPL_PRINT;
  109. return MP_OBJ_SMALL_INT_VALUE(0);
  110. }
  111. MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq);
  112. STATIC mp_obj_t pyb_wfi(void) {
  113. //TODO __WFI();
  114. MP_RTT_NOT_IMPL_PRINT;
  115. return mp_const_none;
  116. }
  117. MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi);
  118. static rt_base_t int_lvl;
  119. STATIC mp_obj_t pyb_disable_irq(void) {
  120. int_lvl = rt_hw_interrupt_disable();
  121. return mp_obj_new_bool(1);
  122. }
  123. MP_DEFINE_CONST_FUN_OBJ_0(pyb_disable_irq_obj, pyb_disable_irq);
  124. STATIC mp_obj_t pyb_enable_irq(uint n_args, const mp_obj_t *arg) {
  125. if (n_args == 0) {
  126. rt_hw_interrupt_enable(int_lvl);
  127. } else {
  128. if (mp_obj_is_true(arg[0])) {
  129. rt_hw_interrupt_enable(int_lvl);
  130. } else {
  131. int_lvl = rt_hw_interrupt_disable();
  132. }
  133. }
  134. return mp_const_none;
  135. }
  136. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj, 0, 1, pyb_enable_irq);
  137. STATIC mp_obj_t machine_sleep (void) {
  138. //TODO
  139. MP_RTT_NOT_IMPL_PRINT;
  140. return mp_const_none;
  141. }
  142. MP_DEFINE_CONST_FUN_OBJ_0(machine_sleep_obj, machine_sleep);
  143. STATIC mp_obj_t machine_deepsleep (void) {
  144. //TODO
  145. MP_RTT_NOT_IMPL_PRINT;
  146. return mp_const_none;
  147. }
  148. MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep);
  149. STATIC mp_obj_t machine_reset_cause(void) {
  150. //TODO
  151. MP_RTT_NOT_IMPL_PRINT;
  152. return MP_OBJ_NEW_SMALL_INT(42);
  153. }
  154. STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
  155. STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
  156. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
  157. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
  158. { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
  159. { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
  160. { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
  161. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
  162. { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&pyb_wfi_obj) },
  163. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_sleep_obj) },
  164. { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
  165. { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
  166. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) },
  167. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) },
  168. // { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
  169. #if MICROPY_PY_PIN
  170. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
  171. #endif
  172. { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
  173. #if MICROPY_PY_MACHINE_I2C
  174. { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
  175. #endif
  176. #if MICROPY_PY_MACHINE_SPI
  177. { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
  178. #endif
  179. #if MICROPY_PY_MACHINE_UART
  180. { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type ) },
  181. #endif
  182. };
  183. STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
  184. const mp_obj_module_t mp_module_machine = {
  185. .base = { &mp_type_module },
  186. .globals = (mp_obj_dict_t*)&machine_module_globals,
  187. };
  188. #endif // MICROPY_PY_MACHINE