modpyb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/runtime.h"
  29. #include "py/gc.h"
  30. #include "py/builtin.h"
  31. #include "py/mphal.h"
  32. #include "lib/utils/pyexec.h"
  33. #include "modmachine.h"
  34. #include "extmod/vfs.h"
  35. #include "extmod/utime_mphal.h"
  36. /// \function elapsed_millis(start)
  37. /// Returns the number of milliseconds which have elapsed since `start`.
  38. ///
  39. /// This function takes care of counter wrap, and always returns a positive
  40. /// number. This means it can be used to measure periods upto about 12.4 days.
  41. ///
  42. /// Example:
  43. /// start = pyb.millis()
  44. /// while pyb.elapsed_millis(start) < 1000:
  45. /// # Perform some operation
  46. STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
  47. uint32_t startMillis = mp_obj_get_int(start);
  48. uint32_t currMillis = mp_hal_ticks_ms();
  49. return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
  50. }
  51. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
  52. /// \function elapsed_micros(start)
  53. /// Returns the number of microseconds which have elapsed since `start`.
  54. ///
  55. /// This function takes care of counter wrap, and always returns a positive
  56. /// number. This means it can be used to measure periods upto about 17.8 minutes.
  57. ///
  58. /// Example:
  59. /// start = pyb.micros()
  60. /// while pyb.elapsed_micros(start) < 1000:
  61. /// # Perform some operation
  62. STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
  63. uint32_t startMicros = mp_obj_get_int(start);
  64. uint32_t currMicros = mp_hal_ticks_us();
  65. return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
  66. }
  67. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
  68. MP_DECLARE_CONST_FUN_OBJ_KW(pyb_main_obj); // defined in main.c
  69. STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
  70. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
  71. { MP_ROM_QSTR(MP_QSTR_hard_reset), MP_ROM_PTR(&machine_reset_obj) },
  72. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
  73. { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
  74. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
  75. { MP_ROM_QSTR(MP_QSTR_repl_info), MP_ROM_PTR(&pyb_set_repl_info_obj) },
  76. { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&pyb_wfi_obj) },
  77. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) },
  78. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) },
  79. { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_sleep_obj) },
  80. { MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&machine_deepsleep_obj) },
  81. // { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) },
  82. // { MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&pyb_repl_uart_obj) },
  83. { MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
  84. { MP_ROM_QSTR(MP_QSTR_elapsed_millis), MP_ROM_PTR(&pyb_elapsed_millis_obj) },
  85. { MP_ROM_QSTR(MP_QSTR_micros), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
  86. { MP_ROM_QSTR(MP_QSTR_elapsed_micros), MP_ROM_PTR(&pyb_elapsed_micros_obj) },
  87. { MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
  88. { MP_ROM_QSTR(MP_QSTR_udelay), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
  89. // { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_os_mount_obj) },
  90. // { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
  91. //#if MICROPY_HW_ENABLE_RNG
  92. // { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&pyb_rng_get_obj) },
  93. //#endif
  94. //
  95. //#if MICROPY_HW_ENABLE_RTC
  96. // { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
  97. //#endif
  98. //
  99. #if MICROPY_PY_PIN
  100. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
  101. #endif
  102. // { MP_ROM_QSTR(MP_QSTR_ExtInt), MP_ROM_PTR(&extint_type) },
  103. //
  104. //#if MICROPY_HW_ENABLE_SERVO
  105. // { MP_ROM_QSTR(MP_QSTR_pwm), MP_ROM_PTR(&pyb_pwm_set_obj) },
  106. // { MP_ROM_QSTR(MP_QSTR_servo), MP_ROM_PTR(&pyb_servo_set_obj) },
  107. // { MP_ROM_QSTR(MP_QSTR_Servo), MP_ROM_PTR(&pyb_servo_type) },
  108. //#endif
  109. //
  110. //#if MICROPY_HW_HAS_SWITCH
  111. // { MP_ROM_QSTR(MP_QSTR_Switch), MP_ROM_PTR(&pyb_switch_type) },
  112. //#endif
  113. //
  114. //#if MICROPY_HW_HAS_FLASH
  115. // { MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&pyb_flash_type) },
  116. //#endif
  117. //
  118. //#if MICROPY_HW_HAS_SDCARD
  119. // { MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sdcard_obj) }, // now obsolete
  120. // { MP_ROM_QSTR(MP_QSTR_SDCard), MP_ROM_PTR(&pyb_sdcard_type) },
  121. //#endif
  122. //
  123. //#if defined(MICROPY_HW_LED1)
  124. // { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
  125. //#endif
  126. // { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
  127. // { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
  128. // { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
  129. //#if MICROPY_HW_ENABLE_CAN
  130. // { MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&pyb_can_type) },
  131. //#endif
  132. //
  133. // { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
  134. // { MP_ROM_QSTR(MP_QSTR_ADCAll), MP_ROM_PTR(&pyb_adc_all_type) },
  135. //
  136. //#if MICROPY_HW_ENABLE_DAC
  137. // { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&pyb_dac_type) },
  138. //#endif
  139. //
  140. //#if MICROPY_HW_HAS_MMA7660
  141. // { MP_ROM_QSTR(MP_QSTR_Accel), MP_ROM_PTR(&pyb_accel_type) },
  142. //#endif
  143. //
  144. //#if MICROPY_HW_HAS_LCD
  145. // { MP_ROM_QSTR(MP_QSTR_LCD), MP_ROM_PTR(&pyb_lcd_type) },
  146. //#endif
  147. };
  148. STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
  149. const mp_obj_module_t pyb_module = {
  150. .base = { &mp_type_module },
  151. .globals = (mp_obj_dict_t*)&pyb_module_globals,
  152. };