Skip to content

Commit 1a4e3eb

Browse files
committed
going for v0.3.0
1 parent 106c211 commit 1a4e3eb

18 files changed

Lines changed: 739 additions & 0 deletions

experiments/picopio/pio_7seg.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import time
2+
import rp2
3+
from machine import Pin
4+
5+
@rp2.asm_pio(out_init=(rp2.PIO.OUT_LOW,) * 4,
6+
out_shiftdir=rp2.PIO.SHIFT_RIGHT,
7+
autopull=True, pull_thresh=4)
8+
def prog():
9+
pull()
10+
out(pins, 4)
11+
12+
13+
sm_1 = rp2.StateMachine(0, prog, freq=2000, out_base=Pin(10))
14+
sm_2 = rp2.StateMachine(1, prog, freq=2000, out_base=Pin(18))
15+
16+
sm_1.active(1)
17+
sm_2.active(1)
18+
19+
sm_1.put(0b1111)
20+
sm_2.put(0b0000)
21+
time.sleep(1000)
22+
23+
24+
25+
oo_1 = [ 0b0111, 0b0100, 0b0011, 0b0110, 0b0100, 0b0110, 0b0111, 0b0100, 0b0111, 0b0110, 0b1000, 0b0011 ]
26+
oo_2 = [ 0b0111, 0b0001, 0b1011, 0b1011, 0b1101, 0b1110, 0b1100, 0b0011, 0b1111, 0b1111, 0b0000, 0b1110 ]
27+
28+
for d in range(0, 12):
29+
sm_1.put(oo_1[d])
30+
sm_2.put(oo_2[d])
31+
time.sleep(1)
32+
33+
blink_sm.active(0)
34+
sm_1.active(0)
35+
sm_2.active(0)

experiments/picopio/pio_blink.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import time
2+
import rp2
3+
from machine import Pin
4+
5+
6+
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
7+
def blink():
8+
wrap_target()
9+
set(pins, 1) [31]
10+
nop() [31]
11+
nop() [31]
12+
nop() [31]
13+
nop() [31]
14+
set(pins, 0) [31]
15+
nop() [31]
16+
nop() [31]
17+
nop() [31]
18+
nop() [31]
19+
wrap()
20+
21+
@rp2.asm_pio(out_init=(rp2.PIO.OUT_LOW,)*4,
22+
out_shiftdir=rp2.PIO.SHIFT_RIGHT,
23+
autopull=True, pull_thresh=4)
24+
def prog():
25+
pull()
26+
out(pins, 4)
27+
28+
29+
sm_1 = rp2.StateMachine(0, prog, freq=2000, out_base=Pin(10))
30+
sm_2 = rp2.StateMachine(1, prog, freq=2000, out_base=Pin(18))
31+
blink_sm = rp2.StateMachine(7, blink, freq=2000, set_base=Pin(2))
32+
33+
blink_sm.active(1)
34+
sm_1.active(1)
35+
sm_2.active(1)
36+
37+
val = 0b1111
38+
sm_1.put(val)
39+
sm_2.put(val)
40+
time.sleep(5)
41+
42+
blink_sm.active(0)
43+
sm_1.active(0)
44+
sm_2.active(0)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import time
2+
import rp2
3+
from machine import Pin
4+
5+
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
6+
def blink():
7+
wrap_target()
8+
set(pins, 1) [31]
9+
nop() [31]
10+
nop() [31]
11+
nop() [31]
12+
nop() [31]
13+
set(pins, 0) [31]
14+
nop() [31]
15+
nop() [31]
16+
nop() [31]
17+
nop() [31]
18+
wrap()
19+
20+
#sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin("LED"))
21+
#sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin(25))
22+
sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin(15))
23+
24+
sm.active(1)
25+
time.sleep(3)
26+
sm.active(0)

experiments/picopio/pio_echo.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import time
2+
import rp2
3+
from machine import Pin
4+
5+
# Create a PIO program to read a number, wait for 10 cycles, and then return it
6+
@rp2.asm_pio(
7+
in_shiftdir=rp2.PIO.SHIFT_LEFT,
8+
out_shiftdir=rp2.PIO.SHIFT_LEFT,
9+
)
10+
11+
def wait_prog():
12+
pull(block) # Pull a number from the FIFO into the OSR
13+
mov(x, osr) # Move the value from the OSR to the X scratch register
14+
nop() [9] # Wait for 10 cycles
15+
mov(isr, x) # Move the value from the X scratch register to the ISR
16+
push() # Push the value from the ISR to the FIFO
17+
18+
# Instantiate a state machine with the wait program, at 2000Hz
19+
sm = rp2.StateMachine(0, wait_prog, freq=2000)
20+
21+
# Activate the state machine
22+
sm.active(1)
23+
24+
# Send a number to the state machine
25+
sm.put(123)
26+
sm.put(578)
27+
sm.put(9999)
28+
sm.put(1111)
29+
sm.put(2222)
30+
31+
# Wait for a moment to let the state machine process the number
32+
#time.sleep(0.01)
33+
34+
# Get the result back from the state machine
35+
print("Result:", sm.get()) # Will print: Result: 123
36+
# Get the result back from the state machine
37+
print("Result:", sm.get()) # Will print: Result: 578
38+
# Get the result back from the state machine
39+
print("Result:", sm.get()) # Will print: Result: 9999
40+
# Get the result back from the state machine
41+
print("Result:", sm.get()) # Will print: Result: 1111
42+
# Get the result back from the state machine
43+
print("Result:", sm.get()) # Will print: Result: 2222
44+
45+
# Deactivate the state machine
46+
sm.active(0)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import time
2+
import rp2
3+
from machine import Pin
4+
5+
# Create a PIO program to read a number, wait for 10 cycles, and then return it
6+
@rp2.asm_pio(
7+
in_shiftdir=rp2.PIO.SHIFT_LEFT,
8+
out_shiftdir=rp2.PIO.SHIFT_LEFT,
9+
)
10+
11+
def wait_prog():
12+
pull(block) # Pull a number from the FIFO into the OSR
13+
mov(x, osr) # Move the value from the OSR to the X scratch register
14+
nop() [9] # Wait for 10 cycles
15+
mov(isr, x) # Move the value from the X scratch register to the ISR
16+
push() # Push the value from the ISR to the FIFO
17+
18+
# Instantiate a state machine with the wait program, at 2000Hz
19+
sm = rp2.StateMachine(0, wait_prog, freq=2000)
20+
21+
# Activate the state machine
22+
sm.active(1)
23+
24+
# Send a number to the state machine
25+
sm.put(123)
26+
sm.put(578)
27+
sm.put(9999)
28+
sm.put(1111)
29+
sm.put(2222)
30+
31+
# Wait for a moment to let the state machine process the number
32+
#time.sleep(0.01)
33+
34+
if False:
35+
# Get the result back from the state machine
36+
print("Result:", sm.get()) # Will print: Result: 123
37+
# Get the result back from the state machine
38+
print("Result:", sm.get()) # Will print: Result: 578
39+
# Get the result back from the state machine
40+
print("Result:", sm.get()) # Will print: Result: 9999
41+
# Get the result back from the state machine
42+
print("Result:", sm.get()) # Will print: Result: 1111
43+
# Get the result back from the state machine
44+
print("Result:", sm.get()) # Will print: Result: 2222
45+
else:
46+
while True:
47+
# if data is available
48+
if not sm.empty():
49+
data = sm.get()
50+
print("Data received:", data)
51+
#await asyncio.sleep(0) # Yield to other tasks
52+
53+
# Deactivate the state machine
54+
sm.active(0)
55+
56+
57+
58+
59+
60+
61+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import time
2+
import rp2
3+
from machine import Pin
4+
5+
# Create a PIO program to read a number, wait for 10 cycles, and then return it
6+
@rp2.asm_pio(
7+
in_shiftdir=rp2.PIO.SHIFT_LEFT,
8+
out_shiftdir=rp2.PIO.SHIFT_LEFT,
9+
)
10+
11+
def wait_prog():
12+
pull(block) # Pull a number from the FIFO into the OSR
13+
mov(x, osr)
14+
mov(y, osr)
15+
label("delay") # Label for the delay loop
16+
jmp(x_dec, "delay") # Jump to the "delay" label if X is not zero, decrementing X each time mov(isr, x) # Move the value from the X scratch register to the ISR
17+
mov(isr, y) # Move the value from the Y scratch register to the ISR
18+
push() # Push the value from the ISR to the FIFO
19+
20+
# Instantiate a state machine with the wait program, at 2000Hz
21+
sm = rp2.StateMachine(0, wait_prog, freq=10000)
22+
23+
# Activate the state machine
24+
sm.active(1)
25+
26+
start_ms = time.ticks_ms()
27+
sm.put(100)
28+
res = sm.get()
29+
taken_ms = time.ticks_ms() - start_ms
30+
print(f"got result {res} in {taken_ms:.2} ms")
31+
sm.active(0)
32+
33+
34+

experiments/picopio/pio_tone.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# https://docs.micropython.org/en/latest/library/rp2.html
2+
3+
4+
import time
5+
import rp2
6+
from machine import Pin
7+
8+
# Create a PIO program to read a number, wait for 10 cycles, and then return it
9+
@rp2.asm_pio(
10+
set_init=rp2.PIO.OUT_LOW,
11+
in_shiftdir=rp2.PIO.SHIFT_LEFT,
12+
out_shiftdir=rp2.PIO.SHIFT_LEFT,
13+
)
14+
15+
def wave_prog():
16+
pull(block)
17+
mov(x, osr) # waves
18+
pull(block)
19+
label("loop")
20+
mov(y, osr) # wave half len # cycles
21+
set(pins, 1) # high
22+
label("high")
23+
jmp(y_dec, "high")
24+
mov(y, osr) # wave half len # cycles
25+
set(pins, 0) # low
26+
label("low")
27+
jmp(y_dec, "low")
28+
jmp(x_dec, "loop")
29+
set(x, 1)
30+
mov(isr, x)
31+
push()
32+
33+
# Instantiate a state machine with the wait program, at 2000Hz
34+
sm = rp2.StateMachine(0, wave_prog, freq=10000, set_base=Pin(15))
35+
36+
37+
def HWPlayTone(freq: int, duration: int):
38+
halfWaveNumCycles = int(10000 / freq / 2)
39+
waveCount = int(duration / freq / 2)
40+
print("halfWaveNumCycles", halfWaveNumCycles)
41+
print("waveCount", waveCount)
42+
sm.active(1)
43+
start_ms = time.ticks_ms()
44+
sm.put(waveCount)
45+
sm.put(halfWaveNumCycles) # 2 * (x / 10) == blink time
46+
res = sm.get()
47+
taken_ms = time.ticks_ms() - start_ms
48+
print(f"got result {res} in {taken_ms:.2} ms")
49+
sm.active(0)
50+
51+
if True:
52+
53+
HWPlayTone(1, 2)
54+
55+
else:
56+
57+
# Activate the state machine
58+
sm.active(1)
59+
60+
start_ms = time.ticks_ms()
61+
sm.put(2)
62+
sm.put(10000) # 2 * (x / 10) == blink time
63+
res = sm.get()
64+
taken_ms = time.ticks_ms() - start_ms
65+
print(f"got result {res} in {taken_ms:.2} ms")
66+
sm.active(0)
67+
68+
69+
70+

0 commit comments

Comments
 (0)