main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "py/lexer.h"
  6. #include "py/runtime.h"
  7. #include "py/stackctrl.h"
  8. #include "py/gc.h"
  9. #include "py/mphal.h"
  10. #include "gccollect.h"
  11. #include "lib/utils/pyexec.h"
  12. #include "lib/mp-readline/readline.h"
  13. #include "lexermemzip.h"
  14. #include "Arduino.h"
  15. #include "servo.h"
  16. #include "led.h"
  17. #include "uart.h"
  18. #include "pin.h"
  19. extern uint32_t _heap_start;
  20. void flash_error(int n) {
  21. for (int i = 0; i < n; i++) {
  22. led_state(PYB_LED_BUILTIN, 1);
  23. delay(250);
  24. led_state(PYB_LED_BUILTIN, 0);
  25. delay(250);
  26. }
  27. }
  28. void NORETURN __fatal_error(const char *msg) {
  29. for (volatile uint delay = 0; delay < 10000000; delay++) {
  30. }
  31. led_state(1, 1);
  32. led_state(2, 1);
  33. led_state(3, 1);
  34. led_state(4, 1);
  35. mp_hal_stdout_tx_strn("\nFATAL ERROR:\n", 14);
  36. mp_hal_stdout_tx_strn(msg, strlen(msg));
  37. for (uint i = 0;;) {
  38. led_toggle(((i++) & 3) + 1);
  39. for (volatile uint delay = 0; delay < 10000000; delay++) {
  40. }
  41. if (i >= 16) {
  42. // to conserve power
  43. __WFI();
  44. }
  45. }
  46. }
  47. void nlr_jump_fail(void *val) {
  48. printf("FATAL: uncaught exception %p\n", val);
  49. __fatal_error("");
  50. }
  51. void __assert_func(const char *file, int line, const char *func, const char *expr) {
  52. printf("Assertion failed: %s, file %s, line %d\n", expr, file, line);
  53. __fatal_error("");
  54. }
  55. mp_obj_t pyb_analog_read(mp_obj_t pin_obj) {
  56. uint pin = mp_obj_get_int(pin_obj);
  57. int val = analogRead(pin);
  58. return MP_OBJ_NEW_SMALL_INT(val);
  59. }
  60. mp_obj_t pyb_analog_write(mp_obj_t pin_obj, mp_obj_t val_obj) {
  61. uint pin = mp_obj_get_int(pin_obj);
  62. int val = mp_obj_get_int(val_obj);
  63. analogWrite(pin, val);
  64. return mp_const_none;
  65. }
  66. mp_obj_t pyb_analog_write_resolution(mp_obj_t res_obj) {
  67. int res = mp_obj_get_int(res_obj);
  68. analogWriteResolution(res);
  69. return mp_const_none;
  70. }
  71. mp_obj_t pyb_analog_write_frequency(mp_obj_t pin_obj, mp_obj_t freq_obj) {
  72. uint pin = mp_obj_get_int(pin_obj);
  73. int freq = mp_obj_get_int(freq_obj);
  74. analogWriteFrequency(pin, freq);
  75. return mp_const_none;
  76. }
  77. #if 0
  78. // get lots of info about the board
  79. static mp_obj_t pyb_info(void) {
  80. // get and print unique id; 96 bits
  81. {
  82. byte *id = (byte*)0x40048058;
  83. 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]);
  84. }
  85. // get and print clock speeds
  86. printf("CPU=%u\nBUS=%u\nMEM=%u\n", F_CPU, F_BUS, F_MEM);
  87. // to print info about memory
  88. {
  89. printf("_sdata=%p\n", &_sdata);
  90. printf("_edata=%p\n", &_edata);
  91. printf("_sbss=%p\n", &_sbss);
  92. printf("_ebss=%p\n", &_ebss);
  93. printf("_estack=%p\n", &_estack);
  94. printf("_etext=%p\n", &_etext);
  95. printf("_heap_start=%p\n", &_heap_start);
  96. }
  97. // GC info
  98. {
  99. gc_info_t info;
  100. gc_info(&info);
  101. printf("GC:\n");
  102. printf(" %u total\n", info.total);
  103. printf(" %u used %u free\n", info.used, info.free);
  104. printf(" 1=%u 2=%u m=%u\n", info.num_1block, info.num_2block, info.max_block);
  105. }
  106. #if 0
  107. // free space on flash
  108. {
  109. DWORD nclst;
  110. FATFS *fatfs;
  111. f_getfree("0:", &nclst, &fatfs);
  112. printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512));
  113. }
  114. #endif
  115. return mp_const_none;
  116. }
  117. #endif
  118. #define RAM_START (0x1FFF8000) // fixed for chip
  119. #define HEAP_END (0x20006000) // tunable
  120. #define RAM_END (0x20008000) // fixed for chip
  121. #if 0
  122. void gc_helper_get_regs_and_clean_stack(mp_uint_t *regs, mp_uint_t heap_end);
  123. mp_obj_t pyb_gc(void) {
  124. gc_collect();
  125. return mp_const_none;
  126. }
  127. mp_obj_t pyb_gpio(int n_args, mp_obj_t *args) {
  128. //assert(1 <= n_args && n_args <= 2);
  129. uint pin = mp_obj_get_int(args[0]);
  130. if (pin > CORE_NUM_DIGITAL) {
  131. goto pin_error;
  132. }
  133. if (n_args == 1) {
  134. // get pin
  135. pinMode(pin, INPUT);
  136. return MP_OBJ_NEW_SMALL_INT(digitalRead(pin));
  137. }
  138. // set pin
  139. pinMode(pin, OUTPUT);
  140. digitalWrite(pin, mp_obj_is_true(args[1]));
  141. return mp_const_none;
  142. pin_error:
  143. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %d does not exist", pin));
  144. }
  145. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);
  146. #if 0
  147. mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
  148. mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
  149. uint8_t data[4];
  150. data[0] = mp_obj_get_int(items[0]);
  151. data[1] = mp_obj_get_int(items[1]);
  152. data[2] = mp_obj_get_int(items[2]);
  153. data[3] = mp_obj_get_int(items[3]);
  154. usb_hid_send_report(data);
  155. return mp_const_none;
  156. }
  157. #endif
  158. #endif // 0
  159. STATIC mp_obj_t pyb_config_source_dir = MP_OBJ_NULL;
  160. STATIC mp_obj_t pyb_config_main = MP_OBJ_NULL;
  161. STATIC mp_obj_t pyb_config_usb_mode = MP_OBJ_NULL;
  162. mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
  163. if (MP_OBJ_IS_STR(source_dir)) {
  164. pyb_config_source_dir = source_dir;
  165. }
  166. return mp_const_none;
  167. }
  168. MP_DEFINE_CONST_FUN_OBJ_1(pyb_source_dir_obj, pyb_source_dir);
  169. mp_obj_t pyb_main(mp_obj_t main) {
  170. if (MP_OBJ_IS_STR(main)) {
  171. pyb_config_main = main;
  172. }
  173. return mp_const_none;
  174. }
  175. MP_DEFINE_CONST_FUN_OBJ_1(pyb_main_obj, pyb_main);
  176. STATIC mp_obj_t pyb_usb_mode(mp_obj_t usb_mode) {
  177. if (MP_OBJ_IS_STR(usb_mode)) {
  178. pyb_config_usb_mode = usb_mode;
  179. }
  180. return mp_const_none;
  181. }
  182. MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_mode_obj, pyb_usb_mode);
  183. #if 0
  184. mp_obj_t pyb_delay(mp_obj_t count) {
  185. delay(mp_obj_get_int(count));
  186. return mp_const_none;
  187. }
  188. mp_obj_t pyb_led(mp_obj_t state) {
  189. led_state(PYB_LED_BUILTIN, mp_obj_is_true(state));
  190. return state;
  191. }
  192. #endif // 0
  193. #if 0
  194. char *strdup(const char *str) {
  195. uint32_t len = strlen(str);
  196. char *s2 = m_new(char, len + 1);
  197. memcpy(s2, str, len);
  198. s2[len] = 0;
  199. return s2;
  200. }
  201. #endif
  202. int main(void) {
  203. // TODO: Put this in a more common initialization function.
  204. // Turn on STKALIGN which keeps the stack 8-byte aligned for interrupts
  205. // (per EABI)
  206. #define SCB_CCR_STKALIGN (1 << 9)
  207. SCB_CCR |= SCB_CCR_STKALIGN;
  208. mp_stack_ctrl_init();
  209. mp_stack_set_limit(10240);
  210. pinMode(LED_BUILTIN, OUTPUT);
  211. led_init();
  212. // int first_soft_reset = true;
  213. soft_reset:
  214. led_state(PYB_LED_BUILTIN, 1);
  215. // GC init
  216. gc_init(&_heap_start, (void*)HEAP_END);
  217. // MicroPython init
  218. mp_init();
  219. mp_obj_list_init(mp_sys_path, 0);
  220. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  221. mp_obj_list_init(mp_sys_argv, 0);
  222. readline_init0();
  223. pin_init0();
  224. #if 0
  225. // add some functions to the python namespace
  226. {
  227. mp_store_name(MP_QSTR_help, mp_make_function_n(0, pyb_help));
  228. mp_obj_t m = mp_obj_new_module(MP_QSTR_pyb);
  229. mp_store_attr(m, MP_QSTR_info, mp_make_function_n(0, pyb_info));
  230. mp_store_attr(m, MP_QSTR_source_dir, mp_make_function_n(1, pyb_source_dir));
  231. mp_store_attr(m, MP_QSTR_main, mp_make_function_n(1, pyb_main));
  232. mp_store_attr(m, MP_QSTR_gc, mp_make_function_n(0, pyb_gc));
  233. mp_store_attr(m, MP_QSTR_delay, mp_make_function_n(1, pyb_delay));
  234. mp_store_attr(m, MP_QSTR_led, mp_make_function_n(1, pyb_led));
  235. mp_store_attr(m, MP_QSTR_LED, (mp_obj_t)&pyb_led_type);
  236. mp_store_attr(m, MP_QSTR_analogRead, mp_make_function_n(1, pyb_analog_read));
  237. mp_store_attr(m, MP_QSTR_analogWrite, mp_make_function_n(2, pyb_analog_write));
  238. mp_store_attr(m, MP_QSTR_analogWriteResolution, mp_make_function_n(1, pyb_analog_write_resolution));
  239. mp_store_attr(m, MP_QSTR_analogWriteFrequency, mp_make_function_n(2, pyb_analog_write_frequency));
  240. mp_store_attr(m, MP_QSTR_gpio, (mp_obj_t)&pyb_gpio_obj);
  241. mp_store_attr(m, MP_QSTR_Servo, mp_make_function_n(0, pyb_Servo));
  242. mp_store_name(MP_QSTR_pyb, m);
  243. }
  244. #endif
  245. #if MICROPY_MODULE_FROZEN
  246. pyexec_frozen_module("boot.py");
  247. #else
  248. if (!pyexec_file("/boot.py")) {
  249. flash_error(4);
  250. }
  251. #endif
  252. // Turn bootup LED off
  253. led_state(PYB_LED_BUILTIN, 0);
  254. // run main script
  255. #if MICROPY_MODULE_FROZEN
  256. pyexec_frozen_module("main.py");
  257. #else
  258. {
  259. vstr_t *vstr = vstr_new(16);
  260. vstr_add_str(vstr, "/");
  261. if (pyb_config_main == MP_OBJ_NULL) {
  262. vstr_add_str(vstr, "main.py");
  263. } else {
  264. vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
  265. }
  266. if (!pyexec_file(vstr_null_terminated_str(vstr))) {
  267. flash_error(3);
  268. }
  269. vstr_free(vstr);
  270. }
  271. #endif
  272. // enter REPL
  273. // REPL mode can change, or it can request a soft reset
  274. for (;;) {
  275. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  276. if (pyexec_raw_repl() != 0) {
  277. break;
  278. }
  279. } else {
  280. if (pyexec_friendly_repl() != 0) {
  281. break;
  282. }
  283. }
  284. }
  285. printf("PYB: soft reboot\n");
  286. // first_soft_reset = false;
  287. goto soft_reset;
  288. }
  289. // stub out __libc_init_array. It's called by mk20dx128.c and is used to call
  290. // global C++ constructors. Since this is a C-only projects, we don't need to
  291. // call constructors.
  292. void __libc_init_array(void) {
  293. }
  294. // ultoa is used by usb_init_serialnumber. Normally ultoa would be provided
  295. // by nonstd.c from the teensy core, but it conflicts with some of the
  296. // MicroPython functions in string0.c, so we provide ultoa here.
  297. char * ultoa(unsigned long val, char *buf, int radix)
  298. {
  299. unsigned digit;
  300. int i=0, j;
  301. char t;
  302. while (1) {
  303. digit = val % radix;
  304. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  305. val /= radix;
  306. if (val == 0) break;
  307. i++;
  308. }
  309. buf[i + 1] = 0;
  310. for (j=0; j < i; j++, i--) {
  311. t = buf[j];
  312. buf[j] = buf[i];
  313. buf[i] = t;
  314. }
  315. return buf;
  316. }