Skip to content

machine/rp2040: implement i2c #2013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/machine/board_feather_rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ const (
xoscFreq = 12 // MHz
)

// I2C Pins.
const (
I2C0_SDA_PIN = GPIO24
I2C0_SCL_PIN = GPIO25

I2C1_SDA_PIN = GPIO2
I2C1_SCL_PIN = GPIO3
)

// SPI default pins
const (
// Default Serial Clock Bus 0 for SPI communications
Expand Down
7 changes: 5 additions & 2 deletions src/machine/board_nano-rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ const (

// I2C pins
const (
SDA_PIN Pin = GPIO12
SCL_PIN Pin = GPIO13
I2C0_SDA_PIN Pin = GPIO12
I2C0_SCL_PIN Pin = GPIO13

I2C1_SDA_PIN Pin = GPIO18
I2C1_SCL_PIN Pin = GPIO19
)

// SPI pins. SPI1 not available on Nano RP2040 Connect.
Expand Down
9 changes: 9 additions & 0 deletions src/machine/board_pico.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const (
xoscFreq = 12 // MHz
)

// I2C Default pins on Raspberry Pico.
const (
I2C0_SDA_PIN = GP4
I2C0_SCL_PIN = GP5

I2C1_SDA_PIN = GP2
I2C1_SCL_PIN = GP3
)

// SPI default pins
const (
// Default Serial Clock Bus 0 for SPI communications
Expand Down
2 changes: 1 addition & 1 deletion src/machine/i2c.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build atmega nrf sam stm32 fe310 k210
// +build atmega nrf sam stm32 fe310 k210 rp2040

package machine

Expand Down
43 changes: 37 additions & 6 deletions src/machine/machine_rp2040_gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,27 @@ type pinFunc uint8
// GPIO function selectors
const (
fnJTAG pinFunc = 0
fnSPI pinFunc = 1
fnSPI pinFunc = 1 // Connect one of the internal PL022 SPI peripherals to GPIO
fnUART pinFunc = 2
fnI2C pinFunc = 3
fnPWM pinFunc = 4
fnSIO pinFunc = 5
fnPIO0 pinFunc = 6
fnPIO1 pinFunc = 7
// Connect a PWM slice to GPIO. There are eight PWM slices,
// each with two outputchannels (A/B). The B pin can also be used as an input,
// for frequency and duty cyclemeasurement
fnPWM pinFunc = 4
// Software control of GPIO, from the single-cycle IO (SIO) block.
// The SIO function (F5)must be selected for the processors to drive a GPIO,
// but the input is always connected,so software can check the state of GPIOs at any time.
fnSIO pinFunc = 5
// Connect one of the programmable IO blocks (PIO) to GPIO. PIO can implement a widevariety of interfaces,
// and has its own internal pin mapping hardware, allowing flexibleplacement of digital interfaces on bank 0 GPIOs.
// The PIO function (F6, F7) must beselected for PIO to drive a GPIO, but the input is always connected,
// so the PIOs canalways see the state of all pins.
fnPIO0, fnPIO1 pinFunc = 6, 7
// General purpose clock inputs/outputs. Can be routed to a number of internal clock domains onRP2040,
// e.g. Input: to provide a 1 Hz clock for the RTC, or can be connected to an internalfrequency counter.
// e.g. Output: optional integer divide
fnGPCK pinFunc = 8
// USB power control signals to/from the internal USB controller
fnUSB pinFunc = 9
fnNULL pinFunc = 0x1f

Expand All @@ -68,6 +81,7 @@ const (
PinInputPullup
PinAnalog
PinUART
PinI2C
PinSPI
)

Expand All @@ -91,7 +105,7 @@ func (p Pin) xor() {

// get returns the pin value
func (p Pin) get() bool {
return rp.SIO.GPIO_IN.HasBits(uint32(1) << p)
return rp.SIO.GPIO_IN.HasBits(1 << p)
}

func (p Pin) ioCtrl() *volatile.Register32 {
Expand All @@ -117,6 +131,17 @@ func (p Pin) pulloff() {
p.padCtrl().ClearBits(rp.PADS_BANK0_GPIO0_PUE)
}

// setSlew sets pad slew rate control.
// true sets to fast. false sets to slow.
func (p Pin) setSlew(sr bool) {
p.padCtrl().ReplaceBits(boolToBit(sr)<<rp.PADS_BANK0_GPIO0_SLEWFAST_Pos, rp.PADS_BANK0_GPIO0_SLEWFAST_Msk, 0)
}

// setSchmitt enables or disables Schmitt trigger.
func (p Pin) setSchmitt(trigger bool) {
p.padCtrl().ReplaceBits(boolToBit(trigger)<<rp.PADS_BANK0_GPIO0_SCHMITT_Pos, rp.PADS_BANK0_GPIO0_SCHMITT_Msk, 0)
}

// setFunc will set pin function to fn.
func (p Pin) setFunc(fn pinFunc) {
// Set input enable, Clear output disable
Expand Down Expand Up @@ -156,6 +181,12 @@ func (p Pin) Configure(config PinConfig) {
p.pulloff()
case PinUART:
p.setFunc(fnUART)
case PinI2C:
// IO config according to 4.3.1.3 of rp2040 datasheet.
p.setFunc(fnI2C)
p.pullup()
p.setSchmitt(true)
p.setSlew(false)
case PinSPI:
p.setFunc(fnSPI)
}
Expand Down
Loading