Arduino Due stops working after one iteration

Hello all! I am using an Arduino Due, and I wrote some code to read some register values of the microcontroller. I then imported in a Due timer interrupt library, so I can read and print out register values every 30 ms.

I tested that I can read all 6 registers using the timer at 30ms. My setup() initializes the registers and the timer. It then interrupts every 30 ms to read the values, prints them out, and sets the counter back to zero. I don’t actually care what’s in the loop() function. I am primarily focused on collecting the values.

The issue I am having is that as soon as I add “Serial.print(“Printing …something…\n”)” in the myHandler() function, the program stops working. It only runs once, and then just idles doing nothing. I also noticed that the led in my loop() doesn’t light on and off. I’m not sure why adding one Serial.print() causes the program to not function. I have left the lines of code causing problems commented in the myHandler() function.

Any assistance would be appreciated!

#include <DueTimer.h>    // library I found on https://github.com/ivanseidel/DueTimer


 
volatile uint32_t count1 = 0;
volatile uint32_t count2 = 0;
volatile uint32_t count3 = 0;
volatile uint32_t count4 = 0;
volatile uint32_t count5 = 0;
volatile uint32_t count6 = 0;


// addresses of registers
volatile uint32_t *DWT_CONTROL = (uint32_t *)0xE0001000;    // set to allow read of registers
volatile uint32_t *DEMCR = (uint32_t *)0xE000EDFC;            // initialize debug register to allow DWT



volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xE0001004; // cycle count ---get numbers
volatile uint32_t *DWT_FOLDCNT = (uint32_t *)0xE0001018;   //fold count register    -7/8
volatile uint32_t *DWT_LSUCNT = (uint32_t *)0xE0001014; // load store u count register  - get numbers
volatile uint32_t *DWT_SLEEPCNT = (uint32_t *)0xE0001010; // sleep count register - 0
volatile uint32_t *DWT_EXCCNT = (uint32_t *)0xE000100C;  // exception overhead count - get numbers
volatile uint32_t *DWT_CPICNT = (uint32_t *)0xE0001008;  //cpi count  - get numbers



void myHandler()
{


       count1 = *DWT_CYCCNT;
       // Serial.print("Printing cycles\n");                                // this line of code bricks my whole program
       Serial.print(count1);
       Serial.print("\n");
       *DWT_CYCCNT = 0;
        
        
         
         
       
         count2 = *DWT_FOLDCNT;
        //   Serial.print("Printing fold\n");                                          // this line of code bricks my whole program
        Serial.print(count2);
          Serial.print("\n");
           *DWT_FOLDCNT = 0;
           
            
        
        
        
         count3 = *DWT_LSUCNT;
         //  Serial.print("Printing lsu\n");                                   // this line of code bricks my whole program
        Serial.print(count3);
          Serial.print("\n");
           *DWT_LSUCNT = 0;
          

            
           
            count4 = *DWT_SLEEPCNT;
          // Serial.print("Printing sleep\n");                                // this line of code bricks my whole program
        Serial.print(count4);
          Serial.print("\n");
           *DWT_SLEEPCNT = 0;
           
            
         count5 = *DWT_EXCCNT;
           // Serial.print("Printing exc\n");                                      // this line of code bricks my whole program
        Serial.print(count5);
          Serial.print("\n");
           *DWT_EXCCNT = 0;
           
            
        
         count6 = *DWT_CPICNT;
           //Serial.print("Printing cpi\n");                                             // this line of code bricks my whole program
        Serial.print(count6);
          Serial.print("\n");
          *DWT_CPICNT = 0; 
          
          
            
}

void setup()
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  
  *DEMCR = *DEMCR | 0x01000000;                               // enable DWT 
   *DWT_CYCCNT = 0; 
    *DWT_FOLDCNT = 0;
   *DWT_LSUCNT = 0;
    *DWT_SLEEPCNT = 0;
    *DWT_EXCCNT = 0;
    *DWT_CPICNT = 0; 
    
   *DWT_CONTROL = *DWT_CONTROL | 1; 
    *DWT_CONTROL = *DWT_CONTROL | 0x00200000; 
    *DWT_CONTROL = *DWT_CONTROL | 0x00100000; 
    *DWT_CONTROL = *DWT_CONTROL | 0x00080000;           //enables appropriate bits to all register reads
    *DWT_CONTROL = *DWT_CONTROL | 0x00040000; 
   *DWT_CONTROL = *DWT_CONTROL | 0x00020000; 

   
    
  Timer3.attachInterrupt(myHandler);                                              // set timer
  Timer3.start(30000);  // 50000 = 50ms
  

  
}

void loop()
{     
      digitalWrite(LED_BUILTIN, HIGH);                                                    // turn the LED on (HIGH is the voltage level)
      Serial.print("Loop1---------------------------------------------\n");
      Serial.print("123456789\n");
      delay(1000);                                                                                 // wait for a second
      
      digitalWrite(LED_BUILTIN, LOW);                                                    // turn the LED off by making the voltage LOW
      Serial.print("Loop2---------------------------------------------\n");
      Serial.print("987654321\n");
      delay(1000);                                                                                  // wait for a second
  
}

My guess is the extra Serial.print() calls are causing the transmit buffer to fill up, which causes the prints to go from asynchronous to blocking mode. Since you're printing from an ISR, the transmit buffer never empties and so the Serial.print() call blocks forever.

That's purely a guess though. I would never use Serial in an ISR, so I don't have any direct knowledge of the implications.

+1

Always avoid serial.print() into an interrupt function. A usual workaround consist of setting a flag in the interrupt handler, test this flag in loop() to Serial.print() accordingly and reset the flag in loop().

Plus select a higher baud rate for Serial, e.g. 250000.