首页 论坛 原厂专区 NXP(freescale) MCU Flashloader 应用案例
分享到:
在痞子衡嵌入式博客中,《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.2 打开TXT文件,利用文档工具中Replace功能将TXT中的空格转换为逗号。
1.3 生成包含Flashloader固件的C数组
通过下方的Python代码将TXT文件内容转换成包含Flashloader固件的C数组,生成结果如图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 添加代码,建立完善工程
具体代码如下
复制代码
测试工程
3.1 烧录工程代码到MIMXRT1050开发板中;
但要实现上述Flashloader应用的前提是让RT 系列MCU进入Serial Downloader模式,所以就有客户想实现在应用程序中运行Flashloader,当如何实现呢??
如《Serial Downloader模式(sdphost/MfgTool)》的介绍,在运行Flashloader之前需要将其代码加载到SRAM内,如下所示。这也给我们了启示,首先,在应用程序中将拷贝Flashloader固件到SRAM内,然后跳转运行Flashloader固件。
- 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所示。
-
## 生成包含Flashloader固件的C数组
-
fout_update = open('flashloader_Update.c','w',encoding='utf-8')
-
fout_update.write('const uint8_t FlashLoaderIAR[] = {'+'\n')
-
with open('flashloader.txt',) as fout:
-
for lines in fout.readlines():
-
lines_list = lines.strip().split(',')
-
print('The len is {} '.format(len(lines_list)))
-
for i in range(len(lines_list)):
-
lines_list[i]= "{}{}".format('0x',lines_list[i])
-
fout_update.write(lines_list[i])
-
fout_update.write(',')
-
print('The order {} is {}'.format(i,lines_list[i]))
-
fout_update.write('\n')
-
fout_update.write('}'+'\n')
- 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 添加代码,建立完善工程
具体代码如下
-
/*
-
* Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
-
* Copyright 2016-2017 NXP
-
* All rights reserved.
-
*
-
* SPDX-License-Identifier: BSD-3-Clause
-
*/
-
-
#include "fsl_device_registers.h"
-
#include "fsl_debug_console.h"
-
#include "board.h"
-
#include "flash_loader.h"
-
-
#include "pin_mux.h"
-
#include "clock_config.h"
-
/*******************************************************************************
-
* Definitions
-
******************************************************************************/
-
#define VectorTableAddress 0x20002000
-
#define Flashloader_lenght 82142
-
-
#define kDefaultVectorTableAddress 0
-
/*******************************************************************************
-
* Prototypes
-
******************************************************************************/
-
-
-
void bootloader_cleanup()
-
{
-
// Turn off interrupts.
-
__disable_irq();
-
-
// Set the VTOR to default.
-
-
SCB->VTOR = kDefaultVectorTableAddress;
-
-
// Memory barriers for good measure.
-
__ISB();
-
__DSB();
-
}
-
-
static void jump_to_application(uint32_t applicationAddress, uint32_t stackPointer)
-
{
-
-
-
// Create the function call to the user application.
-
// Static variables are needed since changed the stack pointer out from under the compiler
-
// we need to ensure the values we are using are not stored on the previous stack
-
static uint32_t s_stackPointer = 0;
-
s_stackPointer = stackPointer;
-
static void (*farewellBootloader)(void) = 0;
-
farewellBootloader = (void (*)(void))applicationAddress;
-
-
// Set the VTOR to the application vector table address.
-
SCB->VTOR = (uint32_t)VectorTableAddress;
-
-
// Set stack pointers to the application stack pointer.
-
__set_MSP(s_stackPointer);
-
__set_PSP(s_stackPointer);
-
-
// Jump to the application.
-
farewellBootloader();
-
-
}
-
/*******************************************************************************
-
* Code
-
******************************************************************************/
-
/*!
-
* @brief Main function
-
*/
-
int main(void)
-
{
-
char ch;
-
//uint8_t *pointer_flashloader;
-
-
/* Init board hardware. */
-
BOARD_ConfigMPU();
-
BOARD_InitPins();
-
BOARD_InitBootClocks();
-
BOARD_InitDebugConsole();
-
-
PRINTF("Flashloader testing.\r\n");
-
-
-
// Copy flashloader image to RAM.
-
memcpy((void *)VectorTableAddress, FlashLoaderIAR, Flashloader_lenght);
-
-
-
while (1)
-
{
-
ch = GETCHAR();
-
PUTCHAR(ch);
-
bootloader_cleanup();
-
jump_to_application(*(uint32_t*)(VectorTableAddress+4),*(uint32_t*)VectorTableAddress);
-
}
- }
测试工程
3.1 烧录工程代码到MIMXRT1050开发板中;
3.2 打开串口终端工具,传递一任意字符给开发板,触发应用代码跳转到Flashloader固件中运行;
3.3 在Window command窗口中敲入blhost.exe -p COM7 -- get-property 1代码尝试与 Flashloader建立连接。
图4
(0 )
(0 )
回复
举报
发表回复
块
导
航
举报
请选择举报类别
- 广告垃圾
- 违规内容
- 恶意灌水
- 重复发帖