-
Notifications
You must be signed in to change notification settings - Fork 957
rp2040: SPI implementation #1991
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
Conversation
Let me know of any comments so I can apply fixes and suggestions to i2c and PWM implementation branches! |
Have you tested yet? If not, i can test when i'm free. |
@efectn I have not. Having trouble building TinyGo :( |
I've managed to build tinygo and flash programs to the pico. Currently dont have SPI devices on hand so I'll probably be PRing the i2c branch as soon as I get it working and then fix this branch (already noticed a couple bugs in how I use ReplaceBits) |
So I have not gotten the driver working. I'm going to take a break from this driver and try the PWM out. I've managed to get the Loopback function working but struggling reading data from my SPI device (see details below). Example program with loopback testpackage main
import (
"device/rp"
"machine"
"time"
)
var (
LED = machine.GPIO25
b = machine.SPI{Bus: rp.SPI0}
uart = machine.UART0
MISO = machine.GPIO4
CS = machine.GPIO5
SCK = machine.GPIO6
MOSI = machine.GPIO7
)
func main() {
// configure UART to print stuff
{
err := uart.Configure(machine.UARTConfig{BaudRate: 9600})
if err != nil {
panic(err)
}
}
println("start program")
time.Sleep(time.Millisecond)
err := b.Configure(machine.SPIConfig{Frequency: 8.1e6,
SDO: MOSI,
SDI: MISO,
SCK: SCK})
if err != nil {
println(err.Error())
} else {
println("spi baud:", b.GetBaudRate())
}
// Loopback test.
b.Bus.SSPCR1.SetBits(rp.SPI0_SSPCR1_LBM)
for i := 0; i < 10; i++ {
ret, err := b.Transfer(uint8(i))
if err != nil {
println(err.Error())
}
println(i, ret)
}
// Output:
// 0 0
// 1 1
// 2 2
// 3 3
// 4 4
// 5 5
// 6 6
// 7 7
// 8 8
// 9 9
// End loopback test.
b.Bus.SSPCR1.ClearBits(rp.SPI0_SSPCR1_LBM)
LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
CS.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
CS.High() // Motorola SPI has Active Low.
// Do SPI stuff
for { // blinky when done.
time.Sleep(time.Second)
LED.Set(!LED.Get())
}
} |
In order to test this on my rp2040 pico board, I had to add the following code:
|
By the way, the SPI interface worked on my pico using an APA102 LED strip. Good work @soypat! |
I think you still need to add the following to // SPI on the RP2040
var (
SPI0 = &_SPI0
_SPI0 = SPI{
Bus: rp.SPI0,
}
SPI1 = &_SPI1
_SPI1 = SPI{
Bus: rp.SPI1,
}
) Add that, and squash everything into a single commit, and I think we are about ready to merge this! 😸 |
spi working with loopback SPI working apply @deadprogram's suggestions consolidate SPI board pin naming fix up SPI configuration add feather-rp2040 SPI pins add arduino connect SPI pins add SPI handle variables
Thanks very much for all the work on this PR! Now merging. |
Not tested yet. I've never contributed before and looking forward to learning what the workflow looks like. Solves #1987