前面大致了解了一些rp2040的PIO外设,以及它的一些硬件细节,接下来我们看一下circuitPython是如何使用PIO功能点亮WS2812的。
以下是源码。
import time
import rp2pio
import board
import microcontroller
import adafruit_pioasm
program = """
.program ws2812
.side_set 1
.wrap_target
bitloop:
out x 1 side 0 [6]; Drive low. Side-set still takes place before instruction stalls.
jmp !x do_zero side 1 [3]; Branch on the bit we shifted out previous delay. Drive high.
do_one:
jmp bitloop side 1 [4]; Continue driving high, for a one (long pulse)
do_zero:
nop side 0 [4]; Or drive low, for a zero (short pulse)
.wrap
"""
assembled = adafruit_pioasm.assemble(program)
if hasattr(board, "NEOPIXEL"):
NEOPIXEL = board.NEOPIXEL
else:
NEOPIXEL = microcontroller.pin.GPIO16
sm = rp2pio.StateMachine(
assembled,
frequency=12_800_000,
first_sideset_pin=NEOPIXEL,
auto_pull=True,
out_shift_right=False,
pull_threshold=8,
)
print("real frequency", sm.frequency)
for i in range(30):
sm.write(b"\x0a\x00\x00")
time.sleep(0.1)
sm.write(b"\x00\x0a\x00")
time.sleep(0.1)
sm.write(b"\x00\x00\x0a")
time.sleep(0.1)
print("writes done")
time.sleep(2)
在引入了所需要的必要依赖库后,程序首先定义了一段汇编代码。这段汇编代码就是PIO状态机的程序。由于PIO状态机的指令空间有限,在设计时就加入了指令执行后等待N个周期的设定(可以节省nop指令占用的空间)。
program = """
.program ws2812
.side_set 1
.wrap_target
bitloop:
out x 1 side 0 [6]; Drive low. Side-set still takes place before instruction stalls.
jmp !x do_zero side 1 [3]; Branch on the bit we shifted out previous delay. Drive high.
do_one:
jmp bitloop side 1 [4]; Continue driving high, for a one (long pulse)
do_zero:
nop side 0 [4]; Or drive low, for a zero (short pulse)
.wrap
"""
然后在接下来的程序里,指定PIO输出的引脚
if hasattr(board, "NEOPIXEL"):
NEOPIXEL = board.NEOPIXEL
else:
NEOPIXEL = microcontroller.pin.GPIO16
然后初始化PIO状态机
sm = rp2pio.StateMachine(
assembled,
frequency=12_800_000, # to get appropriate sub-bit times in PIO program
first_sideset_pin=NEOPIXEL,
auto_pull=True,
out_shift_right=False,
pull_threshold=8,
)
print("real frequency", sm.frequency)
最后则是向状态机写入数据,实现WS2812灯珠的闪烁
for i in range(30):
sm.write(b"\x0a\x00\x00")
time.sleep(0.1)
sm.write(b"\x00\x0a\x00")
time.sleep(0.1)
sm.write(b"\x00\x00\x0a")
time.sleep(0.1)
print("writes done")
至此,板子上的LED灯就按红绿蓝三种颜色快闪的形式点亮啦。