modpyb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <mk20dx128.h>
  29. #include "Arduino.h"
  30. #include "py/obj.h"
  31. #include "py/gc.h"
  32. #include "py/mphal.h"
  33. #include "lib/utils/pyexec.h"
  34. #include "gccollect.h"
  35. #include "irq.h"
  36. #include "systick.h"
  37. #include "led.h"
  38. #include "pin.h"
  39. #include "timer.h"
  40. #include "extint.h"
  41. #include "usrsw.h"
  42. #include "rng.h"
  43. //#include "rtc.h"
  44. //#include "i2c.h"
  45. //#include "spi.h"
  46. #include "uart.h"
  47. #include "adc.h"
  48. #include "storage.h"
  49. #include "sdcard.h"
  50. #include "accel.h"
  51. #include "servo.h"
  52. #include "dac.h"
  53. #include "usb.h"
  54. #include "portmodules.h"
  55. /// \module pyb - functions related to the pyboard
  56. ///
  57. /// The `pyb` module contains specific functions related to the pyboard.
  58. /// \function bootloader()
  59. /// Activate the bootloader without BOOT* pins.
  60. STATIC mp_obj_t pyb_bootloader(void) {
  61. printf("bootloader command not current supported\n");
  62. return mp_const_none;
  63. }
  64. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_bootloader_obj, pyb_bootloader);
  65. /// \function info([dump_alloc_table])
  66. /// Print out lots of information about the board.
  67. STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
  68. // get and print unique id; 96 bits
  69. {
  70. byte *id = (byte*)0x40048058;
  71. printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]);
  72. }
  73. // get and print clock speeds
  74. printf("CPU=%u\nBUS=%u\nMEM=%u\n", F_CPU, F_BUS, F_MEM);
  75. // to print info about memory
  76. {
  77. printf("_etext=%p\n", &_etext);
  78. printf("_sidata=%p\n", &_sidata);
  79. printf("_sdata=%p\n", &_sdata);
  80. printf("_edata=%p\n", &_edata);
  81. printf("_sbss=%p\n", &_sbss);
  82. printf("_ebss=%p\n", &_ebss);
  83. printf("_estack=%p\n", &_estack);
  84. printf("_ram_start=%p\n", &_ram_start);
  85. printf("_heap_start=%p\n", &_heap_start);
  86. printf("_heap_end=%p\n", &_heap_end);
  87. printf("_ram_end=%p\n", &_ram_end);
  88. }
  89. // qstr info
  90. {
  91. uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
  92. qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
  93. printf("qstr:\n n_pool=%u\n n_qstr=%u\n n_str_data_bytes=%u\n n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
  94. }
  95. // GC info
  96. {
  97. gc_info_t info;
  98. gc_info(&info);
  99. printf("GC:\n");
  100. printf(" " UINT_FMT " total\n", info.total);
  101. printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
  102. printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
  103. }
  104. if (n_args == 1) {
  105. // arg given means dump gc allocation table
  106. gc_dump_alloc_table();
  107. }
  108. return mp_const_none;
  109. }
  110. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
  111. /// \function unique_id()
  112. /// Returns a string of 12 bytes (96 bits), which is the unique ID for the MCU.
  113. STATIC mp_obj_t pyb_unique_id(void) {
  114. byte *id = (byte*)0x40048058;
  115. return mp_obj_new_bytes(id, 12);
  116. }
  117. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
  118. /// \function freq()
  119. /// Return a tuple of clock frequencies: (SYSCLK, HCLK, PCLK1, PCLK2).
  120. // TODO should also be able to set frequency via this function
  121. STATIC mp_obj_t pyb_freq(void) {
  122. mp_obj_t tuple[3] = {
  123. mp_obj_new_int(F_CPU),
  124. mp_obj_new_int(F_BUS),
  125. mp_obj_new_int(F_MEM),
  126. };
  127. return mp_obj_new_tuple(3, tuple);
  128. }
  129. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_freq_obj, pyb_freq);
  130. /// \function sync()
  131. /// Sync all file systems.
  132. STATIC mp_obj_t pyb_sync(void) {
  133. printf("sync not currently implemented\n");
  134. return mp_const_none;
  135. }
  136. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
  137. /// \function millis()
  138. /// Returns the number of milliseconds since the board was last reset.
  139. ///
  140. /// The result is always a MicroPython smallint (31-bit signed number), so
  141. /// after 2^30 milliseconds (about 12.4 days) this will start to return
  142. /// negative numbers.
  143. STATIC mp_obj_t pyb_millis(void) {
  144. // We want to "cast" the 32 bit unsigned into a small-int. This means
  145. // copying the MSB down 1 bit (extending the sign down), which is
  146. // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
  147. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms());
  148. }
  149. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
  150. /// \function elapsed_millis(start)
  151. /// Returns the number of milliseconds which have elapsed since `start`.
  152. ///
  153. /// This function takes care of counter wrap, and always returns a positive
  154. /// number. This means it can be used to measure periods upto about 12.4 days.
  155. ///
  156. /// Example:
  157. /// start = pyb.millis()
  158. /// while pyb.elapsed_millis(start) < 1000:
  159. /// # Perform some operation
  160. STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
  161. uint32_t startMillis = mp_obj_get_int(start);
  162. uint32_t currMillis = mp_hal_ticks_ms();
  163. return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
  164. }
  165. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
  166. /// \function micros()
  167. /// Returns the number of microseconds since the board was last reset.
  168. ///
  169. /// The result is always a MicroPython smallint (31-bit signed number), so
  170. /// after 2^30 microseconds (about 17.8 minutes) this will start to return
  171. /// negative numbers.
  172. STATIC mp_obj_t pyb_micros(void) {
  173. // We want to "cast" the 32 bit unsigned into a small-int. This means
  174. // copying the MSB down 1 bit (extending the sign down), which is
  175. // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
  176. return MP_OBJ_NEW_SMALL_INT(micros());
  177. }
  178. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
  179. /// \function elapsed_micros(start)
  180. /// Returns the number of microseconds which have elapsed since `start`.
  181. ///
  182. /// This function takes care of counter wrap, and always returns a positive
  183. /// number. This means it can be used to measure periods upto about 17.8 minutes.
  184. ///
  185. /// Example:
  186. /// start = pyb.micros()
  187. /// while pyb.elapsed_micros(start) < 1000:
  188. /// # Perform some operation
  189. STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
  190. uint32_t startMicros = mp_obj_get_int(start);
  191. uint32_t currMicros = micros();
  192. return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
  193. }
  194. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
  195. /// \function delay(ms)
  196. /// Delay for the given number of milliseconds.
  197. STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
  198. mp_int_t ms = mp_obj_get_int(ms_in);
  199. if (ms >= 0) {
  200. mp_hal_delay_ms(ms);
  201. }
  202. return mp_const_none;
  203. }
  204. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
  205. /// \function udelay(us)
  206. /// Delay for the given number of microseconds.
  207. STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
  208. mp_int_t usec = mp_obj_get_int(usec_in);
  209. delayMicroseconds(usec);
  210. return mp_const_none;
  211. }
  212. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
  213. STATIC mp_obj_t pyb_stop(void) {
  214. printf("stop not currently implemented\n");
  215. return mp_const_none;
  216. }
  217. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_stop_obj, pyb_stop);
  218. STATIC mp_obj_t pyb_standby(void) {
  219. printf("standby not currently implemented\n");
  220. return mp_const_none;
  221. }
  222. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
  223. /// \function have_cdc()
  224. /// Return True if USB is connected as a serial device, False otherwise.
  225. STATIC mp_obj_t pyb_have_cdc(void ) {
  226. return mp_obj_new_bool(usb_vcp_is_connected());
  227. }
  228. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc);
  229. /// \function hid((buttons, x, y, z))
  230. /// Takes a 4-tuple (or list) and sends it to the USB host (the PC) to
  231. /// signal a HID mouse-motion event.
  232. STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
  233. #if 1
  234. printf("hid_send_report not currently implemented\n");
  235. #else
  236. mp_obj_t *items;
  237. mp_obj_get_array_fixed_n(arg, 4, &items);
  238. uint8_t data[4];
  239. data[0] = mp_obj_get_int(items[0]);
  240. data[1] = mp_obj_get_int(items[1]);
  241. data[2] = mp_obj_get_int(items[2]);
  242. data[3] = mp_obj_get_int(items[3]);
  243. usb_hid_send_report(data);
  244. #endif
  245. return mp_const_none;
  246. }
  247. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
  248. MP_DECLARE_CONST_FUN_OBJ_1(pyb_source_dir_obj); // defined in main.c
  249. MP_DECLARE_CONST_FUN_OBJ_1(pyb_main_obj); // defined in main.c
  250. MP_DECLARE_CONST_FUN_OBJ_1(pyb_usb_mode_obj); // defined in main.c
  251. STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
  252. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
  253. { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&pyb_bootloader_obj) },
  254. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&pyb_info_obj) },
  255. { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&pyb_unique_id_obj) },
  256. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&pyb_freq_obj) },
  257. { MP_ROM_QSTR(MP_QSTR_repl_info), MP_ROM_PTR(&pyb_set_repl_info_obj) },
  258. { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&pyb_wfi_obj) },
  259. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) },
  260. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) },
  261. { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&pyb_stop_obj) },
  262. { MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&pyb_standby_obj) },
  263. { MP_ROM_QSTR(MP_QSTR_source_dir), MP_ROM_PTR(&pyb_source_dir_obj) },
  264. { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) },
  265. { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
  266. { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
  267. { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
  268. { MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&pyb_millis_obj) },
  269. { MP_ROM_QSTR(MP_QSTR_elapsed_millis), MP_ROM_PTR(&pyb_elapsed_millis_obj) },
  270. { MP_ROM_QSTR(MP_QSTR_micros), MP_ROM_PTR(&pyb_micros_obj) },
  271. { MP_ROM_QSTR(MP_QSTR_elapsed_micros), MP_ROM_PTR(&pyb_elapsed_micros_obj) },
  272. { MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&pyb_delay_obj) },
  273. { MP_ROM_QSTR(MP_QSTR_udelay), MP_ROM_PTR(&pyb_udelay_obj) },
  274. { MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&pyb_sync_obj) },
  275. { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
  276. //#if MICROPY_HW_ENABLE_RNG
  277. // { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&pyb_rng_get_obj) },
  278. //#endif
  279. //#if MICROPY_HW_ENABLE_RTC
  280. // { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
  281. //#endif
  282. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
  283. // { MP_ROM_QSTR(MP_QSTR_ExtInt), MP_ROM_PTR(&extint_type) },
  284. #if MICROPY_HW_ENABLE_SERVO
  285. { MP_ROM_QSTR(MP_QSTR_pwm), MP_ROM_PTR(&pyb_pwm_set_obj) },
  286. { MP_ROM_QSTR(MP_QSTR_servo), MP_ROM_PTR(&pyb_servo_set_obj) },
  287. { MP_ROM_QSTR(MP_QSTR_Servo), MP_ROM_PTR(&pyb_servo_type) },
  288. #endif
  289. #if MICROPY_HW_HAS_SWITCH
  290. { MP_ROM_QSTR(MP_QSTR_Switch), MP_ROM_PTR(&pyb_switch_type) },
  291. #endif
  292. //#if MICROPY_HW_HAS_SDCARD
  293. // { MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sdcard_obj) },
  294. //#endif
  295. { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
  296. // { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
  297. // { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
  298. { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
  299. // { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
  300. // { MP_ROM_QSTR(MP_QSTR_ADCAll), MP_ROM_PTR(&pyb_adc_all_type) },
  301. //#if MICROPY_HW_ENABLE_DAC
  302. // { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&pyb_dac_type) },
  303. //#endif
  304. //#if MICROPY_HW_HAS_MMA7660
  305. // { MP_ROM_QSTR(MP_QSTR_Accel), MP_ROM_PTR(&pyb_accel_type) },
  306. //#endif
  307. };
  308. STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
  309. const mp_obj_module_t pyb_module = {
  310. .base = { &mp_type_module },
  311. .globals = (mp_obj_dict_t*)&pyb_module_globals,
  312. };