aboutsummaryrefslogtreecommitdiff
path: root/fsm/adc.c (unfollow)
Commit message (Collapse)AuthorFilesLines
2024-03-27increased voltage precision from 0.025V to 0.02VSelene ToyKeeper1-29/+29
(so 0 to 255 now goes from 0.00V to 5.10V)
2023-11-28fixed incorrect temperature history for a few seconds after wakingSelene ToyKeeper1-11/+9
(it used raw ADC units instead of cooked Kelvin units ... ... which was only noticeable on hardware which has different ADC units)
2023-11-28fsm/adc: removed dead codeSelene ToyKeeper1-57/+32
2023-11-22ADC voltage: battcheck 3 digits, fixed t1616, switched back to 8-bit ↵Selene ToyKeeper1-18/+29
internal volt unit Before this branch, `voltage` was 6 bits: Volts * 10 A couple patches ago, I upgraded it to 16 bits: 65535 * Volts / 10.24 That costs too much extra ROM on attiny85 though, for extra precision it doesn't even use... so I switched back to an 8-bit value. It's still more precise than before though: Volts * 40 ... and battcheck displays an extra digit now, on devices with ROM for it. ... and battcheck waits a second to get a more accurate measurement before displaying the first value. It has *much* less variation between first and later readings now. Also: - got t1616 builds working again (tested fc13 and thefreeman-boost-fwaa) - upgraded t1616 voltage and temp to 12-bit (10 bits + 4x oversampling) - removed expensive temp conversion from t1616 ADC interrupt - recalibrated t1616 bogomips again; runs faster after interrupt fix - increased t1616 internal VDD measurement resolution by 36% (1.5V Vref, not 1.1V) - fixed sloppy setting of Vref bits I still need to test / update other t1616 builds, and fix all the t85 + t1634 code and build targets.
2023-11-21got ADC voltage+temp working on avrdd... but broke all other builds/MCUsSelene ToyKeeper1-2/+29
This patch changes the ADC code to use two internal standard units, and everything else must convert to these units: - FSM Volts: centiVolts << 6 (range 0 to 10.24 V per cell) - FSM Kelvin: Kelvin << 6 (range 0 to 1024 K) UI-level voltage is still "Volts * 10", and temperature is still Celsius. FSM expects functions to be provided, to convert from the hardware's raw ADC measurements to these internal units: `voltage_raw2cooked()` and `temp_raw2cooked()`. Defaults will be provided by arch/*.[ch] for each MCU type, or the hwdef can make its own. Anyway, gotta go fix all the other MCUs and builds now. :(
2023-11-19started refactoring fsm/adc.*, but need a checkpoint before continuingSelene ToyKeeper1-19/+4
2023-11-10refactor checkpoint: splitting MCU-specific code into arch/$MCU.[ch]Selene ToyKeeper1-139/+20
Phew, that's a lot of changes! And there's still a lot more to do...
2023-11-02reorganized project files (part 1)Selene ToyKeeper1-0/+0
(just moved files, didn't change the contents yet, and nothing will work without updating #includes and build scripts and stuff)
2023-07-08Partially fixed oscillating aux LED voltage colors while asleep.Selene ToyKeeper1-9/+14
Amplitude is smaller, frequency is slower, but it can still happen sometimes at color boundaries on models with bright aux LEDs on high mode. (because that's not a measurement error, it's actually oscillating voltage) The Wurkkos TS11 in particular tends to bounce when crossing certain color boundaries, because the aux LEDs are bright and change the voltage enough to push it back and forth across the boundary. Also sped up voltage measurement during first few seconds after turning off, to compensate for slowdown caused by the lowpass filter... while slowing down measurements afterward to slow the oscillation. I tried a bunch of different methods, spanning a range of "stable but slow" to "fast but unstable", and kept code clauses for three representative methods. The one enabled by default is the best compromise I found to reduce the issue as much as possible while keeping readings reasonably responsive.
2023-07-08Fixed spurious voltage warnings in attiny1616 sleep mode. Fixed by SammysHP.Selene ToyKeeper1-9/+7
https://budgetlightforum.com/t/anduril-2-feature-change-suggestions/218045/467 I was never able to reproduce the issue here, but the fix seems good and others tested it successfully.
2023-04-26made sleep voltage work on attiny1616 againSelene ToyKeeper1-0/+25
(oops, it has no "ADC Noise Reduction" mode... needs different setup code)
2023-04-25fixed bug behind K93_LOCKOUT_KLUDGE which could exit lockout in solid aux modeSelene ToyKeeper1-0/+2
(also reduced avg standby power by about 15 uA) (also fixed oscillating voltage mode colors, I think) The bug happened because sometimes sleep LVP would get triggered, because the ADC would read zero under some conditions. This in turn happened because the ADC needs the MCU to be at least partially awake in order to finish a measurement. So in standby mode, with the MCU only waking up very briefly to send a sleep tick and go back to sleep, the ADC required several cycles (like 375ms to 625ms) to finish a single measurement. This varied depending on how many instructions the MCU executed while it was awake. In the single-color mode, so few instructions were being executed that the ADC seemed to time out and abort its measurement, returning a zero. Then a low voltage warning was sent, which knocked the light back into "Off" mode. Adding no-op instructions inside the single-color clause was sufficient to prevent the ADC from timing out, because it kept the MCU awake just barely long enough. But it was a kludge, and it still took like half a second to finish a measurement, and the measurements were noisy. It also used more power, because it required keeping the ADC powered on far too long. This fix puts the MCU into "ADC Noise Reduction" mode instead, when a voltage measurement is needed during sleep. It reduces noise to make measurements more stable... but more importantly, it lets the measurement finish in like 0.5ms instead of 500ms. So it uses less power and isn't dependent on the number of calculations the MCU does during each "sleep tick". As a bonus, this can also measure voltage much more often, while still using less total energy than before. It was once every 8 seconds, and now it's once per second. Avg power use in aux low mode, on a D4Sv2: (avg of 30k samples each) - before: 101 uA - after: 86 uA
2023-04-17switched the rest of FSM + Anduril to use SPDX license headersSelene ToyKeeper1-22/+5
instead of full GPL headers (or all too often, nothing at all) There are a few "FIXME" entries where I'm not sure about the correct copyright.
2023-04-16reduced ROM by ~600 bytes by moving all eeprom config values to a "cfg" structSelene ToyKeeper1-7/+7
(this also made some parts of the code cleaner)
2023-03-28just whitespace / commentsSelene ToyKeeper1-1/+1
2022-10-21fixed voltage calibration resolution on SP10 ProSelene ToyKeeper1-2/+2
and other devices which use a voltage divider (it was 0.1V per step, and is now 0.05V per step)
2021-12-08added a compile option for USE_LOWPASS_WHILE_ASLEEP,Selene ToyKeeper1-2/+20
but it doesn't actually fix the issue I was hoping it'd fix, so it's disabled by default (when the battery is right on a threshold between colors for aux LED "voltage" mode, it can bounce between colors until the cell isn't on the boundary any more... but a simple lowpass doesn't really help) (but I also didn't want to throw out the code, in case it's useful later as a reference for a more effective solution)
2021-01-12Add Sofirn SP10S (adapter) and dual-voltage logicGabriel Hart1-0/+4
2020-12-18Add AVR 1-Series and t1616 board and scriptsGabriel Hart1-1/+52
2020-09-07added support for external thermal sensors in fsm-adc.cSelene ToyKeeper1-0/+14
2020-08-23attempt to fix laggy voltage readings right after waking, on FW3ASelene ToyKeeper1-5/+4
(with no sleep ticks, the voltage value never gets reset to the raw value, and instead only goes through the lowpass filter) (this fix is not yet tested... will test before uploading) (also carries a risk of messing up thermal values after being asleep, so that needs to be tested too)
2020-07-31added a voltage calibration / correction function, 7C from battcheck to ↵Selene ToyKeeper1-2/+12
configure
2020-04-27set default thermal response magnitude back to how it was before the KR4 ↵Selene ToyKeeper1-1/+1
updates, because it worked well on many hosts before...
2020-04-19added tweakable thermal_response_magnitude option, adjusted KR4 thermal ↵Selene ToyKeeper1-10/+10
vars, made KR4 rainbow mode faster
2020-04-19made thermal response larger when error is large, smaller when error is smallSelene ToyKeeper1-2/+8
(helps on KR4, but will probably need to add the response magnitude thing to adjust speed per build target)
2020-03-26made thermal regulation use a smaller target window, and prioritize cooling moreSelene ToyKeeper1-3/+3
2020-03-16fixed calc_voltage_divider() (use 10-bit calibration values, not 8-bit)Selene ToyKeeper1-1/+1
2020-03-16merged some misc fixes from pakutrai, cleaned up comments, removed unused ↵Selene ToyKeeper1-2/+3
symbols
2020-03-16reduced regulation jitter by biasing errors toward zero by a constant amount,Selene ToyKeeper1-1/+14
which mostly impacts small errors (and reduces jitter during the flat phase of regulation) while leaving large errors pretty much unaffected... also, made acceptable thermal window smaller to make up for this new extra tolerance
2020-03-15the ADC sample count doesn't need to be 16-bit any more, and isn't really a ↵Selene ToyKeeper1-2/+3
count any more... ... just a boolean flag for whether this is the first sample or a later sample (so I changed it and reduced the ROM size by ~28 bytes)
2020-03-15removed dead comments and dead codeSelene ToyKeeper1-28/+2
2020-03-15replaced temperature_timer (which wasn't even being used) with a variable ↵Selene ToyKeeper1-27/+43
delay between warnings, so large warnings can remain frequent while small warnings are separated by more time, based on a cumulative error counter which must pass a threshold before the next warning is sent (this is producing good test results so far on D4v2 and D4Sv2)
2020-03-13tried to make thermal code a bit less twitchy...Selene ToyKeeper1-7/+10
it regulates really fast on D4, but once it's stable, the adjustments are too large
2020-03-05initial support for Noctigon KR4Selene ToyKeeper1-0/+6
(not complete, but far enough that it installs and runs) New hardware support features: - allow using PCINT other than 0 (PCINT1, PCINT2, etc) - option to ignore voltage ADC while the button is pressed (because my prototype shorts the voltage divider to 0 while the button is down)
2020-02-28went back to continuous lowpass because it had the best noise reductionSelene ToyKeeper1-51/+73
(also, now treating smoothed ADC values as 11-bit, with the lowest 5 bits chopped off to eliminate noise)
2020-02-05brute force method for reducing ADC noise -- average a ridiculous number of ↵Selene ToyKeeper1-19/+13
samples (because, for some reason, even though 64 samples is plenty in a test program, it ends up being extremely erratic when used inside Anduril... and I'm not sure why) also, use 15-bit ADC values instead of 16 bits, in the temperature logic (to help protect against integer overflows) ... but this code still doesn't work well. It regulates down *very* fast, and then gradually rises until the next extra-fast drop-down. :( ... also, tempcheck mode sometimes changes by 4-5 C between readouts, which is worrisome. ... and factory reset is still broken.
2020-02-05still doesn't work, but at least it's a bit less broken than before...Selene ToyKeeper1-8/+12
(ceiling value was all wrong, and the response magnitude was way too big) (also, temperatures here are unsigned, since freezing is about 270 in ADC units)
2020-02-05first pass at a smaller simpler thermal regulation algorithm...Selene ToyKeeper1-75/+52
... doesn't work well, but I'm saving it so I can experiment with other methods and maybe revert back later.
2020-01-30saving state of ADC / WDT refactoring before doing more changes... what ↵Selene ToyKeeper1-69/+38
changed so far: - removed LVP lowpass and thermal regulation lowpass logic; it's probably redundant now - slowed ADC deferred logic timing to 4X per second instead of 16X, because there doesn't seem to be much reason to do it any faster - reduced thermal event rate-limit to just 1 second, for more responsive regulation - added "EV_temperature_okay" signal, to help stop adjustments at an appropriate time instead of going to far - sped up sleep LVP to one measurement every 8 seconds instead of 16, to help the aux LEDs respond to voltage changes faster (effect on standby time is negligible) - make sure the WDT doesn't set the ADC channel or counter... except in standby mode
2020-01-30switched to a pseudo-rolling-average method to reduce noise, set prescaler ↵Selene ToyKeeper1-8/+13
back to 64
2020-01-29increased effective ADC resolution by switching from continuous-lowpass to ↵Selene ToyKeeper1-64/+43
sum-of-discrete-window method
2020-01-29remove truncation noise by adding 0.5 to each resultSelene ToyKeeper1-24/+22
(keeps stable values within ~0.46 to ~0.54 range, so truncated result has no noise)
2020-01-29rewrote ADC code to use a continuous lowpass system on all measurements, to ↵Selene ToyKeeper1-121/+175
eliminate noise and maybe increase precision (thermal code still needs to be rewritten though)
2019-12-17fixed too-slow thermal response (was introduced in the irq-refactor branch)Selene ToyKeeper1-5/+8
2019-11-25calibrated Noctigon K1, changed voltage divider calibration values to 10-bit ↵Selene ToyKeeper1-1/+1
(was 8-bit before)
2019-11-19reduced build size by a few bytesSelene ToyKeeper1-1/+1
2019-11-19fixed ADC cycles running 2X as fast as intendedSelene ToyKeeper1-8/+8
2019-11-14added a lowpass filter for battery voltage measurementsSelene ToyKeeper1-37/+22
(but only on attiny1634 devices, since it costs a bit of space and isn't strictly necessary)
2019-11-14fixed ADC code; measures and behaves correctly now, and is easier to read...Selene ToyKeeper1-39/+37
... but factory reset's auto-calibrate still doesn't get the right values for some reason (manual calibration works, but not auto)
2019-11-14started refactoring ADC code to split voltage and temperature into their own ↵Selene ToyKeeper1-210/+222
functions