modmachine.c 8.1 KB

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