Teensy 4, Interrupt is blocking USB Sketch Upload

Richardson

Active member
I have this code:

Code:
attachInterrupt(digitalPinToInterrupt(CLOCK_PIN), interrupt_func, RISING);

FASTRUN void interrupt_func() {

  noInterrupts();
  
  // Waiting for LOW
  while(digitalReadFast(CLOCK_PIN) == HIGH) {  } 

  // wait 4x 280ns  cycles inside this interrupt  
  delayNanoseconds(1050);
 
  interrupts();
}


No Sketch upload is possible. I have to press the Teensy Reset Button to upload a new sketch everytime.

It there a fix for this problem?
 
That is not a complete .INO file.
You need to write a complete ino file for Arduino/Teensyduino to compile and upload to a Teensy.
What is being programmed into the Teensy by pressing the Program button (it's NOT A TEENSY RESET BUTTON) is the last file successfully compiled.
 
No Sketch upload is possible. I have to press the Teensy Reset Button to upload a new sketch everytime.

Yes, indeed, every Teensy has a pushbutton dedicated to entering programming mode (not reset to re-run your program) because you can create programs like this which stay inside an interrupt, which prevents the USB interrupt from running.

Hopefully it's easy to understand Teensy can't respond to any USB requests if your program prevents the USB interrupt from running.


It there a fix for this problem?

Yes.

You could delete that while loop and otherwise change your program so you don't stay inside the interrupt for a long period of time.

You could delete the noInterrupts() line, and raise the USB interrupt priority, so it is able to interrupt your interrupt code.

You could keep your code exactly as-is, but change the hardware so CLOCK_PIN never stays at logic high for a long time. Maybe use a 555 timer chip or similar circuitry?
 
Many thanks for the answers. Yes, I could change the code, but that is difficult in this case. If I turn the board off and on again, then the upload will start. It's not ideal, but ok. This is better than always pressing the button, as it is difficult to access.
 
Back
Top