how do I use startup_default_middle_hook in my program

jrdarrah

Well-known member
I think I have a slow power supply problem with my Teensy 3.1 when running it via 2 AA batteries and a 5V up converter. I want to see how far into the startup code it gets before it hangs. I have a blue and a red led available so my plan was to add a startup_default_middle_hook in my program to turn on the LED. I'm using blink to keep things simple. Since I have to have the USB connected to upload the program I expected no issues. In my code I'd expect the Red LED to come on but it doesn't. The orange LED on pin 13 works.

pins_teensy.c has this definition:

Code:
static void startup_default_middle_hook(void) {
void startup_middle_hook(void)	__attribute__ ((weak, alias("startup_default_middle_hook")));

I'm using Arduino IDE 1.8.19 and Teensy loader 1.56.
How do I get the startup process to call my startup_default_middle_hook routine? I also tried defining a startup_middle_hook instead. Got the same result -- no red led.

Code:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// Pin 11 has the LED on Teensy 2.0
// Pin 6  has the LED on Teensy++ 2.0
// Pin 13 has the LED on Teensy 3.0
// give it a name:
#define BlueLED 16               // indicator when MIDI is enabled, only used with USB connection so no power drain
#define RedLED 17                // redLED
int led = 13;

// the setup routine runs once when you press reset
void startup_default_middle_hook()
{
    pinMode(BlueLED, OUTPUT);               //port to drive LED
  digitalWrite(BlueLED, LOW);             // LED off to start
  pinMode(RedLED, OUTPUT);               //port to drive LED
   digitalWrite(RedLED, HIGH);  

  }
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
 
Use it like this.

Code:
extern "C" void startup_middle_hook(void);

void startup_middle_hook() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void setup() {
}

void loop() {
}
 
extern definition got the program working, but still not via battery

Use it like this.

Code:
extern "C" void startup_middle_hook(void);

void startup_middle_hook() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void setup() {
}

void loop() {
}

Thanks for the help. The hook now gets called when using USB to power but not when running via battery even though I see 4.67V at VIN. I have a scope coming next week to look at the power supply rise time. I'll open a new topic when I gather additional data points.
 
Back
Top