diff --git a/bthome_phy6222/Makefile b/bthome_phy6222/Makefile index 10626f0..c3810e7 100644 --- a/bthome_phy6222/Makefile +++ b/bthome_phy6222/Makefile @@ -5,9 +5,7 @@ PROJECT_NAME ?= bthome_phy6222 COM_PORT = COM11 ############################################################################## # Source - SRC_PATH = ./source - SRC_PRJ = main.c SRC_PRJ += battery.c SRC_PRJ += battservice.c @@ -23,18 +21,47 @@ SRC_PRJ += thb2_main.c SRC_PRJ += thb2_peripheral.c SRC_PRJ += thservice.c SRC_PRJ += flash_eep.c +SRC_PRJ += ble_ota.c INCLUDES = -I$(SRC_PATH) SRCS = $(addprefix $(SRC_PATH)/, $(SRC_PRJ)) - +############################################################################## +DEFINES = -D__GCC +DEFINES += $(POJECT_DEF) +DEFINES += -DDEBUG_INFO=0 +DEFINES += -DMTU_SIZE=247 +DEFINES += -DCFG_SLEEP_MODE=PWR_MODE_SLEEP +DEFINES += -DADV_NCONN_CFG=0x01 +DEFINES += -DADV_CONN_CFG=0x02 +DEFINES += -DSCAN_CFG=0x04 +DEFINES += -DINIT_CFG=0x08 +DEFINES += -DBROADCASTER_CFG=0x01 +DEFINES += -DOBSERVER_CFG=0x02 +DEFINES += -DPERIPHERAL_CFG=0x04 +DEFINES += -DCENTRAL_CFG=0x08 +DEFINES += -DHOST_CONFIG=0x4 +DEFINES += -DARMCM0 +DEFINES += -D_RTE_ +DEFINES += -DCFG_CP +DEFINES += -DOSAL_CBTIMER_NUM_TASKS=1 +DEFINES += -DHCI_TL_NONE=1 +DEFINES += -DENABLE_LOG_ROM_=0 +DEFINES += -D_OBJ_DIR_FOR_DTM_=0 +DEFINES += -DDBG_ROM_MAIN=0 +DEFINES += -DAPP_CFG=0 +DEFINES += -DOSALMEM_METRICS=0 +DEFINES += -DPHY_MCU_TYPE=MCU_BUMBEE_M0 +DEFINES += -DDEF_GAPBOND_MGR_ENABLE=0 +DEFINES += -DUSE_FS=0 +DEFINES += -DMAX_NUM_LL_CONN=1 +#DEFINES += -DXFLASH_HIGH_SPEED=1 ############################################################################## BIN_DIR = ./bin OBJ_DIR = ./build SDK_PATH = ./SDK -GCC_PATH = PYTHON = python3 - +GCC_PATH = CC = $(GCC_PATH)arm-none-eabi-gcc OBJCOPY = $(GCC_PATH)arm-none-eabi-objcopy OBJDUMP = $(GCC_PATH)arm-none-eabi-objdump @@ -46,17 +73,19 @@ OPT_CFLAGS ?= -Os DEB_CFLAGS ?= -g3 -ggdb ############################################################################## ASFLAGS := $(ARCH_FLAGS) $(OPT_CFLAGS) $(DEB_CFLAGS) - CFLAGS := $(ARCH_FLAGS) $(OPT_CFLAGS) $(DEB_CFLAGS) CFLAGS += -W -Wall --std=gnu99 CFLAGS += -fno-diagnostics-show-caret CFLAGS += -fdata-sections -ffunction-sections CFLAGS += -funsigned-char -funsigned-bitfields +#CFLAGS += -fpack-struct +CFLAGS += -fms-extensions CFLAGS += -specs=nosys.specs CFLAGS += -Wl,--gc-sections #CFLAGS += -MM $(CFLAGS) $(INCFLAGS) $< -MT $@ -MF $(OBJ_DIR)/$(patsubst %.o,%.d,$@) -LDSCRIPT ?= $(SDK_PATH)/misc/phy6222.ld +#LDSCRIPT ?= $(SDK_PATH)/misc/phy6222.ld +LDSCRIPT ?= $(SDK_PATH)/misc/ota_phy62x2.ld LDFLAGS := $(ARCH_FLAGS) LDFLAGS += --static -nostartfiles -nostdlib LDFLAGS += -Wl,--gc-sections @@ -65,8 +94,8 @@ LDFLAGS += -Wl,--script=$(LDSCRIPT) LDFLAGS += -Wl,--just-symbols=$(SDK_PATH)/misc/bb_rom_sym_m0.gcc LDFLAGS += -Wl,-Map=$(OBJ_DIR)/$(PROJECT_NAME).map LIBS += -Wl,--start-group -lgcc -lnosys -Wl,--end-group -############################################################################## +############################################################################## INCLUDES += -I$(SDK_PATH)/misc INCLUDES += -I$(SDK_PATH)/misc/CMSIS/include INCLUDES += -I$(SDK_PATH)/components/arch/cm0 @@ -106,7 +135,6 @@ INCLUDES += -I$(SDK_PATH)/components/libraries/cliface #INCLUDES += -I$(SDK_PATH)/components/libraries/fs INCLUDES += -I$(SDK_PATH)/components/driver/watchdog - ########################################### # LIBs SRCS += $(SDK_PATH)/lib/rf/patch.c @@ -187,40 +215,8 @@ STARTUP_ASM = $(SDK_PATH)/misc/CMSIS/device/phyplus/phy6222_start.s SRCS += $(SDK_PATH)/misc/CMSIS/device/phyplus/phy6222_cstart.c SRCS += $(SDK_PATH)/misc/CMSIS/device/phyplus/phy6222_vectors.c -STARTUP_OBJ = - ############################################################################## -DEFINES = -D__GCC -DEFINES += $(POJECT_DEF) -DEFINES += -DDEBUG_INFO=0 -DEFINES += -DMTU_SIZE=247 -DEFINES += -DCFG_SLEEP_MODE=PWR_MODE_SLEEP -DEFINES += -DADV_NCONN_CFG=0x01 -DEFINES += -DADV_CONN_CFG=0x02 -DEFINES += -DSCAN_CFG=0x04 -DEFINES += -DINIT_CFG=0x08 -DEFINES += -DBROADCASTER_CFG=0x01 -DEFINES += -DOBSERVER_CFG=0x02 -DEFINES += -DPERIPHERAL_CFG=0x04 -DEFINES += -DCENTRAL_CFG=0x08 -DEFINES += -DHOST_CONFIG=0x4 -DEFINES += -DARMCM0 -DEFINES += -D_RTE_ -DEFINES += -DCFG_CP -DEFINES += -DOSAL_CBTIMER_NUM_TASKS=1 -DEFINES += -DHCI_TL_NONE=1 -DEFINES += -DENABLE_LOG_ROM_=0 -DEFINES += -D_OBJ_DIR_FOR_DTM_=0 -DEFINES += -DDBG_ROM_MAIN=0 -DEFINES += -DAPP_CFG=0 -DEFINES += -DOSALMEM_METRICS=0 -DEFINES += -DPHY_MCU_TYPE=MCU_BUMBEE_M0 -DEFINES += -DDEF_GAPBOND_MGR_ENABLE=0 -DEFINES += -DUSE_FS=0 -DEFINES += -DMAX_NUM_LL_CONN=1 -#DEFINES += -DXFLASH_HIGH_SPEED=1 - CFLAGS += $(DEFINES) $(INCLUDES) #SRC_O = $(patsubst %.c,%.o,$(patsubst sdk/%, $(SDK_PATH)%, $(SRCS))) @@ -251,7 +247,6 @@ all: directory $(SRC_O) $(OBJ_DIR)/$(PROJECT_NAME).elf $(OBJ_DIR)/$(PROJECT_NAME @echo OBJDUMP: $@ @$(OBJDUMP) -s -S $^ >$@ - %.o : %.c @echo CC: $< @mkdir -p $(OBJ_DIR)/$(dir $@) diff --git a/bthome_phy6222/SDK/lib/rf/patch.c b/bthome_phy6222/SDK/lib/rf/patch.c index a58f3e3..5cc91fa 100644 --- a/bthome_phy6222/SDK/lib/rf/patch.c +++ b/bthome_phy6222/SDK/lib/rf/patch.c @@ -54,7 +54,7 @@ //#endif #ifndef USE_CODED_PHY -#define USE_CODED_PHY 1 +#define USE_CODED_PHY 0 #endif #define DBG_BUILD_LL_TIMING 0 //0x01 for enable LL timing debug diff --git a/bthome_phy6222/SDK/misc/ota_phy62x2.ld b/bthome_phy6222/SDK/misc/ota_phy62x2.ld new file mode 100644 index 0000000..b54f5d7 --- /dev/null +++ b/bthome_phy6222/SDK/misc/ota_phy62x2.ld @@ -0,0 +1,146 @@ +/* SRAM + XIP linker script */ +/* https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/6n33n7fds/index.html */ +PHDRS +{ + romdata PT_LOAD FLAGS(6); + text PT_LOAD FLAGS(5); + data PT_LOAD FLAGS(6); + xip PT_LOAD FLAGS(5); + rodata PT_LOAD FLAGS(4); +} + +MEMORY +{ + jumptbl (rw) : ORIGIN = 0x1fff0000, LENGTH = 0x00400 + gcfgtbl (rw) : ORIGIN = 0x1fff0400, LENGTH = 0x00400 + sram (rwx) : ORIGIN = 0x1fff1838, LENGTH = 0x0E7C8 + flash (rx) : ORIGIN = 0x11003000, LENGTH = 0x0F000 + sram2 (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 + sram3 (rwx) : ORIGIN = 0x20010000, LENGTH = 0x02000 + sram4 (rwx) : ORIGIN = 0x20012000, LENGTH = 0x00800 +} + +OUTPUT_ARCH(arm) +EXTERN(_vectors) +ENTRY(__start) + +SECTIONS +{ + .jumptbl : { + *(jump_table_mem_area) + } > jumptbl : romdata + + .gcfgtbl (NOLOAD) : { + *(global_config_area) + } > gcfgtbl + + .textentry : { + *(*.isr_vector) + } > sram : text + + _sdata = ABSOLUTE(.); + .text : { + _stextram = ABSOLUTE(.); + + *phy6222_start.o(.text) + *.o(_section_standby_code_) + *.o(_section_sram_code_) + + *patch.o(.text.*) + *patch_ext_adv.o(.text.*) + + *rf_phy_driver.o(.text.*) + *pwrmgr.o(.text .text.*) + *timer.o(.text .text.*) + + *flash.o(.text .text.*) + *clock.o(.text.*) + + *phy_sec_ext.o(.text .text.*) + + *libgcc.a:*.o(.text .text.*) + + _etextram = ABSOLUTE(.); + } > sram : text + + .data : { + *(.data .data.*) + *(.gnu.linkonce.d.*) + CONSTRUCTORS + } > sram : data + . = ALIGN(4); + _edata = ABSOLUTE(.); + + .bss : { + _sbss = ABSOLUTE(.); + *(.bss .bss.*) + *(.gnu.linkonce.b.*) + *(COMMON) + . = ALIGN(4); + _ebss = ABSOLUTE(.); + } > sram + + .irq_stack : { + *(g_irqstack_base) + } > sram + + g_stack = ORIGIN(sram) + LENGTH(sram); + + .xip : { + _stext = ABSOLUTE(.); + + *.o(_func_xip_code_) + *.o(_section_xip_code_) + *(.text .text.*) + + _etext = ABSOLUTE(.); + } > flash : xip + + .rodata : { + *(.rodata .rodata.*) + *(.fixup) + *(.gnu.warning) + *(.rodata .rodata.*) + *(.gnu.linkonce.t.*) + *(.glue_7) + *(.glue_7t) + *(.got) + *(.gcc_except_table) + *(.gnu.linkonce.r.*) + } > flash : rodata + + .init_section : { + _sinit = ABSOLUTE(.); + *(.init_array .init_array.*) + _einit = ABSOLUTE(.); + } > flash : rodata + + .ARM.extab : { + *(.ARM.extab*) + } > flash : rodata + + .ARM.exidx : { + __exidx_start = ABSOLUTE(.); + *(.ARM.exidx*) + __exidx_end = ABSOLUTE(.); + } > flash : rodata + + ._sjtblsstore : { + _sjtblss = ABSOLUTE(.); + } > flash : rodata + + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } + +} diff --git a/bthome_phy6222/source/ble_ota.c b/bthome_phy6222/source/ble_ota.c index 666680a..b1b99fd 100644 --- a/bthome_phy6222/source/ble_ota.c +++ b/bthome_phy6222/source/ble_ota.c @@ -4,25 +4,48 @@ ******************************************************************************/ #include "bcomdef.h" #include "config.h" +#if OTA_TYPE #include "OSAL.h" #include "sbp_profile.h" #include "flash.h" #include "ble_ota.h" -struct { - uint8_t err_flag; - uint8_t version; - uint8_t start_flag; - uint8_t reboot_flag; - uint32_t program_offset; - uint16_t pkt_index; - uint16_t pkt_total; - uint32_t fw_value; - uint32_t crc32; - uint32_t erase_addr; -} ota; - +/******************************************************************************* +* CONSTANTS */ #define ota_timeout_us 30000; // default 30 second +/******************************************************************************* +* Prototypes */ +typedef struct _app_info_t { + uint32_t flag; + uint32_t seg_count; + uint32_t start_addr; + uint32_t app_size; +} app_info_t; + +typedef struct _app_info_seg_t { + uint32_t faddr; + uint32_t size; + uint32_t saddr; + uint32_t chk; +} app_info_seg_t; + +/******************************************************************************* +* LOCAL VARIABLES */ +ota_par_t ota = { + .err_flag = OTA_SUCCESS, + .version = 1, + .start_flag = 0, + .reboot_flag = 0, + .program_offset = FADDR_APP_SEC, + .pkt_index = -1, + .fw_value = START_UP_FLAG, +}; +/********************************************************************* +* EXTERNAL VARIABLES */ + + +/********************************************************************* +* LOCAL FUNCTION */ unsigned short crc16(unsigned char *pD, int len) { static unsigned short poly[2] = { 0, 0xa001 }; //0x8005 <==> 0xa001 @@ -68,11 +91,12 @@ uint32_t get_crc32_16bytes(unsigned int crc_init, unsigned char *data) { } -void ota_reload_timer(void) { +void ota_timer(void) { } -int ota_parse(unsigned char *pmsg, unsigned int msg_size) { + +int ota_parser(unsigned char *pout, unsigned char *pmsg, unsigned int msg_size) { uint32_t tmp; uint16_t crc; uint16_t ota_adr = pmsg[0] | (pmsg[1] << 8); @@ -82,7 +106,13 @@ int ota_parse(unsigned char *pmsg, unsigned int msg_size) { // ota_reload_imer(); if (ota_adr >= CMD_OTA_START) { if (ota_adr == CMD_OTA_START) { - ota.erase_addr = FADDR_START_ADDR; + if(msg_size == 2 + 4) { + ota.erase_addr = (pmsg[2] + | (pmsg[3] << 8) + | (pmsg[4] << 16) + | (pmsg[5] << 24)); + } else + ota.erase_addr = FADDR_APP_SEC-1; ota.fw_value = START_UP_FLAG; ota.start_flag = 0; ota.err_flag = OTA_SUCCESS; @@ -110,6 +140,7 @@ int ota_parse(unsigned char *pmsg, unsigned int msg_size) { err_flg = OTA_PKT_SIZE_ERR; // size error } else if (ota_adr == CMD_OTA_END) { //@TODO go to reboot or start app + //ota_timer() //terminateConnection(0x13); //timer(reboot) } else @@ -156,11 +187,13 @@ int ota_parse(unsigned char *pmsg, unsigned int msg_size) { ota.erase_addr = tmp; hal_flash_erase_sector(tmp); } - if (ota_adr == 0) - hal_flash_write(ota.program_offset + (ota_adr << 4) + 4, - pmsg + 6, 12); - else - hal_flash_write(ota.program_offset + (ota_adr << 4), + if (ota_adr == 0) { + pmsg[2] = 0xff; + pmsg[3] = 0xff; + pmsg[4] = 0xff; + pmsg[5] = 0xff; + } + hal_flash_write(ota.program_offset + (ota_adr << 4), pmsg + 2, 16); hal_flash_read(ota.program_offset + (ota_adr << 4), flash_check, 16); @@ -184,7 +217,86 @@ int ota_parse(unsigned char *pmsg, unsigned int msg_size) { err_flg = OTA_PKT_SIZE_ERR; // size error if (err_flg) { ota.err_flag = err_flg; - //@TODO send/Notify? + //send/Notify? + osal_memcpy(pout, &ota, 20); + return 20; } return 0; } + +__ATTR_SECTION_XIP__ +static uint32_t start_app(void) { + app_info_t info_app; + app_info_seg_t info_seg; + uint32_t info_seg_faddr = FADDR_APP_SEC; + + spif_read(info_seg_faddr, (uint8_t*)&info_app, sizeof(info_app)); + if(info_app.flag == START_UP_FLAG) { +/* Move OTA not released! + if(info_app.app_size != 0xFFFFFFFF) { + info_app.app_size += FLASH_SECTOR_SIZE-1; + info_app.app_size &= ~(FLASH_SECTOR_SIZE-1); + info_app.app_size += FADDR_START_ADDR; + spif_read(info_app.app_size, (uint8_t*)&info_app.flag, sizeof(info_app.flag)); + if(info_app.flag == START_UP_FLAG) + move_ota_app(info_app.app_size); + } +*/ + if(info_app.seg_count <= 16) { + while(info_app.seg_count) { + info_seg_faddr += sizeof(info_app); + spif_read(info_seg_faddr, (uint8_t*)&info_seg, sizeof(info_seg)); + if(info_app.start_addr == 0xffffffff) // если не назначен + info_app.start_addr = info_seg.saddr; // берется первый сегмент + info_seg.faddr += FADDR_START_ADDR; + info_seg.size &= 0x000fffff; + if (info_seg.saddr != info_seg.faddr // не XIP + && info_seg.size < (128*1024)) { // < 128k + osal_memcpy((void *)info_seg.saddr, (void *)info_seg.faddr, info_seg.size); + } + info_app.seg_count--; + } + } + if(info_app.start_addr == 0xffffffff) { + info_app.start_addr = 0; + } + } else + info_app.start_addr = 0; + return info_app.start_addr; +} + +#if defined ( __CC_ARM ) +__asm void __attribute__((section("ota_app_loader_area"))) jump2app(uint32_t entry) +{ + LDR R0, = __APP_RUN_ADDR__ + LDR R1, [R0, #4] + BX R1 + ALIGN +} +#elif defined ( __GNUC__ ) +__ATTR_SECTION_XIP__ +void jump2app(uint32_t entry) +{ + __ASM volatile("ldr r0, %0\n\t" + "ldr r1, [r0, #4]\n\t" + "bx r1" + :"+m"(entry) + ); +} +#endif + +__ATTR_SECTION_XIP__ +void startup_ota(void) { + uint32_t start_addr; + + HAL_ENTER_CRITICAL_SECTION(); + + start_addr = start_app(); + if(start_addr) { + //AP_PCR->CACHE_BYPASS = 1; // bypass cache + jump2app(start_addr); + } +} + +#endif // OTA_TYPE + diff --git a/bthome_phy6222/source/ble_ota.h b/bthome_phy6222/source/ble_ota.h index 80b591a..08e3392 100644 --- a/bthome_phy6222/source/ble_ota.h +++ b/bthome_phy6222/source/ble_ota.h @@ -6,22 +6,24 @@ #ifndef BLE_OTA_H_ #define BLE_OTA_H_ +#if OTA_TYPE /* FLASH */ +#ifndef FLASH_SIZE #define FLASH_SIZE 0x80000 // 512k (512*1024) +#endif #define FLASH_MAX_SIZE 0x200000 // 2M (2048*1024) +#ifndef FLASH_SECTOR_SIZE #define FLASH_SECTOR_SIZE 0x01000 // 4k (4*1024) +#endif #define FADDR_START_ADDR 0x11000000 #define FADDR_BOOT_ROM_INFO (FADDR_START_ADDR + 0x02000) // 4k -#define FADDR_BOOT_OTA (FADDR_START_ADDR + 0x03000) // 4k -#define FADDR_APP_INFO (FADDR_START_ADDR + 0x04000) // 4k -#define FADDR_OTA_SEC (FADDR_START_ADDR + 0x05000) // 44k -#define FADDR_APP_SEC (FADDR_START_ADDR + 0x10000) // 236k (2x118k) -#define FADDR_DATA_SEC (FADDR_START_ADDR + 0x40000) // 248k -#define FADDR_EEP_SEC (FADDR_START_ADDR + (FLASH_SIZE - 2*FLASH_SECTOR_SIZE)) - -#define START_UP_FLAG 0x12345678 +#define FADDR_OTA_SEC (FADDR_START_ADDR + 0x03000) // 60k +#define FADDR_APP_SEC (FADDR_START_ADDR + 0x12000) // +//#define FADDR_DATA_SEC (FADDR_START_ADDR + 0x40000) +#define FADDR_EEP_SEC (FADDR_START_ADDR + (FLASH_SIZE - 4*FLASH_SECTOR_SIZE)) +#define START_UP_FLAG 0x36594850 // "PHY6" #define CMD_OTA_START 0xff00 #define CMD_OTA_SET 0xff01 @@ -45,6 +47,27 @@ enum { OTA_END = 0xff }; -int ota_parse(unsigned char *pmsg, unsigned int msg_size); +typedef struct _ota_par_t { + uint8_t err_flag; + uint8_t version; + uint8_t start_flag; + uint8_t reboot_flag; + uint32_t program_offset; + uint16_t pkt_index; + uint16_t pkt_total; + uint32_t fw_value; + uint32_t crc32; + uint32_t erase_addr; +} ota_par_t; + +extern ota_par_t ota; + +int ota_parser(unsigned char *pout, unsigned char *pmsg, unsigned int msg_size); + +#if OTA_TYPE == OTA_TYPE_BOOT +void startup_ota(void); +#endif + +#endif // OTA_TYPE #endif /* BLE_OTA_H_ */ diff --git a/bthome_phy6222/source/bleperipheral.h b/bthome_phy6222/source/bleperipheral.h index c18a190..a03f41f 100644 --- a/bthome_phy6222/source/bleperipheral.h +++ b/bthome_phy6222/source/bleperipheral.h @@ -45,10 +45,11 @@ extern "C" // Simple BLE Peripheral Task Events #define SBP_START_DEVICE_EVT 0x0001 // start #define SBP_RESET_ADV_EVT 0x0002 // enable adv (from gaprole_start) -#define SBP_DEALDATA 0x0004 // receive command data +#define SBP_CMDDATA 0x0004 // receive command data #define TIMER_BATT_EVT 0x0008 // for battery detect #define BATT_VALUE_EVT 0x0010 // Event for battery voltage value update #define ADV_BROADCAST_EVT 0x0020 // Advent. Event Done Notice +#define SBP_OTADATA 0x0040 // receive OTA data /********************************************************************* * MACROS diff --git a/bthome_phy6222/source/config.h b/bthome_phy6222/source/config.h index 2c667d6..027577f 100644 --- a/bthome_phy6222/source/config.h +++ b/bthome_phy6222/source/config.h @@ -37,7 +37,15 @@ #define DEVICE_TH05 21 #ifndef DEVICE -#define DEVICE DEVICE_BTH01 +#define DEVICE DEVICE_THB2 +#endif + +#define OTA_TYPE_NONE 0 +#define OTA_TYPE_BOOT 1 +#define OTA_TYPE_APP 2 + +#ifndef OTA_TYPE +#define OTA_TYPE OTA_TYPE_BOOT #endif #define DEF_SOFTWARE_REVISION {'V', '0'+ (APP_VERSION >> 4), '.' , '0'+ (APP_VERSION & 0x0F), 0} @@ -82,6 +90,7 @@ #error "DEVICE Not released!" #endif +#define OTA_MODE_SELECT_REG 0x4000f034 // == 0x55 -> OTA // Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled #define DEFAULT_DESIRED_MIN_CONN_INTERVAL 24 // 12 -> 15 ms diff --git a/bthome_phy6222/source/main.c b/bthome_phy6222/source/main.c index 39c2a11..8bee942 100644 --- a/bthome_phy6222/source/main.c +++ b/bthome_phy6222/source/main.c @@ -19,6 +19,8 @@ #include "version.h" #include "watchdog.h" #include "adc.h" +#include "ble_ota.h" + #define DEFAULT_UART_BAUD 115200 /********************************************************************* @@ -288,6 +290,16 @@ int main(void) { osal_memcpy((void*) 0x1fff0000, (void*) jump_table_base, 1024); #endif +#if defined(OTA_TYPE) && OTA_TYPE == OTA_TYPE_BOOT + if (read_reg(OTA_MODE_SELECT_REG) != 0x55 + && hal_gpio_read(GPIO_KEY) ) { + spif_config(SYS_CLK_DLL_64M, 1, XFRD_FCMD_READ_DUAL, 0, 0); + AP_PCR->CACHE_BYPASS = 1; // just bypass cache + startup_ota(); + } + write_reg(OTA_MODE_SELECT_REG, 0); +#endif + watchdog_config(WDG_2S); // spif_config(SYS_CLK_DLL_64M, 1, XFRD_FCMD_READ_DUAL, 0, 0); diff --git a/bthome_phy6222/source/sbp_profile.c b/bthome_phy6222/source/sbp_profile.c index 88cebce..f750af1 100644 --- a/bthome_phy6222/source/sbp_profile.c +++ b/bthome_phy6222/source/sbp_profile.c @@ -23,6 +23,7 @@ #include "bleperipheral.h" #include "sbp_profile.h" #include "cmd_parcer.h" +#include "ble_ota.h" /********************************************************************* * MACROS @@ -44,12 +45,13 @@ CONST uint8_t simpleProfileServUUID[ATT_BT_UUID_SIZE] = LO_UINT16(SIMPLEPROFILE_SERV_UUID), HI_UINT16(SIMPLEPROFILE_SERV_UUID) }; +#if OTA_TYPE // Characteristic 1 UUID: 0xFFF3 CONST uint8_t simpleProfilechar1UUID[ATT_BT_UUID_SIZE] = { LO_UINT16(SIMPLEPROFILE_CHAR1_UUID), HI_UINT16(SIMPLEPROFILE_CHAR1_UUID) }; - +#endif // Characteristic 2 UUID: 0xFFF4 CONST uint8_t simpleProfilechar2UUID[ATT_BT_UUID_SIZE] = { @@ -78,7 +80,7 @@ static simpleProfileCBs_t *simpleProfile_AppCBs = NULL; // Simple Profile Service attribute 0xFFF0 static CONST gattAttrType_t simpleProfileService = { ATT_BT_UUID_SIZE, simpleProfileServUUID }; - +#if OTA_TYPE // Simple Profile Characteristic 1 Properties static CONST uint8_t simpleProfileChar1Props = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP; //static CONST uint8_t simpleProfileChar1UserDesp[] = "OTA\0"; // Simple Profile Characteristic 1 User Description @@ -86,6 +88,8 @@ static CONST uint8_t simpleProfileChar1Props = GATT_PROP_READ | GATT_PROP_WRIT static uint8_t ota_in_buffer[20]; // Characteristic 1 Value static uint8_t ota_in_len; +#endif + // Simple Profile Characteristic 2 Properties static CONST uint8_t simpleProfileChar2Props = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP | GATT_PROP_NOTIFY; //static CONST uint8_t simpleProfileChar2UserDesp[] = "CMD\0"; // Simple Profile Characteristic 2 User Description @@ -97,10 +101,14 @@ static uint8_t cmd_in_len; // Characteristic 2 Value /********************************************************************* * Profile Attributes - Table */ - +#if OTA_TYPE #define SERVAPP_NUM_ATTR_SUPPORTED 6 #define OTA_DATA_ATTR_IDX 2 // Position of OTA in attribute array #define CDM_DATA_ATTR_IDX 4 // Position of CMD in attribute array +#else +#define SERVAPP_NUM_ATTR_SUPPORTED 4 +#define CDM_DATA_ATTR_IDX 2 // Position of CMD in attribute array +#endif static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] = { @@ -111,7 +119,7 @@ static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] = 0, /* handle */ (uint8_t *)&simpleProfileService /* pValue */ }, - +#if OTA_TYPE // Characteristic 1 Declaration { { ATT_BT_UUID_SIZE, characterUUID }, @@ -135,6 +143,7 @@ static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] = (uint8_t *)simpleProfileChar1UserDesp }, #endif +#endif // OTA_TYPE // Characteristic 2 Declaration { { ATT_BT_UUID_SIZE, characterUUID }, @@ -285,11 +294,13 @@ static bStatus_t simpleProfile_ReadAttrCB( uint16_t connHandle, gattAttribute_t { // No need for "GATT_SERVICE_UUID" or "GATT_CLIENT_CHAR_CFG_UUID" cases; // gattserverapp handles those reads +#if OTA_TYPE case SIMPLEPROFILE_CHAR1_UUID: - *pLen = sizeof(ota_in_buffer); - osal_memcpy( pValue, pAttr->pValue, *pLen ); + *pLen = 20; + osal_memcpy( pValue, &ota, *pLen ); LOG("Read_UUID1:\n"); break; +#endif case SIMPLEPROFILE_CHAR2_UUID: *pLen = sizeof(cfg); osal_memcpy( pValue, &cfg, *pLen ); @@ -340,6 +351,7 @@ static bStatus_t simpleProfile_ReadAttrCB( uint16_t connHandle, gattAttribute_t uint16_t uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]); switch ( uuid ) { +#if OTA_TYPE case SIMPLEPROFILE_CHAR1_UUID: //Validate the value // Make sure it's not a blob oper @@ -353,9 +365,12 @@ static bStatus_t simpleProfile_ReadAttrCB( uint16_t connHandle, gattAttribute_t if ( status == SUCCESS) { osal_memcpy( pAttr->pValue, pValue, len ); ota_in_len = len; + LOG("OTA receive data = 0x "); + LOG_DUMP_BYTE(pAttr->pValue, len); + osal_set_event(simpleBLEPeripheral_TaskID, SBP_OTADATA); } break; - +#endif // OTA_TYPE case SIMPLEPROFILE_CHAR2_UUID: // Validate the value // Make sure it's not a blob oper @@ -368,9 +383,9 @@ static bStatus_t simpleProfile_ReadAttrCB( uint16_t connHandle, gattAttribute_t if ( status == SUCCESS ) { osal_memcpy(pAttr->pValue, pValue, len ); cmd_in_len = len; - LOG("receive data = 0x "); + LOG("CMD receive data = 0x "); LOG_DUMP_BYTE(pAttr->pValue, len); - osal_set_event(simpleBLEPeripheral_TaskID, SBP_DEALDATA); + osal_set_event(simpleBLEPeripheral_TaskID, SBP_CMDDATA); } break; @@ -414,7 +429,7 @@ static void simpleProfile_HandleConnStatusCB( uint16_t connHandle, uint8_t chang } -void new_cmd(void) { +void new_cmd_data(void) { attHandleValueNoti_t noti; noti.handle = simpleProfileAttrTbl[CDM_DATA_ATTR_IDX].handle; noti.len = cmd_parser(noti.value, cmd_in_buffer, cmd_in_len); @@ -422,3 +437,14 @@ void new_cmd(void) { GATT_Notification(gapRole_ConnectionHandle, ¬i, FALSE ); } } + +#if OTA_TYPE +void new_ota_data(void) { + attHandleValueNoti_t noti; + noti.handle = simpleProfileAttrTbl[OTA_DATA_ATTR_IDX].handle; + noti.len = ota_parser(noti.value, cmd_in_buffer, cmd_in_len); + if(noti.len) { + GATT_Notification(gapRole_ConnectionHandle, ¬i, FALSE ); + } +} +#endif diff --git a/bthome_phy6222/source/sbp_profile.h b/bthome_phy6222/source/sbp_profile.h index 5c2c1cc..0d204ff 100644 --- a/bthome_phy6222/source/sbp_profile.h +++ b/bthome_phy6222/source/sbp_profile.h @@ -76,7 +76,8 @@ extern bStatus_t SimpleProfile_AddService( uint32_t services ); //extern bStatus_t simpleProfile_Notify( uint8_t param, uint8_t len, void *value ); -void new_cmd(void); +void new_cmd_data(void); +void new_ota_data(void); #ifdef __cplusplus } diff --git a/bthome_phy6222/source/thb2_main.c b/bthome_phy6222/source/thb2_main.c index 7bc2143..43bd262 100644 --- a/bthome_phy6222/source/thb2_main.c +++ b/bthome_phy6222/source/thb2_main.c @@ -515,13 +515,22 @@ uint16 BLEPeripheral_ProcessEvent( uint8 task_id, uint16 events ) // return unprocessed events return ( events ^ SBP_START_DEVICE_EVT ); } - if(events & SBP_DEALDATA) + if(events & SBP_CMDDATA) { - LOG("\ndeal app datas in events!\n"); - new_cmd(); + LOG("CMD data events\n"); + new_cmd_data(); // return unprocessed events - return(events ^ SBP_DEALDATA); + return(events ^ SBP_CMDDATA); } +#if OTA_TYPE + if(events & SBP_OTADATA) + { + LOG("OTA data events\n"); + new_ota_data(); + // return unprocessed events + return(events ^ SBP_OTADATA); + } +#endif // Discard unknown events return 0; }