modmachine.c 7.7 KB

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