I'm trying to make an RBG led blink at 1Hz (pink -> off -> pink -> off ...)
So I'd like to use analogWrite on the 3 pins to set the pink color, then 1s later alalogWrite to 0.
I tried with the Portenta_H7_TimerInterrupt library. It works if I use digitalWrite, but the portenta crashes if I use analogWrite to change my PWM value.
All variables use both inside and outside an ISR should be declared as volatile.
It is also possible that the timer you are using is involved with the generation of the PWM signal and you are pulling the rug from underneath your self.
Just try using one PWM pin as a test, change the number until layout find one that doesn’t crash.
You should set compiler warnings on. This should be 1000UL * 1000UL, otherwise it truncates, which is probably what @Grumpy_Mike was referring to. I make it 16.96 seconds.
This is from one of the examples in that library;
// In Portenta_H7, avoid doing something fancy in ISR, for example Serial.print
// Or you can get this run-time error / crash
In general, analogWrite is also using Timer Interrupt, and can create crashing issue if used inside ISR and must be avoided. This issue happens so far in many platforms and very implementation-dependent. Only try-and-error can guarantee which platform / core is working OK.
You can see the crashing effect, just by using analogWrite for not-PWM pin, no compile error and no warning at all, just crash.
The correct way for coding inside ISR is just using very short code, possibly just setting a flag, then use a non-ISR timer to check and do real / lengthy / complex works.