Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/telink/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 := \
Expand Down
2 changes: 2 additions & 0 deletions src/telink/hal/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions src/telink/patch_sdk/flash_zbit.c
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions src/telink/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down