Skip to content

Telink: do not drop flash writes on ZBIT flash chips (512 kB Zbeacon / HOBEIAN lose pairing on restart) - #554

Open
gyimesibalazs wants to merge 1 commit into
romasku:mainfrom
gyimesibalazs:fix-zbit-flash-writes
Open

gyimesibalazs wants to merge 1 commit into
romasku:mainfrom
gyimesibalazs:fix-zbit-flash-writes

Conversation

@gyimesibalazs

Copy link
Copy Markdown

Problem

On modules with a ZBIT flash chip (JEDEC id 5e3213 / mid 0x13325E, also 0x14325E) the Zigbee stack's data never reaches the flash: network key, bindings, frame counter. The device joins and works until the next restart, then comes up factory new and tries to join again.

This is the Zbeacon TS0001 behaviour reported in #219 ("after power is removed the pairing is lost and it enters pairing mode immediately", "changing the config string enters pairing mode"), and very likely the cause of the open issues on the 512 kB HOBEIAN / Zbeacon devices listed in docs/known_issues.md, which use the same flash.

Cause

For ZBIT flash the SDK measures the supply voltage with the ADC before every program and erase command and silently skips the command when the reading is not above the threshold or fluctuates (flash_mspi_write_ram() in platform/chip_8258/flash.c, enabled by flash_is_zb() in drv_hw.c).

On the TLSR8258 the supply can only be measured through a free GPIO in VBAT mode. This firmware has VOLTAGE_DETECT_ADC_PIN = 0, so the reading is meaningless and most writes are dropped. Writes that happen to pass (for example the firmware's own items early at start-up) are stored, which is why the device settings survive while the network data does not.

Evidence (Zbeacon TS0001, JEDEC 5e3213, unmodified MODULE_TUYA_DEFAULT_A_TS0001 1.1.3 build)

  • Flash read back over SWS after a successful join and TCLK exchange: 0x34000–0x3FFFF (ZB_INFO, address table, APS, ZCL, frame counter, OTA) completely blank, the APP module at 0x7A000 written.
  • A diagnostic build that called nv_flashWriteNew() on a module in 0x34000–0x3FFFF one minute after start got NV_ITEM_CHECK_ERROR (the data was not there on read-back), while the stack reported joined / not factory new / commissioning success.
  • With this change: the same test write succeeds, the ZB_INFO sector header is written, and the device comes back after a restart with a plain device announce (no new join) with permit join closed. The pairing also survived a firmware change by wire without erasing the NV area.

Fix

flash.c is compiled with adc_get_result_with_fluct renamed to a small function in patch_sdk/flash_zbit.c (same mechanism as the existing drv_nv.c patch):

  • mains powered devices: report a good supply, so the write is carried out;
  • devices with a battery pin (BT<pin>): the ADC is set up on that pin, so the SDK's check is kept.

No behaviour change for other flash chips: the SDK only runs this check when flash_is_zb() is true.

Not covered

For ZBIT flash (JEDEC 0x13325E / 0x14325E) the SDK measures the supply voltage
with the ADC before every program and erase command and silently skips the
command when the reading is bad. No ADC pin is configured here, so the Zigbee
stack's data was never stored and the device lost its network at every restart.
Mains devices now report a good supply to that check; battery devices keep it.
@gyimesibalazs

Copy link
Copy Markdown
Author

A follow-up on why this probably also matters for the "bad router" reports (#501), not only for the lost pairing.

A router keeps its outgoing NWK frame counter in flash: NV_MODULE_NWK_FRAME_COUNT, at 0x3C000–0x3DFFF on 512 kB flash (nv_nwkFrameCountSaveToFlash() in drv_nv.c; bdb_outgoingFrameCountUpdate() restores it at start-up). On a ZBIT chip without this fix those writes are dropped as well. In the dump of the Zbeacon TS0001 running the unmodified 1.1.3 build, after it had joined and been on the network for a while, that area was blank, together with the rest of 0x34000–0x3FFFF.

So after a power loss such a router starts counting its outgoing NWK frames again from a low value. Its neighbours keep the highest counter they have seen from it and drop every frame below that as a replay, silently. Until the counter has passed the old value the router can neither forward nor be heard: its children are cut off and routes through it fail, which from the outside looks like other devices dropping out of the network, and power-cycling things around it "helps" only by chance.

That fits the descriptions in #501 (devices started to drop after the switch joined; power-cycling the switch and the coordinator fixed it), but it is an explanation from the code and the flash dump: I have not observed such a drop-out on my network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant