modmachine.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /*
  113. * @param clkid - range 0~127 (e.g 0:SYSCLK 1:HCLK 2:PCLK1 etc)
  114. *
  115. * @return 0 - ok, -1 - no such clock
  116. */
  117. RT_WEAK int mp_port_get_freq(int clkid, int *freq)
  118. {
  119. return -1;
  120. }
  121. STATIC mp_obj_t machine_freq(void) {
  122. int i;
  123. mp_obj_list_t *ret_list = m_new(mp_obj_list_t, 1);
  124. mp_obj_list_init(ret_list, 0);
  125. int freq;
  126. for (i = 0; i < 128; i ++)
  127. {
  128. if (mp_port_get_freq(i, &freq) != 0)
  129. break;
  130. mp_obj_list_append(ret_list, mp_obj_new_int(freq));
  131. }
  132. return MP_OBJ_FROM_PTR(ret_list);
  133. }
  134. MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq);
  135. STATIC mp_obj_t pyb_wfi(void) {
  136. //TODO __WFI();
  137. MP_RTT_NOT_IMPL_PRINT;
  138. return mp_const_none;
  139. }
  140. MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi);
  141. static rt_base_t int_lvl;
  142. STATIC mp_obj_t pyb_disable_irq(void) {
  143. int_lvl = rt_hw_interrupt_disable();
  144. return mp_obj_new_bool(1);
  145. }
  146. MP_DEFINE_CONST_FUN_OBJ_0(pyb_disable_irq_obj, pyb_disable_irq);
  147. STATIC mp_obj_t pyb_enable_irq(size_t n_args, const mp_obj_t *arg) {
  148. if (n_args == 0) {
  149. rt_hw_interrupt_enable(int_lvl);
  150. } else {
  151. if (mp_obj_is_true(arg[0])) {
  152. rt_hw_interrupt_enable(int_lvl);
  153. } else {
  154. int_lvl = rt_hw_interrupt_disable();
  155. }
  156. }
  157. return mp_const_none;
  158. }
  159. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj, 0, 1, pyb_enable_irq);
  160. STATIC mp_obj_t machine_sleep (void) {
  161. //TODO
  162. MP_RTT_NOT_IMPL_PRINT;
  163. return mp_const_none;
  164. }
  165. MP_DEFINE_CONST_FUN_OBJ_0(machine_sleep_obj, machine_sleep);
  166. STATIC mp_obj_t machine_deepsleep (void) {
  167. //TODO
  168. MP_RTT_NOT_IMPL_PRINT;
  169. return mp_const_none;
  170. }
  171. MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep);
  172. STATIC mp_obj_t machine_reset_cause(void) {
  173. //TODO
  174. MP_RTT_NOT_IMPL_PRINT;
  175. return MP_OBJ_NEW_SMALL_INT(42);
  176. }
  177. STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
  178. STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
  179. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
  180. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
  181. { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
  182. { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
  183. { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
  184. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
  185. { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&pyb_wfi_obj) },
  186. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_sleep_obj) },
  187. { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
  188. { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
  189. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) },
  190. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) },
  191. // { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
  192. #if MICROPY_PY_PIN
  193. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
  194. #endif
  195. { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
  196. #if MICROPY_PY_MACHINE_I2C
  197. { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
  198. #endif
  199. #if MICROPY_PY_MACHINE_SPI
  200. { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
  201. #endif
  202. #if MICROPY_PY_MACHINE_UART
  203. { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
  204. #endif
  205. #if MICROPY_PY_MACHINE_RTC
  206. { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
  207. #endif
  208. #if MICROPY_PY_MACHINE_LCD
  209. { MP_ROM_QSTR(MP_QSTR_LCD), MP_ROM_PTR(&machine_lcd_type ) },
  210. #endif
  211. #if MICROPY_PY_MACHINE_PWM
  212. { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
  213. #endif
  214. #if MICROPY_PY_MACHINE_ADC
  215. { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
  216. #endif
  217. #if MICROPY_PY_MACHINE_WDT
  218. { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
  219. #endif
  220. #if MICROPY_PY_MACHINE_TIMER
  221. { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
  222. #endif
  223. };
  224. STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
  225. const mp_obj_module_t mp_module_machine = {
  226. .base = { &mp_type_module },
  227. .globals = (mp_obj_dict_t*)&machine_module_globals,
  228. };
  229. #endif // MICROPY_PY_MACHINE