Bryan Kadzban 01d4b8686a Add duty-cycle fading support
For now, only the -c3.

---

Open up LEDC fade support to all chips.

The C6 chip needs some special handling because its fade registers also
handle gamma, and the ESP chip needs some special handling because it
has two banks of channels.  The code to handle these is already present
in channel.rs, but needs to be copied and adapted.  Do that, and drop
all the esp32c3 feature checks.

---

Add a function to poll the duty-fade state

Use the unmasked interrupt bit in the LEDC register block, since that
will get updated by the hardware whether or not we've connected anything
to the interrupt source.  Also be sure to clear that bit before starting
a new fade, so it's always clear while fading.

This will allow dumb (non-async-code) polling of the fade state after
one is started by the start_duty_fade API.

---

Fix non-C3 devices to use the right int_raw bits

These are inconsistently named between the esp32 variants.

---

Add examples of hardware duty-cycle fading

Just a relatively simple zero to 100 and back to zero, over a total of 2
seconds, to get a breathing effect.

This does make the main loop{} have a 2-second period instead of the
current nearly-zero period, but nothing else is happening so that's
fine.

---

Fix two bugs in hardware fading

When figuring out how many duty-cycle changes need to happen per counter
overflow, we need to use the absolute value of the difference between
the start and end duty values, not the raw difference.  When fading from
(e.g.) 100% to 0, this will overflow, and both the debug-mode panic and
the release-mode wrapping behavior give the wrong delta value.

So calculate an absolute value difference first, and use that.

Then, when running through the while loop that allocates bits between
pwm_cycles and duty_cycle, the check on pwm_cycles was wrong -- since
the value reduces each time through the loop, we need to keep looping as
long as it's *above* some threshold, not below.

---

Simplify and refactor duty-cycle fade code

I'm not sure if this will fix the extremely-short fade times that we're
seeing with the older code, but we'll see.

Move all the calculations out of the ChannelHW implementations, and make
those *just* set registers.  The calculations are the same for all chip
variants, so don't need to be duplicated for each chip feature, like the
register macros are.

Change the calculations from a loop doing bit shifts, to an explicit
division and a couple of range checks.  This way we can get a lot closer
to the requested percentages and durations.

Use the u32::abs_diff function instead of open-coding it (now that I see
it exists).

Use u16::try_from() to limit the range of values, and use try_into<u16>
and map_err and the ? operator to more clearly handle numbers out of
range.

Drop the Result<> return type from the ChannelHW function, as it can't
fail anymore.

Fix the duty_range value -- before, when duty_exp was (say) 8,
duty_range would be 256, and if one of the *_duty_pct values was 100,
the start or end duty value would be too big.  The range of start and
end duty values is 0..255, so we have to subtract one to handle 100%.

Finally, add a comment on the is_duty_fade_running{,_hw} methods.

---

Some fades can't work; return errors for them.

Add a new Error enum value with a sub-error enum with more details.
Return it from the error cases in the fade method.

If the calculated cycles_per_step is more than 10 bits, fail as well;
the field in the register is only 10 bits wide.

Fix all the examples to run a 1-second fade instead of a 2-second, since
the 2-second fade will run into this error.  (Assert that, as well.)

---

When fading on a -c6 chip, set two more registers

The gamma functionality of -c6 chips needs two more fields set.  One
tells the chip how many gamma stages it should iterate through, but we
only implement linear fading, so always use 1.  The other tells the chip
to latch the value of the other gamma registers into the chosen slot, so
even though its value never changes, the write needs to happen.

---

Add changelog entry
2023-05-15 17:11:31 +00:00
..
2023-05-12 13:46:56 +02:00
2023-05-05 09:05:15 -07:00
2023-05-15 17:11:31 +00:00
2023-05-04 12:21:24 +01:00
2023-05-12 13:46:56 +02:00
2023-04-18 13:48:01 +02:00