modmachine.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2015 Damien P. George
  7. * Copyright (c) 2016 Glenn Ruben Bakke
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include "modmachine.h"
  29. #include "py/gc.h"
  30. #include "py/runtime.h"
  31. #include "py/mphal.h"
  32. #include "extmod/machine_mem.h"
  33. #include "extmod/machine_pulse.h"
  34. #include "extmod/machine_i2c.h"
  35. #include "lib/utils/pyexec.h"
  36. #include "lib/oofatfs/ff.h"
  37. #include "lib/oofatfs/diskio.h"
  38. #include "gccollect.h"
  39. #include "pin.h"
  40. #include "uart.h"
  41. #include "spi.h"
  42. #include "i2c.h"
  43. #include "timer.h"
  44. #if MICROPY_PY_MACHINE_HW_PWM
  45. #include "pwm.h"
  46. #endif
  47. #if MICROPY_PY_MACHINE_ADC
  48. #include "adc.h"
  49. #endif
  50. #if MICROPY_PY_MACHINE_TEMP
  51. #include "temp.h"
  52. #endif
  53. #if MICROPY_PY_MACHINE_RTCOUNTER
  54. #include "rtcounter.h"
  55. #endif
  56. #define PYB_RESET_HARD (0)
  57. #define PYB_RESET_WDT (1)
  58. #define PYB_RESET_SOFT (2)
  59. #define PYB_RESET_LOCKUP (3)
  60. #define PYB_RESET_POWER_ON (16)
  61. #define PYB_RESET_LPCOMP (17)
  62. #define PYB_RESET_DIF (18)
  63. #define PYB_RESET_NFC (19)
  64. STATIC uint32_t reset_cause;
  65. void machine_init(void) {
  66. uint32_t state = NRF_POWER->RESETREAS;
  67. if (state & POWER_RESETREAS_RESETPIN_Msk) {
  68. reset_cause = PYB_RESET_HARD;
  69. } else if (state & POWER_RESETREAS_DOG_Msk) {
  70. reset_cause = PYB_RESET_WDT;
  71. } else if (state & POWER_RESETREAS_SREQ_Msk) {
  72. reset_cause = PYB_RESET_SOFT;
  73. } else if (state & POWER_RESETREAS_LOCKUP_Msk) {
  74. reset_cause = PYB_RESET_LOCKUP;
  75. } else if (state & POWER_RESETREAS_OFF_Msk) {
  76. reset_cause = PYB_RESET_POWER_ON;
  77. } else if (state & POWER_RESETREAS_LPCOMP_Msk) {
  78. reset_cause = PYB_RESET_LPCOMP;
  79. } else if (state & POWER_RESETREAS_DIF_Msk) {
  80. reset_cause = PYB_RESET_DIF;
  81. #if NRF52
  82. } else if (state & POWER_RESETREAS_NFC_Msk) {
  83. reset_cause = PYB_RESET_NFC;
  84. #endif
  85. }
  86. // clear reset reason
  87. NRF_POWER->RESETREAS = (1 << reset_cause);
  88. }
  89. // machine.info([dump_alloc_table])
  90. // Print out lots of information about the board.
  91. STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) {
  92. // to print info about memory
  93. {
  94. printf("_etext=%p\n", &_etext);
  95. printf("_sidata=%p\n", &_sidata);
  96. printf("_sdata=%p\n", &_sdata);
  97. printf("_edata=%p\n", &_edata);
  98. printf("_sbss=%p\n", &_sbss);
  99. printf("_ebss=%p\n", &_ebss);
  100. printf("_estack=%p\n", &_estack);
  101. printf("_ram_start=%p\n", &_ram_start);
  102. printf("_heap_start=%p\n", &_heap_start);
  103. printf("_heap_end=%p\n", &_heap_end);
  104. printf("_ram_end=%p\n", &_ram_end);
  105. }
  106. // qstr info
  107. {
  108. mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
  109. qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
  110. 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);
  111. }
  112. // GC info
  113. {
  114. gc_info_t info;
  115. gc_info(&info);
  116. printf("GC:\n");
  117. printf(" " UINT_FMT " total\n", info.total);
  118. printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
  119. printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
  120. }
  121. if (n_args == 1) {
  122. // arg given means dump gc allocation table
  123. gc_dump_alloc_table();
  124. }
  125. return mp_const_none;
  126. }
  127. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info);
  128. // Resets the board in a manner similar to pushing the external RESET button.
  129. STATIC mp_obj_t machine_reset(void) {
  130. NVIC_SystemReset();
  131. return mp_const_none;
  132. }
  133. MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
  134. STATIC mp_obj_t machine_soft_reset(void) {
  135. pyexec_system_exit = PYEXEC_FORCED_EXIT;
  136. nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
  137. }
  138. MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
  139. STATIC mp_obj_t machine_sleep(void) {
  140. __WFE();
  141. return mp_const_none;
  142. }
  143. MP_DEFINE_CONST_FUN_OBJ_0(machine_sleep_obj, machine_sleep);
  144. STATIC mp_obj_t machine_deepsleep(void) {
  145. __WFI();
  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. return MP_OBJ_NEW_SMALL_INT(reset_cause);
  151. }
  152. STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
  153. STATIC mp_obj_t machine_enable_irq(void) {
  154. #ifndef BLUETOOTH_SD
  155. __enable_irq();
  156. #else
  157. #endif
  158. return mp_const_none;
  159. }
  160. MP_DEFINE_CONST_FUN_OBJ_0(machine_enable_irq_obj, machine_enable_irq);
  161. // Resets the board in a manner similar to pushing the external RESET button.
  162. STATIC mp_obj_t machine_disable_irq(void) {
  163. #ifndef BLUETOOTH_SD
  164. __disable_irq();
  165. #else
  166. #endif
  167. return mp_const_none;
  168. }
  169. MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
  170. STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
  171. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
  172. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
  173. { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
  174. { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
  175. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
  176. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
  177. #if MICROPY_HW_ENABLE_RNG
  178. { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&random_module) },
  179. #endif
  180. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_sleep_obj) },
  181. { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
  182. { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
  183. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
  184. #if MICROPY_PY_MACHINE_UART
  185. { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_hard_uart_type) },
  186. #endif
  187. #if MICROPY_PY_MACHINE_HW_SPI
  188. { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) },
  189. #endif
  190. #if MICROPY_PY_MACHINE_I2C
  191. { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
  192. #endif
  193. #if MICROPY_PY_MACHINE_ADC
  194. { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
  195. #endif
  196. #if MICROPY_PY_MACHINE_RTCOUNTER
  197. { MP_ROM_QSTR(MP_QSTR_RTCounter), MP_ROM_PTR(&machine_rtcounter_type) },
  198. #endif
  199. #if MICROPY_PY_MACHINE_TIMER
  200. { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
  201. #endif
  202. #if MICROPY_PY_MACHINE_HW_PWM
  203. { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_hard_pwm_type) },
  204. #endif
  205. #if MICROPY_PY_MACHINE_TEMP
  206. { MP_ROM_QSTR(MP_QSTR_Temp), MP_ROM_PTR(&machine_temp_type) },
  207. #endif
  208. { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(PYB_RESET_HARD) },
  209. { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(PYB_RESET_WDT) },
  210. { MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(PYB_RESET_SOFT) },
  211. { MP_ROM_QSTR(MP_QSTR_LOCKUP_RESET), MP_ROM_INT(PYB_RESET_LOCKUP) },
  212. { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(PYB_RESET_POWER_ON) },
  213. { MP_ROM_QSTR(MP_QSTR_LPCOMP_RESET), MP_ROM_INT(PYB_RESET_LPCOMP) },
  214. { MP_ROM_QSTR(MP_QSTR_DEBUG_IF_RESET), MP_ROM_INT(PYB_RESET_DIF) },
  215. #if NRF52
  216. { MP_ROM_QSTR(MP_QSTR_NFC_RESET), MP_ROM_INT(PYB_RESET_NFC) },
  217. #endif
  218. };
  219. STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
  220. const mp_obj_module_t machine_module = {
  221. .base = { &mp_type_module },
  222. .globals = (mp_obj_dict_t*)&machine_module_globals,
  223. };