405 iap 工程结构调试

Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
2024-07-24 16:22:31 +08:00
parent b1d8ce2643
commit c1bcad0c50
8 changed files with 378 additions and 195 deletions

View File

@@ -195,7 +195,8 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8
#
# Utilities
#
# CONFIG_RT_USING_RYM is not set
CONFIG_RT_USING_RYM=y
# CONFIG_YMODEM_USING_CRC_TABLE is not set
# CONFIG_RT_USING_ULOG is not set
# CONFIG_RT_USING_UTEST is not set
# CONFIG_RT_USING_VAR_EXPORT is not set

View File

@@ -60,7 +60,7 @@ objs.extend(SConscript(os.path.join(libraries_path_prefix, stm32_library, 'SCons
# include drivers
objs.extend(SConscript(os.path.join(libraries_path_prefix, 'HAL_Drivers', 'SConscript')))
# objs.extend(SConscript(os.path.join(SDK_ROOT, 'iap', 'SConscript')))
objs.extend(SConscript(os.path.join(SDK_ROOT, 'iap', 'SConscript')))
objs.extend(SConscript(os.path.join(SDK_ROOT, 'packages', 'SConscript')))

View File

@@ -59,8 +59,17 @@ void SystemClock_Config(void)
HAL_RCC_EnableCSS();
}
//extern int iap_main_entry(void);
//INIT_ENV_EXPORT(iap_main_entry);
extern int iap_main_entry(void);
INIT_ENV_EXPORT(iap_main_entry);
#include "shell.h"
INIT_APP_EXPORT(finsh_system_init);
extern void fal(uint8_t argc, char **argv);
MSH_CMD_EXPORT(fal, FAL (Flash Abstraction Layer) operate.);
extern int fal_init(void);
INIT_COMPONENT_EXPORT(fal_init);
#include <stdlib.h>
static void reset_fc(int argc, char **argv)

View File

@@ -22,12 +22,10 @@
#define STM32_FLASH_START_ADRESS_64K (STM32_FLASH_START_ADRESS_16K + FLASH_SIZE_GRANULARITY_16K)
#define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K + FLASH_SIZE_GRANULARITY_64K)
#define FLASH_SIZE_APP (4 * 128 * 1024)
#define FLASH_SIZE_APP (6 * 128 * 1024)
#define FLASH_SIZE_PARAM (128 * 1024)
#define FLASH_SIZE_LOG (2 * 128 * 1024)
#define FLASH_OFFSET_PARAM (FLASH_SIZE_APP)
#define FLASH_OFFSET_LOG (FLASH_SIZE_APP + FLASH_SIZE_PARAM)
extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k;
@@ -45,12 +43,11 @@ extern const struct fal_flash_dev stm32_onchip_flash_128k;
/* partition table */
/**
* @brief flash ??:
* @brief flash:
* section addr size
* 0 - 4 0x08000000 128kb bootloader
* 5 - 8 0x08020000 512KB app
* 9 0x080A0000 128KB param
* 10 - 11 0x080C0000 256kb ulog
* 5 - 10 0x08020000 768KB app
* 11 0x080E0000 128KB param
*
*/
#define FAL_PART_TABLE \
@@ -58,7 +55,6 @@ extern const struct fal_flash_dev stm32_onchip_flash_128k;
{FAL_PART_MAGIC_WROD, "boot", "onchip_flash_64k", 0 , FLASH_SIZE_GRANULARITY_16K + FLASH_SIZE_GRANULARITY_64K , 0}, \
{FAL_PART_MAGIC_WROD, "app", "onchip_flash_128k", 0 , FLASH_SIZE_APP, 0}, \
{FAL_PART_MAGIC_WROD, "param", "onchip_flash_128k", FLASH_OFFSET_PARAM , FLASH_SIZE_PARAM, 0}, \
{FAL_PART_MAGIC_WROD, "ulog", "onchip_flash_128k", FLASH_OFFSET_LOG , FLASH_SIZE_LOG, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */

View File

@@ -14,33 +14,33 @@
<uAC6>0</uAC6>
<TargetOption>
<TargetCommonOption>
<Device>STM32F405RGTx</Device>
<Device>STM32F405RG</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32F4xx_DFP.2.17.0</PackID>
<PackURL>https://www.keil.com/pack/</PackURL>
<Cpu>IRAM(0x20000000,0x00020000) IRAM2(0x10000000,0x00010000) IROM(0x08000000,0x00100000) CPUTYPE("Cortex-M4") FPU2 CLOCK(12000000) ELITTLE</Cpu>
<Cpu>IRAM(0x20000000-0x2001FFFF) IRAM2(0x10000000-0x1000FFFF) IROM(0x8000000-0x80FFFFF) CLOCK(25000000) CPUTYPE("Cortex-M4") FPU2</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
<FlashDriverDll>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F4xx_1024 -FS08000000 -FL0100000 -FP0($$Device:STM32F405RGTx$CMSIS\Flash\STM32F4xx_1024.FLM))</FlashDriverDll>
<DeviceId>0</DeviceId>
<RegisterFile>$$Device:STM32F405RGTx$Drivers\CMSIS\Device\ST\STM32F4xx\Include\stm32f4xx.h</RegisterFile>
<StartupFile>"Startup\ST\STM32F4xx\startup_stm32f40_41xxx.s" ("STM32F40/41xxx Startup Code")</StartupFile>
<FlashDriverDll>UL2CM3(-O207 -S0 -C0 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F4xx_1024 -FS08000000 -FL0100000)</FlashDriverDll>
<DeviceId>6100</DeviceId>
<RegisterFile>stm32f4xx.h</RegisterFile>
<MemoryEnv></MemoryEnv>
<Cmp></Cmp>
<Asm></Asm>
<Linker></Linker>
<OHString></OHString>
<InfinionOptionDll></InfinionOptionDll>
<SLE66CMisc></SLE66CMisc>
<SLE66CMisc>-DSTM32F40_41xxx</SLE66CMisc>
<SLE66AMisc></SLE66AMisc>
<SLE66LinkerMisc></SLE66LinkerMisc>
<SFDFile>$$Device:STM32F405RGTx$CMSIS\SVD\STM32F405.svd</SFDFile>
<SFDFile>SFD\ST\STM32F4xx\STM32F40x.sfr</SFDFile>
<bCustSvd>0</bCustSvd>
<UseEnv>0</UseEnv>
<BinPath></BinPath>
<IncludePath></IncludePath>
<LibPath></LibPath>
<RegisterFilePath></RegisterFilePath>
<DBRegisterFilePath></DBRegisterFilePath>
<RegisterFilePath>ST\STM32F4xx\</RegisterFilePath>
<DBRegisterFilePath>ST\STM32F4xx\</DBRegisterFilePath>
<TargetStatus>
<Error>0</Error>
<ExitCodeStop>0</ExitCodeStop>
@@ -49,10 +49,10 @@
<InvalidFlash>1</InvalidFlash>
</TargetStatus>
<OutputDirectory>.\build\</OutputDirectory>
<OutputName>F405_RTT_iap</OutputName>
<OutputName>F405_RTT_IAP</OutputName>
<CreateExecutable>0</CreateExecutable>
<CreateLib>1</CreateLib>
<CreateHexFile>0</CreateHexFile>
<CreateHexFile>1</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>1</BrowseInformation>
<ListingPath>.\build\keil\List\</ListingPath>
@@ -110,11 +110,11 @@
</CommonProperty>
<DllOption>
<SimDllName>SARMCM3.DLL</SimDllName>
<SimDllArguments> -REMAP -MPU</SimDllArguments>
<SimDllArguments>-MPU -REMAP</SimDllArguments>
<SimDlgDll>DCM.DLL</SimDlgDll>
<SimDlgDllArguments>-pCM4</SimDlgDllArguments>
<TargetDllName>SARMCM3.DLL</TargetDllName>
<TargetDllArguments> -MPU</TargetDllArguments>
<TargetDllArguments>-MPU</TargetDllArguments>
<TargetDlgDll>TCM.DLL</TargetDlgDll>
<TargetDlgDllArguments>-pCM4</TargetDlgDllArguments>
</DllOption>
@@ -138,7 +138,7 @@
</Flash1>
<bUseTDR>1</bUseTDR>
<Flash2>BIN\UL2CM3.DLL</Flash2>
<Flash3></Flash3>
<Flash3>"" ()</Flash3>
<Flash4></Flash4>
<pFcarmOut></pFcarmOut>
<pFcarmGrp></pFcarmGrp>
@@ -183,7 +183,7 @@
<hadIRAM>1</hadIRAM>
<hadXRAM>0</hadXRAM>
<uocXRam>0</uocXRam>
<RvdsVP>2</RvdsVP>
<RvdsVP>1</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>1</hadIRAM2>
@@ -194,7 +194,7 @@
<uLtcg>0</uLtcg>
<nSecure>0</nSecure>
<RoSelD>3</RoSelD>
<RwSelD>4</RwSelD>
<RwSelD>3</RwSelD>
<CodeSel>0</CodeSel>
<OptFeed>0</OptFeed>
<NoZi1>0</NoZi1>
@@ -339,7 +339,7 @@
<MiscControls></MiscControls>
<Define>__STDC_LIMIT_MACROS, USE_HAL_DRIVER, RT_USING_LIBC, __CLK_TCK=RT_TICK_PER_SECOND, __RTTHREAD__, STM32F405xx, RT_USING_ARM_LIBC</Define>
<Undefine></Undefine>
<IncludePath>..\rt-thread\components\libc\compilers\common\extension;..\rt-thread\components\libc\posix\io\stdio;..\libraries\HAL_Drivers\CMSIS\Include;..\rt-thread\include;board;..\rt-thread\components\libc\posix\io\poll;..\rt-thread\components\drivers\include;..\rt-thread\components\drivers\include;..\libraries\STM32F4xx_HAL\CMSIS\Device\ST\STM32F4xx\Include;board\CubeMX_Config\Inc;..\rt-thread\components\drivers\include;..\libraries\HAL_Drivers\drv_flash;..\rt-thread\components\fal\inc;..\rt-thread\libcpu\arm\cortex-m4;..\libraries\HAL_Drivers\config;..\libraries\STM32F4xx_HAL\STM32F4xx_HAL_Driver\Inc;..\rt-thread\components\libc\posix\ipc;..\rt-thread\components\libc\compilers\common\include;..\rt-thread\components\libc\compilers\common\extension\fcntl\octal;..\rt-thread\components\finsh;..\rt-thread\libcpu\arm\common;..\libraries\HAL_Drivers;.</IncludePath>
<IncludePath>..\rt-thread\components\utilities\ymodem;..\rt-thread\components\libc\compilers\common\extension;..\rt-thread\components\libc\posix\io\stdio;..\libraries\HAL_Drivers\CMSIS\Include;..\rt-thread\include;board;..\iap;..\rt-thread\components\libc\posix\io\poll;..\rt-thread\components\drivers\include;..\rt-thread\components\drivers\include;..\libraries\STM32F4xx_HAL\CMSIS\Device\ST\STM32F4xx\Include;board\CubeMX_Config\Inc;..\rt-thread\components\drivers\include;..\libraries\HAL_Drivers\drv_flash;..\rt-thread\components\fal\inc;..\rt-thread\libcpu\arm\cortex-m4;..\libraries\HAL_Drivers\config;..\libraries\STM32F4xx_HAL\STM32F4xx_HAL_Driver\Inc;..\rt-thread\components\libc\posix\ipc;..\rt-thread\components\libc\compilers\common\include;..\rt-thread\components\libc\compilers\common\extension\fcntl\octal;..\rt-thread\components\finsh;..\rt-thread\libcpu\arm\common;..\libraries\HAL_Drivers;.</IncludePath>
</VariousControls>
</Cads>
<Aads>
@@ -352,7 +352,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<ClangAsOpt>1</ClangAsOpt>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@@ -549,25 +549,25 @@
<GroupName>Fal</GroupName>
<Files>
<File>
<FileName>fal.c</FileName>
<FileName>fal_rtt.c</FileName>
<FileType>1</FileType>
<FilePath>..\rt-thread\components\fal\src\fal.c</FilePath>
<FilePath>..\rt-thread\components\fal\src\fal_rtt.c</FilePath>
</File>
<File>
<FileName>fal_partition.c</FileName>
<FileType>1</FileType>
<FilePath>..\rt-thread\components\fal\src\fal_partition.c</FilePath>
</File>
<File>
<FileName>fal_rtt.c</FileName>
<FileType>1</FileType>
<FilePath>..\rt-thread\components\fal\src\fal_rtt.c</FilePath>
</File>
<File>
<FileName>fal_flash.c</FileName>
<FileType>1</FileType>
<FilePath>..\rt-thread\components\fal\src\fal_flash.c</FilePath>
</File>
<File>
<FileName>fal.c</FileName>
<FileType>1</FileType>
<FilePath>..\rt-thread\components\fal\src\fal.c</FilePath>
</File>
</Files>
</Group>
<Group>
@@ -595,6 +595,16 @@
</File>
</Files>
</Group>
<Group>
<GroupName>IAP</GroupName>
<Files>
<File>
<FileName>iap.c</FileName>
<FileType>1</FileType>
<FilePath>..\iap\iap.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>Kernel</GroupName>
<Files>
@@ -780,6 +790,16 @@
</File>
</Files>
</Group>
<Group>
<GroupName>Utilities</GroupName>
<Files>
<File>
<FileName>ymodem.c</FileName>
<FileType>1</FileType>
<FilePath>..\rt-thread\components\utilities\ymodem\ymodem.c</FilePath>
</File>
</Files>
</Group>
</Groups>
</Target>
</Targets>
@@ -790,14 +810,4 @@
<files/>
</RTE>
<LayerInfo>
<Layers>
<Layer>
<LayName>&lt;Project Info&gt;</LayName>
<LayTarg>0</LayTarg>
<LayPrjMark>1</LayPrjMark>
</Layer>
</Layers>
</LayerInfo>
</Project>

View File

@@ -104,6 +104,7 @@
/* Utilities */
#define RT_USING_RYM
/* RT-Thread online packages */

View File

@@ -10,37 +10,36 @@
<TargetName>rt-thread</TargetName>
<ToolsetNumber>0x4</ToolsetNumber>
<ToolsetName>ARM-ADS</ToolsetName>
<pCCUsed>5060750::V5.06 update 6 (build 750)::ARMCC</pCCUsed>
<uAC6>0</uAC6>
<TargetOption>
<TargetCommonOption>
<Device>STM32F405RGTx</Device>
<Device>STM32F405RG</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32F4xx_DFP.2.17.0</PackID>
<PackURL>https://www.keil.com/pack/</PackURL>
<Cpu>IRAM(0x20000000,0x00020000) IRAM2(0x10000000,0x00010000) IROM(0x08000000,0x00100000) CPUTYPE("Cortex-M4") FPU2 CLOCK(12000000) ELITTLE</Cpu>
<Cpu>IRAM(0x20000000-0x2001FFFF) IRAM2(0x10000000-0x1000FFFF) IROM(0x8000000-0x80FFFFF) CLOCK(25000000) CPUTYPE("Cortex-M4") FPU2</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
<FlashDriverDll>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F4xx_1024 -FS08000000 -FL0100000 -FP0($$Device:STM32F405RGTx$CMSIS\Flash\STM32F4xx_1024.FLM))</FlashDriverDll>
<DeviceId>0</DeviceId>
<RegisterFile>$$Device:STM32F405RGTx$Drivers\CMSIS\Device\ST\STM32F4xx\Include\stm32f4xx.h</RegisterFile>
<StartupFile>"Startup\ST\STM32F4xx\startup_stm32f40_41xxx.s" ("STM32F40/41xxx Startup Code")</StartupFile>
<FlashDriverDll>UL2CM3(-O207 -S0 -C0 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F4xx_1024 -FS08000000 -FL0100000)</FlashDriverDll>
<DeviceId>6100</DeviceId>
<RegisterFile>stm32f4xx.h</RegisterFile>
<MemoryEnv></MemoryEnv>
<Cmp></Cmp>
<Asm></Asm>
<Linker></Linker>
<OHString></OHString>
<InfinionOptionDll></InfinionOptionDll>
<SLE66CMisc></SLE66CMisc>
<SLE66CMisc>-DSTM32F40_41xxx</SLE66CMisc>
<SLE66AMisc></SLE66AMisc>
<SLE66LinkerMisc></SLE66LinkerMisc>
<SFDFile>$$Device:STM32F405RGTx$CMSIS\SVD\STM32F405.svd</SFDFile>
<SFDFile>SFD\ST\STM32F4xx\STM32F40x.sfr</SFDFile>
<bCustSvd>0</bCustSvd>
<UseEnv>0</UseEnv>
<BinPath></BinPath>
<IncludePath></IncludePath>
<LibPath></LibPath>
<RegisterFilePath></RegisterFilePath>
<DBRegisterFilePath></DBRegisterFilePath>
<RegisterFilePath>ST\STM32F4xx\</RegisterFilePath>
<DBRegisterFilePath>ST\STM32F4xx\</DBRegisterFilePath>
<TargetStatus>
<Error>0</Error>
<ExitCodeStop>0</ExitCodeStop>
@@ -49,10 +48,10 @@
<InvalidFlash>1</InvalidFlash>
</TargetStatus>
<OutputDirectory>.\build\</OutputDirectory>
<OutputName>F405_RTT_iap</OutputName>
<OutputName>F405_RTT_IAP</OutputName>
<CreateExecutable>0</CreateExecutable>
<CreateLib>1</CreateLib>
<CreateHexFile>0</CreateHexFile>
<CreateHexFile>1</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>1</BrowseInformation>
<ListingPath>.\build\keil\List\</ListingPath>
@@ -110,11 +109,11 @@
</CommonProperty>
<DllOption>
<SimDllName>SARMCM3.DLL</SimDllName>
<SimDllArguments> -REMAP -MPU</SimDllArguments>
<SimDllArguments>-MPU -REMAP</SimDllArguments>
<SimDlgDll>DCM.DLL</SimDlgDll>
<SimDlgDllArguments>-pCM4</SimDlgDllArguments>
<TargetDllName>SARMCM3.DLL</TargetDllName>
<TargetDllArguments> -MPU</TargetDllArguments>
<TargetDllArguments>-MPU</TargetDllArguments>
<TargetDlgDll>TCM.DLL</TargetDlgDll>
<TargetDlgDllArguments>-pCM4</TargetDlgDllArguments>
</DllOption>
@@ -138,7 +137,7 @@
</Flash1>
<bUseTDR>1</bUseTDR>
<Flash2>BIN\UL2CM3.DLL</Flash2>
<Flash3></Flash3>
<Flash3>"" ()</Flash3>
<Flash4></Flash4>
<pFcarmOut></pFcarmOut>
<pFcarmGrp></pFcarmGrp>
@@ -183,7 +182,7 @@
<hadIRAM>1</hadIRAM>
<hadXRAM>0</hadXRAM>
<uocXRam>0</uocXRam>
<RvdsVP>2</RvdsVP>
<RvdsVP>1</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>1</hadIRAM2>
@@ -194,7 +193,7 @@
<uLtcg>0</uLtcg>
<nSecure>0</nSecure>
<RoSelD>3</RoSelD>
<RwSelD>4</RwSelD>
<RwSelD>3</RwSelD>
<CodeSel>0</CodeSel>
<OptFeed>0</OptFeed>
<NoZi1>0</NoZi1>
@@ -337,9 +336,9 @@
<v6Rtti>0</v6Rtti>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
<Define>STM32F405xx, USE_HAL_DRIVER</Define>
<Undefine></Undefine>
<IncludePath></IncludePath>
<IncludePath>.;..\..\..\include;applications;.;board;board\CubeMX_Config\Inc;..\libraries\HAL_Drivers;..\libraries\HAL_Drivers\config;packages\led;..\..\..\libcpu\arm\common;..\..\..\libcpu\arm\cortex-m4;..\..\..\components\drivers\include;..\..\..\components\drivers\include;..\..\..\components\drivers\include;..\..\..\components\finsh;..\libraries\STM32F4xx_HAL\STM32F4xx_HAL_Driver\Inc;..\libraries\STM32F4xx_HAL\CMSIS\Device\ST\STM32F4xx\Include;..\libraries\STM32F4xx_HAL\CMSIS\Include</IncludePath>
</VariousControls>
</Cads>
<Aads>
@@ -352,7 +351,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<ClangAsOpt>1</ClangAsOpt>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@@ -373,17 +372,12 @@
<ScatterFile>.\board\linker_scripts\link.sct</ScatterFile>
<IncludeLibs></IncludeLibs>
<IncludeLibsPath></IncludeLibsPath>
<Misc></Misc>
<Misc>--keep *.o(.rti_fn.*) --keep *.o(FSymTab)</Misc>
<LinkerInputFile></LinkerInputFile>
<DisabledWarnings></DisabledWarnings>
</LDads>
</TargetArmAds>
</TargetOption>
<Groups>
<Group>
<GroupName>Source Group 1</GroupName>
</Group>
</Groups>
</Target>
</Targets>
@@ -393,14 +387,4 @@
<files/>
</RTE>
<LayerInfo>
<Layers>
<Layer>
<LayName>&lt;Project Info&gt;</LayName>
<LayTarg>0</LayTarg>
<LayPrjMark>1</LayPrjMark>
</Layer>
</Layers>
</LayerInfo>
</Project>

402
iap/iap.c
View File

@@ -1,13 +1,46 @@
#include "rtthread.h"
#include "ipc/completion.h"
#include "ymodem.h"
#include <stdint.h>
#include <stddef.h>
#include "fal.h"
#define IAP_FLASH_APP_SEC "app"
#define IAP_FLASH_BOOT_SEC "boot"
static rt_device_t console_dev;
struct rt_completion _wait;
#define IFLASH_PAGE_SIZE (128 * 1024)
// #define IAP_DBG
#include "rthw.h"
#ifdef IAP_DBG
char iap_dbg_log[4096] = "";
int iap_dbg_index = 0;
#endif
void iap_dbg(const char *fmt, ...)
{
#ifdef IAP_DBG
va_list args;
rt_base_t level = rt_hw_interrupt_disable();
va_start(args, fmt);
iap_dbg_index += rt_vsnprintf(&iap_dbg_log[iap_dbg_index], sizeof(iap_dbg_log) - iap_dbg_index, fmt, args);
va_end(args);
rt_hw_interrupt_enable(level);
#endif
}
static struct rt_completion _wait;
rt_err_t port_rx_ind(rt_device_t dev, rt_size_t size)
{
rt_completion_done(&_wait);
return RT_EOK;
}
#define IS_AF(c) ((c >= 'A') && (c <= 'F'))
#define IS_af(c) ((c >= 'a') && (c <= 'f'))
@@ -109,64 +142,122 @@ rt_inline rt_uint32_t Str2Int(uint8_t *inputstr, int32_t *intnum)
return res;
}
rt_err_t port_rx_ind(rt_device_t dev, rt_size_t size)
static const char *port_list[] = {
"uart1",
"uart2",
};
#define PORTS_NUM (sizeof(port_list)/sizeof(port_list[0]))
void rt_hw_shell_relink(const char *name, rt_uint32_t baud)
{
rt_completion_done(&_wait);
return RT_EOK;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = baud;
rt_device_t dev = rt_device_find(name);
RT_ASSERT(dev);
rt_device_control(dev, RT_DEVICE_CTRL_CONFIG, &config);
rt_console_set_device(name);
rt_thread_t tid = rt_thread_find(FINSH_THREAD_NAME);
if (tid)
{
rt_thread_resume(tid);
rt_schedule();
}
}
static rt_device_t _read_ports(rt_device_t ports[], rt_uint8_t want)
{
rt_uint8_t tmp;
for (int i = 0; i < PORTS_NUM; i++)
{
if (ports[i] == RT_NULL)
continue;
if (rt_device_read(ports[i], 0, &tmp, 1) != 1)
continue;
if (tmp == want)
return ports[i];
}
return RT_NULL;
}
static void _init_all_ports(rt_device_t ports[])
{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_460800;
for (int i = 0; i < PORTS_NUM; i++)
{
ports[i] = rt_device_find(port_list[i]);
if (ports[i] == RT_NULL)
continue;
rt_enter_critical();
ports[i]->rx_indicate = port_rx_ind;
rt_exit_critical();
rt_device_control(ports[i], RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(ports[i], RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
}
}
static void _close_other_ports(rt_device_t ports[], rt_device_t console_dev)
{
for (int i = 0; i < PORTS_NUM; i++)
{
if (ports[i] == RT_NULL)
continue;
if (ports[i] == console_dev)
continue;
rt_device_close(ports[i]);
ports[i] = RT_NULL;
}
}
static void _print_ports(rt_device_t ports[], const char *fmt, ...)
{
va_list args;
char line_buf[128] = { 0 };
rt_size_t real;
va_start(args, fmt);
real = rt_vsnprintf(line_buf, sizeof(line_buf), fmt, args);
va_end(args);
for (int i = 0; i < PORTS_NUM; i++)
{
if (ports[i] == RT_NULL)
continue;
rt_device_write(ports[i], 0, line_buf, real);
}
}
static const char version_header[] = {0xB5,0x5B,0xff,'\0'};
static const char Firmware_Version[] = "\r\nXY_F405_IAP_460800_FV2.0_R20240724";
static const char version_end[] = {0xBB,'\0'};
static void iap_show_version_string(rt_device_t ports[])
{
char iap_version[100]= {'\0'};
strcat(iap_version,version_header);
strcat(iap_version,Firmware_Version);
strcat(iap_version,version_end);
iap_version[2] = strlen(iap_version);
_print_ports(ports, "%s\n", iap_version);
}
#define RT_BUF_SIZE (1024)
/*
* 固件格式:
*
* 作用 & 说明
* - 记录固件对应硬件 & 版本信息
* - 简便起见,不包含校验信息
* - 允许 iap 升级不含 header 的普通固件
*
* index len desc
* 0 4 'XYFC'
* 4 4 固件信息长度
* 8 N 固件信息 ('类型'-'版本')
* 8+N M 固件数据
*/
static const char FIXED_HEAD[] = {'X', 'Y', 'F', 'C'};
extern const char IAP_BIN_PREFIX[];
static int _check_head(const rt_uint8_t *buf, rt_size_t len)
{
if (len < 8)
{
return 0;
}
for (int i = 0; i < sizeof(FIXED_HEAD); i++)
{
if (FIXED_HEAD[i] != buf[i])
{
return 0;
}
}
rt_uint32_t real = *((const rt_uint32_t *)&buf[4]);
rt_uint32_t want = rt_strlen(IAP_BIN_PREFIX);
if (real <= want || len - 8 < real)
{
return -3;
}
for(int i = 0; i < want; i++)
{
if (IAP_BIN_PREFIX[i] != buf[8 + i])
{
return -4;
}
}
return real + 8;
}
struct custom_ctx {
struct rym_ctx parent;
int32_t file_size;
@@ -181,8 +272,6 @@ static enum rym_code _rym_recv_begin(
rt_uint8_t *buf,
rt_size_t len)
{
// return RYM_ERR_ACK;
// return RYM_CODE_CAN;
struct custom_ctx *cctx = (struct custom_ctx *) ctx;
RT_ASSERT(cctx);
@@ -234,15 +323,8 @@ static enum rym_code _rym_recv_data(
RT_ASSERT(cctx);
RT_ASSERT(len <= RT_BUF_SIZE);
int offset = 0;
if (cctx->recevied_head == 0)
{
offset = _check_head(buf, len);
if (offset < 0)
{
return RYM_ERR_FILE;
}
if (fal_partition_erase(cctx->flash, 0, cctx->file_size) < 0)
{
return RYM_CODE_CAN;
@@ -261,22 +343,22 @@ static enum rym_code _rym_recv_data(
len = cctx->file_size - cctx->w_index;
}
if (fal_partition_write(cctx->flash, cctx->w_index, &buf[offset], len - offset) <= 0)
if (fal_partition_write(cctx->flash, cctx->w_index, buf, len) <= 0)
{
return RYM_CODE_CAN;
}
if (fal_partition_read(cctx->flash, cctx->w_index, cctx->ck_buffer, len - offset) <= 0)
if (fal_partition_read(cctx->flash, cctx->w_index, cctx->ck_buffer, len) <= 0)
{
return RYM_CODE_CAN;
}
for (int i = 0; i < len - offset; i++)
for (int i = 0; i < len; i++)
{
if (cctx->ck_buffer[i] != buf[i + offset])
if (cctx->ck_buffer[i] != buf[i])
return RYM_CODE_CAN;
}
cctx->w_index += len - offset;
cctx->w_index += len;
return RYM_CODE_ACK;
}
@@ -321,26 +403,20 @@ static const rt_uint8_t *_pack_cmd(enum iap_v5_cmd cmd)
#define _response_cmd(cmd) \
rt_device_write(console_dev, 0, _pack_cmd(cmd), IAP_V5_CMD_SIZE)
static rt_bool_t iap_v5_req_char(rt_device_t console_dev,
static rt_device_t iap_v5_req_char(rt_device_t ports[],
struct rt_completion *_wait,
rt_uint8_t want,
rt_tick_t wait)
{
rt_uint8_t ch;
rt_tick_t start = rt_tick_get();
rt_bool_t rt = RT_FALSE;
rt_device_t get = RT_NULL;
while (rt_tick_get() - start < wait)
{
if (rt_device_read(console_dev, 0, &ch, 1) == 1)
get = _read_ports(ports, want);
if (get != RT_NULL)
{
if (ch == want)
{
rt = RT_TRUE;
break;
}
continue;
break;
}
if (rt_completion_wait(_wait, wait - (rt_tick_get() - start))
@@ -350,7 +426,7 @@ static rt_bool_t iap_v5_req_char(rt_device_t console_dev,
}
}
return rt;
return get;
}
static void jump_to_app(void);
@@ -362,9 +438,57 @@ int iap_main_entry(void)
{
return _iap_entry(RT_TRUE);
}
// INIT_APP_EXPORT(iap_main_entry);
#include "shell.h"
static void _jump_to_app_entry(void *p)
{
rt_thread_delay(500);
rt_kprintf("jump to app ...\n");
jump_to_app();
}
void _on_iap_end(rt_device_t console_dev)
{
rt_device_close(console_dev);
rt_hw_shell_relink(console_dev->parent.name, BAUD_RATE_460800);
}
static rt_bool_t _handle_connect(rt_device_t console_dev, rt_tick_t timeout)
{
rt_int32_t cnt_c = 0;
rt_uint8_t tmp;
rt_tick_t start = rt_tick_get();
while (rt_tick_get() - start < timeout)
{
if (rt_device_read(console_dev, 0, &tmp, 1) != 1)
{
if (rt_completion_wait(&_wait, timeout - (rt_tick_get() - start)) != RT_EOK)
break;
continue;
}
if (tmp == '1')
{
_response_cmd(IAP_V5_CMD_ONE_REPLY);
continue;
}
if (tmp == 'c')
{
cnt_c ++;
_response_cmd(IAP_V5_CMD_C_REPLY);
break;
}
}
return cnt_c > 0;
}
static int _iap_entry(rt_bool_t is_boot)
{
if (IapAppAddr && is_boot)
@@ -372,42 +496,50 @@ static int _iap_entry(rt_bool_t is_boot)
return 0;
}
if ((!IapAppAddr) && is_boot)
{
finsh_set_prompt("msh (IAP)");
}
rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size);
rt_device_t _ports[PORTS_NUM] = {0};
rt_err_t err = RT_EOK;
struct custom_ctx *ctx = RT_NULL;
console_dev = rt_console_get_device();
rt_device_t console_dev = rt_console_get_device();
RT_ASSERT(console_dev);
rt_completion_init(&_wait);
odev_rx_ind = console_dev->rx_indicate;
rt_device_set_rx_indicate(console_dev, port_rx_ind);
if (!iap_v5_req_char(console_dev, &_wait, '1', is_boot? 1000: 100000))
// 使用特殊设备暂时替代 console 口, 避免逻辑出错
rt_hw_shell_relink("block_r", BAUD_RATE_460800);
rt_thread_delay(200);
_init_all_ports(_ports);
iap_show_version_string(_ports);
rt_device_t get_port = iap_v5_req_char(_ports, &_wait, '1', is_boot? 1000: 100000);
if (get_port == RT_NULL)
{
_close_other_ports(_ports, console_dev);
goto _exit;
}
if (get_port != console_dev)
{
console_dev = get_port;
}
_close_other_ports(_ports, console_dev);
_response_cmd(IAP_V5_CMD_ONE_REPLY);
rt_thread_mdelay(200);
// if (!iap_v5_req_char(console_dev, &_wait, 'c', 1000))
// {
// err = RT_ENOSYS;
// goto _exit;
// }
_response_cmd(IAP_V5_CMD_C_REPLY);
if (!_handle_connect(console_dev, 1000))
{
err = RT_ERROR;
iap_dbg("_handle_connect failed\n");
goto _exit;
}
ctx = rt_calloc(1, sizeof(*ctx));
if (!ctx)
{
err = RT_ENOMEM;
rt_kprintf("rt_malloc failed\n");
iap_dbg("rt_malloc failed\n");
goto _exit;
}
ctx->parent.v5_iap_adapt = RT_TRUE;
@@ -416,7 +548,7 @@ static int _iap_entry(rt_bool_t is_boot)
if (ctx->flash == RT_NULL)
{
err = RT_ENOSYS;
rt_kprintf("fal_partition_find %s failed: %d\n", IapAppAddr? IAP_FLASH_BOOT_SEC: IAP_FLASH_APP_SEC, rt_get_errno());
iap_dbg("fal_partition_find %s failed: %d\n", IapAppAddr? IAP_FLASH_BOOT_SEC: IAP_FLASH_APP_SEC, rt_get_errno());
rt_free(ctx);
goto _exit;
}
@@ -430,25 +562,25 @@ static int _iap_entry(rt_bool_t is_boot)
if (err == RT_EOK)
{
_response_cmd(IAP_V5_CMD_UPDATE_SUCCEED);
_on_iap_end(console_dev);
return 0;
}
_exit:
if (err == RT_EOK && is_boot)
{
rt_kprintf("jump to app ...\n");
jump_to_app();
rt_thread_startup(rt_thread_create("iap_boot", _jump_to_app_entry, RT_NULL, 2048, 10, 10));
_on_iap_end(console_dev);
return 0;
}
_response_cmd(IAP_V5_CMD_UPDATE_FAILED);
_response_cmd(IAP_V5_CMD_POWER_ON_AGAIN);
rt_device_set_rx_indicate(console_dev, odev_rx_ind);
_on_iap_end(console_dev);
return 0;
}
#include <stm32f4xx.h>
#include <stdint.h>
#define FLASH_RUN_BASEADDR 0x08020000
@@ -458,13 +590,13 @@ typedef void (*pFunction)(void);
static pFunction JumpToApplication;
static uint32_t JumpAddress;
static void jump_to_app(void)
void jump_to_app(void)
{
SysTick->CTRL = 0; //关键代码
SysTick->CTRL = 0; //关键代码
HAL_DeInit(); //可选
HAL_NVIC_DisableIRQ(SysTick_IRQn); //可选
HAL_NVIC_ClearPendingIRQ(SysTick_IRQn); //可选
HAL_DeInit();
HAL_NVIC_DisableIRQ(SysTick_IRQn);
HAL_NVIC_ClearPendingIRQ(SysTick_IRQn);
/* Jump to user application */
__disable_irq();
@@ -475,6 +607,10 @@ static void jump_to_app(void)
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) FLASH_RUN_BASEADDR);
__set_CONTROL(0);
__enable_irq();
JumpToApplication();
}
MSH_CMD_EXPORT(jump_to_app, jump_to_app);
@@ -484,3 +620,49 @@ static void _cmd_iap(void)
_iap_entry(RT_FALSE);
}
MSH_CMD_EXPORT_ALIAS(_cmd_iap, iap, iap bootloader / app start);
static void shell_relink(int argc, char **argv)
{
if (argc < 2)
{
rt_kprintf("%s prt_name\n", argv[0]);
return;
}
rt_hw_shell_relink(argv[1], BAUD_RATE_460800);
}
MSH_CMD_EXPORT(shell_relink, relink msh to other port);
#ifdef IAP_DBG
void iap_log()
{
int index = 0;
for (int i = 0; i < iap_dbg_index; i++)
{
if (iap_dbg_log[i] == '\n' || iap_dbg_log[i] == '\0')
{
iap_dbg_log[i] = '\0';
rt_kprintf("log: %s\n", &iap_dbg_log[index]);
index = i+1;
}
}
}
MSH_CMD_EXPORT(iap_log, iap log);
#endif
static struct rt_device blocked_reader_ = {0};
static rt_size_t _blocked_reader_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
return 0;
}
int init_block_reader()
{
blocked_reader_.read = _blocked_reader_read;
rt_device_register(&blocked_reader_, "block_r", RT_DEVICE_FLAG_RDWR);
return 0;
}
INIT_BOARD_EXPORT(init_block_reader);