Designing a custom lithium charger: what firmware controls and what must stay in hardware
When no charger chip fits the pack, the source or the product, the charger is built from parts: a power stage of discrete MOSFETs, usually a buck or buck-boost, current and voltage references, often set by a DAC, measurement of the pack’s current and voltage, and firmware that runs precharge, constant current, constant voltage, termination and every fault. The firmware then owns every control point a chip would have trimmed in the factory, so the accuracy comes from your divider, reference and ADC: one microcontroller’s internal reference alone is specified at ±1.5 percent, about 63 mV on a 4.2 V cell, three times the ±0.5 percent of a good charger chip. What must stay in hardware is whatever protects the cells when the firmware is wrong, hung or blank: overvoltage, overcurrent and temperature cut-offs with their own references, and power switches that stay off while the processor is in reset.
The phases are defined in How lithium-ion charging works, the tolerance arithmetic is in the tolerances article, and protection and balancing have their own articles. Here they meet in one design, where you write the parts a chip would have hidden.
The large packs I designed chargers for were around 30 V each, custom and multi-cell, 10 to 25 Ah across a couple of versions, and the product they were in recharged three to six of them from an external supply. There were no charger chips: discrete FETs, DACs and the rest, and I wrote the firmware and protections.
When to build one, and when a chip is the better answer
Use a chip when one fits. Its loops are compensated, its references trimmed, and its timers, thermal regulation and battery detection characterized over temperature; a discrete design has to earn each of those in its own testing.
Building can be worth it when no chip covers the combination: a high-voltage multi-cell pack at several amps, several packs on one supply, or a profile the chips don’t offer, such as the temperature-stepped currents some cell datasheets give. Controller chips exist for many multi-cell packs, so check first. A middle path is a controller chip for the power stage and its fast loops, with firmware setting its references and running the state machine.
The power stage
A buck converter suits an input that’s always above the pack’s charge voltage plus headroom; a four-switch buck-boost suits one that overlaps the pack’s range, as the sources article works through. Unlike a power supply’s, its output is a battery that can push current back, and its load can vanish at full current.
A synchronous buck can run backwards
In a synchronous buck the low-side switch replaces the diode, and a switch conducts both ways. Ripple that dips below zero at light load only circulates; the stage pushes net charge back out of the battery when its average current goes negative, and then it runs as a boost from the battery to its own input. That happens when the commanded voltage falls below the pack’s, as at start-up into a charged pack while a soft-start ramps up from zero, or when the voltage setpoint steps down in a warm band; when a current-sense offset near zero lets the loop aim slightly negative; and when the input goes away while the stage keeps switching. With the low-side switch on across an example 29.4 V pack and 22 µH, reverse current rises at about 1.3 A per microsecond. The last case is the quiet one: the stage holds its own input up, the processor never sees the supply leave, and the pack drains into whatever else is on the input. The fixes are diode emulation at light load (the low-side switch turns off when the current reaches zero), a start-up that measures the pack and begins with the reference at its voltage, an input-voltage monitor that stops switching when the supply goes, and a reverse-current comparator.
The high-side switch’s body diode is a second path: when the input falls below the pack, the pack conducts through it to the input, switching or not. Blocking it needs an input switch with its body diode facing the other way, or an ideal-diode controller, which holds about 20 mV across its MOSFET and turns it off within about 0.45 µs typical once the current reverses. A block at the input stops back-feed into the supply, but charge the stage boosts is then trapped on its input capacitor, so the stage itself still has to stop; a blocking switch on the pack side prevents both.
When the pack lets go mid-charge
A pack’s protection can open at full current, or the pack can be pulled out. In constant current the loop keeps pushing current into the only thing left, the output capacitor, and the voltage climbs at I / C: an example 5 A into 100 µF is 50 mV per microsecond. If the current is measured at the pack, after the output capacitor, the open pack reads zero and the loop drives harder, so I becomes the stage’s hardware current limit, not the setting, and ceramic capacitors lose much of their value at 30 V, which steepens the slope further. A voltage loop that takes an example 50 µs to respond lets the output rise 2.5 V. With a firmware loop updating once a millisecond, nothing stops it short of the input: a buck can’t drive its output much above its input, but on a 36 or 48 V supply that’s far past a 29.4 V pack’s parts, and a buck-boost has no such ceiling. The inductor’s stored energy adds only about 0.1 V (an example 22 µH at 5 A). So the overshoot is set by how fast the voltage limit acts, and that limit has to be an analog loop or a comparator that stops switching, not a firmware decision. When the pack reconnects, the overcharged capacitor dumps into it: 2.5 V across an example 50 mΩ is 50 A.
Control: who closes the loop
In one split of the work, analog error amplifiers close both loops, one on current and one on voltage, fast but at a fraction of the switching frequency, and whichever asks for less wins. A DAC sets both references and the firmware only moves them. The handover from constant current to constant voltage is automatic, though the idle amplifier winds up toward its rail and overshoots at the handover unless it’s clamped. A firmware crash leaves the loops regulating at the last references: the pack held at its charge voltage with no termination, no timer and no temperature check, so the stage’s enable has to be one the firmware must keep refreshing (below).
In the other, firmware sets the switching duty itself. It’s flexible, but only as fast as the code, and a PWM peripheral keeps running at its last duty when the code stalls unless a hardware fault input shuts it down. The handover becomes a mode change, and the voltage loop has to start from the present duty, not from zero or full, or the output jumps. Either way, a cycle-by-cycle current limit and an output overvoltage comparator belong in hardware.
Setting the references
Resolution is rarely the problem. Charger chips set the charge voltage in steps of 16 or 32 mV per cell, so on three read 4.20 V itself isn’t available: the nearest settings are 4.192 and 4.208 V, or 4.176 and 4.208 V. An example 12-bit DAC on a 2.5 V reference, scaled to 30 V at the pack, steps 7.3 mV, about 1 mV per cell on an example seven-cell pack.
The accuracy is harder. The setpoint is only as good as the DAC’s reference and the feedback divider, and if the ADC that checks the result uses the same reference, an error in it moves the setpoint and the measurement together, so the firmware sees nothing wrong. The overvoltage cut-off needs a reference of its own for the same reason.
Measuring voltage
On the examples read, one microcontroller’s internal reference is ±1.5 percent and another’s is 1.0 to 1.2 V, about ±9 percent uncalibrated; one wireless chip’s ADC gain error is −3 to +4 percent, before temperature drift. On a 4.2 V cell, ±1.5 percent is 63 mV, against the 50 mV between a 4.20 V setting and the 4.25 V limit of a cell specified at 4.20 V ± 0.05 V, and the ±21 mV a ±0.5 percent charger chip holds. A processor that uses its own supply as the reference reads a divider as a fraction of that supply, so the supply’s tolerance becomes the measurement’s.
Calibration takes out the fixed part. One maker’s factory calibration brings its processor’s ADC gain and offset error to ±0.5 LSB at room temperature, which doesn’t remove an external reference’s or divider’s initial error; an end-of-line calibration of the whole board does. Drift is left. A separate reference specified at 0.2 percent initial and up to 50 ppm/°C from 0 to 70 °C can move 0.35 percent across that span, about 15 mV at 4.2 V, plus up to 100 ppm of thermal hysteresis. That alone uses most of a ±21 mV budget, leaving little for the divider, whose worst case the tolerances article works. Store calibration with a checksum and a range check, and refuse to charge if either fails.
On a series pack, the pack voltage isn’t the number that matters: a charger holding an example seven-cell pack at 29.4 V can still take one cell past its limit, so the charge has to answer to the pack monitor’s per-cell readings.
Measuring current, from fast charge to termination
On packs of 10 to 25 Ah, a 0.02C to 0.05C termination runs from 200 mA to 1.25 A, while an example 0.5C fast charge on an example 25 Ah pack is 12.5 A. Through an example 5 mΩ shunt that’s 62.5 mV and 0.78 W at the top, and 1 mV at the bottom. Fed straight to a microcontroller ADC with an offset specified at ±4 mV, the offset alone is ±0.8 A, four times the smallest termination current. With an example gain of 20 ahead of the ADC it’s ±40 mA at the shunt, still a fifth of 200 mA. The amplifier’s own offset isn’t divided by the gain: an example 0.5 mV is another 100 mA. A shunt in the high side at 30 V adds common-mode error: a discrete difference amplifier at gain 20 with 0.1 percent resistors rejects common mode only about 5,000 to one, about 6 mV at the shunt, over an amp, moving with the pack voltage; a current-sense amplifier built for it, or a low-side shunt, avoids that. So measure the zero with the power stage off and nothing else flowing in the shunt, subtract it, as gauge chips do with their autocalibration, and repeat it as the temperature changes. A single-supply amplifier clips a negative offset at zero, so give its output a small pedestal, which reverse-current detection needs anyway.
Offset matters more when the firmware also counts charge, because the count integrates it. One gauge’s integrating converter has a typical input offset of 10 µV; across the example 5 mΩ that’s 2 mA, 48 mAh a day, about 14 percent of a 10 Ah pack in a month if nothing corrects it. That’s a gauge’s best case; the ±40 mA left above would be about 1 Ah a day.
Sample against the switching. A shunt at the pack, after the output capacitor, sees mostly DC; one in the switching path sees the inductor’s ripple, and a reading at a random point in the cycle is off by up to half of it. Triggering the conversion mid-way through the on-time reads the average in continuous conduction, but at the light currents where termination is decided the stage may run discontinuously, especially with diode emulation, and a mid-on-time sample then reads well above the average. So measure termination current on the DC side, after the output capacitor, or filtered well below the switching frequency.
The state machine
| State | Enters when | Does | Leaves when | Time limit on chips |
|---|---|---|---|---|
| Detect | Input present | Checks that a pack is there and reads it | Pack found and in range; otherwise waits or faults | — |
| Precharge | Lowest cell below the precharge threshold, typically 2.5 to 3.0 V | A small current, often 10 to 20 percent of fast charge | Above the threshold plus hysteresis | 30 minutes to 4 hours |
| Constant current | Above the threshold, temperature in the window | The charge current, reduced in a cool band | Charge voltage reached | 5 to 20 hours for fast charge |
| Constant voltage | At the charge voltage | Holds the voltage while the current tapers | Current below termination for a set time | Within the fast-charge timer |
| Done | Terminated | Watches the voltage | Falls about 60 to 265 mV per cell below the charge voltage | — |
| Fault | Any limit, timer or check fails | Power stage off, and stays off | Cause cleared, and a defined reset | — |
With no chip, every control point in that table is a decision in the firmware.
Where I see designs go wrong is each of those decisions made at its nominal value. The work is following each one out to where tolerance, temperature and the system load can put it on a real unit, and deciding what the charger does when a reading lands right on the edge.
A cell far below the precharge threshold may be past safe recovery, and the state machine should refuse it below the cell maker’s floor; the lifetime article gives the makers’ floors.
Copy a chip’s behaviour where it’s well thought out. Chips suspend termination while they’re limiting for heat or input current, because a reduced current looks like the end of the taper, and one family runs its timer at half rate meanwhile. Every threshold gets hysteresis and a time filter, and a charge paused for temperature holds its state with the timer paused.
Detection is where chips differ. One pulls its output below regulation with a 6 to 10 mA sink and watches whether it recovers; another sources about 6 µA and decides there’s no battery if its output rises past regulation plus about 100 mV. Both leave their status output off rather than showing “charging”. A discrete charger can do the same, with a source strong enough to move its output against its own feedback divider. A pack behind an open protection switch reads like no pack, so detect has to decide what it applies to wake one and what it refuses. A charger for a family of packs also has to know which one it has: 12.5 A is 0.5C on an example 25 Ah pack and 1.25C on a 10 Ah one, so read an identity from the pack, and default to the lowest current when it can’t.
Timers that survive a reset
A watchdog reset mid-charge mustn’t hand the pack fresh timers. If the charge state and elapsed time live in ordinary memory, every reset restarts the safety timer, and a charger that resets every few minutes never times out. Keep the state, the elapsed time and a reset count in memory that survives a reset, with a checksum; on start-up, read the reset cause, resume with the time already used, and fault after a set number of resets in one charge.
Decide what the fallback is. Serial-bus charger chips show the trap: when their watchdog expires they return to their defaults, which on three read is a 4.208 V charge voltage and about 2 A, safe for some cells and not others, and a host that disables the watchdog to avoid that keeps its last settings for as long as it’s hung. For a discrete charger the safe fallback is almost always off.
Several packs from one supply
Several packs on one supply are a power budget and a set of switches. As an example, four seven-cell 10 Ah packs at 0.5C take 5 A each at up to 29.4 V, about 150 W a pack and 590 W together before conversion losses, so either the supply is sized for all of them or the firmware decides who charges when.
The layouts trade parts for time:
- A converter per pack: each charges independently, and a fault stays in its channel.
- One converter switched between packs in turn: fewer power parts, but the total time is the sum of the charges, and each switch-over is a disconnect at current unless the current is ramped down first; the output also has to be brought to the next pack’s voltage before its switch closes, or the output capacitor dumps into it.
- One converter into several packs at once: packs at different voltages don’t share a voltage source evenly, the flattest takes the most current, and a full pack can feed a flat one. Limiting current per pack brings back most of a converter per pack.
Whatever the layout, each pack needs its own back-to-back switches, so it can be both blocked and switched out: no pack can drive the shared bus, the other packs or the input when the supply goes away, and a faulted or unselected pack is disconnected even when the bus is above it. An ideal diode alone blocks only one way, and a single MOSFET conducts through its body diode, as the sources article explains. A faulted pack is switched out and reported while the others carry on, and each channel needs its own fuse, so a shorted switch in one doesn’t take the shared supply down. When the supply can’t feed every pack, the priority is a decision, such as nearest-full first for a usable pack soonest, and staggered starts with a shared input limit in hardware keep the supply from collapsing when several packs start at once.
When something fails
Decide the behaviour for each failure before writing the code:
- Reset and power-up. Processor pins are typically inputs during reset, so every gate driver, enable and switch needs a resistor that holds it off, and the current reference has to command zero until the firmware sets it. Check the DAC’s power-on output; it isn’t always zero.
- A crash or hang. A PWM peripheral or DAC holds its last setting until the watchdog acts. Make the power stage’s enable a signal the firmware has to keep toggling, through a charge pump or a monostable, so a stuck pin can’t hold it on, and give the watchdog its own clock and a window, serviced from the main loop rather than an interrupt that keeps running when the main loop hangs.
- Brown-out mid-charge. Gate-drive and input undervoltage lockouts are separate from the processor’s brown-out reset, which has to be enabled, and memory that survives a reset isn’t guaranteed through a brown-out, so decide what a power cycle mid-charge resumes. A supply that sags when charging starts gives a reset loop with a fresh inrush each time: cap the retries and back off.
- A sensor reading wrong. A reading stuck at a plausible value is the dangerous case. Cross-check the output voltage against the sum of cells, the shunt against the converter’s own current sense, and each thermistor against its neighbours, and fault on a disagreement beyond the error budget. Stale or missing cell data from the pack monitor stops the charge too.
- Corrupted calibration. Refuse to charge; never fall back to nominal values silently.
- One voltage measurement. Add a hardware comparator with its own reference and divider beside the ADC, so no single drift or failed resistor moves both.
What stays in hardware
Hardware protection is the layer that works with the processor blank, crashed or wrong. For a multi-cell pack:
- Overvoltage per cell, in the pack: secondary protectors for two to four cells, stacked to cover the pack, compare each cell with an internal reference, at fixed thresholds from 4.30 V upward for 4.2 V cells. A limit on the charger’s output voltage, with its own reference, catches a runaway loop or the disconnect overshoot, but a sum can’t see one cell over.
- Overcurrent: a cycle-by-cycle limit in the power stage, plus the pack’s own tiers. A trip sensed across a MOSFET moves with its temperature: on the protection switches read, on-resistance at a 125 °C junction is typically about 1.4 to 1.5 times its 25 °C value, more on maximum figures and on higher-voltage parts. The hardware limit is sized for the power stage, not the pack, so on a small pack the pack’s own protection covers the difference.
- Temperature: a window comparator, hot and cold, on the sensors most likely to see the extremes, removing the power stage’s enable directly, so a cold cell and an open thermistor, which reads cold, stop the charge as well as a hot one.
- A shorted high-side switch, the worst single fault: it connects the supply to the pack through the inductor, and stopping the switching does nothing. It needs an independent series switch the hardware trips can open, or a fuse sized to clear it.
- A fuse per pack, rated for the highest voltage it can see, the supply’s as well as the pack’s, and able to interrupt the pack’s short-circuit current, which on a multi-amp-hour pack is far beyond a single cell’s. Fuse makers say the voltage rating must be at least the battery’s maximum open-circuit voltage, and small surface-mount fuses are commonly rated 32 V DC: below an eight-cell pack’s 33.6 V, and below the supply of a buck charging a seven-cell one.
Hardware trips latch until a defined reset, so a fault can’t clear itself and restart the charge.
The JEITA guideline puts the principle plainly: “A protection system shall also be prepared and implemented in case of faulty charging control by a charger”. Firmware adds what comparators can’t, such as a slowly rising temperature, a cell charging faster than its neighbours or a pack slow to leave precharge, layered on the hardware rather than in place of it.
Standards when firmware is part of the protection
IEC 62133-2, the safety standard for portable lithium cells and batteries, says “Each battery should have an independent control and protection for current, voltage, temperature and any other parameter required for safety and to maintain the cells within their operating region.” It allows that protection to be “within the charger or the end devices”, provided the battery maker passes the safety information on, and asks for a fault analysis of the protection circuit. It doesn’t say the protection must be hardware, or that it can’t be software. A reasonable reading of “independent” is that protection sharing the charger’s processor, reference and firmware isn’t independent of the charger it guards against. Where software is part of the protection, the functional-safety standards for software in controls are where that evidence comes from; check which apply to your product.
For the product, IEC 62368-1, for audio, video, information and communication technology equipment, has a battery annex with clauses on protection circuits and charging safeguards, and the standard tests charging under single fault conditions. A product that charges packs separate from itself should check that annex’s scope, and whether 62368-1 applies to the product at all. Which standards apply where is in compliance by market.
The charger inside the product
The rest of the firmware can flatten the pack or mislead the charger:
- A power-state table, with the rails on in each state; the power path article has one to start from, including the blank-processor row. On a discrete charger, a blank or crashed processor should charge nothing, and the product should still be programmable on a flat pack.
- Reboot loops at low battery: a product that boots, browns out and boots again drains the pack. Reset hysteresis, a rule that a low pack stays off until it has charged past a threshold, and a cap on charge retries stop it.
- Battery level before a firmware update writes flash, so an update never starts on a pack that can’t finish it.
- The battery-sense divider: the ADC pin stays inside the processor’s supply at the highest pack voltage, including a failed charger’s output, and a switched divider switches its top, so the pin isn’t pulled up to pack voltage when it’s off.
Two sample-board faults in the power path article belong on this list: a battery measurement wired to the switched system rail, which read the input instead of the cell, and a low-battery cutoff that read the cell under its heaviest load.
A third, a USB-powered status pin wired to a 3.3 V processor pin, is in what to check before you order an AI-designed board.
Testing it
Test the failures as hard as the charging, each step safe if the last found a problem:
- A simulated pack: a bench supply that can sink as well as source, swept through every threshold, with each transition, timer and hysteresis checked. Hold the processor in reset with the input applied, then run it blank; the power stage stays off.
- Every fault injected in every state: each sensor open, shorted and stuck at a plausible value; calibration corrupted; the watchdog forced; the supply browned out; a shorted high-side switch; the hardware trips with the firmware told to ignore them.
- Disconnects at the highest current and input voltage, the pack pulled and its protection opened, with a scope on the output; loop stability measured into a battery and into no battery.
- Sources plugged and pulled in every state, and every combination of packs present, absent, full, flat and faulted.
- The charge window’s edges in a chamber, with the calibration’s drift measured rather than assumed.
- Real packs, attended, then long runs logging every state change, reset cause, timer, voltage and current, so a rare fault leaves a record.
The bench order for the pack’s own protection is in the protection article. Production test has to check each unit’s calibration against a reference, because a firmware charger is only as accurate as the calibration it was given (designing for production test).
If you’re designing a charger from discrete parts, or want a second look at which protections sit in firmware and which in hardware, get in touch.