Author Topic: Use SPI, I2C, Serial1 pins as GPIO  (Read 1742 times)

anand

  • NewMember
  • *
  • Posts: 4
Use SPI, I2C, Serial1 pins as GPIO
« on: February 04, 2019, 10:25:37 AM »
I have a Moteino M0 with flash and radio.

One of my projects does not require flash, radio, SPI, I2C, or Serial1.

How do I repurpose these pins (SCL, SDA, MISO, SCK, MOSI, RFM SS, FLASH-MEM SS, RX1, TX1 as GPIO pins ?

I am finding that some pins (e.g. MISO) refuse to cooperate through the regular pinMode and digitalWrite commands (e.g. to light an LED).

Thanks
Anand

anand

  • NewMember
  • *
  • Posts: 4
Re: Use SPI, I2C, Serial1 pins as GPIO
« Reply #1 on: February 05, 2019, 04:01:21 AM »
I've narrowed my problem to a dependency between MISO and SS. I would like to use these pins as independent GPIO (if possible) for a matrix keypad, without the tangled behavior detailed below.
NOTE: This is based on a Moteino M0 with RFM95. No SPI/I2C configuration in the sketches.

1. If I set MISO to OUTPUT HIGH and SS to INPUT or INPUT_PULLDOWN, an LED connected to MISO and GND in series with a resistor does not light up. Works correctly if SS is left unconfigured or SS is INPUT_PULLUP.

2. The following sketch does not produce expected output when MISO and SS pins are jumpered:
Code: [Select]
void setup() {
  SerialUSB.begin(115200);
  while (!SerialUSB); //Wait for serial monitor
  pinMode(SS,INPUT_PULLUP);
  SerialUSB.println(digitalRead(SS)); // Expected:1 Actual:1
  pinMode(MISO,OUTPUT);
  SerialUSB.println(digitalRead(SS)); // Expected:1 Actual:0
  digitalWrite(MISO,HIGH);
  SerialUSB.println(digitalRead(SS)); // Expected:1 Actual:0
  digitalWrite(MISO,LOW);
  SerialUSB.println(digitalRead(SS)); // Expected:0 Actual:0
  digitalWrite(MISO,HIGH);
  SerialUSB.println(digitalRead(SS)); // Expected:1 Actual:0
}

void loop() {
}
This sketch works as expected if MISO is swapped with another pin, say pin 1.
« Last Edit: February 05, 2019, 04:08:38 AM by anand »

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Use SPI, I2C, Serial1 pins as GPIO
« Reply #2 on: February 05, 2019, 08:54:16 AM »
Generally you can reuse SPI signal pins but ONLY if the SS pins are maintained HIGH.  Otherwise the radio or flash 'thinks' it's being addressed and will drive the MISO signal.

anand

  • NewMember
  • *
  • Posts: 4
Re: Use SPI, I2C, Serial1 pins as GPIO
« Reply #3 on: February 05, 2019, 01:03:19 PM »
Generally you can reuse SPI signal pins but ONLY if the SS pins are maintained HIGH.  Otherwise the radio or flash 'thinks' it's being addressed and will drive the MISO signal.

Hi TomWS, thank you! This does explain the behavior I'm seeing.

Is there some SERCOM/PMUX incantation that will make these pins behave like regular GPIO (i.e. regain independent use of SS and MISO) ? Or is it impossible because of radio/flash on those pins ?

-Thanks
Anand

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Use SPI, I2C, Serial1 pins as GPIO
« Reply #4 on: February 05, 2019, 02:25:02 PM »
Is there some SERCOM/PMUX incantation that will make these pins behave like regular GPIO (i.e. regain independent use of SS and MISO) ? Or is it impossible because of radio/flash on those pins ?
The issue is with the behavior of the external hardware.  Unless you unsolder these, you can't use the SS pins for anything but their intended purpose.


anand

  • NewMember
  • *
  • Posts: 4
Re: Use SPI, I2C, Serial1 pins as GPIO
« Reply #5 on: February 05, 2019, 04:04:02 PM »
Good to know. Thanks for your help!