main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2018 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 <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/stackctrl.h"
  30. #include "py/gc.h"
  31. #include "py/mphal.h"
  32. #include "lib/mp-readline/readline.h"
  33. #include "lib/utils/pyexec.h"
  34. #include "lib/oofatfs/ff.h"
  35. #include "lwip/init.h"
  36. #include "extmod/vfs.h"
  37. #include "extmod/vfs_fat.h"
  38. #include "systick.h"
  39. #include "pendsv.h"
  40. #include "pybthread.h"
  41. #include "gccollect.h"
  42. #include "modmachine.h"
  43. #include "i2c.h"
  44. #include "spi.h"
  45. #include "uart.h"
  46. #include "timer.h"
  47. #include "led.h"
  48. #include "pin.h"
  49. #include "extint.h"
  50. #include "usrsw.h"
  51. #include "usb.h"
  52. #include "rtc.h"
  53. #include "storage.h"
  54. #include "sdcard.h"
  55. #include "rng.h"
  56. #include "accel.h"
  57. #include "servo.h"
  58. #include "dac.h"
  59. #include "can.h"
  60. #include "modnetwork.h"
  61. void SystemClock_Config(void);
  62. pyb_thread_t pyb_thread_main;
  63. fs_user_mount_t fs_user_mount_flash;
  64. void flash_error(int n) {
  65. for (int i = 0; i < n; i++) {
  66. led_state(PYB_LED_RED, 1);
  67. led_state(PYB_LED_GREEN, 0);
  68. mp_hal_delay_ms(250);
  69. led_state(PYB_LED_RED, 0);
  70. led_state(PYB_LED_GREEN, 1);
  71. mp_hal_delay_ms(250);
  72. }
  73. led_state(PYB_LED_GREEN, 0);
  74. }
  75. void NORETURN __fatal_error(const char *msg) {
  76. for (volatile uint delay = 0; delay < 10000000; delay++) {
  77. }
  78. led_state(1, 1);
  79. led_state(2, 1);
  80. led_state(3, 1);
  81. led_state(4, 1);
  82. mp_hal_stdout_tx_strn("\nFATAL ERROR:\n", 14);
  83. mp_hal_stdout_tx_strn(msg, strlen(msg));
  84. for (uint i = 0;;) {
  85. led_toggle(((i++) & 3) + 1);
  86. for (volatile uint delay = 0; delay < 10000000; delay++) {
  87. }
  88. if (i >= 16) {
  89. // to conserve power
  90. __WFI();
  91. }
  92. }
  93. }
  94. void nlr_jump_fail(void *val) {
  95. printf("FATAL: uncaught exception %p\n", val);
  96. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(val));
  97. __fatal_error("");
  98. }
  99. #ifndef NDEBUG
  100. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  101. (void)func;
  102. printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  103. __fatal_error("");
  104. }
  105. #endif
  106. STATIC mp_obj_t pyb_main(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  107. static const mp_arg_t allowed_args[] = {
  108. { MP_QSTR_opt, MP_ARG_INT, {.u_int = 0} }
  109. };
  110. if (MP_OBJ_IS_STR(pos_args[0])) {
  111. MP_STATE_PORT(pyb_config_main) = pos_args[0];
  112. // parse args
  113. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  114. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  115. MP_STATE_VM(mp_optimise_value) = args[0].u_int;
  116. }
  117. return mp_const_none;
  118. }
  119. MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main);
  120. #if MICROPY_HW_ENABLE_STORAGE
  121. static const char fresh_boot_py[] =
  122. "# boot.py -- run on boot-up\r\n"
  123. "# can run arbitrary Python, but best to keep it minimal\r\n"
  124. "\r\n"
  125. "import machine\r\n"
  126. "import pyb\r\n"
  127. "#pyb.main('main.py') # main script to run after this one\r\n"
  128. #if MICROPY_HW_ENABLE_USB
  129. "#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n"
  130. "#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n"
  131. #endif
  132. ;
  133. static const char fresh_main_py[] =
  134. "# main.py -- put your code here!\r\n"
  135. ;
  136. static const char fresh_pybcdc_inf[] =
  137. #include "genhdr/pybcdc_inf.h"
  138. ;
  139. static const char fresh_readme_txt[] =
  140. "This is a MicroPython board\r\n"
  141. "\r\n"
  142. "You can get started right away by writing your Python code in 'main.py'.\r\n"
  143. "\r\n"
  144. "For a serial prompt:\r\n"
  145. " - Windows: you need to go to 'Device manager', right click on the unknown device,\r\n"
  146. " then update the driver software, using the 'pybcdc.inf' file found on this drive.\r\n"
  147. " Then use a terminal program like Hyperterminal or putty.\r\n"
  148. " - Mac OS X: use the command: screen /dev/tty.usbmodem*\r\n"
  149. " - Linux: use the command: screen /dev/ttyACM0\r\n"
  150. "\r\n"
  151. "Please visit http://micropython.org/help/ for further help.\r\n"
  152. ;
  153. // avoid inlining to avoid stack usage within main()
  154. MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
  155. // init the vfs object
  156. fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
  157. vfs_fat->flags = 0;
  158. pyb_flash_init_vfs(vfs_fat);
  159. // try to mount the flash
  160. FRESULT res = f_mount(&vfs_fat->fatfs);
  161. if (reset_mode == 3 || res == FR_NO_FILESYSTEM) {
  162. // no filesystem, or asked to reset it, so create a fresh one
  163. // LED on to indicate creation of LFS
  164. led_state(PYB_LED_GREEN, 1);
  165. uint32_t start_tick = HAL_GetTick();
  166. uint8_t working_buf[_MAX_SS];
  167. res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));
  168. if (res == FR_OK) {
  169. // success creating fresh LFS
  170. } else {
  171. printf("PYB: can't create flash filesystem\n");
  172. return false;
  173. }
  174. // set label
  175. f_setlabel(&vfs_fat->fatfs, MICROPY_HW_FLASH_FS_LABEL);
  176. // create empty main.py
  177. FIL fp;
  178. f_open(&vfs_fat->fatfs, &fp, "/main.py", FA_WRITE | FA_CREATE_ALWAYS);
  179. UINT n;
  180. f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n);
  181. // TODO check we could write n bytes
  182. f_close(&fp);
  183. // create .inf driver file
  184. f_open(&vfs_fat->fatfs, &fp, "/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS);
  185. f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n);
  186. f_close(&fp);
  187. // create readme file
  188. f_open(&vfs_fat->fatfs, &fp, "/README.txt", FA_WRITE | FA_CREATE_ALWAYS);
  189. f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n);
  190. f_close(&fp);
  191. // keep LED on for at least 200ms
  192. sys_tick_wait_at_least(start_tick, 200);
  193. led_state(PYB_LED_GREEN, 0);
  194. } else if (res == FR_OK) {
  195. // mount sucessful
  196. } else {
  197. fail:
  198. printf("PYB: can't mount flash\n");
  199. return false;
  200. }
  201. // mount the flash device (there should be no other devices mounted at this point)
  202. // we allocate this structure on the heap because vfs->next is a root pointer
  203. mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t);
  204. if (vfs == NULL) {
  205. goto fail;
  206. }
  207. vfs->str = "/flash";
  208. vfs->len = 6;
  209. vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
  210. vfs->next = NULL;
  211. MP_STATE_VM(vfs_mount_table) = vfs;
  212. // The current directory is used as the boot up directory.
  213. // It is set to the internal flash filesystem by default.
  214. MP_STATE_PORT(vfs_cur) = vfs;
  215. // Make sure we have a /flash/boot.py. Create it if needed.
  216. FILINFO fno;
  217. res = f_stat(&vfs_fat->fatfs, "/boot.py", &fno);
  218. if (res != FR_OK) {
  219. // doesn't exist, create fresh file
  220. // LED on to indicate creation of boot.py
  221. led_state(PYB_LED_GREEN, 1);
  222. uint32_t start_tick = HAL_GetTick();
  223. FIL fp;
  224. f_open(&vfs_fat->fatfs, &fp, "/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
  225. UINT n;
  226. f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
  227. // TODO check we could write n bytes
  228. f_close(&fp);
  229. // keep LED on for at least 200ms
  230. sys_tick_wait_at_least(start_tick, 200);
  231. led_state(PYB_LED_GREEN, 0);
  232. }
  233. return true;
  234. }
  235. #endif
  236. #if MICROPY_HW_HAS_SDCARD
  237. STATIC bool init_sdcard_fs(void) {
  238. bool first_part = true;
  239. for (int part_num = 1; part_num <= 4; ++part_num) {
  240. // create vfs object
  241. fs_user_mount_t *vfs_fat = m_new_obj_maybe(fs_user_mount_t);
  242. mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t);
  243. if (vfs == NULL || vfs_fat == NULL) {
  244. break;
  245. }
  246. vfs_fat->flags = FSUSER_FREE_OBJ;
  247. sdcard_init_vfs(vfs_fat, part_num);
  248. // try to mount the partition
  249. FRESULT res = f_mount(&vfs_fat->fatfs);
  250. if (res != FR_OK) {
  251. // couldn't mount
  252. m_del_obj(fs_user_mount_t, vfs_fat);
  253. m_del_obj(mp_vfs_mount_t, vfs);
  254. } else {
  255. // mounted via FatFs, now mount the SD partition in the VFS
  256. if (first_part) {
  257. // the first available partition is traditionally called "sd" for simplicity
  258. vfs->str = "/sd";
  259. vfs->len = 3;
  260. } else {
  261. // subsequent partitions are numbered by their index in the partition table
  262. if (part_num == 2) {
  263. vfs->str = "/sd2";
  264. } else if (part_num == 2) {
  265. vfs->str = "/sd3";
  266. } else {
  267. vfs->str = "/sd4";
  268. }
  269. vfs->len = 4;
  270. }
  271. vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
  272. vfs->next = NULL;
  273. for (mp_vfs_mount_t **m = &MP_STATE_VM(vfs_mount_table);; m = &(*m)->next) {
  274. if (*m == NULL) {
  275. *m = vfs;
  276. break;
  277. }
  278. }
  279. #if MICROPY_HW_ENABLE_USB
  280. if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
  281. // if no USB MSC medium is selected then use the SD card
  282. pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
  283. }
  284. #endif
  285. #if MICROPY_HW_ENABLE_USB
  286. // only use SD card as current directory if that's what the USB medium is
  287. if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD)
  288. #endif
  289. {
  290. if (first_part) {
  291. // use SD card as current directory
  292. MP_STATE_PORT(vfs_cur) = vfs;
  293. }
  294. }
  295. first_part = false;
  296. }
  297. }
  298. if (first_part) {
  299. printf("PYB: can't mount SD card\n");
  300. return false;
  301. } else {
  302. return true;
  303. }
  304. }
  305. #endif
  306. #if !MICROPY_HW_USES_BOOTLOADER
  307. STATIC uint update_reset_mode(uint reset_mode) {
  308. #if MICROPY_HW_HAS_SWITCH
  309. if (switch_get()) {
  310. // The original method used on the pyboard is appropriate if you have 2
  311. // or more LEDs.
  312. #if defined(MICROPY_HW_LED2)
  313. for (uint i = 0; i < 3000; i++) {
  314. if (!switch_get()) {
  315. break;
  316. }
  317. mp_hal_delay_ms(20);
  318. if (i % 30 == 29) {
  319. if (++reset_mode > 3) {
  320. reset_mode = 1;
  321. }
  322. led_state(2, reset_mode & 1);
  323. led_state(3, reset_mode & 2);
  324. led_state(4, reset_mode & 4);
  325. }
  326. }
  327. // flash the selected reset mode
  328. for (uint i = 0; i < 6; i++) {
  329. led_state(2, 0);
  330. led_state(3, 0);
  331. led_state(4, 0);
  332. mp_hal_delay_ms(50);
  333. led_state(2, reset_mode & 1);
  334. led_state(3, reset_mode & 2);
  335. led_state(4, reset_mode & 4);
  336. mp_hal_delay_ms(50);
  337. }
  338. mp_hal_delay_ms(400);
  339. #elif defined(MICROPY_HW_LED1)
  340. // For boards with only a single LED, we'll flash that LED the
  341. // appropriate number of times, with a pause between each one
  342. for (uint i = 0; i < 10; i++) {
  343. led_state(1, 0);
  344. for (uint j = 0; j < reset_mode; j++) {
  345. if (!switch_get()) {
  346. break;
  347. }
  348. led_state(1, 1);
  349. mp_hal_delay_ms(100);
  350. led_state(1, 0);
  351. mp_hal_delay_ms(200);
  352. }
  353. mp_hal_delay_ms(400);
  354. if (!switch_get()) {
  355. break;
  356. }
  357. if (++reset_mode > 3) {
  358. reset_mode = 1;
  359. }
  360. }
  361. // Flash the selected reset mode
  362. for (uint i = 0; i < 2; i++) {
  363. for (uint j = 0; j < reset_mode; j++) {
  364. led_state(1, 1);
  365. mp_hal_delay_ms(100);
  366. led_state(1, 0);
  367. mp_hal_delay_ms(200);
  368. }
  369. mp_hal_delay_ms(400);
  370. }
  371. #else
  372. #error Need a reset mode update method
  373. #endif
  374. }
  375. #endif
  376. return reset_mode;
  377. }
  378. #endif
  379. void stm32_main(uint32_t reset_mode) {
  380. // Enable caches and prefetch buffers
  381. #if defined(STM32F4)
  382. #if INSTRUCTION_CACHE_ENABLE
  383. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  384. #endif
  385. #if DATA_CACHE_ENABLE
  386. __HAL_FLASH_DATA_CACHE_ENABLE();
  387. #endif
  388. #if PREFETCH_ENABLE
  389. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  390. #endif
  391. #elif defined(STM32F7) || defined(STM32H7)
  392. #if ART_ACCLERATOR_ENABLE
  393. __HAL_FLASH_ART_ENABLE();
  394. #endif
  395. SCB_EnableICache();
  396. SCB_EnableDCache();
  397. #elif defined(STM32L4)
  398. #if !INSTRUCTION_CACHE_ENABLE
  399. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  400. #endif
  401. #if !DATA_CACHE_ENABLE
  402. __HAL_FLASH_DATA_CACHE_DISABLE();
  403. #endif
  404. #if PREFETCH_ENABLE
  405. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  406. #endif
  407. #endif
  408. #if __CORTEX_M >= 0x03
  409. // Set the priority grouping
  410. NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  411. #endif
  412. // SysTick is needed by HAL_RCC_ClockConfig (called in SystemClock_Config)
  413. HAL_InitTick(TICK_INT_PRIORITY);
  414. // set the system clock to be HSE
  415. SystemClock_Config();
  416. // enable GPIO clocks
  417. __HAL_RCC_GPIOA_CLK_ENABLE();
  418. __HAL_RCC_GPIOB_CLK_ENABLE();
  419. __HAL_RCC_GPIOC_CLK_ENABLE();
  420. __HAL_RCC_GPIOD_CLK_ENABLE();
  421. #if defined(STM32F4) || defined(STM32F7)
  422. #if defined(__HAL_RCC_DTCMRAMEN_CLK_ENABLE)
  423. // The STM32F746 doesn't really have CCM memory, but it does have DTCM,
  424. // which behaves more or less like normal SRAM.
  425. __HAL_RCC_DTCMRAMEN_CLK_ENABLE();
  426. #elif defined(CCMDATARAM_BASE)
  427. // enable the CCM RAM
  428. __HAL_RCC_CCMDATARAMEN_CLK_ENABLE();
  429. #endif
  430. #elif defined(STM32H7)
  431. // Enable D2 SRAM1/2/3 clocks.
  432. __HAL_RCC_D2SRAM1_CLK_ENABLE();
  433. __HAL_RCC_D2SRAM2_CLK_ENABLE();
  434. __HAL_RCC_D2SRAM3_CLK_ENABLE();
  435. #endif
  436. #if defined(MICROPY_BOARD_EARLY_INIT)
  437. MICROPY_BOARD_EARLY_INIT();
  438. #endif
  439. // basic sub-system init
  440. #if MICROPY_PY_THREAD
  441. pyb_thread_init(&pyb_thread_main);
  442. #endif
  443. pendsv_init();
  444. led_init();
  445. #if MICROPY_HW_HAS_SWITCH
  446. switch_init0();
  447. #endif
  448. machine_init();
  449. #if MICROPY_HW_ENABLE_RTC
  450. rtc_init_start(false);
  451. #endif
  452. spi_init0();
  453. #if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C
  454. i2c_init0();
  455. #endif
  456. #if MICROPY_HW_HAS_SDCARD
  457. sdcard_init();
  458. #endif
  459. #if MICROPY_HW_ENABLE_STORAGE
  460. storage_init();
  461. #endif
  462. #if MICROPY_PY_LWIP
  463. // lwIP doesn't allow to reinitialise itself by subsequent calls to this function
  464. // because the system timeout list (next_timeout) is only ever reset by BSS clearing.
  465. // So for now we only init the lwIP stack once on power-up.
  466. lwip_init();
  467. #endif
  468. soft_reset:
  469. #if defined(MICROPY_HW_LED2)
  470. led_state(1, 0);
  471. led_state(2, 1);
  472. #else
  473. led_state(1, 1);
  474. led_state(2, 0);
  475. #endif
  476. led_state(3, 0);
  477. led_state(4, 0);
  478. #if !MICROPY_HW_USES_BOOTLOADER
  479. // check if user switch held to select the reset mode
  480. reset_mode = update_reset_mode(1);
  481. #endif
  482. // Python threading init
  483. #if MICROPY_PY_THREAD
  484. mp_thread_init();
  485. #endif
  486. // Stack limit should be less than real stack size, so we have a chance
  487. // to recover from limit hit. (Limit is measured in bytes.)
  488. // Note: stack control relies on main thread being initialised above
  489. mp_stack_set_top(&_estack);
  490. mp_stack_set_limit((char*)&_estack - (char*)&_heap_end - 1024);
  491. // GC init
  492. gc_init(&_heap_start, &_heap_end);
  493. #if MICROPY_ENABLE_PYSTACK
  494. static mp_obj_t pystack[384];
  495. mp_pystack_init(pystack, &pystack[384]);
  496. #endif
  497. // MicroPython init
  498. mp_init();
  499. mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
  500. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  501. mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
  502. // Initialise low-level sub-systems. Here we need to very basic things like
  503. // zeroing out memory and resetting any of the sub-systems. Following this
  504. // we can run Python scripts (eg boot.py), but anything that is configurable
  505. // by boot.py must be set after boot.py is run.
  506. readline_init0();
  507. pin_init0();
  508. extint_init0();
  509. timer_init0();
  510. uart_init0();
  511. // Define MICROPY_HW_UART_REPL to be PYB_UART_6 and define
  512. // MICROPY_HW_UART_REPL_BAUD in your mpconfigboard.h file if you want a
  513. // REPL on a hardware UART as well as on USB VCP
  514. #if defined(MICROPY_HW_UART_REPL)
  515. {
  516. mp_obj_t args[2] = {
  517. MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL),
  518. MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL_BAUD),
  519. };
  520. MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
  521. uart_attach_to_repl(MP_STATE_PORT(pyb_stdio_uart), true);
  522. }
  523. #else
  524. MP_STATE_PORT(pyb_stdio_uart) = NULL;
  525. #endif
  526. #if MICROPY_HW_ENABLE_CAN
  527. can_init0();
  528. #endif
  529. #if MICROPY_HW_ENABLE_USB
  530. pyb_usb_init0();
  531. #endif
  532. // Initialise the local flash filesystem.
  533. // Create it if needed, mount in on /flash, and set it as current dir.
  534. bool mounted_flash = false;
  535. #if MICROPY_HW_ENABLE_STORAGE
  536. mounted_flash = init_flash_fs(reset_mode);
  537. #endif
  538. bool mounted_sdcard = false;
  539. #if MICROPY_HW_HAS_SDCARD
  540. // if an SD card is present then mount it on /sd/
  541. if (sdcard_is_present()) {
  542. // if there is a file in the flash called "SKIPSD", then we don't mount the SD card
  543. if (!mounted_flash || f_stat(&fs_user_mount_flash.fatfs, "/SKIPSD", NULL) != FR_OK) {
  544. mounted_sdcard = init_sdcard_fs();
  545. }
  546. }
  547. #endif
  548. #if MICROPY_HW_ENABLE_USB
  549. // if the SD card isn't used as the USB MSC medium then use the internal flash
  550. if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
  551. pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
  552. }
  553. #endif
  554. // set sys.path based on mounted filesystems (/sd is first so it can override /flash)
  555. if (mounted_sdcard) {
  556. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
  557. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
  558. }
  559. if (mounted_flash) {
  560. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
  561. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
  562. }
  563. // reset config variables; they should be set by boot.py
  564. MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
  565. // run boot.py, if it exists
  566. // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
  567. if (reset_mode == 1 || reset_mode == 3) {
  568. const char *boot_py = "boot.py";
  569. mp_import_stat_t stat = mp_import_stat(boot_py);
  570. if (stat == MP_IMPORT_STAT_FILE) {
  571. int ret = pyexec_file(boot_py);
  572. if (ret & PYEXEC_FORCED_EXIT) {
  573. goto soft_reset_exit;
  574. }
  575. if (!ret) {
  576. flash_error(4);
  577. }
  578. }
  579. }
  580. // turn boot-up LEDs off
  581. #if !defined(MICROPY_HW_LED2)
  582. // If there is only one LED on the board then it's used to signal boot-up
  583. // and so we turn it off here. Otherwise LED(1) is used to indicate dirty
  584. // flash cache and so we shouldn't change its state.
  585. led_state(1, 0);
  586. #endif
  587. led_state(2, 0);
  588. led_state(3, 0);
  589. led_state(4, 0);
  590. // Now we initialise sub-systems that need configuration from boot.py,
  591. // or whose initialisation can be safely deferred until after running
  592. // boot.py.
  593. #if MICROPY_HW_ENABLE_USB
  594. // init USB device to default setting if it was not already configured
  595. if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
  596. pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL);
  597. }
  598. #endif
  599. #if MICROPY_HW_HAS_MMA7660
  600. // MMA accel: init and reset
  601. accel_init();
  602. #endif
  603. #if MICROPY_HW_ENABLE_SERVO
  604. servo_init();
  605. #endif
  606. #if MICROPY_HW_ENABLE_DAC
  607. dac_init();
  608. #endif
  609. #if MICROPY_PY_NETWORK
  610. mod_network_init();
  611. #endif
  612. // At this point everything is fully configured and initialised.
  613. // Run the main script from the current directory.
  614. if ((reset_mode == 1 || reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
  615. const char *main_py;
  616. if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) {
  617. main_py = "main.py";
  618. } else {
  619. main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
  620. }
  621. mp_import_stat_t stat = mp_import_stat(main_py);
  622. if (stat == MP_IMPORT_STAT_FILE) {
  623. int ret = pyexec_file(main_py);
  624. if (ret & PYEXEC_FORCED_EXIT) {
  625. goto soft_reset_exit;
  626. }
  627. if (!ret) {
  628. flash_error(3);
  629. }
  630. }
  631. }
  632. // Main script is finished, so now go into REPL mode.
  633. // The REPL mode can change, or it can request a soft reset.
  634. for (;;) {
  635. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  636. if (pyexec_raw_repl() != 0) {
  637. break;
  638. }
  639. } else {
  640. if (pyexec_friendly_repl() != 0) {
  641. break;
  642. }
  643. }
  644. }
  645. soft_reset_exit:
  646. // soft reset
  647. #if MICROPY_HW_ENABLE_STORAGE
  648. printf("PYB: sync filesystems\n");
  649. storage_flush();
  650. #endif
  651. printf("PYB: soft reboot\n");
  652. #if MICROPY_PY_NETWORK
  653. mod_network_deinit();
  654. #endif
  655. timer_deinit();
  656. uart_deinit();
  657. #if MICROPY_HW_ENABLE_CAN
  658. can_deinit();
  659. #endif
  660. machine_deinit();
  661. #if MICROPY_PY_THREAD
  662. pyb_thread_deinit();
  663. #endif
  664. gc_sweep_all();
  665. goto soft_reset;
  666. }