This could almost be a log for #The Rise and Fall of Pulses but we're operating in the microsecond range, which is a relatively geological timescale compared to those pulses!
There's a certain amount of curiosity that drives this project along. The pulse generation is a point in case. As you may have noticed, I've recently been playing with the new-ish ATtiny402, a part that shares many of its peripheral and register settings with two families of parts the ATtiny 0 series and ATtiny 1 series. In theory, there's a huge amount of flexibility for an engineer to switch quickly between parts because the code will be so portable. In reality, the datasheets have suffered from what appears to be a relatively high degree of copy and paste, or at least the assumption of commonality hasn't been tested. This can mean that discovering how a peripheral needs to be set up for parts like this that don't have many readily published examples, can be a "voyage of discovery" to put it mildly.
#Yapolamp needs pulses (although as I have pondered @Sander van de Bor's #ATtiny Super Capacitor Spider which drives its LEDs purely with PWM to produce a fade effect, I wonder if protection against MOSFET failing ON could allow this to be a contender for the #Yapolamp . Anyway, I'm deep into the inductor driver rabbit hole at the moment and the delights of the ATtiny402 (not its datasheet, but the IC itself!) are more than adequately holding my attention.
You may recall that I used TCB0 on the ATtiny402 to generate a nice, duration controllable pulse on the Waveform Output (WO) pin, PA6. This was lovely because using another neat trick of the ATtiny402, I can measure the continually decreasing supply voltage from the supercapacitor to the circuit and automatically adjust the pulse to assure we were achieving the same sawtooth current profile throughout the full voltage operating range, which is over 3V. By controlling the current, I am happy that this is a proxy for brightness, so I don't need to "test" that we are maintaining the same light level across the range of supply voltage. I also chose this current profile because it represents the most efficient conversion of electrons to photons, according to my shonky tests.
But all of this was using delay() and as I'm playing with the 402, why don't we see if we can generate the pause, after the pulse, during which we wait for the inductor to discharge through the LEDs, in a more convoluted way? Great, glad you're onboard with that. Options to do this with the older ATtinies would have included timers. Here's the timer option set for the 402:
We have already committed TCB0 to doing duty on the single shot pulse generator, so that's off limits. We do have TCA0 on the ATtiny402, although this is reserved in the megaTinyCore for all the Arduino timing stuff, exactly like millis(), delay(), micros() and microSeconds(). So best not to mess with that for now (although you can mess with what you like if you can live with the consequences). The last one, the RTC, is a nice little thing not really with an equivalent in the older ATtinies. It runs from the 32kHz oscillator though, so the fastest period it has is 1/32000 or ~ 30us. My sawtooth discharge at 12-14mA takes around 10us, so we'd be extending total duty from between 30-60% (for 1.8v - 4.8V respectively) down to 25-50%. Maybe not drastic but I think we can do better, and there's no guarantee that we can actually do anything within a single RTC clock cycle. In fact, as far as I can tell from the dubious datasheet, the shortest Periodic Interrupt Timer is 4 cycles, plus whatever time it takes you to service that ISR, and the fastest you could do an event driven approach is 64 cycles. Not worth investing in for this use but definitely worth returning to, perhaps as part of #ATtiny 0 Series programming on the cheap.
Events, dear boy
The new kid on the 0 and 1 series block is the core-independent peripherals. Core independent means that they don't need the CPU to service an ISR, some of them don't even need to wait for the clock ticks to trigger and many of them will work with low power modes. The Events System is like the glue that ties all your peripherals together and I wanted to try using this to optimise the driver MOSFET pulse, beginning as soon as the current had stopped discharging from the inductor through the LEDs. In voltage terms, we can see this drop in the voltage at the connection between the inductor and MOSFET.
I managed to use the Event System to glue the logic of a falling edge from the inductor attached to an input pin on the ATtiny402 and set that to generate the pulse using TCB0 in single shot mode. However, real life often turns out differently than (a badly set up) SPICE simulation. The lowest point on the inductor falling edge never got low enough to trigger a logic low on my GPIO. This meant the whole closed-loop feedback died after the first pulse. At least I could see this on my oscilloscope - it really is an amazing tool. So then I tried a voltage divider and VICTORY! There was light...
However, my joy was short lived, because when I tested this arrangement across the 1.8V - 4.8V working range of Yapolamp 1.1, above 4V this arrangement didn't work because the voltage dip didn't drop low enough. If I adjusted the voltage divider ratios, the feedback seemed to fail at one or other end of the operating voltage range.
At this point, I have found another way around. I know that the peak voltage of my inductor input into the ATtiny402 is Vcapacitor + Vforward for my LED. This means that if I "knock" Vforward off the top of the pin input, it will see more normal ranges of 0V - Vcapacitor. So far, a 7th LED is acting as my voltage drop diode in this way, with a V - 4.7k - 10k - GND voltage divider in series. This sustains the feedback loop at all operating voltages. The disadvantage is that now I have a small current across the voltage-dropper LED, so I will be looking into how to turn this off when we get to some power saving techniques for the "always on" or "find me in the dark when I'm turned off" mode.
In the following traces, yellow represents the MOSFET gate-driving pin on the ATtiny402, so high means the MOSFET is ON, blue represents the "full" voltage at the inductor and going into the LEDs, and magenta represents the "dropped" voltage going into the ATtiny402's input, from which the asynchronous pulse is generated using the Event System.
Just to clarify, the following pulse regime has NO code running in the loop() section. There is ONLY one timer and the Event System dynamically adjusting to the varying discharge durations. There is a noticeable change in brightness because the discharge durations vary with the inductor charge voltage (Vcapacitor), but we already have a solution for varying driver pulse duration automatically, which will drop nicely into loop() later!
I have suffered some painful grappling with the datasheet of this new-ish chip, thanks in part to the ambiguity and perhaps errors in the datasheet but also due to me being unfamiliar with reading microcontroller datasheets. I have certainly learned a lot and will see if I can maintain this trend for CPU-independent performance. It may come in handy when we need #Yapolamp to go to go low power...