Skip to content

Add audiobusio examples and fix simple test frequency #9

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 3 commits into from
Mar 4, 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
69 changes: 69 additions & 0 deletions examples/pioasm_i2sout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# SPDX-FileCopyrightText: 2021 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import array
import math
import time
import board
import digitalio
import rp2pio
import adafruit_pioasm

trigger = digitalio.DigitalInOut(board.D4)
trigger.switch_to_output(True)

# Generate one period of sine wav.
length = 8000 // 440

# signed 16 bit
s16 = array.array("h", [0] * length)
for i in range(length):
s16[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15))
print(s16[i])

program = """
.program i2s_with_hold
.side_set 2

; Load the next set of samples
; /--- LRCLK
; |/-- BCLK
; ||
pull noblock side 0b01 ; Loads OSR with the next FIFO value or X
mov x osr side 0b01 ; Save the new value in case we need it again

set y 14 side 0b01
bitloop1:
out pins 1 side 0b10 [2]
jmp y-- bitloop1 side 0b11 [2]
out pins 1 side 0b10 [2]

set y 14 side 0b11 [2]
bitloop0:
out pins 1 side 0b00 [2]
jmp y-- bitloop0 side 0b01 [2]
out pins 1 side 0b00 [2]
"""

assembled = adafruit_pioasm.assemble(program)

dac = rp2pio.StateMachine(
assembled,
frequency=800000 * 6,
first_out_pin=board.D12,
first_sideset_pin=board.D10,
sideset_pin_count=2,
auto_pull=False,
out_shift_right=False,
pull_threshold=32,
wait_for_txstall=False,
)

trigger.value = False
dac.write(s16)
time.sleep(1)
dac.stop()
trigger.value = True

print("done")
1 change: 0 additions & 1 deletion examples/pioasm_neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
sm = rp2pio.StateMachine(
assembled,
frequency=800000 * 6, # 800khz * 6 clocks per bit
first_set_pin=NEOPIXEL,
first_sideset_pin=NEOPIXEL,
auto_pull=True,
out_shift_right=False,
Expand Down
64 changes: 64 additions & 0 deletions examples/pioasm_pdm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SPDX-FileCopyrightText: 2021 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import array
import time
import board
import digitalio
import rp2pio
import adafruit_pioasm

trigger = digitalio.DigitalInOut(board.D4)
trigger.switch_to_output(True)

# signed 16 bit
s16 = array.array("H", [0] * 10000)

# Capturing on the rising edge is the left PDM channel. To do right, swap the
# side set values.
#
# push iffull means it'll push every 32 bits and noop otherwise. noblock causes
# data to be dropped instead of stopping the clock. This allows the mic to warm
# up before the readinto.
program = """
.program pdmin
.side_set 1
in pins 1 side 0b1
push iffull noblock side 0b0
"""

assembled = adafruit_pioasm.assemble(program)

sm = rp2pio.StateMachine(
assembled,
frequency=24000 * 2 * 32,
first_in_pin=board.D12,
first_sideset_pin=board.D11,
auto_push=False,
in_shift_right=True,
push_threshold=32,
)

# Give the mic a bit of time to warm up (thanks to our noblock.)
time.sleep(0.1)

print("starting read")
trigger.value = False
# Clear the fifo to ignore old values and reset rxstall.
sm.clear_rxfifo()
sm.readinto(s16)
# Capture rxstall quickly so we can hopefully tell if we dropped data. (We
# definitely will at some point after readinto is done.)
stalled = sm.rxstall
trigger.value = True
print("read done")

if stalled:
print("missed samples")

# These are raw one bit samples. audiobusio.PDMIn does an extra filtering step.
# for v in s16:
# print(v)

print("done")
7 changes: 3 additions & 4 deletions examples/pioasm_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@

squarewave = """
.program squarewave
set pins 1 [1] ; Drive pin high and then delay for one cycle
set pins 1 ; Drive pin high and then delay for one cycle
set pins 0 ; Drive pin low
"""

assembled = adafruit_pioasm.assemble(squarewave)

sm = rp2pio.StateMachine(
assembled,
frequency=80,
init=adafruit_pioasm.assemble("set pindirs 1"),
first_set_pin=board.LED,
frequency=1000 * 2,
first_set_pin=board.D13,
)
print("real frequency", sm.frequency)

Expand Down