From b966e3d7d233624b1c181a16127989e442697904 Mon Sep 17 00:00:00 2001 From: zjli-2019 Date: Wed, 9 Dec 2020 13:52:36 +0800 Subject: [PATCH] v5.1.1: * Library: * [NEW] api in `eflash` module for create FOTA metadata * Examples: * [UPD] "Thermoeter with FOTA", "Secondary FOTA" use new api --- .../peripheral_pedometer.uvoptx | 2 +- .../peripheral_pedometer.uvprojx | 105 +++++++++-------- examples/secondary_fota/secondary_fota.uvoptx | 20 +++- .../secondary_fota/secondary_fota.uvprojx | 108 +++++++++-------- examples/secondary_fota/src/ota_service.c | 21 ++++ examples/smart_meter/smart_meter.uvprojx | 105 +++++++++-------- examples/thermo_ota/thermo_ota.uvoptx | 45 +++++-- examples/thermo_ota/thermo_ota.uvprojx | 110 ++++++++++-------- src/BSP/eflash.c | 47 ++++++++ src/BSP/eflash.h | 17 +++ src/Tools/ota_service.c | 34 ++++-- src/Tools/ota_service.h | 11 ++ 12 files changed, 403 insertions(+), 222 deletions(-) diff --git a/examples/peripheral_pedometer/peripheral_pedometer.uvoptx b/examples/peripheral_pedometer/peripheral_pedometer.uvoptx index a6363edfd..3dafb61b2 100644 --- a/examples/peripheral_pedometer/peripheral_pedometer.uvoptx +++ b/examples/peripheral_pedometer/peripheral_pedometer.uvoptx @@ -265,7 +265,7 @@ 0 0 0 - ..\..\bundles\typical\ING9188xx\symdefs.m + ..\..\bundles\typical\ING9185xx\symdefs.m symdefs.m 0 0 diff --git a/examples/peripheral_pedometer/peripheral_pedometer.uvprojx b/examples/peripheral_pedometer/peripheral_pedometer.uvprojx index 588e34e74..147be8d3b 100644 --- a/examples/peripheral_pedometer/peripheral_pedometer.uvprojx +++ b/examples/peripheral_pedometer/peripheral_pedometer.uvprojx @@ -1,7 +1,10 @@ - + + 2.1 +
### uVision Project, (C) Keil Software
+ Target 1 @@ -15,28 +18,28 @@ INGChips.INGCHIPS_DeviceFamilyPack.0.9.0 http://www.ingchips.com CLOCK(32000000) CPUTYPE("Cortex-M3") ELITTLE IROM(0x4000-0x43fff) IRAM(0x20000000-0x2000ffff) - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:ING91800$SVD\ING91800.svd 0 0 - - - - - + + + + + 0 0 @@ -58,8 +61,8 @@ 0 0 - - + + 0 0 0 @@ -68,8 +71,8 @@ 0 0 - - + + 0 0 0 @@ -79,14 +82,14 @@ 1 0 fromelf.exe --bin -o peripheral_pedometer.bin ./output/peripheral_pedometer.axf - + 0 0 0 0 0 - + 0 @@ -100,8 +103,8 @@ 0 0 3 - - + + 1 @@ -135,10 +138,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -171,7 +174,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -269,8 +272,8 @@ 1 - 147456 - 131072 + 0x24000 + 0x20000 1 @@ -294,8 +297,8 @@ 0 - 536894776 - 8904 + 0x20005d38 + 0x22c8 0 @@ -303,7 +306,7 @@ 0x0 - + 1 @@ -331,7 +334,7 @@ --gnu USE_STDPERIPH_DRIVER - + ../../bundles/typical/inc;../../bundles/typical/inc/os;../../bundles/typical/inc/os/portable/RVDS/ARM_CM3;../../src/BSP;../../src/FWlib;../../src/StartUP;../../src/Tools;../../src @@ -347,10 +350,10 @@ 0 0 - - - - + + + + @@ -362,13 +365,13 @@ 0 0x00000000 0x00000000 - - - - - - - + + + + + + + @@ -467,11 +470,12 @@ + - + @@ -479,4 +483,5 @@ +
diff --git a/examples/secondary_fota/secondary_fota.uvoptx b/examples/secondary_fota/secondary_fota.uvoptx index db9a2f14c..7ab8a68d8 100644 --- a/examples/secondary_fota/secondary_fota.uvoptx +++ b/examples/secondary_fota/secondary_fota.uvoptx @@ -218,7 +218,7 @@ 1 3 1 - 1 + 0 0 0 .\src\ota_service.c @@ -253,7 +253,7 @@ 0 0 0 - ..\..\bundles\typical\ING9188xx\symdefs.m + ..\..\bundles\typical\ING9185xx\symdefs.m symdefs.m 0 0 @@ -266,6 +266,18 @@ 0 0 0 + + 3 + 6 + 1 + 0 + 0 + 0 + ..\..\src\BSP\eflash.c + eflash.c + 0 + 0 + @@ -276,7 +288,7 @@ 0 4 - 6 + 7 1 0 0 @@ -288,7 +300,7 @@ 4 - 7 + 8 1 0 0 diff --git a/examples/secondary_fota/secondary_fota.uvprojx b/examples/secondary_fota/secondary_fota.uvprojx index 6c787edd8..bc1d01835 100644 --- a/examples/secondary_fota/secondary_fota.uvprojx +++ b/examples/secondary_fota/secondary_fota.uvprojx @@ -1,7 +1,10 @@ - + + 2.1 +
### uVision Project, (C) Keil Software
+ Target 1 @@ -15,28 +18,28 @@ INGChips.INGCHIPS_DeviceFamilyPack.0.9.0 http://www.ingchips.com CLOCK(32000000) CPUTYPE("Cortex-M3") ELITTLE IROM(0x4000-0x43fff) IRAM(0x20000000-0x2000ffff) - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:ING91800$SVD\ING91800.svd 0 0 - - - - - + + + + + 0 0 @@ -58,8 +61,8 @@ 0 0 - - + + 0 0 0 @@ -68,8 +71,8 @@ 0 0 - - + + 0 0 0 @@ -79,14 +82,14 @@ 1 0 fromelf.exe --bin -o secondary_fota.bin ./output/secondary_fota.axf - + 0 0 0 0 0 - + 0 @@ -100,8 +103,8 @@ 0 0 3 - - + + 1 @@ -135,10 +138,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -171,7 +174,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -294,8 +297,8 @@ 0 - 536894776 - 8904 + 0x20005d38 + 0x22c8 0 @@ -303,7 +306,7 @@ 0x0 - + 1 @@ -331,7 +334,7 @@ --gnu USE_STDPERIPH_DRIVER - + ../../bundles/typical/inc;../../bundles/typical/inc/os;../../bundles/typical/inc/os/portable/RVDS/ARM_CM3;../../src/BSP;../../src/FWlib;../../src/StartUP;../../src/Tools;../../src @@ -347,10 +350,10 @@ 0 0 - - - - + + + + @@ -362,13 +365,13 @@ 0 0x00000000 0x00000000 - - - - - - - + + + + + + + @@ -410,6 +413,13 @@
BSP + + + eflash.c + 1 + ..\..\src\BSP\eflash.c + + FWlib @@ -435,11 +445,12 @@ + - + @@ -447,4 +458,5 @@ + diff --git a/examples/secondary_fota/src/ota_service.c b/examples/secondary_fota/src/ota_service.c index 32608a022..36c7563f1 100644 --- a/examples/secondary_fota/src/ota_service.c +++ b/examples/secondary_fota/src/ota_service.c @@ -129,6 +129,27 @@ int ota_write_callback(uint16_t att_handle, uint16_t transaction_mode, uint16_t ota_ctrl[0] = OTA_STATUS_OK; } break; + case OTA_CTRL_METADATA: + if (OTA_STATUS_OK != ota_ctrl[0]) + break; + if ((0 == ota_downloading) || (buffer_size < 1 + sizeof(ota_meta_t))) + { + const ota_meta_t *meta = (const ota_meta_t *)(buffer + 1); + int s = buffer_size - 1; + if (crc((uint8_t *)&meta->entry, s - sizeof(meta->crc_value)) != meta->crc_value) + { + ota_ctrl[0] = OTA_STATUS_ERROR; + break; + } + program_fota_metadata(meta->entry, + (s - sizeof(ota_meta_t)) / sizeof(meta->blocks[0]), + meta->blocks); + } + else + { + ota_ctrl[0] = OTA_STATUS_ERROR; + } + break; case OTA_CTRL_REBOOT: if (OTA_STATUS_OK == ota_ctrl[0]) { diff --git a/examples/smart_meter/smart_meter.uvprojx b/examples/smart_meter/smart_meter.uvprojx index aa5f7e9da..4887b6a23 100644 --- a/examples/smart_meter/smart_meter.uvprojx +++ b/examples/smart_meter/smart_meter.uvprojx @@ -1,7 +1,10 @@ - + + 2.1 +
### uVision Project, (C) Keil Software
+ Target 1 @@ -15,28 +18,28 @@ INGChips.INGCHIPS_DeviceFamilyPack.0.9.0 http://www.ingchips.com CLOCK(32000000) CPUTYPE("Cortex-M3") ELITTLE IROM(0x4000-0x43fff) IRAM(0x20000000-0x2000ffff) - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:ING91800$SVD\ING91800.svd 0 0 - - - - - + + + + + 0 0 @@ -58,8 +61,8 @@ 0 0 - - + + 0 0 0 @@ -68,8 +71,8 @@ 0 0 - - + + 0 0 0 @@ -79,14 +82,14 @@ 1 0 fromelf.exe --bin -o smart_meter.bin ./output/smart_meter.axf - + 0 0 0 0 0 - + 0 @@ -100,8 +103,8 @@ 0 0 3 - - + + 1 @@ -135,10 +138,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -171,7 +174,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -269,8 +272,8 @@ 1 - 155648 - 385024 + 0x26000 + 0x5e000 1 @@ -294,8 +297,8 @@ 0 - 536902272 - 34176 + 0x20007a80 + 0x8580 0 @@ -303,7 +306,7 @@ 0x0 - + 1 @@ -331,7 +334,7 @@ --gnu USE_STDPERIPH_DRIVER - + ../../bundles/typical/inc;../../bundles/typical/inc/os;../../bundles/typical/inc/os/portable/RVDS/ARM_CM3;../../src/BSP;../../src/FWlib;../../src/StartUP;../../src/Tools;../../src @@ -347,10 +350,10 @@ 0 0 - - - - + + + + @@ -362,13 +365,13 @@ 0 0x00000000 0x00000000 - - - - - - - + + + + + + + @@ -457,11 +460,12 @@ + - + @@ -469,4 +473,5 @@ +
diff --git a/examples/thermo_ota/thermo_ota.uvoptx b/examples/thermo_ota/thermo_ota.uvoptx index d4deb5285..b0f486ff7 100644 --- a/examples/thermo_ota/thermo_ota.uvoptx +++ b/examples/thermo_ota/thermo_ota.uvoptx @@ -145,12 +145,29 @@ UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0NEW_DEVICE -FS00 -FL040000 -FP0($$Device:ARMCM3$Device\ARM\Flash\NEW_DEVICE.FLM)) - + + + 0 + 0 + 168 + 1 +
152704
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\..\src\Tools\ota_service.c + + \\thermo_ota\../../src/Tools/ota_service.c\168 +
+
1 2 - 0x40040164 + 0x87f00 0 @@ -286,6 +303,18 @@ 0 0 + + 3 + 7 + 1 + 0 + 0 + 0 + ..\..\src\BSP\eflash.c + eflash.c + 0 + 0 +
@@ -296,7 +325,7 @@ 0 4 - 7 + 8 1 0 0 @@ -308,7 +337,7 @@ 4 - 8 + 9 1 0 0 @@ -320,7 +349,7 @@ 4 - 9 + 10 1 0 0 @@ -332,7 +361,7 @@ 4 - 10 + 11 1 0 0 @@ -352,7 +381,7 @@ 0 5 - 11 + 12 1 0 0 @@ -364,7 +393,7 @@ 5 - 12 + 13 1 0 0 diff --git a/examples/thermo_ota/thermo_ota.uvprojx b/examples/thermo_ota/thermo_ota.uvprojx index 094ffbc7f..d20321aa0 100644 --- a/examples/thermo_ota/thermo_ota.uvprojx +++ b/examples/thermo_ota/thermo_ota.uvprojx @@ -1,7 +1,10 @@ - + + 2.1 +
### uVision Project, (C) Keil Software
+ Target 1 @@ -15,28 +18,28 @@ INGChips.INGCHIPS_DeviceFamilyPack.0.9.0 http://www.ingchips.com CLOCK(32000000) CPUTYPE("Cortex-M3") ELITTLE IROM(0x4000-0x43fff) IRAM(0x20000000-0x2000ffff) - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:ING91800$SVD\ING91800.svd 0 0 - - - - - + + + + + 0 0 @@ -58,8 +61,8 @@ 0 0 - - + + 0 0 0 @@ -68,8 +71,8 @@ 0 0 - - + + 0 0 0 @@ -79,14 +82,14 @@ 1 0 fromelf.exe --bin -o thermo_ota.bin ./output/thermo_ota.axf - + 0 0 0 0 0 - + 0 @@ -100,8 +103,8 @@ 0 0 3 - - + + 1 @@ -135,10 +138,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -171,7 +174,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -269,8 +272,8 @@ 1 - 147456 - 131072 + 0x24000 + 0x20000 1 @@ -294,8 +297,8 @@ 0 - 536894776 - 8904 + 0x20005d38 + 0x22c8 0 @@ -303,7 +306,7 @@ 0x0 - + 1 @@ -331,7 +334,7 @@ --gnu USE_STDPERIPH_DRIVER - + ../../bundles/typical/inc;../../bundles/typical/inc/os;../../bundles/typical/inc/os/portable/RVDS/ARM_CM3;../../src/BSP;../../src/FWlib;../../src/StartUP;../../src/Tools;../../src @@ -347,10 +350,10 @@ 0 0 - - - - + + + + @@ -362,13 +365,13 @@ 0 0x00000000 0x00000000 - - - - - - - + + + + + + + @@ -416,6 +419,11 @@ 1 ..\..\src\BSP\bme280.c
+ + eflash.c + 1 + ..\..\src\BSP\eflash.c +
@@ -464,11 +472,12 @@ + - + @@ -476,4 +485,5 @@ + diff --git a/src/BSP/eflash.c b/src/BSP/eflash.c index deecd5ef8..9f00946ba 100644 --- a/src/BSP/eflash.c +++ b/src/BSP/eflash.c @@ -1,4 +1,5 @@ #include +#include static uint32_t ClkFreq; //0:16M 1:24M @@ -81,3 +82,49 @@ int write_flash(const uint32_t dest_addr, const uint8_t *buffer, uint32_t size) return program_flash0(dest_addr, buffer, size, 0); } +int program_fota_metadata(const uint32_t entry, const int block_num, const fota_update_block_t *blocks) +{ +#define START (EFLASH_BASE + EFLASH_SIZE + 2 * PAGE_SIZE) +#define DEF_UPDATE_FLAG (0x5A5A5A5A) + + uint32_t backup[4]; + uint32_t addr = START - 4; + int i; + + init(); + memcpy(backup, (void *)(START - sizeof(backup)), sizeof(backup)); + if (entry) + { + backup[1] = entry; + backup[2] = DEF_UPDATE_FLAG; + } + + EflashProgramEnable(); + + *(volatile uint32_t *)(0xc40a0) = 0x4; + EraseEFlashPage(1); + *(volatile uint32_t *)(0xc40a0) = 0x0; + + for (i = sizeof(backup) / sizeof(backup[0]) - 1; i >= 0; i--) + { + EflashProgram(addr, backup[i]); + addr -= 4; + } + + for (i = 0; i < block_num; i++) + { + EflashProgram(addr, DEF_UPDATE_FLAG); + addr -= 4; + EflashProgram(addr, blocks[i].src); + addr -= 4; + EflashProgram(addr, blocks[i].dest); + addr -= 4; + EflashProgram(addr, blocks[i].size); + addr -= 4; + } + + EflashProgramDisable(); + uninit(); + return 0; +} + diff --git a/src/BSP/eflash.h b/src/BSP/eflash.h index a46f8d9af..f8ab0dce5 100644 --- a/src/BSP/eflash.h +++ b/src/BSP/eflash.h @@ -28,6 +28,23 @@ int program_flash(const uint32_t dest_addr, const uint8_t *buffer, uint32_t size */ int write_flash(const uint32_t dest_addr, const uint8_t *buffer, uint32_t size); +typedef struct fota_update_block +{ + uint32_t src; + uint32_t dest; + uint32_t size; +} fota_update_block_t; + +/** + * @brief Program FOTA metadata. + * + * @param[in] entry new entry address (0 if use old entry address) + * @param[in] block_num number of blocks + * @param[in] blocks an array of `fota_update_block_t` + * @return 0 if successful else non-0 + */ +int program_fota_metadata(const uint32_t entry, const int block_num, const fota_update_block_t *blocks); + #ifdef __cplusplus } #endif diff --git a/src/Tools/ota_service.c b/src/Tools/ota_service.c index b40cb069a..1006b674b 100644 --- a/src/Tools/ota_service.c +++ b/src/Tools/ota_service.c @@ -116,7 +116,7 @@ int ota_write_callback(uint16_t att_handle, uint16_t transaction_mode, uint16_t { case OTA_CTRL_PAGE_BEGIN: ota_addr = *(uint32_t *)(buffer + 1); - if (ota_addr & 0x3) + if ((ota_addr & 0x3) || (ota_addr >= EFLASH_END)) { ota_ctrl[0] = OTA_STATUS_ERROR; return 0; @@ -124,16 +124,7 @@ int ota_write_callback(uint16_t att_handle, uint16_t transaction_mode, uint16_t else ota_ctrl[0] = OTA_STATUS_OK; EflashProgramEnable(); - if (ota_addr >= EFLASH_END) - { - *(volatile uint32_t *)(0xc40a0) = 0x4; - EraseEFlashPage(ota_addr >= EFLASH_END + PAGE_SIZE ? 1 : 0); - *(volatile uint32_t *)(0xc40a0) = 0x0; - } - else - { - EraseEFlashPage(((ota_addr - EFLASH_BASE) >> 13) & 0x3f); - } + EraseEFlashPage(((ota_addr - EFLASH_BASE) >> 13) & 0x3f); ota_downloading = 1; ota_start_addr = ota_addr; break; @@ -169,6 +160,27 @@ int ota_write_callback(uint16_t att_handle, uint16_t transaction_mode, uint16_t case OTA_CTRL_SWITCH_APP: platform_switch_app(SEC_FOTA_APP_ADDR); break; + case OTA_CTRL_METADATA: + if (OTA_STATUS_OK != ota_ctrl[0]) + break; + if ((0 == ota_downloading) || (buffer_size < 1 + sizeof(ota_meta_t))) + { + const ota_meta_t *meta = (const ota_meta_t *)(buffer + 1); + int s = buffer_size - 1; + if (crc((uint8_t *)&meta->entry, s - sizeof(meta->crc_value)) != meta->crc_value) + { + ota_ctrl[0] = OTA_STATUS_ERROR; + break; + } + program_fota_metadata(meta->entry, + (s - sizeof(ota_meta_t)) / sizeof(meta->blocks[0]), + meta->blocks); + } + else + { + ota_ctrl[0] = OTA_STATUS_ERROR; + } + break; case OTA_CTRL_REBOOT: if (OTA_STATUS_OK == ota_ctrl[0]) { diff --git a/src/Tools/ota_service.h b/src/Tools/ota_service.h index 12dfddc83..b5cb67838 100644 --- a/src/Tools/ota_service.h +++ b/src/Tools/ota_service.h @@ -9,6 +9,7 @@ #define _ota_service_h #include +#include "eflash.h" #ifdef __cplusplus extern "C" { @@ -45,11 +46,21 @@ typedef struct ota_ver prog_ver_t app; } ota_ver_t; +#pragma pack (push, 1) +typedef struct ota_meta +{ + uint16_t crc_value; + uint32_t entry; + fota_update_block_t blocks[0]; +} ota_meta_t; +#pragma pack (pop) + #define OTA_CTRL_START 0xAA // param: no #define OTA_CTRL_PAGE_BEGIN 0xB0 // param: page address (32 bit), following DATA contains the data #define OTA_CTRL_PAGE_END 0xB1 // param: size (16bit), crc (16bit) #define OTA_CTRL_READ_PAGE 0xC0 // param: page address, following DATA reading contains the data #define OTA_CTRL_SWITCH_APP 0xD0 // param: no +#define OTA_CTRL_METADATA 0xE0 // param: ota_meta_t #define OTA_CTRL_REBOOT 0xFF // param: no #define OTA_STATUS_DISABLED 0