TI Cortex M3串口转以太网例程分析2-----bootloader

2019-07-26 19:44发布

bootloader是TI串口转以太网代码的一小部分,位于Flash开始的4KB空间内。它的一个重要作用是在应用远程升级,可以通过串口、USB、IIC、以太网等通道进行远程固件升级。bootloader是CPU启动后最先执行的程序,它会把自己拷贝到SRAM,并判断是否有固件升级,如果有升级请求,则执行升级程序;反之,执行用户程序。  一.流程图      
           由于这里只考虑基于以太网的bootloader,其流程图如图2-1所示:

图2-1 二.配置文件     
        由于bootlaoder可以使用串口、USB、IIC、以太网等通道进行远程固件升级,那么怎么样配置才可以使用以太网呢?这就牵扯到bl_config文件。此文件是专门配置bootloader的。代码就不贴了,看一下这里面几个必须配置的选项:
1. 以下至少且只能定义一个,用于指明使用何种方式升级。

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
12条回答
侣行天下
2019-07-27 03:02
  1. ;******************************************************************************
  2. ;
  3. ; The NMI handler.
  4. ;
  5. ;******************************************************************************
  6. NmiSR
  7. if :def:_ENABLE_MOSCFAIL_HANDLER
  8. ;
  9. ; Grab the fault frame from the stack (the stack will be cleared by the
  10. ; processor initialization that follows).
  11. ;
  12. ldm sp, {r4-r11}
  13. mov r12, lr

  14. ;
  15. ; Initialize the processor.
  16. ;
  17. bl ProcessorInit

  18. ;
  19. ; Branch to the SRAM copy of the NMI handler.
  20. ;
  21. ldr pc, =NmiSR_In_SRAM
  22. else
  23. ;
  24. ; Loop forever since there is nothing that we can do about a NMI.
  25. ;
  26. b .
  27. endif

  28. ;******************************************************************************
  29. ;
  30. ; The hard fault handler.
  31. ;
  32. ;******************************************************************************
  33. FaultISR
  34. ;
  35. ; Loop forever since there is nothing that we can do about a hard fault.
  36. ;
  37. b .

  38. ;******************************************************************************
  39. ;
  40. ; The update handler, which gets called when the application would like to
  41. ; start an update.
  42. ; 升级服务函数,当应用程序想要开始升级时,调用这个函数.
  43. ;
  44. ;******************************************************************************
  45. UpdateHandler
  46. ;
  47. ; Initialize the processor. 初始化处理器
  48. ;
  49. bl ProcessorInit ;调用子程序

  50. ;
  51. ; Branch to the SRAM copy of the update handler.
  52. ;
  53. ldr pc, =UpdateHandler_In_SRAM

  54. ;******************************************************************************
  55. ;
  56. ; This portion of the file goes into the text section.
  57. ;
  58. ;******************************************************************************
  59. align 4
  60. area ||.text||, code, readonly, align=2

  61. Reset_Handler_In_SRAM
  62. ;
  63. ; Call the user-supplied low level hardware initialization function
  64. ; if provided.
  65. ; 如果用户提供了底层硬件初始化函数,则调用这个函数
  66. ;
  67. if :def:_BL_HW_INIT_FN_HOOK
  68. import $_BL_HW_INIT_FN_HOOK
  69. bl $_BL_HW_INIT_FN_HOOK
  70. endif

  71. ;
  72. ; See if an update should be performed.
  73. ; 检查是否有升级请求
  74. ;
  75. import CheckForceUpdate
  76. bl CheckForceUpdate
  77. cbz r0, CallApplication ;结果为零则转移(只能跳到下一行)

  78. ;
  79. ; Configure the microcontroller.
  80. ;
  81. EnterBootLoader
  82. if :def:_ENET_ENABLE_UPDATE
  83. import ConfigureEnet
  84. bl ConfigureEnet
  85. elif :def:_CAN_ENABLE_UPDATE
  86. import ConfigureCAN
  87. bl ConfigureCAN
  88. elif :def:_USB_ENABLE_UPDATE
  89. import ConfigureUSB
  90. bl ConfigureUSB
  91. else
  92. import ConfigureDevice
  93. bl ConfigureDevice
  94. endif

  95. ;
  96. ; Call the user-supplied initialization function if provided.
  97. ; 如果用户提供了初始化函数,则调用.
  98. ;
  99. if :def:_BL_INIT_FN_HOOK
  100. import $_BL_INIT_FN_HOOK
  101. bl $_BL_INIT_FN_HOOK
  102. endif

  103. ;
  104. ; Branch to the update handler.
  105. ; 进入升级处理程序
  106. ;
  107. if :def:_ENET_ENABLE_UPDATE
  108. import UpdateBOOTP
  109. b UpdateBOOTP
  110. elif :def:_CAN_ENABLE_UPDATE
  111. import UpdaterCAN
  112. b UpdaterCAN
  113. elif :def:_USB_ENABLE_UPDATE
  114. import UpdaterUSB
  115. b UpdaterUSB
  116. else
  117. import Updater
  118. b Updater
  119. endif

  120. ;
  121. ; This is a second symbol to allow starting the application from the boot
  122. ; loader the linker may not like the perceived jump.
  123. ;
  124. export StartApplication
  125. StartApplication
  126. ;
  127. ; Call the application via the reset handler in its vector table. Load the
  128. ; address of the application vector table.
  129. ;
  130. CallApplication
  131. ;
  132. ; Copy the application's vector table to the target address if necessary.
  133. ; Note that incorrect boot loader configuration could cause this to
  134. ; corrupt the code! Setting VTABLE_START_ADDRESS to 0x20000000 (the start
  135. ; of SRAM) is safe since this will use the same memory that the boot loader
  136. ; already uses for its vector table. Great care will have to be taken if
  137. ; other addresses are to be used.
  138. ; 如果必要的话,复制应用程序的向量表到目标地址.
  139. ; 请注意,不正确的boot loader配置会破坏整个程序!设置VTABLE_START_ADDRESS为
  140. ; 0x2000 0000(从SRAM启动)也是可以的,因为这将和boot loader使用同样的内存
  141. ;
  142. if (_APP_START_ADDRESS != _VTABLE_START_ADDRESS) ;看应用程序的起始地址是否和应用程序的向量表存储地址相同
  143. movw r0, #(_VTABLE_START_ADDRESS & 0xffff)
  144. if (_VTABLE_START_ADDRESS > 0xffff)
  145. movt r0, #(_VTABLE_START_ADDRESS >> 16)
  146. endif
  147. movw r1, #(_APP_START_ADDRESS & 0xffff)
  148. if (_APP_START_ADDRESS > 0xffff)
  149. movt r1, #(_APP_START_ADDRESS >> 16)
  150. endif

  151. ;
  152. ; Calculate the end address of the vector table assuming that it has the
  153. ; maximum possible number of vectors. We don't know how many the app has
  154. ; populated so this is the safest approach though it may copy some non
  155. ; vector data if the app table is smaller than the maximum.
  156. ; 计算向量表的结束地址,假设向量表有最大数目. 我们不知道应用程序使用了多少
  157. ; 向量表,但这样是最安全的
  158. ;
  159. movw r2, #(70 * 4)
  160. adds r2, r2, r0
  161. VectorCopyLoop
  162. ldr r3, [r1], #4
  163. str r3, [r0], #4
  164. cmp r0, r2
  165. blt VectorCopyLoop
  166. endif

  167. ;
  168. ; Set the vector table address to the beginning of the application.
  169. ; 将向量表重定位到应用程序开始处
  170. ;
  171. movw r0, #(_VTABLE_START_ADDRESS & 0xffff)
  172. if (_VTABLE_START_ADDRESS > 0xffff)
  173. movt r0, #(_VTABLE_START_ADDRESS >> 16)
  174. endif
  175. movw r1, #(NVIC_VTABLE & 0xffff) ;向量表偏移寄存器
  176. movt r1, #(NVIC_VTABLE >> 16)
  177. str r0, [r1]

  178. ;
  179. ; Load the stack pointer from the application's vector table.
  180. ; 从应用程序向量表装载用户堆栈.
  181. ;
  182. if (_APP_START_ADDRESS != _VTABLE_START_ADDRESS)
  183. movw r0, #(_APP_START_ADDRESS & 0xffff)
  184. if (_APP_START_ADDRESS > 0xffff)
  185. movt r0, #(_APP_START_ADDRESS >> 16)
  186. endif
  187. endif
  188. ldr sp, [r0]

  189. ;
  190. ; Load the initial PC from the application's vector table and branch to
  191. ; the application's entry point.
  192. ;
  193. ldr r0, [r0, #4]
  194. bx r0

  195. ;******************************************************************************
  196. ;
  197. ; The update handler, which gets called when the application would like to
  198. ; start an update.
  199. ; 升级处理函数,当用户程序想要开始升级时,调用此函数
  200. ;
  201. ;******************************************************************************
  202. UpdateHandler_In_SRAM
  203. ;
  204. ; Load the stack pointer from the vector table.
  205. ; 从boot loader向量表中装载堆栈指针
  206. ;
  207. if :def:_FLASH_PATCH_COMPATIBLE
  208. movs r0, #0x1000
  209. else
  210. movs r0, #0x0000
  211. endif
  212. ldr sp, [r0]

  213. ;
  214. ; Call the user-supplied low level hardware initialization function
  215. ; if provided.
  216. ; 调用用户提供的底层硬件初始化函数
  217. ;
  218. if :def:_BL_HW_INIT_FN_HOOK
  219. bl $_BL_HW_INIT_FN_HOOK
  220. endif

  221. ;
  222. ; Call the user-supplied re-initialization function if provided.
  223. ; 调用用户提供的初始化函数
  224. ;
  225. if :def:_BL_REINIT_FN_HOOK
  226. import $_BL_REINIT_FN_HOOK
  227. bl $_BL_REINIT_FN_HOOK
  228. endif

  229. ;
  230. ; Branch to the update handler.
  231. ; 进入升级例程
  232. ;
  233. if :def:_ENET_ENABLE_UPDATE
  234. b UpdateBOOTP ;在bl_enet.c中
  235. elif :def:_CAN_ENABLE_UPDATE
  236. import AppUpdaterCAN
  237. b AppUpdaterCAN
  238. elif :def:_USB_ENABLE_UPDATE
  239. import AppUpdaterUSB
  240. b AppUpdaterUSB
  241. else
  242. b Updater
  243. endif

  244. ;******************************************************************************
  245. ;
  246. ; The NMI handler.
  247. ; NMI异常服务例程,处理主振荡器失败
  248. ;
  249. ;******************************************************************************
  250. if :def:_ENABLE_MOSCFAIL_HANDLER
  251. NmiSR_In_SRAM
  252. ;
  253. ; Restore the stack frame.
  254. ;
  255. mov lr, r12
  256. stm sp, {r4-r11}

  257. ;
  258. ; Save the link register.
  259. ;
  260. mov r9, lr

  261. ;
  262. ; Call the user-supplied low level hardware initialization function
  263. ; if provided.
  264. ;
  265. if :def:_BL_HW_INIT_FN_HOOK
  266. bl _BL_HW_INIT_FN_HOOK
  267. endif

  268. ;
  269. ; See if an update should be performed.
  270. ;
  271. bl CheckForceUpdate
  272. cbz r0, EnterApplication

  273. ;
  274. ; Clear the MOSCFAIL bit in RESC.
  275. ;
  276. movw r0, #(SYSCTL_RESC & 0xffff)
  277. movt r0, #(SYSCTL_RESC >> 16)
  278. ldr r1, [r0]
  279. bic r1, r1, #SYSCTL_RESC_MOSCFAIL
  280. str r1, [r0]

  281. ;
  282. ; Fix up the PC on the stack so that the boot pin check is bypassed
  283. ; (since it has already been performed).
  284. ;
  285. ldr r0, =EnterBootLoader
  286. bic r0, #0x00000001
  287. str r0, [sp, #0x18]

  288. ;
  289. ; Return from the NMI handler. This will then start execution of the
  290. ; boot loader.
  291. ;
  292. bx r9

  293. ;
  294. ; Restore the link register.
  295. ;
  296. EnterApplication
  297. mov lr, r9

  298. ;
  299. ; Copy the application's vector table to the target address if necessary.
  300. ; Note that incorrect boot loader configuration could cause this to
  301. ; corrupt the code! Setting VTABLE_START_ADDRESS to 0x20000000 (the start
  302. ; of SRAM) is safe since this will use the same memory that the boot loader
  303. ; already uses for its vector table. Great care will have to be taken if
  304. ; other addresses are to be used.
  305. ;
  306. if (_APP_START_ADDRESS != _VTABLE_START_ADDRESS)
  307. movw r0, #(_VTABLE_START_ADDRESS & 0xffff)
  308. if (_VTABLE_START_ADDRESS > 0xffff)
  309. movt r0, #(_VTABLE_START_ADDRESS >> 16)
  310. endif
  311. movw r1, #(_APP_START_ADDRESS & 0xffff)
  312. if (_APP_START_ADDRESS > 0xffff)
  313. movt r1, #(_APP_START_ADDRESS >> 16)
  314. endif

  315. ;
  316. ; Calculate the end address of the vector table assuming that it has the
  317. ; maximum possible number of vectors. We don't know how many the app has
  318. ; populated so this is the safest approach though it may copy some non
  319. ; vector data if the app table is smaller than the maximum.
  320. ;
  321. movw r2, #(70 * 4)
  322. adds r2, r2, r0
  323. VectorCopyLoop2
  324. ldr r3, [r1], #4
  325. str r3, [r0], #4
  326. cmp r0, r2
  327. blt VectorCopyLoop2
  328. endif

  329. ;
  330. ; Set the application's vector table start address. Typically this is the
  331. ; application start address but in some cases an application may relocate
  332. ; this so we can't assume that these two addresses are equal.
  333. ;
  334. movw r0, #(_VTABLE_START_ADDRESS & 0xffff)
  335. if (_VTABLE_START_ADDRESS > 0xffff)
  336. movt r0, #(_VTABLE_START_ADDRESS >> 16)
  337. endif
  338. movw r1, #(NVIC_VTABLE & 0xffff)
  339. movt r1, #(NVIC_VTABLE >> 16)
  340. str r0, [r1]

  341. ;
  342. ; Remove the NMI stack frame from the boot loader's stack.
  343. ;
  344. ldmia sp, {r4-r11}

  345. ;
  346. ; Get the application's stack pointer.
  347. ;
  348. if (_APP_START_ADDRESS != _VTABLE_START_ADDRESS)
  349. movw r0, #(_APP_START_ADDRESS & 0xffff)
  350. if (_APP_START_ADDRESS > 0xffff)
  351. movt r0, #(_APP_START_ADDRESS >> 16)
  352. endif
  353. endif
  354. ldr sp, [r0, #0x00]

  355. ;
  356. ; Fix up the NMI stack frame's return address to be the reset handler of
  357. ; the application.
  358. ;
  359. ldr r10, [r0, #0x04]
  360. bic r10, #0x00000001

  361. ;
  362. ; Store the NMI stack frame onto the application's stack.
  363. ;
  364. stmdb sp!, {r4-r11}

  365. ;
  366. ; Branch to the application's NMI handler.
  367. ;
  368. ldr r0, [r0, #0x08]
  369. bx r0
  370. endif

  371. ;******************************************************************************
  372. ;
  373. ; The default interrupt handler.
  374. ;
  375. ;******************************************************************************
  376. IntDefaultHandler
  377. ;
  378. ; Loop forever since there is nothing that we can do about an unexpected
  379. ; interrupt.
  380. ;
  381. b .

  382. ;******************************************************************************
  383. ;
  384. ; Provides a small delay. The loop below takes 3 cycles/loop.
  385. ; 提供一个小的延时函数. 循环一次需要3个时钟周期.
  386. ;
  387. ;******************************************************************************
  388. export Delay
  389. Delay
  390. subs r0, #1
  391. bne Delay
  392. bx lr

  393. ;******************************************************************************
  394. ;
  395. ; This is the end of the file.
  396. ;
  397. ;******************************************************************************
  398. align 4
  399. end
复制代码

一周热门 更多>