File tree Expand file tree Collapse file tree 4 files changed +138
-6
lines changed Expand file tree Collapse file tree 4 files changed +138
-6
lines changed Original file line number Diff line number Diff line change 1+ # SPDX-FileCopyrightText: 2021 Scott Shawcroft, written for Adafruit Industries
2+ #
3+ # SPDX-License-Identifier: MIT
4+
5+ import audiocore
6+ import audiopwmio
7+ import board
8+ import digitalio
9+ import array
10+ import time
11+ import math
12+ import rp2pio
13+ import adafruit_pioasm
14+
15+ trigger = digitalio .DigitalInOut (board .D4 )
16+ trigger .switch_to_output (True )
17+
18+ # Generate one period of sine wav.
19+ length = 8000 // 440
20+
21+ # signed 16 bit
22+ s16 = array .array ("h" , [0 ] * length )
23+ for i in range (length ):
24+ s16 [i ] = int (math .sin (math .pi * 2 * i / length ) * (2 ** 15 ))
25+ print (s16 [i ])
26+
27+ program = """
28+ .program i2s_with_hold
29+ .side_set 2
30+
31+ ; Load the next set of samples
32+ ; /--- LRCLK
33+ ; |/-- BCLK
34+ ; ||
35+ pull noblock side 0b01 ; Loads OSR with the next FIFO value or X
36+ mov x osr side 0b01 ; Save the new value in case we need it again
37+
38+ set y 14 side 0b01
39+ bitloop1:
40+ out pins 1 side 0b10 [2]
41+ jmp y-- bitloop1 side 0b11 [2]
42+ out pins 1 side 0b10 [2]
43+
44+ set y 14 side 0b11 [2]
45+ bitloop0:
46+ out pins 1 side 0b00 [2]
47+ jmp y-- bitloop0 side 0b01 [2]
48+ out pins 1 side 0b00 [2]
49+ """
50+
51+ assembled = adafruit_pioasm .assemble (program )
52+
53+ dac = rp2pio .StateMachine (
54+ assembled ,
55+ frequency = 800000 * 6 ,
56+ first_out_pin = board .D12 ,
57+ first_sideset_pin = board .D10 ,
58+ sideset_pin_count = 2 ,
59+ auto_pull = False ,
60+ out_shift_right = False ,
61+ pull_threshold = 32 ,
62+ wait_for_txstall = False ,
63+ )
64+
65+ trigger .value = False
66+ dac .write (s16 )
67+ time .sleep (1 )
68+ dac .stop ()
69+ trigger .value = True
70+
71+ print ("done" )
Original file line number Diff line number Diff line change 2828sm = rp2pio .StateMachine (
2929 assembled ,
3030 frequency = 800000 * 6 , # 800khz * 6 clocks per bit
31- init = adafruit_pioasm .assemble ("set pindirs 1" ),
32- first_set_pin = board .D12 ,
3331 first_sideset_pin = board .D12 ,
3432 auto_pull = True ,
3533 out_shift_right = False ,
Original file line number Diff line number Diff line change 1+ # SPDX-FileCopyrightText: 2021 Scott Shawcroft, written for Adafruit Industries
2+ #
3+ # SPDX-License-Identifier: MIT
4+
5+ import board
6+ import digitalio
7+ import array
8+ import time
9+ import rp2pio
10+ import adafruit_pioasm
11+
12+ trigger = digitalio .DigitalInOut (board .D4 )
13+ trigger .switch_to_output (True )
14+
15+ # signed 16 bit
16+ s16 = array .array ("H" , [0 ] * 10000 )
17+
18+ # Capturing on the rising edge is the left PDM channel. To do right, swap the
19+ # side set values.
20+ #
21+ # push iffull means it'll push every 32 bits and noop otherwise. noblock causes
22+ # data to be dropped instead of stopping the clock. This allows the mic to warm
23+ # up before the readinto.
24+ program = """
25+ .program pdmin
26+ .side_set 1
27+ in pins 1 side 0b1
28+ push iffull noblock side 0b0
29+ """
30+
31+ assembled = adafruit_pioasm .assemble (program )
32+
33+ sm = rp2pio .StateMachine (
34+ assembled ,
35+ frequency = 24000 * 2 * 32 ,
36+ first_in_pin = board .D12 ,
37+ first_sideset_pin = board .D11 ,
38+ auto_push = False ,
39+ in_shift_right = True ,
40+ push_threshold = 32 ,
41+ )
42+
43+ # Give the mic a bit of time to warm up (thanks to our noblock.)
44+ time .sleep (0.1 )
45+
46+ print ("starting read" )
47+ trigger .value = False
48+ # Clear the fifo to ignore old values and reset rxstall.
49+ sm .clear_rxfifo ()
50+ sm .readinto (s16 )
51+ # Capture rxstall quickly so we can hopefully tell if we dropped data. (We
52+ # definitely will at some point after readinto is done.)
53+ stalled = sm .rxstall
54+ trigger .value = True
55+ print ("read done" )
56+
57+ if stalled :
58+ print ("missed samples" )
59+
60+ # These are raw one bit samples. audiobusio.PDMIn does an extra filtering step.
61+ # for v in s16:
62+ # print(v)
63+
64+ print ("done" )
Original file line number Diff line number Diff line change 99
1010squarewave = """
1111.program squarewave
12- set pins 1 [1] ; Drive pin high and then delay for one cycle
12+ set pins 1 ; Drive pin high and then delay for one cycle
1313 set pins 0 ; Drive pin low
1414"""
1515
1616assembled = adafruit_pioasm .assemble (squarewave )
1717
1818sm = rp2pio .StateMachine (
1919 assembled ,
20- frequency = 80 ,
21- init = adafruit_pioasm .assemble ("set pindirs 1" ),
22- first_set_pin = board .LED ,
20+ frequency = 1000 * 2 ,
21+ first_set_pin = board .D13 ,
2322)
2423print ("real frequency" , sm .frequency )
2524
You can’t perform that action at this time.
0 commit comments