memory corruption RP2040 - Page 1
Forum section: electronics / general

author: PinGNU
author_rank: Newbie
timestamp: March 13, 2023, 12:18:00 pm
content: HiHow can I implement a power-loss detection mechanism to detect when power is lost and take appropriate actions to prevent memory corruption. This mechanism could include a hardware watchdog timer, software timer, or interrupt service routine that can trigger a graceful shutdown of the RP2040. I am actually using a PGA2040 a cut-down version of PiPicoCarl

author: shapirus
author_rank: Super Contributor
timestamp: March 13, 2023, 01:58:27 pm
content: If power is already lost, then it's too late to shut down.Does your question imply that you use a backup power device that takes over when the primary power source fails?

author: dobsonr741
author_rank: Frequent Contributor
timestamp: March 13, 2023, 02:22:59 pm
content: Not clear which memory we talk about. RAM? That’s a high bar never intended to be achieved on the Pico. Flash memory writes? Perhaps frequent and alternating atomic A/B writes can survive power loss without corruption for a price of loosing the latest but not the one before. Same applies to a flash alternative ext memory, like an FRAM, if internal flash endurance is a concern.One example of this atomic flash update ishttps://github.com/olegv142/stm32-config, though not RP2040 adopted. Orhttps://github.com/littlefs-project/littlefs

author: hans
author_rank: Super Contributor
timestamp: March 13, 2023, 07:23:32 pm
content: A classic strategy is a large cap on your MCU VDD, and monitor the power source and trigger if it's drop below a certain threshold. E.g. a hardware comparator that flips, which could trigger an IRQ. Then dump contents to non-volatile ASAP and halt other write operations that could corrupt the state.Sometimes you can also use the ADC for this, through some hardware monitor configuration, or manually on a certain polling interval.I don't think the other internal peripherals are of much use. Perhaps make sure that the brownout voltage threshold is also set accordingly, or use an external reset monitor.

author: darkspr1te
author_rank: Frequent Contributor
timestamp: March 14, 2023, 05:53:05 am
content: Quote from: PinGNU on March 13, 2023, 12:18:00 pmI Had this same issue with some power monitoring tech, when the power fails so did the monitors and that in turn ment that the data collection system would re-use last known value. to get around this i got a Geekpi UPS for PI pico(look for the model with power management serial chip) , it has a on board 18650 with serial feedback of current power input. when input power fails then the battery pmc send a signal and the device is able to update the host server and go into power save mode with a IRQ on the serial for when the power returns.The Only issue with this device is it does now like input power via the pico's USB power when the 18650 is flat, it heats up the chips a lot and i've yet to figure out why (black soldermask is hell for tracing a pcb trace)so power is applied via the UPS board and in turn does not allow access to the pico for programming, We switched to picow to get around this fact and use OTA updates instead.darkspr1te

author: barshatriplee
author_rank: Regular Contributor
timestamp: March 26, 2023, 02:31:43 pm
content: Configuring the watchdog timer seems to be the easiest option.