RP2040 pin mapping

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
pjforde1978
 
Posts: 21
Joined: Thu Oct 30, 2014 12:48 am

RP2040 pin mapping

Post by pjforde1978 »

I am working with an RP2040 (Kee Boar) in the Arduino 2 IDE, and I am confused about pin mapping best practices.

I would like to use pins 8 and 9 for PIN_WIRE0_SDA and PIN_WIRE0_SCL. The only way I have been able to make this happen is to change the pins_arduino.h and restart the IDE. This does work, but it seems heavy-handed; not only will it impose this pin mapping on all of my projects that use a KB2040, I suspect that this change will need to be done again if a new version of the rp2040 firmware is released.

It feels like there should be a way to do this inside of a sketch, but I have not been able to figure it out.

First, apparently this is easy if you are using MicroPython:

Code: Select all

import machine
sda=machine.Pin(8)
scl=machine.Pin(9)
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
That's the exact API I want to use, except that I'm not working in Python. ("Just switch to Python" is not a productive answer, unfortunately.)

Second, the SerialPIO library seems to be able to dynamically set the pins used for TX0 and RX0 without having to modify pins_arduino.h:

Code: Select all

using Transport = MIDI_NAMESPACE::SerialMIDI<SerialPIO>;
SerialPIO pins(4, 5);
Transport serialMIDI(pins);
MIDI_NAMESPACE::MidiInterface<Transport> MIDI((Transport&)serialMIDI);
I feel like there's a solid chance that this is easy to do, and so obvious to the people who do it that it's not particularly well documented.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: RP2040 pin mapping

Post by adafruit_support_carter »

If you're using the Philhower Arduino BSP, there are a couple of set methods you can try:
https://github.com/earlephilhower/ardui ... .h#L48-L50

User avatar
pjforde1978
 
Posts: 21
Joined: Thu Oct 30, 2014 12:48 am

Re: RP2040 pin mapping

Post by pjforde1978 »

Great find, @adafruit_support_carter. Thank you.

Here is the "just the code" for anyone coming in the future who wants to skip the detective work and just set the I2C pins they want to use inside a sketch:

Code: Select all

#include <Wire.h>

void setup() {
  Wire.setSDA(8);
  Wire.setSCL(9);
  Wire.begin();
}
I'm not entirely clear on the how/why behind this approach; is Wire typically being called for us (transparently, behind-the-scenes) unless we call it explicitly? I think that I'm greatful for the abstraction, even if I am frequently frustrated by all of the abstractions.

Still, for all of the references to and suggestions for changing pins on microprocessors, it's surprising that this code isn't presented "above the fold". In particular, this technique seems like it would make sense as a built-in example sketch in the IDE, no?

I don't use SPI frequently, but it would be nice to complete the pin changing trifecta and document how to change the SPI pins inside of a sketch as well.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: RP2040 pin mapping

Post by adafruit_support_carter »

The general Arduino approach is to provide I2C interfaces, named Wire, Wire1, etc. on specific pins. Those pins are defined in the board description buried in the Board Support Package for the given platform. For example, here's the entry for the Kee Boar in the Philhower core:

https://github.com/earlephilhower/ardui ... .h#L29-L35

There is no Arduino standard for being able to dynamically change these pins in user code. It is not even possible on some boards. So the ability to do this will vary from core to core.

ESP32 is another example of a core that has added this feature:
https://github.com/espressif/arduino-esp32/issues/3779

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”