三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 360|回复: 0
打印 上一主题 下一主题

stm32f405-main.c-源码分析8

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-3 12:18:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

文件路径:
  1. phospherus/stmhal/main.c
复制代码

源码分析:

  1. //首先软件复位
  2. int first_soft_reset = true;
  3. //软件复位
  4. soft_reset:

  5. // check if user switch held to select the reset mode
  6. //检查用户切换是否选择重置模式
  7. #if defined(MICROPY_HW_LED2)
  8. led_state(1, 0);
  9. led_state(2, 1);
  10. #else
  11. led_state(1, 1);
  12. led_state(2, 0);
  13. #endif
  14. led_state(3, 0);
  15. led_state(4, 0);
  16. //复位模式更新
  17. uint reset_mode = update_reset_mode(1);
  18. //machine初始化
  19. machine_init();
  20. //rtc初始化并启动
  21. #if MICROPY_HW_ENABLE_RTC
  22. if (first_soft_reset) {
  23. rtc_init_start(false);
  24. }
  25. #endif

  26. // more sub-system init
  27. //更多的子系统初始化
  28. //SD卡初始化
  29. #if MICROPY_HW_HAS_SDCARD
  30. if (first_soft_reset) {
  31. sdcard_init();
  32. }
  33. #endif
  34. if (first_soft_reset) {
  35. storage_init();
  36. }

  37. // Python threading init
  38. //python 线程初始化
  39. #if MICROPY_PY_THREAD
  40. mp_thread_init();
  41. #endif

  42. // Stack limit should be less than real stack size, so we have a chance
  43. // to recover from limit hit. (Limit is measured in bytes.)
  44. // Note: stack control relies on main thread being initialised above
  45. //堆栈限制应该小于实际堆栈大小,因此我们有机会从限制命中恢复。(限制是以字节为单位度量的。)
  46. //注意:堆栈控制依赖于上面初始化的主线程
  47. mp_stack_set_top(&_estack);
  48. mp_stack_set_limit((char*)&_estack - (char*)&_heap_end - 1024);

  49. // GC init
  50. //GC初始化
  51. gc_init(&_heap_start, &_heap_end);

  52. // MicroPython init
  53. //MicroPython初始化
  54. mp_init();
  55. //mp对象列表初始化
  56. mp_obj_list_init(mp_sys_path, 0);
  57. //mp对象列表append初始化
  58. //当前目录(或脚本的基本目录)
  59. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  60. //mp对象列表初始化
  61. mp_obj_list_init(mp_sys_argv, 0);

  62. // Initialise low-level sub-systems. Here we need to very basic things like
  63. // zeroing out memory and resetting any of the sub-systems. Following this
  64. // we can run Python scripts (eg boot.py), but anything that is configurable
  65. // by boot.py must be set after boot.py is run.
  66. /*
  67. 初始化低层次的子系统。这里我们需要一些基本的东西,比如零内存和重新设置任何子系统。
  68. 接下来,我们可以运行Python脚本(如boot.py),但是任何可以通过引导配置的东西。必须在启动后设置boot.py是运行。
  69. */
  70. //读行初始化
  71. readline_init0();
  72. //引脚初始化
  73. pin_init0();
  74. //外部中断初始化
  75. extint_init0();
  76. //定时器初始化
  77. timer_init0();
  78. //串口初始化
  79. uart_init0();

  80. // Define MICROPY_HW_UART_REPL to be PYB_UART_6 and define
  81. // 定义 MICROPY_HW_UART_REPL 为 PYB_UART_6
  82. // MICROPY_HW_UART_REPL_BAUD in your mpconfigboard.h file if you want a
  83. // 在mpconfigboard.h中定义 MICROPY_HW_UART_REPL_BAUD
  84. // REPL on a hardware UART as well as on USB VCP
  85. // 如果想RUSB VCP的REPL中支持硬件串口
  86. #if defined(MICROPY_HW_UART_REPL)
  87. {
  88. mp_obj_t args[2] = {
  89. MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL),
  90. MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL_BAUD),
  91. };
  92. MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
  93. }
  94. #else
  95. MP_STATE_PORT(pyb_stdio_uart) = NULL;
  96. #endif
  97. //can初始化
  98. #if MICROPY_HW_ENABLE_CAN
  99. can_init0();
  100. #endif

  101. #if MICROPY_HW_ENABLE_RNG
  102. rng_init0();
  103. #endif
  104. //i2c初始化
  105. i2c_init0();
  106. //spi初始化
  107. spi_init0();
  108. //pyb_usb初始化
  109. pyb_usb_init0();

  110. // Initialise the local flash filesystem.
  111. //初始化本地闪存文件系统
  112. // Create it if needed, mount in on /flash, and set it as current dir.
  113. // 如果需要创建并挂载文件系统到/flash ,并设置文件系统为当前目录
  114. bool mounted_flash = init_flash_fs(reset_mode);

  115. bool mounted_sdcard = false;
  116. #if MICROPY_HW_HAS_SDCARD
  117. // if an SD card is present then mount it on /sd/
  118. // 如果有SD卡,则挂载于/SD/
  119. if (sdcard_is_present()) {
  120. // if there is a file in the flash called "SKIPSD", then we don't mount the SD card
  121. //如果flash中有一个名为“SKIPSD”的文件,那么我们就不会安装SD卡.
  122. if (!mounted_flash || f_stat(&fs_user_mount_flash.fatfs, "/SKIPSD", NULL) != FR_OK) {
  123. mounted_sdcard = init_sdcard_fs(first_soft_reset);
  124. }
  125. }
  126. #endif

  127. // set sys.path based on mounted filesystems (/sd is first so it can override /flash)
  128. //设置sys.path挂载文件系统的路径(/sd首选选择,可以覆盖/flash)
  129. if (mounted_sdcard) {
  130. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
  131. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
  132. }
  133. if (mounted_flash) {
  134. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
  135. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
  136. }

  137. // reset config variables; they should be set by boot.py
  138. // 复位配置向量,通过boot.py进行设置
  139. MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;

  140. // run boot.py, if it exists
  141. // 如果boot.py存在,运行boot.py
  142. // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
  143. //pyb.reboot([bootpy])的功能为软件reboot和执行自定义的boot.py
  144. if (reset_mode == 1 || reset_mode == 3) {
  145. const char *boot_py = "boot.py";
  146. mp_import_stat_t stat = mp_import_stat(boot_py);
  147. if (stat == MP_IMPORT_STAT_FILE) {
  148. int ret = pyexec_file(boot_py);
  149. if (ret & PYEXEC_FORCED_EXIT) {
  150. goto soft_reset_exit;
  151. }
  152. if (!ret) {
  153. flash_error(4);
  154. }
  155. }
  156. }

  157. // turn boot-up LEDs off
  158. //boot-up 关闭LEDs
  159. #if !defined(MICROPY_HW_LED2)
  160. // If there is only one LED on the board then it's used to signal boot-up
  161. // 如果板子上面只有一个LED,那么它用来表示boot-up启动信号,因此我们把它关掉
  162. // and so we turn it off here. Otherwise LED(1) is used to indicate dirty
  163. //否则 LED(1)表示flash缓存的心跳,我们不应该改变它的状态。
  164. // flash cache and so we shouldn't change its state.
  165. led_state(1, 0);
  166. #endif
  167. led_state(2, 0);
  168. led_state(3, 0);
  169. led_state(4, 0);

  170. // Now we initialise sub-systems that need configuration from boot.py,
  171. //现在我们从boot.py配置初始化需要配置的子系统
  172. // or whose initialisation can be safely deferred until after running
  173. // boot.py.
  174. //或者它的初始化可以安全地推迟到运行boot.py之后

  175. #if defined(USE_DEVICE_MODE)
  176. // init USB device to default setting if it was not already configured
  177. //如果usb设备没有配置,则初始化USB设备到默认的配置
  178. if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
  179. pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL);
  180. }
  181. #endif
  182. //MMA7660初始化
  183. #if MICROPY_HW_HAS_MMA7660
  184. // MMA accel: init and reset
  185. accel_init();
  186. #endif
  187. //私服初始化
  188. #if MICROPY_HW_ENABLE_SERVO
  189. // servo
  190. servo_init();
  191. #endif
  192. //DAC初始化
  193. #if MICROPY_HW_ENABLE_DAC
  194. // DAC
  195. dac_init();
  196. #endif
  197. //micropython 网络初始化
  198. #if MICROPY_PY_NETWORK
  199. mod_network_init();
  200. #endif

  201. // At this point everything is fully configured and initialised.
  202. //此时,一切都已完全配置和初始化。
  203. // Run the main script from the current directory.
  204. //运行当前目录下的main脚本
  205. if ((reset_mode == 1 || reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
  206. const char *main_py;
  207. if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) {
  208. main_py = "main.py";
  209. } else {
  210. main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
  211. }
  212. //加载main.py脚本
  213. mp_import_stat_t stat = mp_import_stat(main_py);
  214. if (stat == MP_IMPORT_STAT_FILE) {
  215. //执行main.py脚本
  216. int ret = pyexec_file(main_py);
  217. if (ret & PYEXEC_FORCED_EXIT) {
  218. goto soft_reset_exit;
  219. }
  220. if (!ret) {
  221. flash_error(3);//flash错误
  222. }
  223. }
  224. }

  225. // Main script is finished, so now go into REPL mode.
  226. // The REPL mode can change, or it can request a soft reset.
  227. //main脚本完成,现在进入REPL模式。
  228. //REPL模式可以更改,也可以请求软件复位。
  229. for (;;) {
  230. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  231. if (pyexec_raw_repl() != 0) {
  232. break;
  233. }
  234. } else {
  235. if (pyexec_friendly_repl() != 0) {
  236. break;
  237. }
  238. }
  239. }

  240. //退出软件复位
  241. soft_reset_exit:

  242. // soft reset
  243. //软件复位
  244. printf("PYB: sync filesystems\n");
  245. storage_flush();

  246. printf("PYB: soft reboot\n");
  247. timer_deinit();
  248. uart_deinit();
  249. #if MICROPY_HW_ENABLE_CAN
  250. can_deinit();
  251. #endif

  252. #if MICROPY_PY_THREAD
  253. pyb_thread_deinit();
  254. #endif

  255. first_soft_reset = false;
  256. goto soft_reset;
  257. }
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2024-4-20 00:01 , Processed in 0.037356 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表