|
文件路径:
源码分析:
- //首先软件复位
- int first_soft_reset = true;
- //软件复位
- soft_reset:
- // check if user switch held to select the reset mode
- //检查用户切换是否选择重置模式
- #if defined(MICROPY_HW_LED2)
- led_state(1, 0);
- led_state(2, 1);
- #else
- led_state(1, 1);
- led_state(2, 0);
- #endif
- led_state(3, 0);
- led_state(4, 0);
- //复位模式更新
- uint reset_mode = update_reset_mode(1);
- //machine初始化
- machine_init();
- //rtc初始化并启动
- #if MICROPY_HW_ENABLE_RTC
- if (first_soft_reset) {
- rtc_init_start(false);
- }
- #endif
- // more sub-system init
- //更多的子系统初始化
- //SD卡初始化
- #if MICROPY_HW_HAS_SDCARD
- if (first_soft_reset) {
- sdcard_init();
- }
- #endif
- if (first_soft_reset) {
- storage_init();
- }
- // Python threading init
- //python 线程初始化
- #if MICROPY_PY_THREAD
- mp_thread_init();
- #endif
- // Stack limit should be less than real stack size, so we have a chance
- // to recover from limit hit. (Limit is measured in bytes.)
- // Note: stack control relies on main thread being initialised above
- //堆栈限制应该小于实际堆栈大小,因此我们有机会从限制命中恢复。(限制是以字节为单位度量的。)
- //注意:堆栈控制依赖于上面初始化的主线程
- mp_stack_set_top(&_estack);
- mp_stack_set_limit((char*)&_estack - (char*)&_heap_end - 1024);
- // GC init
- //GC初始化
- gc_init(&_heap_start, &_heap_end);
- // MicroPython init
- //MicroPython初始化
- mp_init();
- //mp对象列表初始化
- mp_obj_list_init(mp_sys_path, 0);
- //mp对象列表append初始化
- //当前目录(或脚本的基本目录)
- mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
- //mp对象列表初始化
- mp_obj_list_init(mp_sys_argv, 0);
- // Initialise low-level sub-systems. Here we need to very basic things like
- // zeroing out memory and resetting any of the sub-systems. Following this
- // we can run Python scripts (eg boot.py), but anything that is configurable
- // by boot.py must be set after boot.py is run.
- /*
- 初始化低层次的子系统。这里我们需要一些基本的东西,比如零内存和重新设置任何子系统。
- 接下来,我们可以运行Python脚本(如boot.py),但是任何可以通过引导配置的东西。必须在启动后设置boot.py是运行。
- */
- //读行初始化
- readline_init0();
- //引脚初始化
- pin_init0();
- //外部中断初始化
- extint_init0();
- //定时器初始化
- timer_init0();
- //串口初始化
- uart_init0();
- // Define MICROPY_HW_UART_REPL to be PYB_UART_6 and define
- // 定义 MICROPY_HW_UART_REPL 为 PYB_UART_6
- // MICROPY_HW_UART_REPL_BAUD in your mpconfigboard.h file if you want a
- // 在mpconfigboard.h中定义 MICROPY_HW_UART_REPL_BAUD
- // REPL on a hardware UART as well as on USB VCP
- // 如果想RUSB VCP的REPL中支持硬件串口
- #if defined(MICROPY_HW_UART_REPL)
- {
- mp_obj_t args[2] = {
- MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL),
- MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL_BAUD),
- };
- MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
- }
- #else
- MP_STATE_PORT(pyb_stdio_uart) = NULL;
- #endif
- //can初始化
- #if MICROPY_HW_ENABLE_CAN
- can_init0();
- #endif
- #if MICROPY_HW_ENABLE_RNG
- rng_init0();
- #endif
- //i2c初始化
- i2c_init0();
- //spi初始化
- spi_init0();
- //pyb_usb初始化
- pyb_usb_init0();
- // Initialise the local flash filesystem.
- //初始化本地闪存文件系统
- // Create it if needed, mount in on /flash, and set it as current dir.
- // 如果需要创建并挂载文件系统到/flash ,并设置文件系统为当前目录
- bool mounted_flash = init_flash_fs(reset_mode);
- bool mounted_sdcard = false;
- #if MICROPY_HW_HAS_SDCARD
- // if an SD card is present then mount it on /sd/
- // 如果有SD卡,则挂载于/SD/
- if (sdcard_is_present()) {
- // if there is a file in the flash called "SKIPSD", then we don't mount the SD card
- //如果flash中有一个名为“SKIPSD”的文件,那么我们就不会安装SD卡.
- if (!mounted_flash || f_stat(&fs_user_mount_flash.fatfs, "/SKIPSD", NULL) != FR_OK) {
- mounted_sdcard = init_sdcard_fs(first_soft_reset);
- }
- }
- #endif
- // set sys.path based on mounted filesystems (/sd is first so it can override /flash)
- //设置sys.path挂载文件系统的路径(/sd首选选择,可以覆盖/flash)
- if (mounted_sdcard) {
- mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
- mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
- }
- if (mounted_flash) {
- mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
- mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
- }
- // reset config variables; they should be set by boot.py
- // 复位配置向量,通过boot.py进行设置
- MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
- // run boot.py, if it exists
- // 如果boot.py存在,运行boot.py
- // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
- //pyb.reboot([bootpy])的功能为软件reboot和执行自定义的boot.py
- if (reset_mode == 1 || reset_mode == 3) {
- const char *boot_py = "boot.py";
- mp_import_stat_t stat = mp_import_stat(boot_py);
- if (stat == MP_IMPORT_STAT_FILE) {
- int ret = pyexec_file(boot_py);
- if (ret & PYEXEC_FORCED_EXIT) {
- goto soft_reset_exit;
- }
- if (!ret) {
- flash_error(4);
- }
- }
- }
- // turn boot-up LEDs off
- //boot-up 关闭LEDs
- #if !defined(MICROPY_HW_LED2)
- // If there is only one LED on the board then it's used to signal boot-up
- // 如果板子上面只有一个LED,那么它用来表示boot-up启动信号,因此我们把它关掉
- // and so we turn it off here. Otherwise LED(1) is used to indicate dirty
- //否则 LED(1)表示flash缓存的心跳,我们不应该改变它的状态。
- // flash cache and so we shouldn't change its state.
- led_state(1, 0);
- #endif
- led_state(2, 0);
- led_state(3, 0);
- led_state(4, 0);
- // Now we initialise sub-systems that need configuration from boot.py,
- //现在我们从boot.py配置初始化需要配置的子系统
- // or whose initialisation can be safely deferred until after running
- // boot.py.
- //或者它的初始化可以安全地推迟到运行boot.py之后
- #if defined(USE_DEVICE_MODE)
- // init USB device to default setting if it was not already configured
- //如果usb设备没有配置,则初始化USB设备到默认的配置
- if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
- pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL);
- }
- #endif
- //MMA7660初始化
- #if MICROPY_HW_HAS_MMA7660
- // MMA accel: init and reset
- accel_init();
- #endif
- //私服初始化
- #if MICROPY_HW_ENABLE_SERVO
- // servo
- servo_init();
- #endif
- //DAC初始化
- #if MICROPY_HW_ENABLE_DAC
- // DAC
- dac_init();
- #endif
- //micropython 网络初始化
- #if MICROPY_PY_NETWORK
- mod_network_init();
- #endif
- // At this point everything is fully configured and initialised.
- //此时,一切都已完全配置和初始化。
- // Run the main script from the current directory.
- //运行当前目录下的main脚本
- if ((reset_mode == 1 || reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
- const char *main_py;
- if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) {
- main_py = "main.py";
- } else {
- main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
- }
- //加载main.py脚本
- mp_import_stat_t stat = mp_import_stat(main_py);
- if (stat == MP_IMPORT_STAT_FILE) {
- //执行main.py脚本
- int ret = pyexec_file(main_py);
- if (ret & PYEXEC_FORCED_EXIT) {
- goto soft_reset_exit;
- }
- if (!ret) {
- flash_error(3);//flash错误
- }
- }
- }
- // Main script is finished, so now go into REPL mode.
- // The REPL mode can change, or it can request a soft reset.
- //main脚本完成,现在进入REPL模式。
- //REPL模式可以更改,也可以请求软件复位。
- for (;;) {
- if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
- if (pyexec_raw_repl() != 0) {
- break;
- }
- } else {
- if (pyexec_friendly_repl() != 0) {
- break;
- }
- }
- }
- //退出软件复位
- soft_reset_exit:
- // soft reset
- //软件复位
- printf("PYB: sync filesystems\n");
- storage_flush();
- printf("PYB: soft reboot\n");
- timer_deinit();
- uart_deinit();
- #if MICROPY_HW_ENABLE_CAN
- can_deinit();
- #endif
- #if MICROPY_PY_THREAD
- pyb_thread_deinit();
- #endif
- first_soft_reset = false;
- goto soft_reset;
- }
复制代码
|
|