Hacker News new | past | comments | ask | show | jobs | submit login
How to implement and use .noinit RAM (memfault.com)
35 points by picture 11 days ago | hide | past | favorite | 8 comments





By using this in an Arduino program, you can use the little reset button on the board as a button for user input.

Whenever the button is pressed, the CPU resets, but doesn't erase RAM, so if you write the code with that in mind, you've just saved yourself hooking up a button to the board.

There is also a special register that can be used to know if the CPU was reset or if power was pulled.


Note that this isn't 'production ready'. Specifically, the reset input on a microcontroller is typically asynchronous. That means it's possible for it to happen 'half way through' a clock cycle, and when that happens, things could end up in an unknown state. Specifically, if it happens half way through a write of SRAM, a garbage address might have garbage data written, effectively meaning that while most of SRAM is maintained across a reset, a few random bytes of it might be written to accidentally during the reset process.

In reality, I have never witnessed this happen on the ATmega 328p used in the common arduinos. They may have undocumented circuitry to prevent it, or perhaps its just vanishingly unlikely.


It isn't vanishingly unlikely. Even without considering low level hardware behavior, any non-atomic write could be interrupted halfway through, leaving invalid data in memory.

I’m probably dumb, but isn’t a possible solution to this to leave a single atomic write for the very last operation after a chain of work that was done, one that flags “there is good data here”?

You would need something that is unlikely to happen from random chance like a valid CRC over the block of data. A flag won't cut it since you can't make any guarantees about data integrity when the system is being torn down mid-write.

Sure, but if we’re talking a 32bit system, a magic number can be your final atomic.

Agreed. If you were talking an 8bit system, you could 32bit CRC your actual data. Even though the CRC would be 32bits and 4 stores, it wouldn’t be “set” until the final atomic write.


Saw an ancient robotics controller that had the reset line of a 6502 hooked up to a 555 timer as an even higher level NMI for the PID loop.

WTF. Why on earth you would do this (and deal with all IO and peripherals being reset) while you can just reconfigure RESET as normal GPIO? This makes SPI programming impossible but if you are using bootloader then it doesn't matter anyway.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: