diff --git a/src/telink/Makefile b/src/telink/Makefile index 797ea637ae..0d6c3ef777 100644 --- a/src/telink/Makefile +++ b/src/telink/Makefile @@ -141,7 +141,8 @@ TELINK_SOURCES := \ custom_zcl/zcl_onoff_configuration.c \ custom_zcl/zcl_cover_switch_config.c \ custom_zcl/zcl_window_covering_custom.c \ - patch_sdk/drv_nv.c + patch_sdk/drv_nv.c \ + patch_sdk/flash_zbit.c # Common source files (shared with Silicon Labs build) COMMON_SOURCES := \ diff --git a/src/telink/hal/adc.c b/src/telink/hal/adc.c index 5da2d617e1..1833beaef7 100644 --- a/src/telink/hal/adc.c +++ b/src/telink/hal/adc.c @@ -14,6 +14,8 @@ static _attribute_custom_data_ bool adc_initialized = false; void hal_adc_init(hal_adc_input_t input, hal_gpio_pin_t pin) { drv_adc_init(); if (input == HAL_ADC_INPUT_VBAT) { + extern unsigned char g_zbit_voltage_gate; // patch_sdk/flash_zbit.c + g_zbit_voltage_gate = 1; drv_adc_mode_pin_set(DRV_ADC_VBAT_MODE, (GPIO_PinTypeDef)pin); } else { drv_adc_mode_pin_set(DRV_ADC_BASE_MODE, (GPIO_PinTypeDef)pin); diff --git a/src/telink/patch_sdk/flash_zbit.c b/src/telink/patch_sdk/flash_zbit.c new file mode 100644 index 0000000000..64de5228f8 --- /dev/null +++ b/src/telink/patch_sdk/flash_zbit.c @@ -0,0 +1,27 @@ +#pragma pack(push, 1) +#include "tl_common.h" +#pragma pack(pop) + +/* + * Flash writes on ZBIT flash chips (JEDEC id 0x13325E / 0x14325E, found in 512 kB modules). + * + * For these chips the SDK measures the supply voltage with the ADC before every program and erase + * command (flash_mspi_write_ram in platform/chip_8258/flash.c) and silently drops the command when + * the reading is not above 2.2 V or fluctuates. On the TLSR8258 the supply can only be measured + * through a free GPIO (VBAT mode); this firmware has VOLTAGE_DETECT_ADC_PIN = 0, so the reading is + * meaningless and most writes were dropped: the Zigbee stack's data (network key, bindings, frame + * counter) never reached the flash. The device lost its network at every restart. + * + * sdk.mk compiles flash.c with adc_get_result_with_fluct renamed to this function. + * Mains powered devices: the supply is regulated, report it as good. + * Battery powered devices: the ADC is set up on the battery pin, keep the SDK's check. + */ +unsigned char g_zbit_voltage_gate = 0; // set to 1 when the ADC measures the battery + +_attribute_ram_code_ unsigned int zbit_supply_mv(unsigned int *fluct) { + if (g_zbit_voltage_gate) { + return adc_get_result_with_fluct(fluct); + } + *fluct = 0; + return 3300; +} diff --git a/src/telink/sdk.mk b/src/telink/sdk.mk index 13ab67489a..3b7651cb92 100644 --- a/src/telink/sdk.mk +++ b/src/telink/sdk.mk @@ -163,6 +163,8 @@ sdk-clean: # Special compiler flags for specific SDK files $(BUILD_DIR)/sdk/proj/drivers/drv_nv.o: GCC_FLAGS += -Dnv_resetToFactoryNew=nv_resetToFactoryNew__sdk +# see patch_sdk/flash_zbit.c +$(BUILD_DIR)/sdk/platform/chip_8258/flash.o: GCC_FLAGS += -Dadc_get_result_with_fluct=zbit_supply_mv $(SDK_OBJS): GCC_FLAGS += -fpack-struct