diff --git a/examples/pioasm_i2sout.py b/examples/pioasm_i2sout.py new file mode 100644 index 0000000..36d437b --- /dev/null +++ b/examples/pioasm_i2sout.py @@ -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") diff --git a/examples/pioasm_neopixel.py b/examples/pioasm_neopixel.py index cf72847..00ced1f 100644 --- a/examples/pioasm_neopixel.py +++ b/examples/pioasm_neopixel.py @@ -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, diff --git a/examples/pioasm_pdm.py b/examples/pioasm_pdm.py new file mode 100644 index 0000000..852d33b --- /dev/null +++ b/examples/pioasm_pdm.py @@ -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") diff --git a/examples/pioasm_simpletest.py b/examples/pioasm_simpletest.py index 494467a..ac3694c 100644 --- a/examples/pioasm_simpletest.py +++ b/examples/pioasm_simpletest.py @@ -9,7 +9,7 @@ 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 """ @@ -17,9 +17,8 @@ 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)