modmachine.c 7.4 KB

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