News for hackers. Stuff that matters.
Re:The design of littlefs: A fail-safe filesystem designed f... by technothrasher on Saturday August 31, 2019 @07:04AM (#20845621)
"Usually, microcontroller code is simple and reactive, with no concept of a shutdown routine."

Really? Nobody uses brownout interrupts to lock down on power loss? I almost always do a few things as the power is fading, flagging the situation at the very least in order to clean up when power is restored.

Re:The design of littlefs: A fail-safe filesystem designed f... by Nokinside on Saturday August 31, 2019 @07:44AM (#20845755)
Really. It's best not to trust on shutdown procedures for fail-safe persistence if that really is important. Use log-structured file-system and do cleanup and verification when the power is restored. When microcontrollers have brownouts, it's best to assume that you may have already garbage in the memory bus.

Of course, when improved fail-safety exist just to improve reliability measured in hours and fail-safety is not critical function, all what I said is irrelevant.

Re:The design of littlefs: A fail-safe filesystem designed f... by kabdib on Saturday August 31, 2019 @12:36PM (#20847323)
Brownout detection isn't sufficient. There are many other sources of "instant stop", from simple hard-crash bugs to bit-flips in RAM that either cause a crash or multiple-bit ECC fault, take your pick because they will all turn out the lights without warning.

For consumer devices there's ESD testing, where devices are subjected to several kilovolts of zap in certification tests and are required to survive. Typically this means the device will reset and recover, but it needs to be in usable condition afterwards. If a surprise reset results in a toasted file system and a brick, you won't be able to sell the device in markets that require this testing, which is most of Europe and (I think) the US.

The right way to do a persistent store is to expect the lights to go out on any arbitrary bus cycle and design around that.

I've written a few of these. They're fun.

Re:The design of littlefs: A fail-safe filesystem designed f... by varjag on Saturday August 31, 2019 @12:48PM (#20847395)
My understanding was if the function is interrupted on discharge, you fail the test. At least that's how our equipment was tested by an independent lab.
Re:The design of littlefs: A fail-safe filesystem designed f... by kabdib on Saturday August 31, 2019 @01:02PM (#20847476)
Might be different depending on the market. For us it was "we can reset, but we have to recover within a few seconds". I never questioned the wisdom of our hardware cert engineers.

(My experience at three different companies spread over 35 years was, if you were a software engineer helping the hardware types with ESD or RFI testing in the lab, you were definitely going to get zapped at some point. That made you part of the team, though ... :-) )

Re:The design of littlefs: A fail-safe filesystem designed f... by varjag on Tuesday September 03, 2019 @06:20PM (#20871837)
As it was put to me, if a discharge leaks through, there's really good chance that another, or 3rd, or 5th would be fatal: the device is not adequately protected. But probably that's not a big deal in some applications.
Re:The design of littlefs: A fail-safe filesystem designed f... by garganzol on Sunday September 01, 2019 @07:08AM (#20851622)
My usual design is to monitor AC powerline with opto-coupler and then enter the brownout procedure in MCU when the power loss is detected. The remaining energy stored in AC/DC converter capacitors are then used for a graceful shutdown purposes.

The usual brownout procedure actions are: a) flush the current state to MCU flash memory (EEPROM) so that the work can be automatically resumed; b) wait for a power-on condition.

In this way, the device is able to significantly reduce flash memory wearing. It turns to be a significant winning factor in cheaper MCUs, as they only allow about 100,000 EEPROM write cycles max.

And it comes for cheap, as the only thing you need is an opto-coupler plus a bit of software wizardry.

Re:The design of littlefs: A fail-safe filesystem designed f... by delfinom on Saturday August 31, 2019 @09:32AM (#20846161)
Nope. Because brown-out detectors on micros are meant for momentary glitches and unreliable. The detectors can preserve a brown out detected bit in their reset cause registers. However, they don't guarantee the micro's core will get the interrupt in time before losing power. And if the brown-out goes down to POR levels, you won't even get the brown out cause bit and instead POR.

The best you can do for file systems is track a "dirty flag". You keep it set unless you power down willingly yourself.

Re:The design of littlefs: A fail-safe filesystem designed f... by londons_explore on Saturday August 31, 2019 @09:59AM (#20846286)
You design the circuit... You can decide exactly how many microseconds of power you'll get between the brownout interrupt occurring and the lowest operating voltage. Your job as designer is to write the code to guarantee completion in that time.

Microcontrollers don't have to suffer the same issues as app/web developers. They can know exactly how many clock cycles something will take.

Re:The design of littlefs: A fail-safe filesystem designed f... by ianhowson on Saturday August 31, 2019 @10:45AM (#20846567)
Except that:

(a) you don't choose how long you get because the uC manufacturer already chose. (An external brownout warning is different, and useful, but still not a good solution to the problem of FS integrity.)

(b) those thresholds have nothing to do with the operating thresholds of the flash (which usually require a lot more energy to do work than the uC)

(c) writing to flash is a relatively high-power operation, and if you need to erase, you're doing the highest-power operation at a time when you have no power buffer

(d) warmup time to do an erase is measured in milliseconds, and you probably don't have that much power buffer

The best you can do at brownout is minimize the damage, because your uC is about to switch off and/or silently inject errors.

Re:The design of littlefs: A fail-safe filesystem designed f... by neltnerb on Saturday August 31, 2019 @11:01AM (#20846659)
I don't personally usually bother, but an EE approach would be to put a diode between the 3.3V supply and the MCU/flash power supply pins with a large bulk capacitor, and use the voltage just before the diode as your brownout detection. It can't discharge back into the power supply or other parts on the board because of the diode so the capacitor just needs to store enough charge to run for a specified time after power loss.

Then you can configure how long the MCU runs after power shuts down. Put 470uF there and I imagine you'd have your milliseconds easily.

Re:The design of littlefs: A fail-safe filesystem designed f... by neltnerb on Saturday August 31, 2019 @12:38PM (#20847337)
Another reply mentioned that many MCUs don't like it when power is applied too slowly. Here is a version I just made as an example that uses two MOSFETs and a resistor divider to delay application of power to the MCU and flash memory. It relies on the N-channel MOSFET turn on voltage along with the resistor divider to set the turn on voltage, and when it reaches the threshold the P-channel MOSFET turns on.

Note the resistor R4 provides hysteresis -- after the P-channel MOSFET activates it acts to keep the N-channel MOSFET active unless the voltage drops below some level set by R2, R3, R4, and the turn on voltage of Q1. You just need to tune them appropriate for your components, and granted of course it adds some amount a bit to your BOM cost. But if you need it you need it.

So basically, R2/R3/Q1 set your turn on voltage (experimentally determined or maybe with Spice) and R4 sets your turn off voltage with R2/R3/Q1. The MCU probably uses under 500mA so these parts are pretty cheap... the capacitor is about 5 cents (https://www.digikey.com/product-detail/en/kemet/ESK477M6R3AE...) and the MOSFETs that looks appropriate are in the 6 cent each range.

https://i.postimg.cc/Df9N8fM5/delayed-turn-on-circuit.png

Re:The design of littlefs: A fail-safe filesystem designed f... by londons_explore on Saturday August 31, 2019 @06:28PM (#20849196)
Most mcus are fine with power being applied slowly as long as they have a separate reset signal. It's good practice to have a separate reset signal anyway, to avoid unintended behaviour while the circuit is powering up.
Re:The design of littlefs: A fail-safe filesystem designed f... by kevin_thibedeau on Sunday September 01, 2019 @11:52AM (#20852897)
You are still asking for trouble. Once something goes into latchup, reset is unlikely to recover. If not the MCU, then some peripheral on the same supply may express it's dislike for intermediate voltages.
Re:The design of littlefs: A fail-safe filesystem designed f... by nine_k on Saturday August 31, 2019 @12:31PM (#20847289)
A compact and reliable 470 uF capacitor could cost comparable to to a low-end uC.
Re:The design of littlefs: A fail-safe filesystem designed f... by londons_explore on Saturday August 31, 2019 @06:29PM (#20849200)
The low end uC's use so little power that a 1uF capacitor would be plenty for a few milliseconds.
Re:The design of littlefs: A fail-safe filesystem designed f... by ClumsyPilot on Sunday September 01, 2019 @12:20PM (#20853073)
I mean, you get what you pay for. If your are cost-optimising that hard, you probably aren't controlling something with critical data.
Re:The design of littlefs: A fail-safe filesystem designed f... by ianhowson on Sunday September 01, 2019 @09:15PM (#20856060)
If you aren't cost-optimizing that hard, you aren't shipping meaningful volumes yet. 470uF isn't cheap at 10k+ units.

My last project couldn't use a giant cap because of size, not cost constraints.

Re:The design of littlefs: A fail-safe filesystem designed f... by xondono on Saturday August 31, 2019 @01:08PM (#20847502)
A lot of time is way easier and more reliable to have a secondary battery (Li coin cell) for this kind of purpose.
Re:The design of littlefs: A fail-safe filesystem designed f... by craftinator on Saturday August 31, 2019 @10:56AM (#20846626)
But you can just... Decide how many clock cycles you'll need to finish shutdown, calculate that power usage, then solder a corresponding power source in place. There really isn't guesswork.
Re:The design of littlefs: A fail-safe filesystem designed f... by ianhowson on Saturday August 31, 2019 @11:23AM (#20846799)
Flash over SPI isn't very predictable. There's a boatload of internal state -- they silently do R/M/W, there's internal stepup converters with unknown voltage levels, temperature variation greatly impacts write time, and, depending on your software stack, you might need to do an erase.

Assuming you only need to execute one Flash transaction (doubtful!), covering all of the variation might take upwards of 50ms.

So, yeah, you could just throw a massive cap on the power rail. Except that that conflicts with the other power, cost and size constraints implicit in the context of microcontroller systems.

Re:The design of littlefs: A fail-safe filesystem designed f... by xondono on Saturday August 31, 2019 @01:14PM (#20847532)
In some situations this might be an unsolvable equation.

If your power supply is already 1.8V, most uC will not have much room for procedures when a POR/BOR is detected.

Re:The design of littlefs: A fail-safe filesystem designed f... by rnestler on Saturday August 31, 2019 @08:39AM (#20845943)
In one project I worked on we monitored the 24V rail and when it was dropping, the MCU had enough time to do cleanup /shutdown work until the 3.3V supply dropped.
Re:The design of littlefs: A fail-safe filesystem designed f... by adamweld on Saturday August 31, 2019 @12:14PM (#20847190)
anything with a 24v rail has much more power budget/bulk capacitance than most small embedded devices... so what worked there may not work well in other more common cases
Re:The design of littlefs: A fail-safe filesystem designed f... by kartoffelwaffel on Saturday August 31, 2019 @07:35AM (#20845723)
Also why not chuck a capacitor in the mix to provide a little power buffer to shutdown the filesystem gracefully.
Re:The design of littlefs: A fail-safe filesystem designed f... by delfinom on Saturday August 31, 2019 @09:33AM (#20846169)
Many MCUs do not appreciate slow rise-times when using their internal regulators. Then you start oversizing your regulator or providing them replacement regulators. Which then opens more cans of worms, etc, etc.
Re:The design of littlefs: A fail-safe filesystem designed f... by leggomylibro on Saturday August 31, 2019 @11:53AM (#20847032)
Many voltage regulators have 'power good' pins that you can tie to the MCU's reset line, though.
Re:The design of littlefs: A fail-safe filesystem designed f... by neltnerb on Saturday August 31, 2019 @12:47PM (#20847389)
And if you don't have this, I posted this circuit example I just made for a delayed power turn on time above. Capacitor is about 5 cents, the transistors are about 6 cents each. If you need it you need it, but power good to the reset line is better.

https://i.postimg.cc/Df9N8fM5/delayed-turn-on-circuit.png

Or just put a capacitor after the reset line pullup so that it effects delayed turn on during boot.

Re:The design of littlefs: A fail-safe filesystem designed f... by leggomylibro on Saturday August 31, 2019 @01:23PM (#20847589)
Oh, neat trick - thanks for sharing!
Re:The design of littlefs: A fail-safe filesystem designed f... by Tempest1981 on Saturday August 31, 2019 @02:15PM (#20847915)
What is the voltage drop across D1 and Q2? Is that ever an issue?
Re:The design of littlefs: A fail-safe filesystem designed f... by neltnerb on Saturday August 31, 2019 @11:37PM (#20850359)
The drop across D1 can be significant, using a Schottky helps. Usually within the tolerance of the power supply in any case.

But a capacitor on the reset pin is pretty good ;-)

Re:The design of littlefs: A fail-safe filesystem designed f... by Gibbon1 on Saturday August 31, 2019 @04:58PM (#20848750)
Those microcontrollers aren't reliable anyways. If you design those in you have to accept that if they have persistent state they will eventually brick themselves. If they don't have persistent state they will hang and require user intervention on a regular basis.
Re:The design of littlefs: A fail-safe filesystem designed f... by pantalaimon on Saturday August 31, 2019 @07:50AM (#20845773)
It adds quite some space and probably costs as much as the MCU.
Re:The design of littlefs: A fail-safe filesystem designed f... by kabdib on Saturday August 31, 2019 @06:42PM (#20849274)
Yeah, paying thirty cents for a capacitor to coddle a ten-cent MCU doesn't make a bunch of sense. Much cheaper to teach your software folks how to write transactional updates to that flash. Typically you pay a small speed penalty to get update atomicity (in addition to having a more complicated implementation).
Re:The design of littlefs: A fail-safe filesystem designed f... by keithnz on Saturday August 31, 2019 @08:31AM (#20845921)
not as simple either, you still need to detect that the power is gone or fading independent of the capacitor...which all adds up