Add audiobusio examples and fix simple test frequency

This also uses newer pin auto-init.
This commit is contained in:
Scott Shawcroft 2021-02-23 16:37:25 -08:00
parent a631797c0a
commit 2adf61aae5
No known key found for this signature in database
GPG key ID: 0DFD512649C052DA
4 changed files with 138 additions and 6 deletions

71
examples/pioasm_i2sout.py Normal file
View file

@ -0,0 +1,71 @@
# SPDX-FileCopyrightText: 2021 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import audiocore
import audiopwmio
import board
import digitalio
import array
import time
import math
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")

View file

@ -28,8 +28,6 @@ assembled = adafruit_pioasm.assemble(program)
sm = rp2pio.StateMachine( sm = rp2pio.StateMachine(
assembled, assembled,
frequency=800000 * 6, # 800khz * 6 clocks per bit frequency=800000 * 6, # 800khz * 6 clocks per bit
init=adafruit_pioasm.assemble("set pindirs 1"),
first_set_pin=board.D12,
first_sideset_pin=board.D12, first_sideset_pin=board.D12,
auto_pull=True, auto_pull=True,
out_shift_right=False, out_shift_right=False,

64
examples/pioasm_pdm.py Normal file
View file

@ -0,0 +1,64 @@
# SPDX-FileCopyrightText: 2021 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import board
import digitalio
import array
import time
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")

View file

@ -9,7 +9,7 @@ import adafruit_pioasm
squarewave = """ squarewave = """
.program 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 set pins 0 ; Drive pin low
""" """
@ -17,9 +17,8 @@ assembled = adafruit_pioasm.assemble(squarewave)
sm = rp2pio.StateMachine( sm = rp2pio.StateMachine(
assembled, assembled,
frequency=80, frequency=1000 * 2,
init=adafruit_pioasm.assemble("set pindirs 1"), first_set_pin=board.D13,
first_set_pin=board.LED,
) )
print("real frequency", sm.frequency) print("real frequency", sm.frequency)