Unexpected T4 UART1 initialisation

BBUK

Member
Hi all.

When I initialise the Serial device (i.e. USB serial), it seems to unexpectedly initialise the pins for UART1 as well.

I have an application where I am using digital pins 0 and 1 as ordinary switch inputs. Closing the switch pulls the pin to ground. I naturally need pullups. Using this code:

Code:
[...]
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);

  long unsigned startTime = millis ();
  while (!Serial && ((millis () - startTime) <= 5000));

[...]

The pullups do not appear to be correctly set (pin 1 always reads low - even when the switch is not closed).

When I reorder things this way:

Code:
[...]
  long unsigned startTime = millis ();
  while (!Serial && ((millis () - startTime) <= 5000));

  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
 [...]

Everything works as expected. Digital pins 0 and 1 are UART1 and it seems to me that somehow initialising Serial (i.e. USB serial) is also initialising pins 0 and 1 as if UART1 was also to be active.

I have tested by removing the Serial initialisation line and pins 0 and 1 are correctly set as pullups.

I am using Teensyduino Beta#3.

Apologies if this is expected behaviour but please let me know if it is.

Have fun

BBUK
 
This sketch works for me.
Code:
void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);

  long unsigned startTime = millis ();
  while (!Serial && ((millis () - startTime) <= 5000));
}

void loop() {
  Serial.printf("pin0 %d  pin1 %d\n", digitalRead(0), digitalRead(1));
  delay(1000);
}

output:
pin0 1  pin1 1
pin0 1  pin1 1
pin0 1  pin1 1
I'm not sure what you mean "Beta #3". My test was on Teensy 4 with nothing connected to the pins and using Arduino 1.8.9 and Teensyduino 1.48
 
Thanks manitou.

Oops. I provided a simplified sample of my code without checking it. Your code works fine. This, however, demonstrates what I am experiencing (properly tested this time):
Code:
#include <SPI.h>

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);

  SPI1.begin();

  long unsigned startTime = millis ();
  while (!Serial && ((millis () - startTime) <= 5000));
}

void loop() {
  Serial.printf("pin0 %d  pin1 %d\n", digitalRead(0), digitalRead(1));
  delay(1000);
}

So the issue arises from the initialisation of SPI1. Now the T4 pinout card does not specify which pin takes MISO1. If this is pin1 then that explains everything and this issue can be closed (although I think the pinout card should be updated).

By beta#3, I meant the beta new version of Teensyduino that was pushed here a week or so ago. I needed this to fix a (different) issue I was having with SPI.

Thanks for taking the time to look at this.

BBUK
 
Indeed 0 and 1 is used for SPI1, one is chip select, one is MISO1, I can’t remember which is which but that would be the issue.
 
Back
Top