[分享] Flashloader 应用案例
768 查看
0 回复
 楼主 | 发布于 2020-09-23 | 只看楼主
分享到:
在痞子衡嵌入式博客中,《Serial Downloader模式(sdphost/MfgTool)》《Flashloader初体验(blhost)》介绍了通过Serial Downloader模式加载Flashloader的步骤,以及利用Flashloader更新存储器内的firmware的操作流程。
但要实现上述Flashloader应用的前提是让RT 系列MCU进入Serial Downloader模式,所以就有客户想实现在应用程序中运行Flashloader,当如何实现呢??
《Serial Downloader模式(sdphost/MfgTool)》的介绍,在运行Flashloader之前需要将其代码加载到SRAM内,如下所示。这也给我们了启示,首先,在应用程序中将拷贝Flashloader固件到SRAM内,然后跳转运行Flashloader固件。
  1. C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools\sdphost\win> .\sdphost.exe -u 0x1fc9,0x0130 -- write-file 0x20000000 ..\..\mfgtools-rel\Profiles\MXRT105X\OS Firmware\ivt_flashloader.bin
复制代码
所以具体实现步骤分拆解为以下几步:
  • 生成Flashloader固件
    在SDK library有提供Flashloader的代码工程,此篇文章以 i.MX RT1050为例
    1.1 生成TXT文件
    IAR IDE打开Flashloader工程,如下图配置工程后,编译工程生成TXT文件。


图 1

      1.2 打开TXT文件,利用文档工具中Replace功能将TXT中的空格转换为逗号。
图 2

      1.3 生成包含Flashloader固件的C数组
      通过下方的Python代码将TXT文件内容转换成包含Flashloader固件的C数组,生成结果如图3所示。
  1. ## 生成包含Flashloader固件的C数组
  2. fout_update = open('flashloader_Update.c','w',encoding='utf-8')
  3. fout_update.write('const uint8_t  FlashLoaderIAR[] = {'+'\n')
  4. with open('flashloader.txt',) as fout:
  5.     for lines in fout.readlines():
  6.         lines_list = lines.strip().split(',')
  7.         print('The len is {}  '.format(len(lines_list)))
  8.         for i in range(len(lines_list)):
  9.             lines_list[i]= "{}{}".format('0x',lines_list[i])
  10.             fout_update.write(lines_list[i])
  11.             fout_update.write(',')
  12.             print('The order {} is {}'.format(i,lines_list[i]))
  13.         fout_update.write('\n')
  14. fout_update.write('}'+'\n')
  15. fout_update.close()
复制代码
图 3

2. 建立工程
    2.1 新建Hello_World 工程
通过MCUXpresso基于SDK library快速建立Hello_World工程,具体步骤请参考《MCUXpresso_IDE_User_Guide》的5. Creating New Projects using installed SDK Part Support章节
    2.2 添加代码,建立完善工程
    具体代码如下
  1. /*
  2. * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */

  8. #include "fsl_device_registers.h"
  9. #include "fsl_debug_console.h"
  10. #include "board.h"
  11. #include "flash_loader.h"

  12. #include "pin_mux.h"
  13. #include "clock_config.h"
  14. /*******************************************************************************
  15. * Definitions
  16. ******************************************************************************/
  17. #define VectorTableAddress 0x20002000
  18. #define Flashloader_lenght 82142

  19. #define kDefaultVectorTableAddress 0
  20. /*******************************************************************************
  21. * Prototypes
  22. ******************************************************************************/


  23. void bootloader_cleanup()
  24. {
  25.     // Turn off interrupts.
  26.     __disable_irq();

  27.     // Set the VTOR to default.

  28.     SCB->VTOR = kDefaultVectorTableAddress;

  29.     // Memory barriers for good measure.
  30.     __ISB();
  31.     __DSB();
  32. }

  33. static void jump_to_application(uint32_t applicationAddress, uint32_t stackPointer)
  34. {


  35.     // Create the function call to the user application.
  36.     // Static variables are needed since changed the stack pointer out from under the compiler
  37.     // we need to ensure the values we are using are not stored on the previous stack
  38.     static uint32_t s_stackPointer = 0;
  39.     s_stackPointer = stackPointer;
  40.     static void (*farewellBootloader)(void) = 0;
  41.     farewellBootloader = (void (*)(void))applicationAddress;

  42.     // Set the VTOR to the application vector table address.
  43.     SCB->VTOR = (uint32_t)VectorTableAddress;

  44.     // Set stack pointers to the application stack pointer.
  45.     __set_MSP(s_stackPointer);
  46.     __set_PSP(s_stackPointer);

  47.     // Jump to the application.
  48.     farewellBootloader();

  49. }
  50. /*******************************************************************************
  51. * Code
  52. ******************************************************************************/
  53. /*!
  54. * @brief Main function
  55. */
  56. int main(void)
  57. {
  58.     char ch;
  59.     //uint8_t *pointer_flashloader;

  60.     /* Init board hardware. */
  61.     BOARD_ConfigMPU();
  62.     BOARD_InitPins();
  63.     BOARD_InitBootClocks();
  64.     BOARD_InitDebugConsole();

  65.     PRINTF("Flashloader testing.\r\n");


  66.     // Copy flashloader image to RAM.
  67.     memcpy((void *)VectorTableAddress, FlashLoaderIAR, Flashloader_lenght);


  68.     while (1)
  69.     {
  70.         ch = GETCHAR();
  71.         PUTCHAR(ch);
  72.         bootloader_cleanup();
  73.         jump_to_application(*(uint32_t*)(VectorTableAddress+4),*(uint32_t*)VectorTableAddress);
  74.     }
  75. }
复制代码

测试工程
3.1 烧录工程代码到MIMXRT1050开发板中;

3.2 打开串口终端工具,传递一任意字符给开发板,触发应用代码跳转到Flashloader固件中运行;

3.3 在Window command窗口中敲入blhost.exe -p COM7 -- get-property 1代码尝试与 Flashloader建立连接。  

                                           

图4

(0 ) (0 )
回复 举报
  • 发表回复
    0/3000





    举报

    请选择举报类别

    • 广告垃圾
    • 违规内容
    • 恶意灌水
    • 重复发帖

    全部板块

    返回顶部