logo elektroda
logo elektroda
X
logo elektroda

BK7231N Door Sensor Bricking After Battery Drain: Flash Write Issue and Solutions

protectivedad 78 3
ADVERTISEMENT
  • #1 21819205
    protectivedad
    Level 9  
    Has anyone tracked down the problem with battery devices that become bricked after the batteries drain? I've seen it mentioned but I can't remember where I saw the information and I'm not using the right search to find it.

    I just had it happen to my non-TuyaMCU door sensor. Unfortunately, I didn't save a backup of the FUBAR'd firmware before erasing it. I did hook it up the serial log and saw:
    
    V:BK7231N_1.0.1
    REG:cpsr     spsr     r13      r14
    SVC:000000D3          00401C1C 000033AC
    IRQ:000000d2 00000010 00401e0c 3ffff7f5
    FIR:000000d1 00000010 00401ffc 77ff856f
    SYS:000000df          0040192c 00000158
    ST:00000003
    J 0x10000
    bk_misc_init_start_type 3 3
    prvHeapInit-start addr:0x412bb8, size:119880
    [Flash]id:0x1c7015
    sctrl_sta_ps_init
    cset:0 0 0 0
    OpenBK7231N, version DoorSensors_88af71253b87_doorsensor
    Entering initLog()...
    Commands registered!
    initLog() done!
    Info:GEN:0 - Main_Init_Before_Delay
    


    From this I assumed either the flash Vars or flash CFG had become corrupted. For my device I rewrote the flash vars code to be a little better in how it stores and finds the latest stuff on boot. During this I did think of two solutions that people might benefit from.

    The first is a simple one, after the device registers enough failed boots to trigger RESTARTS_REQUIRED_FOR_SAFE_MODE stop writing to the flash. I did a quick test with some "dead" AAA batteries (~1v each). The device boots and reboots hundreds of times a minute. When I hooked it back up to a power supply, I noticed the device was writing to the flash each boot. With your current implementation that means the flash is being hammered with updates when the device has low batteries. Thousands of writes an hour, etc.

    The second is to NOT write to the flash until "####### Set Boot Complete #######" has been reached. This is a little more complicated and of course there is an exception for the boot counter (which is capped at RESTARTS_REQUIRED_FOR_SAFE_MODE ), but it works well for me. For the CFG it doesn't require a lot of extra coding. I added a safe to write flag and it is tripped after the boot is marked complete or when you exit safe mode which causes a CFG_Save_IfThereArePendingChanges and any stored changes are written. The vars was a bit more complicated. The firmware potentially writes when reading, reads (with potential writes) and explicitly writes all over the place. I suggest a simple rule don't write on a read, ever. Still it's not too bad because the channel changes can get written to memory and when the boot is marked complete all the channel information is written to flash anyway. So I just stop writing (except boot count) while in the "boot" phase.
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #3 21819583
    miegapele
    Level 16  
    Yes, not excessively writing is probably good idea. However, it's interesting would could cause this crash. Broken config should not be an issue, because crc is checked. Maybe easyflash data is corrupted and the action of trying to read crashes somehow.
  • #4 21819865
    protectivedad
    Level 9  
    I didn't like the flash vars code; it was very hard to follow. I just redid it so it was more efficient and logical. It now reads and ONLY reads, and if it needs to write it, then either appends to a good flash vars or resets the partition to hold good information. As for the config, from a logical point of view, I don't want my device changing itself if it isn't booted to a useful state. So, it just makes good sense to have it wait for the boot complete; plus, that reduces some wear on the flash. Win-win.

    I'll see if I get a "bricked" sensor problem anymore. If I do, I'll update this.
ADVERTISEMENT