Start up condition of I/O pins

Hi @casm

There are two things going on here:

  • The STM32F103 does not appear to drive its digital pins to known values immediately at reset. Only the analog pins are defined to high-impedance inputs at reset. If you read the ST Micro forum posts about this, non-Spark users of this processor have problems too.

  • The Spark code intentionally drives some digital pins low briefly during the start-up code. The reason for this is being looked at, but it happens out side your code. There are lots of posts here about the outputs “glitching” at startup.

So you will have a lot fewer problems if you switch your circuit to the analog pins.

1 Like

@casm, I regret having to say you are right and there actually seems to be a problem I didn’t experience before.
When I earlier said: “I can’t see this on my Core”, I did test this on my local build Core that never saw a new Spark firmware :blush:

But when I flashed this test sketch OTA

//SYSTEM_MODE(SEMI_AUTOMATIC);

void setup()
{
    pinMode(D6, INPUT_PULLUP);
    pinMode(D5, INPUT);
    pinMode(D4, INPUT_PULLDOWN);
    pinMode(D3, OUTPUT);
    pinMode(D2, OUTPUT);
    digitalWrite(D2, HIGH);
}

void loop() 
{
}

I’ve got a test circuit 3V3 -> LED -> 470Ohm -> jumper wire to connect to any pin.

And I do see your exact problem. The LED lights up and stays lit while connecting on all D pins, but not on any A pin and on RX/TX.
After connect the LED goes off on D2, D4, D5 and D6 - but not on D7 (partly expected due to on-board LED), D3 (expected due to default LOW) and unexpected on D1 and D0 (not due to I2C, since the same goes for other D pin constellations).

Second oppinions of @mdma, @Dave, @kennethlimcp, @peekay123 and @bko - who has already chimed in :+1: - are very welcome and needed I think.

Especially since several Elites did also comment that the pins are expected to be high impedance (here and in other threads).

According to @peekay123’s early post, the JTAG pins are all applied Pullups or Pulldowns during reset/power on. The JTAG pins map to D3-D7. D5 will have a pull-up until the spark firmware runs and does hardware initialization at least, and maybe even until your setup routine runs and sets its specific hardware value.

@casm, I believe the only TRUE way you can avoid the boot sequence pin-level issue is to either use analog pins or put a tri-state buffer on the digital pins that you control via a single analog pin.

1 Like

Thanks @peekay123, @kennethlimcp, @bko for your advice and @ScruffR for your advice and confirmation. @pra what you say is definitely not the case, the pin is high only briefly, it is then held low until the user code runs.
Analog pins looks like my solution shame i just got my prototypes built.

Best regards,
Clive

3 Likes

In that case it is high until the Spark hardware initialization changes the default pin mapping and mode. Seeing as that is the first thing to run after reset, it would be a very short period of time.

Hi Everyone,

I have modified my prototype board to use A0 and it now performs as expected. The forum contributors and the forum data are incredible resources, I just wish I could read and absorb everything!

Best regards,
Clive

3 Likes

In order to pull some threads about this issue together, I created a new topic in the troubleshooting section

Probably this is not the best place to put my question, but couldn’t find a better place.

I need the digital pin used as an output to be in LOW from the start.
I tried this:

void na_start()
{
   WiFi.selectAntenna(ANT_EXTERNAL);
   System.enableFeature(FEATURE_RETAINED_MEMORY);
   pinMode(D0, OUTPUT);
   digitalWrite(D0, LOW);
}
STARTUP( na_start() );

but it doesn’t fix the problem. It only makes the switch much faster than placing it in the setup function but - is there any way around this problem, or my only solution would be to reverse the logic with the use of some transistors?

How about a pull-down resistor?
Since the pin will be Hi-Z till you set the pinMode() - and there is not internal pull-resistor attached on bootup, as would be with the JTAG pins - that should work.

1 Like

@ScruffR that is an elegant and clean solution, but I never heard of attaching pull-down (or pull-up) resistors on OUTPUT digital pins. This is “by the guide book” solution?

If it’s a “weakish” pull-resistor it’ll be fine on OUTPUT too.
That’s what’s done with I2C when writing LOW against a 4k7 pull-up resistor.

1 Like

Great, i’ll try it. Just tell me, what would you call a “weakish” resistor? Like 10 ohm would be ok?

That would be too little. I’d rather go with 4k7 or 10k

Thank you, I will try that and report did it work. Thanks.