commit 1febecdf7bcdb898661b14e68255565b2f3b316b Author: Ivan Belokobylskiy Date: Mon Oct 2 17:03:43 2023 +0400 working attempt diff --git a/TLSR825xComFlasher.py b/TLSR825xComFlasher.py index a8d704c..9308175 100644 --- a/TLSR825xComFlasher.py +++ b/TLSR825xComFlasher.py @@ -482,7 +482,7 @@ def main(): try: serialPort = serial.Serial(args.port,args.baud) serialPort.reset_input_buffer() - serialPort.timeout = 0.01 + serialPort.timeout = 0.2 except: print ('Error: Open %s, %d baud!' % (args.port, args.baud)) sys.exit(1) diff --git a/src/app.c b/src/app.c index f0db51a..e9a89e2 100644 --- a/src/app.c +++ b/src/app.c @@ -269,10 +269,10 @@ void set_hw_version(void) { #endif } -// go deep-sleep +// go deep-sleep void go_sleep(uint32_t tik) { cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER, - clock_time() + tik); + clock_time() + tik); while(1); } @@ -548,9 +548,9 @@ void user_init_normal(void) {//this will get executed one time after power up start_tst_battery(); flash_unlock(); random_generator_init(); //must -#if (DEVICE_TYPE == DEVICE_MJWSD05MMC) +//#if (DEVICE_TYPE == DEVICE_MJWSD05MMC) test_first_ota(); // MJWSD05MMC: Correct FW OTA address? Reformat Big OTA to Low OTA -#endif // (DEVICE_TYPE == DEVICE_MJWSD05MMC) +//#endif // (DEVICE_TYPE == DEVICE_MJWSD05MMC) #if defined(MI_HW_VER_FADDR) && (MI_HW_VER_FADDR) uint32_t hw_ver = get_mi_hw_version(); #endif // (DEVICE_TYPE == DEVICE_LYWSD03MMC) || (DEVICE_TYPE == DEVICE_MJWSD05MMC) @@ -704,8 +704,11 @@ void main_loop(void) { } else #endif { - if ((ble_connected & BIT(CONNECTED_FLG_PAR_UPDATE))==0) - bls_pm_setManualLatency(0); + if ((ble_connected & BIT(CONNECTED_FLG_PAR_UPDATE))==0) { + bls_pm_setManualLatency(3); + clear_ota_area(); +// bls_pm_setManualLatency(0); + } } bls_pm_setSuspendMask (SUSPEND_ADV | SUSPEND_CONN); } else { @@ -793,7 +796,7 @@ void main_loop(void) { #endif } else { bls_pm_setAppWakeupLowPower(0, 0); // clear callback - if (ble_connected && blc_ll_getTxFifoNumber() < 9) { + if (ble_connected && blc_ll_getTxFifoNumber() < 9) { // connect, TxFifo ready if (end_measure & 1) { end_measure &= ~1; diff --git a/src/app_config.h b/src/app_config.h index 54e8563..db425bb 100644 --- a/src/app_config.h +++ b/src/app_config.h @@ -47,7 +47,7 @@ extern "C" { #define DIY_ADC_TO_TH 0 #define USE_MIHOME_SERVICE 0 // = 1 MiHome service compatibility (missing in current version! Set = 0!) -#define UART_PRINT_DEBUG_ENABLE 0 // =1 use u_printf() (PA7/SWS), source: SDK/components/application/print/u_printf.c +#define UART_PRINT_DEBUG_ENABLE 1 // =1 use u_printf() (PA7/SWS), source: SDK/components/application/print/u_printf.c #if DEVICE_TYPE == DEVICE_MHO_C401 // TLSR8251F512ET24 @@ -638,7 +638,7 @@ extern "C" { #endif // DEVICE_TYPE == ? #if UART_PRINT_DEBUG_ENABLE -#define PRINT_BAUD_RATE 1500000 // real ~1000000 +#define PRINT_BAUD_RATE 115200 // real ~1000000 #define DEBUG_INFO_TX_PIN GPIO_PA7 // SWS #define PA7_DATA_OUT 1 #define PA7_OUTPUT_ENABLE 1 diff --git a/src/ble.c b/src/ble.c index c56aa89..b793d0d 100644 --- a/src/ble.c +++ b/src/ble.c @@ -78,7 +78,7 @@ void app_enter_ota_mode(void) { ota_is_working = OTA_WORK; ble_connected &= ~BIT(CONNECTED_FLG_PAR_UPDATE); bls_pm_setManualLatency(0); - bls_ota_setTimeout(16 * 1000000); // set OTA timeout 16 seconds + bls_ota_setTimeout(120 * 1000000); // set OTA timeout 16 seconds SHOW_OTA_SCREEN(); } diff --git a/src/cmd_parser.c b/src/cmd_parser.c index 30e7b62..0cd6d3d 100644 --- a/src/cmd_parser.c +++ b/src/cmd_parser.c @@ -516,14 +516,14 @@ void cmd_parser(void * p) { ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); // reset device on disconnect olen = 2; } else if (cmd == CMD_ID_SET_OTA) { // Set OTA address and size -#if USE_EXT_OTA // Compatible BigOTA +//#if USE_EXT_OTA // Compatible BigOTA uint32_t ota_addr, ota_size; if (len > 8) { memcpy(&ota_addr, &req->dat[1], 4); memcpy(&ota_size, &req->dat[5], 4); send_buf[1] = check_ext_ota(ota_addr, ota_size); } // else send_buf[1] = 0; -#endif +//#endif memcpy(&send_buf[2], &ota_program_offset, 4); memcpy(&send_buf[2+4], &ota_firmware_size_k, 4); olen = 2 + 8; diff --git a/src/cmd_parser.h b/src/cmd_parser.h index 2ea67ac..d7b3ca4 100644 --- a/src/cmd_parser.h +++ b/src/cmd_parser.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once enum { CMD_ID_DNAME = 0x01, // Get/Set device name, "\0" - default: ATC_xxxx CMD_ID_GDEVS = 0x02, // Get address devises @@ -55,9 +55,9 @@ typedef struct __attribute__((packed)) _blk_mi_keys_t { } blk_mi_keys_t, * pblk_mi_keys_t; extern blk_mi_keys_t keybuf; -#if USE_EXT_OTA // Compatible BigOTA +//#if USE_EXT_OTA // Compatible BigOTA void clear_ota_area(void); -#endif +//#endif uint32_t find_mi_keys(uint16_t chk_id, uint8_t cnt); diff --git a/src/ext_ota.c b/src/ext_ota.c index 2ab75c3..a25394c 100644 --- a/src/ext_ota.c +++ b/src/ext_ota.c @@ -105,27 +105,22 @@ void set_SerialStr(void) { #endif // defined(MI_HW_VER_FADDR) && (MI_HW_VER_FADDR) -#if (DEVICE_TYPE == DEVICE_MJWSD05MMC) - +RAM ext_ota_t ext_ota; extern unsigned char *_icload_size_div_16_; extern unsigned char *_bin_size_; - // Current OTA header: static const uint32_t head_id[4] = { 0x00008026, // asm("tj __reset") 0x025d0000, // id OTA ver ID_BOOTABLE, // id "bootable" = "KNLT" (uint32_t)(&_icload_size_div_16_ ) + 0x00880000 }; -/* Reformat Big OTA to Low OTA */ + void test_first_ota(void) { // find the real FW flash address - uint32_t buf_blk[64], id, size, faddrw = OTA2_FADDR, faddrr = BIG_OTA2_FADDR; + uint32_t buf_blk[64], id, size, faddrr = OTA2_FADDR, faddrw = BIG_OTA2_FADDR; flash_unlock(); flash_read_page(faddrr, 0x20, (unsigned char *) &buf_blk); -#if USE_FLASH_MEMO - if(buf_blk[0] == MEMO_SEC_ID) - return; -#endif + if(memcmp(&buf_blk, &head_id, sizeof(head_id)) == 0) { // calculate size OTA size = (uint32_t)(&_bin_size_); @@ -152,73 +147,30 @@ void test_first_ota(void) { } // set id "bootable" to new segment id = head_id[2]; // = "KNLT" - flash_write_page(OTA2_FADDR_ID, sizeof(id), (unsigned char *) &id); + flash_write_page(BIG_OTA2_FADDR_ID, sizeof(id), (unsigned char *) &id); // clear the "bootable" identifier on the current OTA segment id = 0; - flash_write_page(BIG_OTA2_FADDR_ID, 1, (unsigned char *) &id); + flash_write_page(OTA2_FADDR_ID, 1, (unsigned char *) &id); //flash_erase_sector(CFG_ADR_BIND); // Pair & Security info - while(1) - start_reboot(); +// while(1) +// start_reboot(); } } } -#endif // DEVICE_TYPE == DEVICE_MJWSD05MMC - - -#if USE_EXT_OTA // Compatible BigOTA - -RAM ext_ota_t ext_ota; - -uint32_t check_sector_clear(uint32_t addr) { - uint32_t faddr = addr, efaddr, fbuf; - faddr &= ~(FLASH_SECTOR_SIZE-1); - efaddr = faddr + FLASH_SECTOR_SIZE; - while(faddr < efaddr) { - flash_read_page(faddr, sizeof(fbuf), (unsigned char *) &fbuf); - if(fbuf != 0xffffffff) { - flash_erase_sector(faddr & (~(FLASH_SECTOR_SIZE-1))); - break; - } - faddr += 1024; - } - return efaddr; -} void ota_result_cb(int result) { uint32_t boot_id; if(result == OTA_SUCCESS) { // clear the "bootable" identifier on the current work segment - flash_read_page(OTA2_FADDR_ID, sizeof(boot_id), (unsigned char *) &boot_id); + flash_read_page(BIG_OTA2_FADDR_ID, sizeof(boot_id), (unsigned char *) &boot_id); if(boot_id == ID_BOOTABLE) { boot_id = 0; - flash_write_page(OTA2_FADDR_ID, 1, (unsigned char *) &boot_id); + flash_write_page(BIG_OTA2_FADDR_ID, 1, (unsigned char *) &boot_id); } } } -uint8_t check_ext_ota(uint32_t ota_addr, uint32_t ota_size) { - if(ota_is_working == OTA_EXTENDED) - return EXT_OTA_BUSY; - if(ota_is_working) - return EXT_OTA_WORKS; - if(ota_addr < 0x40000 && ota_size <= ota_firmware_size_k) - return EXT_OTA_OK; - if(ota_size >= 208 - || ota_size < 4 - || ota_addr & (FLASH_SECTOR_SIZE-1)) - return EXT_OTA_ERR_PARM; - SHOW_OTA_SCREEN(); - ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); - ota_is_working = OTA_EXTENDED; // flag ext.ota - ext_ota.start_addr = ota_addr; - ext_ota.check_addr = ota_addr; - ext_ota.ota_size = (ota_size + 3) & 0xfffffc; - bls_ota_registerResultIndicateCb(ota_result_cb); - bls_pm_setManualLatency(3); - return EXT_OTA_BUSY; -} - void clear_ota_area(void) { union { uint8_t b[24]; @@ -233,13 +185,6 @@ void clear_ota_area(void) { if(bls_ll_requestConnBrxEventDisable() < 256 || ext_ota.check_addr == 0) return; if (ext_ota.check_addr >= ext_ota.start_addr + (ext_ota.ota_size << 10)) { -#if (DEVICE_TYPE == DEVICE_MJWSD05MMC) || (DEVICE_TYPE == DEVICE_LYWSD03MMC) - check_sector_clear(MI_HW_VER_FADDR); - flash_write_page(MI_HW_VER_FADDR, sizeof(my_HardStr), (unsigned char *)my_HardStr); - flash_write_page(MI_HW_VER_FADDR + sizeof(my_HardStr), sizeof(my_SerialStr), (unsigned char *)my_SerialStr); -#else -#error "Define MI_HW_VER_FADDR & _mi_hw_vers!" -#endif ota_firmware_size_k = ext_ota.ota_size; ota_program_offset = ext_ota.start_addr; buf.msg.id_ok = (EXT_OTA_READY << 8) + CMD_ID_SET_OTA; @@ -250,7 +195,7 @@ void clear_ota_area(void) { } else { bls_ll_disableConnBrxEvent(); - ext_ota.check_addr = check_sector_clear(ext_ota.check_addr); +// ext_ota.check_addr = check_sector_clear(ext_ota.check_addr); bls_ll_restoreConnBrxEvent(); buf.msg.id_ok = (EXT_OTA_EVENT << 8) + CMD_ID_SET_OTA; buf.msg.start_addr = ext_ota.check_addr; @@ -261,5 +206,185 @@ void clear_ota_area(void) { SUSPEND_ADV | DEEPSLEEP_RETENTION_ADV | SUSPEND_CONN | DEEPSLEEP_RETENTION_CONN); // MCU_STALL); } -#endif // USE_EXT_OTA +uint8_t check_ext_ota(uint32_t ota_addr, uint32_t ota_size) { + if(ota_is_working == OTA_EXTENDED) + return EXT_OTA_BUSY; + if(ota_is_working) + return EXT_OTA_WORKS; +// if(ota_addr < 0x40000 && ota_size <= ota_firmware_size_k) +// return EXT_OTA_OK; + if(ota_size >= 208 + || ota_size < 4 + || ota_addr & (FLASH_SECTOR_SIZE-1)) + return EXT_OTA_ERR_PARM; + SHOW_OTA_SCREEN(); + ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); + ota_is_working = OTA_EXTENDED; // flag ext.ota + ext_ota.start_addr = ota_addr; + ext_ota.check_addr = ota_addr; + ext_ota.ota_size = (ota_size + 3) & 0xfffffc; + bls_ota_registerResultIndicateCb(ota_result_cb); + bls_pm_setManualLatency(3); + return EXT_OTA_BUSY; +} + + +//#if (0 && DEVICE_TYPE == DEVICE_MJWSD05MMC) +// +//extern unsigned char *_icload_size_div_16_; +//extern unsigned char *_bin_size_; +// +//// Current OTA header: +//static const uint32_t head_id[4] = { +// 0x00008026, // asm("tj __reset") +// 0x025d0000, // id OTA ver +// ID_BOOTABLE, // id "bootable" = "KNLT" +// (uint32_t)(&_icload_size_div_16_ ) + 0x00880000 }; +///* Reformat Big OTA to Low OTA */ +//void test_first_ota(void) { +// // find the real FW flash address +// uint32_t buf_blk[64], id, size, faddrw = OTA2_FADDR, faddrr = BIG_OTA2_FADDR; +// flash_unlock(); +// flash_read_page(faddrr, 0x20, (unsigned char *) &buf_blk); +//#if USE_FLASH_MEMO +// if(buf_blk[0] == MEMO_SEC_ID) +// return; +//#endif +// if(memcmp(&buf_blk, &head_id, sizeof(head_id)) == 0) { +// // calculate size OTA +// size = (uint32_t)(&_bin_size_); +// size += 15; +// size &= ~15; +// size += 4; +// if(buf_blk[6] == size) { // OTA bin size +//// bls_ota_clearNewFwDataArea(); +// flash_erase_sector(faddrw); // 45 ms, 4 mA +// flash_read_page(faddrr, sizeof(buf_blk), (unsigned char *) &buf_blk); +// buf_blk[2] &= 0xffffffff; // clear id "bootable" +// faddrr += sizeof(buf_blk); +// flash_write_page(faddrw, sizeof(buf_blk), (unsigned char *) &buf_blk); +// size += faddrw; +// faddrw += sizeof(buf_blk); +// while(faddrw < size) { +// if((faddrw & (FLASH_SECTOR_SIZE - 1)) == 0) +// flash_erase_sector(faddrw); // 45 ms, 4 mA +// // rd-wr 4kB - 20 ms, 4 mA +// flash_read_page(faddrr, sizeof(buf_blk), (unsigned char *) &buf_blk); +// faddrr += sizeof(buf_blk); +// flash_write_page(faddrw, sizeof(buf_blk), (unsigned char *) &buf_blk); +// faddrw += sizeof(buf_blk); +// } +// // set id "bootable" to new segment +// id = head_id[2]; // = "KNLT" +// flash_write_page(OTA2_FADDR_ID, sizeof(id), (unsigned char *) &id); +// // clear the "bootable" identifier on the current OTA segment +// id = 0; +// flash_write_page(BIG_OTA2_FADDR_ID, 1, (unsigned char *) &id); +// //flash_erase_sector(CFG_ADR_BIND); // Pair & Security info +// while(1) +// start_reboot(); +// } +// } +//} +// +//#endif // DEVICE_TYPE == DEVICE_MJWSD05MMC +// +// +//#if USE_EXT_OTA // Compatible BigOTA +// +//RAM ext_ota_t ext_ota; +// +//uint32_t check_sector_clear(uint32_t addr) { +// uint32_t faddr = addr, efaddr, fbuf; +// faddr &= ~(FLASH_SECTOR_SIZE-1); +// efaddr = faddr + FLASH_SECTOR_SIZE; +// while(faddr < efaddr) { +// flash_read_page(faddr, sizeof(fbuf), (unsigned char *) &fbuf); +// if(fbuf != 0xffffffff) { +// flash_erase_sector(faddr & (~(FLASH_SECTOR_SIZE-1))); +// break; +// } +// faddr += 1024; +// } +// return efaddr; +//} +// +//void ota_result_cb(int result) { +// uint32_t boot_id; +// if(result == OTA_SUCCESS) { +// // clear the "bootable" identifier on the current work segment +// flash_read_page(OTA2_FADDR_ID, sizeof(boot_id), (unsigned char *) &boot_id); +// if(boot_id == ID_BOOTABLE) { +// boot_id = 0; +// flash_write_page(OTA2_FADDR_ID, 1, (unsigned char *) &boot_id); +// } +// } +//} +// +//uint8_t check_ext_ota(uint32_t ota_addr, uint32_t ota_size) { +// if(ota_is_working == OTA_EXTENDED) +// return EXT_OTA_BUSY; +// if(ota_is_working) +// return EXT_OTA_WORKS; +// if(ota_addr < 0x40000 && ota_size <= ota_firmware_size_k) +// return EXT_OTA_OK; +// if(ota_size >= 208 +// || ota_size < 4 +// || ota_addr & (FLASH_SECTOR_SIZE-1)) +// return EXT_OTA_ERR_PARM; +// SHOW_OTA_SCREEN(); +// ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); +// ota_is_working = OTA_EXTENDED; // flag ext.ota +// ext_ota.start_addr = ota_addr; +// ext_ota.check_addr = ota_addr; +// ext_ota.ota_size = (ota_size + 3) & 0xfffffc; +// bls_ota_registerResultIndicateCb(ota_result_cb); +// bls_pm_setManualLatency(3); +// return EXT_OTA_BUSY; +//} +// +//void clear_ota_area(void) { +// union { +// uint8_t b[24]; +// struct __attribute__((packed)) { +// uint16_t id_ok; +// uint32_t start_addr; +// uint32_t ota_size; +// } msg; +// } buf; +//// if(bls_pm_getSystemWakeupTick() - clock_time() < 512*CLOCK_16M_SYS_TIMER_CLK_1MS) +//// return; +// if(bls_ll_requestConnBrxEventDisable() < 256 || ext_ota.check_addr == 0) +// return; +// if (ext_ota.check_addr >= ext_ota.start_addr + (ext_ota.ota_size << 10)) { +//#if (DEVICE_TYPE == DEVICE_MJWSD05MMC) || (DEVICE_TYPE == DEVICE_LYWSD03MMC) +// check_sector_clear(MI_HW_VER_FADDR); +// flash_write_page(MI_HW_VER_FADDR, sizeof(my_HardStr), (unsigned char *)my_HardStr); +// flash_write_page(MI_HW_VER_FADDR + sizeof(my_HardStr), sizeof(my_SerialStr), (unsigned char *)my_SerialStr); +//#else +//#error "Define MI_HW_VER_FADDR & _mi_hw_vers!" +//#endif +// ota_firmware_size_k = ext_ota.ota_size; +// ota_program_offset = ext_ota.start_addr; +// buf.msg.id_ok = (EXT_OTA_READY << 8) + CMD_ID_SET_OTA; +// buf.msg.start_addr = ext_ota.start_addr; +// buf.msg.ota_size = ext_ota.ota_size; +// ota_is_working = OTA_WAIT; // flag ext.ota wait +// ext_ota.check_addr = 0; +// } +// else { +// bls_ll_disableConnBrxEvent(); +// ext_ota.check_addr = check_sector_clear(ext_ota.check_addr); +// bls_ll_restoreConnBrxEvent(); +// buf.msg.id_ok = (EXT_OTA_EVENT << 8) + CMD_ID_SET_OTA; +// buf.msg.start_addr = ext_ota.check_addr; +// buf.msg.ota_size = 0; +// } +// bls_att_pushNotifyData(RxTx_CMD_OUT_DP_H, (u8 *)&buf.msg, sizeof(buf.msg)); +// bls_pm_setSuspendMask( +// SUSPEND_ADV | DEEPSLEEP_RETENTION_ADV | SUSPEND_CONN | DEEPSLEEP_RETENTION_CONN); // MCU_STALL); +//} +// +//#endif // USE_EXT_OTA +// diff --git a/src/ext_ota.h b/src/ext_ota.h index b7d18b0..fa38082 100644 --- a/src/ext_ota.h +++ b/src/ext_ota.h @@ -20,7 +20,7 @@ void set_SerialStr(void); #endif -#if USE_EXT_OTA // Compatible BigOTA +//#if USE_EXT_OTA // Compatible BigOTA // Ext.OTA return code enum { @@ -43,6 +43,6 @@ extern ext_ota_t ext_ota; uint8_t check_ext_ota(uint32_t ota_addr, uint32_t ota_size); void clear_ota_area(void); -#endif // USE_EXT_OTA +//#endif // USE_EXT_OTA #endif /* EXT_OTA_H_ */ diff --git a/src/logger.h b/src/logger.h index 24f2988..2be039f 100644 --- a/src/logger.h +++ b/src/logger.h @@ -12,8 +12,8 @@ #if USE_FLASH_MEMO #define MEMO_SEC_ID 0x55AAC0DE // sector head -#define FLASH_ADDR_START_MEMO 0x40000 -#define FLASH_ADDR_END_MEMO 0x74000 // 49 sectors +#define FLASH_ADDR_START_MEMO 0x74000 +#define FLASH_ADDR_END_MEMO 0x76000 // 49 sectors typedef struct _memo_blk_t { uint32_t time; // time (UTC) diff --git a/src/main.c b/src/main.c index cd15226..b7884f5 100644 --- a/src/main.c +++ b/src/main.c @@ -160,6 +160,8 @@ _attribute_ram_code_ void irq_handler(void) { #error "Set CLOCK_SYS_CLOCK_HZ!" #endif +#define OTA_FIRMWARE_SIZE_MAX (240*1024) // 0x3C000 // refer to board.c: ((512-32)/2) =240k; // 32k parameters now +#define OTA_FIRMWARE_SIZE_MAX_K (OTA_FIRMWARE_SIZE_MAX / 1024) /** * @brief This is main function @@ -167,6 +169,10 @@ _attribute_ram_code_ void irq_handler(void) { * @return none */ _attribute_ram_code_ int main (void) { //must run in ramcode +// irq_disable(); + bls_ota_set_fwSize_and_fwBootAddr(200, 0x40000); +// bls_ota_set_fwSize_and_fwBootAddr(OTA_FIRMWARE_SIZE_MAX_K, 0x40000); +// irq_enable(); blc_pm_select_internal_32k_crystal(); // or blc_pm_select_external_32k_crystal(); cpu_wakeup_init(); int deepRetWakeUp = pm_is_MCU_deepRetentionWakeup(); //MCU deep retention wakeUp