defragster
Senior Member+
Using "wfi" on Teensy 4.0 1062 has been odd - in use since noted during Beta with link to NXP doc - finally found an example showing even when it 'works' - it breaks millis() and micros(). And it explains when it works as noted on T_3.x but not T_4 because millis() is not driven by an interrupt.
This example works because it was code for a forum post with a falling _isr() trigger. In adding to the code printing millis() shows it no longer changes - even after 5 minutes
What is the fix or alternative code to get "wfi" to generally work without explicitly setting up an interrupt workaround - which may still break millis()?
Output from a series of presses - millis() never changes and micros() only varies by some limited small amount based on ARM_CNT changes:
NOTE: This is running on T_4.0, when the same code runs on a T_3.6 it has no issue
This example works because it was code for a forum post with a falling _isr() trigger. In adding to the code printing millis() shows it no longer changes - even after 5 minutes
What is the fix or alternative code to get "wfi" to generally work without explicitly setting up an interrupt workaround - which may still break millis()?
Code:
// https://forum.pjrc.com/threads/60313-Configuring-Extenal-Interrupts-(Teensy-LC)?p=234803&viewfull=1#post234803
volatile bool LED_State = LOW;
volatile uint32_t iCnt = 0;
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 4000 );
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
pinMode(0, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(0), startBit_isr, FALLING);
pinMode(LED_BUILTIN, OUTPUT); // LED for debugging
LED_State = HIGH;
digitalWrite(LED_BUILTIN, LED_State);
}
uint32_t myiCnt = 0;
void loop() {
if ( myiCnt != iCnt) {
myiCnt = iCnt;
Serial.printf("_isr() @%lums %luus [iCnt%lu]\n", millis(), micros(), iCnt);
}
[B]asm volatile("wfi");[/B]
}
void startBit_isr()
{
LED_State = !LED_State;
iCnt++;
digitalWriteFast(LED_BUILTIN, LED_State);
}
Output from a series of presses - millis() never changes and micros() only varies by some limited small amount based on ARM_CNT changes:
T:\tCode\FORUM\ISR_LED\ISR_LED.ino Apr 2 2020 11:55:10
_isr() @412ms 412002us [iCnt1]
_isr() @412ms 412008us [iCnt2]
_isr() @412ms 412015us [iCnt3]
_isr() @412ms 412021us [iCnt4]
_isr() @412ms 412027us [iCnt5]
// 5-10 minutes later …
_isr() @412ms 412357us [iCnt55]
_isr() @412ms 412363us [iCnt56]
_isr() @412ms 412370us [iCnt57]
NOTE: This is running on T_4.0, when the same code runs on a T_3.6 it has no issue