Pin Assignment#
Pin assignment keeps a component’s legal pin choices open until the board is routed. A circuit provides the mappings its component supports; another circuit requires an interface and connects to it without choosing physical pins up front. JITX resolves the choice when the routes select physical pads.
This guide uses the maintained RP2040 example in
JS3 Part 1: MCU peripheral muxing.
The code excerpts below come from that example’s jitxexamples sources.
Providers and requirements#
A bundle groups signals into one typed interface. For example,
jitx.common.GPIO contains one gpio port, while
jitxlib.protocols.serial.I2C contains sda and scl. A provider is a
method on a Circuit decorated with @provide. It
returns a list of mappings: each mapping is a dictionary from the offered
bundle’s ports to physical component pins or to ports required from another
provider.
The decorator controls how many choices can be used:
@provide(Bundle)(or@provide.all_of(Bundle)) offers each mapping independently, so several choices can be assigned at once.@provide.one_of(Bundle)offers one interface that uses exactly one of its mappings.@provide.subset_of(Bundle, n)allows up tonof the mappings to be assigned.
A consumer calls provider.require(Bundle) to obtain the interface it needs.
It keeps that required bundle in a local variable and stores the resulting
nets on its circuit.
GPIO offers#
The RP2040 wrapper offers each of its physical GPIO pins as an independent
GPIO choice. In this source, GPIO_Common is an alias for
jitx.common.GPIO:
@provide(GPIO_Common)
def self_provide_gpio(self, g: GPIO_Common):
"""Provide GPIO pins from the RP2040's 30 available GPIO pins"""
return [{g.gpio: gpio} for gpio in self.mcu.GPIO]
Each dictionary maps the offered bundle’s gpio port to one physical
self.mcu.GPIO pin. The list contains one dictionary per pin; the
@provide form allows more than one GPIO to be used by the design.
Choosing an I2C pin#
The RP2040 can put I2C0 SDA and SCL on several GPIO pins. The wrapper first defines a one-pin bundle for each signal. For example:
class I2C0_SDA(Port):
"""I2C0 SDA pin interface"""
p = Port()
"""SDA pin"""
It then offers one SDA pin from the GPIOs allowed by the RP2040 datasheet, and one SCL pin from its separate allowed set:
@provide.one_of(I2C0_SDA)
def self_provide_i2c0_sda(self, i2c0_sda: I2C0_SDA):
"""Provide I2C0 SDA pins (GPIO 0, 4, 8, 12, 16, 20, 24, 28)"""
return [{i2c0_sda.p: self.mcu.GPIO[i]} for i in [0, 4, 8, 12, 16, 20, 24, 28]]
@provide.one_of(I2C0_SCL)
def self_provide_i2c0_scl(self, i2c0_scl: I2C0_SCL):
"""Provide I2C0 SCL pins (GPIO 1, 5, 9, 13, 17, 21, 25, 29)"""
return [{i2c0_scl.p: self.mcu.GPIO[i]} for i in [1, 5, 9, 13, 17, 21, 25, 29]]
@provide.one_of means each signal takes one pin from its options. The
lists of GPIO indexes come from the RP2040’s allowed I2C0 pin functions; they
are not interchangeable with arbitrary GPIO pins.
Combining the signals into I2C#
The wrapper composes those two one-pin providers into an I2C bundle:
@provide(I2C)
def provide_i2c0(self, i2c: I2C):
"""Provide I2C0 interface using available SDA/SCL pin pairs"""
sda = self.require(self.I2C0_SDA)
scl = self.require(self.I2C0_SCL)
return [{i2c.sda: sda.p, i2c.scl: scl.p}]
Here self.require selects from the wrapper’s own SDA and SCL providers.
The outer mapping connects their p ports to the I2C bundle’s sda
and scl ports. The application can require I2C without knowing which
physical GPIOs will carry it. The wrapper also provides I2C1 through another
method, so a design can require both buses.
Using the offered interfaces#
The JS3 application circuit requires SPI and I2C from the RP2040 wrapper, then stores nets from the required ports to the flash and accelerometer interfaces. For example, its SPI connections are:
spi = self.mcu.require(SPI(cs=True))
i2c = self.mcu.require(I2C)
assert spi.cs and spi.copi and spi.cipo
flash_spi = self.flash.spi
assert flash_spi.cs and flash_spi.copi and flash_spi.cipo
self.FLASH_SCK = spi.sck + flash_spi.sck
self.FLASH_COPI = spi.copi + flash_spi.copi
self.FLASH_CIPO = spi.cipo + flash_spi.cipo
self.FLASH_CS = spi.cs + flash_spi.cs
The same circuit connects the required I2C bundle to the accelerometer and its pull-ups:
self.I2C_SDA = i2c.sda + self.accel.i2c.sda + self.i2c_pullups[0].p1
self.I2C_SCL = i2c.scl + self.accel.i2c.scl + self.i2c_pullups[1].p1
The required bundles stay local to __init__; the named nets become circuit
members. The shipping JS3 design leaves routes open, so no single pin mapping
is selected merely by creating the circuit. The JS3 verification designs show
legal and illegal physical pin choices, and the JS3 Part 1 runbook
walks through the result.
See also the jitx.net API reference for provide and
require.