Skip to content

Commit a3b2df7

Browse files
authored
Update the Pico2 example for more boards (#57)
Updates the Pico2 example to use the patched RP2350 SVD file from the rust community and updates to a newer version of swift-mmio with support for emitted "dimensioned" SVD elements as arrays.
1 parent 0783cc1 commit a3b2df7

File tree

5 files changed

+64970
-106176
lines changed

5 files changed

+64970
-106176
lines changed

pico2-neopixel/Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"originHash" : "bcd9dd294c463af6cc0b00a78e58c705dd64d85ba22dabdedcb845394c3b8e0a",
2+
"originHash" : "747e0fc02915a3002cb221ceea9473d78626a10186af199b6615f04105ad994a",
33
"pins" : [
44
{
55
"identity" : "swift-argument-parser",
@@ -16,7 +16,7 @@
1616
"location" : "https://github.com/apple/swift-mmio.git",
1717
"state" : {
1818
"branch" : "swift-embedded-examples",
19-
"revision" : "37ba05e16226deb1742d9fa4d115de3b564a9b4d"
19+
"revision" : "275a877cbdd9d301a783425dbbccc630db364d37"
2020
}
2121
},
2222
{

pico2-neopixel/README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@ An example project demonstrating how to drive a Neopixel RGB LED from an RP2350.
66

77
## Requirements
88

9-
- SparkFun Pro Micro - RP2350 board. If you have another RP2350 board this example will require modifications.
9+
- An RP2350 board, such as the "SparkFun Pro Micro - RP2350".
10+
11+
## Configuring
12+
13+
This example uses the hard coded constant `LED_PIN` in `Application.swift` to select the GPIO pin used to drive the attached Neopixel RGB LED. If you are using the "SparkFun Pro Micro - RP2350" no configuration is necessary, if you are using a different board you will need to adjust this constant to the pin used to drive your LED.
14+
15+
Example diff:
16+
17+
```diff
18+
diff --git a/pico2-neopixel/Sources/Application/Application.swift b/pico2-neopixel/Sources/Application/Application.swift
19+
index f6867b5..a2291db 100644
20+
--- a/pico2-neopixel/Sources/Application/Application.swift
21+
+++ b/pico2-neopixel/Sources/Application/Application.swift
22+
@@ -11,7 +11,7 @@
23+
24+
import RP2350
25+
26+
-let LED_PIN: UInt32 = 25
27+
+let LED_PIN: UInt32 = 18
28+
29+
/// Configures GPIO pin as a front-end to PIO0.
30+
func configure_output_pin() {
31+
```
1032

1133
## How to build and run this example:
1234

pico2-neopixel/Sources/Application/Application.swift

+16-20
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
import RP2350
1313

14+
let LED_PIN: UInt32 = 25
15+
1416
/// Configures GPIO pin 25 as a front-end to PIO0.
1517
func configure_output_pin() {
1618
// Configure GPIO general properties
17-
pads_bank0.gpio25.modify { rw in
19+
pads_bank0.gpio[LED_PIN].modify { rw in
1820
rw.raw.od = 0 // Enable output
1921
rw.raw.ie = 0 // Disable input
2022
rw.raw.pue = 1 // Disable pull up
@@ -24,12 +26,12 @@ func configure_output_pin() {
2426
}
2527

2628
// Configure GPIO function selection to use PIO0
27-
io_bank0.gpio25_ctrl.modify { rw in
29+
io_bank0.gpio[LED_PIN].gpio_ctrl.modify { rw in
2830
rw.raw.funcsel = 0x6 // Forward output from pio0 to this pin
2931
}
3032

3133
// Remove pad isolation now that the correct peripheral is driving the pad
32-
pads_bank0.gpio25.modify { rw in
34+
pads_bank0.gpio[LED_PIN].modify { rw in
3335
rw.raw.iso = 0 // Disable isolation
3436
}
3537
}
@@ -45,22 +47,16 @@ func configure_output_pin() {
4547
/// `clkdiv_restart` to clear any persisted state.
4648
func configure_pio() {
4749
// Load the assembled program directly into the PIO's instruction memory.
48-
//
49-
// Ideally instr_mem would be an array but the svd2swift doesn't currently
50-
// do that.
5150
withUnsafeBytes(of: WS2812.pio_instructions) { pointer in
5251
let pio_instructions = pointer.assumingMemoryBound(to: UInt16.self)
53-
var instr_mem = pio0.instr_mem0
54-
for pio_instr in pio_instructions {
55-
instr_mem.write { w in
56-
w.raw.instr_mem0_field = UInt32(pio_instr)
52+
for (index, pio_instr) in pio_instructions.enumerated() {
53+
pio0.instr_mem[index].write { w in
54+
w.raw.instr_mem0 = UInt32(pio_instr)
5755
}
58-
// Unsafely move to the next instr_mem address.
59-
instr_mem.unsafeAddress += 0x4
6056
}
6157

6258
// Configure the PIO program wrap boundaries.
63-
pio0.sm0_execctrl.modify { r, w in
59+
pio0.sm[0].sm_execctrl.modify { r, w in
6460
w.raw.wrap_bottom = 1 // Continue at 1
6561
w.raw.wrap_top = UInt32(pio_instructions.count - 1) // Wrap after last.
6662
}
@@ -82,7 +78,7 @@ func configure_pio() {
8278
//
8379
// int: floor(1.375) = 1
8480
// frac: floor(0.375 * 255) = 95
85-
pio0.sm0_clkdiv.write { rw in
81+
pio0.sm[0].sm_clkdiv.write { rw in
8682
rw.raw.int = 1
8783
rw.raw.frac = 95
8884
}
@@ -93,18 +89,18 @@ func configure_pio() {
9389
// every 24 bits. If no data is in the txfifo, the state machine will stall.
9490
// Additional bond the RX TX fifos into one larger TX fifo so we can buffer
9591
// more pixel data.
96-
pio0.sm0_shiftctrl.modify { rw in
92+
pio0.sm[0].sm_shiftctrl.modify { rw in
9793
rw.raw.autopull = 1 // Enable autopull
9894
rw.raw.pull_thresh = 24 // 24 bit pull threshold
9995
rw.raw.out_shiftdir = 0 // Left shift from OSR
10096
rw.raw.fjoin_tx = 1 // Join RX TX fifos
10197
}
10298

10399
// Setup the PIO state machine to output to the correct gpio pins.
104-
pio0.sm0_pinctrl.modify { rw in
105-
rw.raw.set_base = 25
100+
pio0.sm[0].sm_pinctrl.modify { rw in
101+
rw.raw.set_base = LED_PIN
106102
rw.raw.set_count = 1
107-
rw.raw.sideset_base = 25
103+
rw.raw.sideset_base = LED_PIN
108104
rw.raw.sideset_count = 1
109105
}
110106

@@ -132,8 +128,8 @@ func pio_write_pixel(_ hsv: HSV8Pixel) {
132128
// Wait for the TX fifo to have space before writing to it.
133129
while tx_fifo_full() { }
134130
// Write the pixel value to TX fifo.
135-
pio0.txf0.write { w in
136-
w.raw.txf0_field = ws2812Value
131+
pio0.txf[0].write { w in
132+
w.raw.txf0 = ws2812Value
137133
}
138134
}
139135

0 commit comments

Comments
 (0)