modpyb.c 8.8 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) 2013, 2014 Damien P. George
  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/mphal.h"
  30. #include "lib/utils/pyexec.h"
  31. #include "drivers/dht/dht.h"
  32. #include "stm32_it.h"
  33. #include "irq.h"
  34. #include "led.h"
  35. #include "timer.h"
  36. #include "extint.h"
  37. #include "usrsw.h"
  38. #include "rng.h"
  39. #include "rtc.h"
  40. #include "i2c.h"
  41. #include "spi.h"
  42. #include "uart.h"
  43. #include "can.h"
  44. #include "adc.h"
  45. #include "storage.h"
  46. #include "sdcard.h"
  47. #include "accel.h"
  48. #include "servo.h"
  49. #include "dac.h"
  50. #include "lcd.h"
  51. #include "usb.h"
  52. #include "portmodules.h"
  53. #include "modmachine.h"
  54. #include "extmod/vfs.h"
  55. #include "extmod/utime_mphal.h"
  56. STATIC mp_obj_t pyb_fault_debug(mp_obj_t value) {
  57. pyb_hard_fault_debug = mp_obj_is_true(value);
  58. return mp_const_none;
  59. }
  60. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_fault_debug_obj, pyb_fault_debug);
  61. #if MICROPY_PY_PYB_LEGACY
  62. // Returns the number of milliseconds which have elapsed since `start`.
  63. // This function takes care of counter wrap and always returns a positive number.
  64. STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
  65. uint32_t startMillis = mp_obj_get_int(start);
  66. uint32_t currMillis = mp_hal_ticks_ms();
  67. return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
  68. }
  69. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
  70. // Returns the number of microseconds which have elapsed since `start`.
  71. // This function takes care of counter wrap and always returns a positive number.
  72. STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
  73. uint32_t startMicros = mp_obj_get_int(start);
  74. uint32_t currMicros = mp_hal_ticks_us();
  75. return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
  76. }
  77. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
  78. #endif
  79. MP_DECLARE_CONST_FUN_OBJ_KW(pyb_main_obj); // defined in main.c
  80. // Get or set the UART object that the REPL is repeated on.
  81. // This is a legacy function, use of uos.dupterm is preferred.
  82. STATIC mp_obj_t pyb_repl_uart(size_t n_args, const mp_obj_t *args) {
  83. if (n_args == 0) {
  84. if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
  85. return mp_const_none;
  86. } else {
  87. return MP_OBJ_FROM_PTR(MP_STATE_PORT(pyb_stdio_uart));
  88. }
  89. } else {
  90. if (args[0] == mp_const_none) {
  91. if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
  92. uart_attach_to_repl(MP_STATE_PORT(pyb_stdio_uart), false);
  93. MP_STATE_PORT(pyb_stdio_uart) = NULL;
  94. }
  95. } else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
  96. MP_STATE_PORT(pyb_stdio_uart) = MP_OBJ_TO_PTR(args[0]);
  97. uart_attach_to_repl(MP_STATE_PORT(pyb_stdio_uart), true);
  98. } else {
  99. mp_raise_ValueError("need a UART object");
  100. }
  101. return mp_const_none;
  102. }
  103. }
  104. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
  105. STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
  106. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
  107. { MP_ROM_QSTR(MP_QSTR_fault_debug), MP_ROM_PTR(&pyb_fault_debug_obj) },
  108. #if MICROPY_PY_PYB_LEGACY
  109. { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) },
  110. { MP_ROM_QSTR(MP_QSTR_hard_reset), MP_ROM_PTR(&machine_reset_obj) },
  111. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
  112. { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
  113. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
  114. #endif
  115. { MP_ROM_QSTR(MP_QSTR_repl_info), MP_ROM_PTR(&pyb_set_repl_info_obj) },
  116. { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&pyb_wfi_obj) },
  117. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) },
  118. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) },
  119. #if IRQ_ENABLE_STATS
  120. { MP_ROM_QSTR(MP_QSTR_irq_stats), MP_ROM_PTR(&pyb_irq_stats_obj) },
  121. #endif
  122. #if MICROPY_PY_PYB_LEGACY
  123. { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_sleep_obj) },
  124. { MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&machine_deepsleep_obj) },
  125. #endif
  126. { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) },
  127. { MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&pyb_repl_uart_obj) },
  128. #if MICROPY_HW_ENABLE_USB
  129. { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
  130. { MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },
  131. { MP_ROM_QSTR(MP_QSTR_hid_keyboard), MP_ROM_PTR(&pyb_usb_hid_keyboard_obj) },
  132. { MP_ROM_QSTR(MP_QSTR_USB_VCP), MP_ROM_PTR(&pyb_usb_vcp_type) },
  133. { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&pyb_usb_hid_type) },
  134. #if MICROPY_PY_PYB_LEGACY
  135. // these 2 are deprecated; use USB_VCP.isconnected and USB_HID.send instead
  136. { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
  137. { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
  138. #endif
  139. #endif
  140. #if MICROPY_PY_PYB_LEGACY
  141. { MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
  142. { MP_ROM_QSTR(MP_QSTR_elapsed_millis), MP_ROM_PTR(&pyb_elapsed_millis_obj) },
  143. { MP_ROM_QSTR(MP_QSTR_micros), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
  144. { MP_ROM_QSTR(MP_QSTR_elapsed_micros), MP_ROM_PTR(&pyb_elapsed_micros_obj) },
  145. { MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
  146. { MP_ROM_QSTR(MP_QSTR_udelay), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
  147. { MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&mod_os_sync_obj) },
  148. { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
  149. #endif
  150. // This function is not intended to be public and may be moved elsewhere
  151. { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
  152. { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
  153. #if MICROPY_HW_ENABLE_RNG
  154. { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&pyb_rng_get_obj) },
  155. #endif
  156. #if MICROPY_HW_ENABLE_RTC
  157. { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
  158. #endif
  159. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
  160. { MP_ROM_QSTR(MP_QSTR_ExtInt), MP_ROM_PTR(&extint_type) },
  161. #if MICROPY_HW_ENABLE_SERVO
  162. { MP_ROM_QSTR(MP_QSTR_pwm), MP_ROM_PTR(&pyb_pwm_set_obj) },
  163. { MP_ROM_QSTR(MP_QSTR_servo), MP_ROM_PTR(&pyb_servo_set_obj) },
  164. { MP_ROM_QSTR(MP_QSTR_Servo), MP_ROM_PTR(&pyb_servo_type) },
  165. #endif
  166. #if MICROPY_HW_HAS_SWITCH
  167. { MP_ROM_QSTR(MP_QSTR_Switch), MP_ROM_PTR(&pyb_switch_type) },
  168. #endif
  169. #if MICROPY_HW_HAS_FLASH
  170. { MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&pyb_flash_type) },
  171. #endif
  172. #if MICROPY_HW_HAS_SDCARD
  173. #if MICROPY_PY_PYB_LEGACY
  174. { MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sdcard_obj) }, // now obsolete
  175. #endif
  176. { MP_ROM_QSTR(MP_QSTR_SDCard), MP_ROM_PTR(&pyb_sdcard_type) },
  177. #endif
  178. #if defined(MICROPY_HW_LED1)
  179. { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
  180. #endif
  181. #if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C
  182. { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
  183. #endif
  184. { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
  185. { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
  186. #if MICROPY_HW_ENABLE_CAN
  187. { MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&pyb_can_type) },
  188. #endif
  189. #if MICROPY_HW_ENABLE_ADC
  190. { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
  191. { MP_ROM_QSTR(MP_QSTR_ADCAll), MP_ROM_PTR(&pyb_adc_all_type) },
  192. #endif
  193. #if MICROPY_HW_ENABLE_DAC
  194. { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&pyb_dac_type) },
  195. #endif
  196. #if MICROPY_HW_HAS_MMA7660
  197. { MP_ROM_QSTR(MP_QSTR_Accel), MP_ROM_PTR(&pyb_accel_type) },
  198. #endif
  199. #if MICROPY_HW_HAS_LCD
  200. { MP_ROM_QSTR(MP_QSTR_LCD), MP_ROM_PTR(&pyb_lcd_type) },
  201. #endif
  202. };
  203. STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
  204. const mp_obj_module_t pyb_module = {
  205. .base = { &mp_type_module },
  206. .globals = (mp_obj_dict_t*)&pyb_module_globals,
  207. };