Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Teensy-LC Watchdog and Serial.begin not working together

  1. #1
    Junior Member
    Join Date
    Dec 2022
    Posts
    4

    Teensy-LC Watchdog and Serial.begin not working together

    I created a simple Watchdog timer program but Serial.begin interferes with its operation.
    When I run with the "Serial Monitor" window open, it runs fine. When its closed, it does not.
    I need it run under both conditions.
    I am using a Teensy-LC board.
    I am posting the code here to see if someone can help.

    #define LOOP_DELAYS 500

    extern "C" void startup_early_hook(void);

    void startup_early_hook(void) {
    SIM_COPC = 0x0C; // 1024 ms
    //SIM_COPC = 0x08; // 256 ms
    //SIM_COPC = 0x04; // 32 ms
    }

    void watchdog_reset() {
    SIM_SRVCOP = 0x55;
    SIM_SRVCOP = 0xAA;
    }

    void setup()
    {
    pinMode(13, OUTPUT);
    // Calling Serial.begin causes the Watchdog timer to reset continuously
    // However, if it's run with the "Serial Monitor" Window open, it runs fine.!!!
    // I need the watchdog timer to run with the serial port active and with the "Serial Monitor" window closed or open.
    // What's missing???

    Serial.begin(57600);
    }

    void loop() {
    watchdog_reset();
    digitalWrite(13,HIGH);
    delay(LOOP_DELAYS);
    digitalWrite(13,LOW);
    delay(LOOP_DELAYS);
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Take out the call to Serial.begin(), or put it before the watchdog is started.

    Serial.begin() is not needed to start USB Serial and can wait some ~2 secs time before returning unless Serial connects sooner.

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Source code shows this - 2 second delay - perhaps 750 ms if(Serial) doesn't arrive: ...hardware\teensy\avr\cores\teensy4\usb_serial.h
    Code:
    	// Serial.begin(baud) is optional on Teensy.  The main USB device port
    	// is always initialized early during startup.  The baud rate setting
    	// is not used.  Communication occurs at USB native speed.  For
    	// compatibility with Arduino code, Serial.begin waits up to 2 seconds
    	// for your PC to open the virtual serial port.
            void begin(long baud_unused) {
    		uint32_t millis_begin = systick_millis_count;
    		while (!(*this)) {
    			uint32_t elapsed = systick_millis_count - millis_begin;
    			if (usb_configuration) {
    				// Wait up to 2 seconds for Arduino Serial Monitor
    				if (elapsed > 2000) break;
    			} else {
    				// But wait only 3/4 second if there is no sign the
    				// USB host has begun the USB enumeration process.
    				if (elapsed > 750) break;
    			}
    			yield();
    		}
    	}

  4. #4
    Junior Member
    Join Date
    Dec 2022
    Posts
    4
    I added the watchdog start after the Serial.begin() as follows:
    Serial.begin(57600);
    SIM_COPC = 0x0C;

    and the uP continuously resets if I don't open the "Serial Monitor" window. The behaviour has not changed.

    I suspect the code starts up with the watchdog timer enabled, so enabling it after does not make a difference.

    I I disable the watchdog timer before, I cannot enable it afterwards, due to only the 1st call to SIM_COPC being addressed. shown below, this results in the watch dog timer staying disabled:

    SIM_COPC = 0x00;
    Serial.begin(57600);
    SIM_COPC = 0x0C;

  5. #5
    Junior Member
    Join Date
    Dec 2022
    Posts
    4
    I forget to mention that I commented out the SIM_COPC line in the startup_early_hook() as shown:

    void startup_early_hook(void) {
    // SIM_COPC = 0x0C; // 1024 ms
    //SIM_COPC = 0x08; // 256 ms
    //SIM_COPC = 0x04; // 32 ms
    }

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    What happens if the Serial.begin() is not included?

    As noted, it is not needed. But after it completes it shouldn't be a factor.

    However, the watchdog may be feeding/watchdog_reset()

  7. #7
    Junior Member
    Join Date
    Dec 2022
    Posts
    4
    Thank you for the feedback.
    Removing the Serial.begin() call was the trick. This function is not compatible with WatchDog Timer calls due to its excessive delay.
    The code worked well without this function.

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Glad to hear that was the issue as suspected - and that work there can continue!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •