File tree Expand file tree Collapse file tree 4 files changed +136
-5
lines changed Expand file tree Collapse file tree 4 files changed +136
-5
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 array
6
+ import math
7
+ import time
8
+ import board
9
+ import digitalio
10
+ import rp2pio
11
+ import adafruit_pioasm
12
+
13
+ trigger = digitalio .DigitalInOut (board .D4 )
14
+ trigger .switch_to_output (True )
15
+
16
+ # Generate one period of sine wav.
17
+ length = 8000 // 440
18
+
19
+ # signed 16 bit
20
+ s16 = array .array ("h" , [0 ] * length )
21
+ for i in range (length ):
22
+ s16 [i ] = int (math .sin (math .pi * 2 * i / length ) * (2 ** 15 ))
23
+ print (s16 [i ])
24
+
25
+ program = """
26
+ .program i2s_with_hold
27
+ .side_set 2
28
+
29
+ ; Load the next set of samples
30
+ ; /--- LRCLK
31
+ ; |/-- BCLK
32
+ ; ||
33
+ pull noblock side 0b01 ; Loads OSR with the next FIFO value or X
34
+ mov x osr side 0b01 ; Save the new value in case we need it again
35
+
36
+ set y 14 side 0b01
37
+ bitloop1:
38
+ out pins 1 side 0b10 [2]
39
+ jmp y-- bitloop1 side 0b11 [2]
40
+ out pins 1 side 0b10 [2]
41
+
42
+ set y 14 side 0b11 [2]
43
+ bitloop0:
44
+ out pins 1 side 0b00 [2]
45
+ jmp y-- bitloop0 side 0b01 [2]
46
+ out pins 1 side 0b00 [2]
47
+ """
48
+
49
+ assembled = adafruit_pioasm .assemble (program )
50
+
51
+ dac = rp2pio .StateMachine (
52
+ assembled ,
53
+ frequency = 800000 * 6 ,
54
+ first_out_pin = board .D12 ,
55
+ first_sideset_pin = board .D10 ,
56
+ sideset_pin_count = 2 ,
57
+ auto_pull = False ,
58
+ out_shift_right = False ,
59
+ pull_threshold = 32 ,
60
+ wait_for_txstall = False ,
61
+ )
62
+
63
+ trigger .value = False
64
+ dac .write (s16 )
65
+ time .sleep (1 )
66
+ dac .stop ()
67
+ trigger .value = True
68
+
69
+ print ("done" )
Original file line number Diff line number Diff line change 36
36
sm = rp2pio .StateMachine (
37
37
assembled ,
38
38
frequency = 800000 * 6 , # 800khz * 6 clocks per bit
39
- first_set_pin = NEOPIXEL ,
40
39
first_sideset_pin = NEOPIXEL ,
41
40
auto_pull = True ,
42
41
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 array
6
+ import time
7
+ import board
8
+ import digitalio
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 9
9
10
10
squarewave = """
11
11
.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
13
13
set pins 0 ; Drive pin low
14
14
"""
15
15
16
16
assembled = adafruit_pioasm .assemble (squarewave )
17
17
18
18
sm = rp2pio .StateMachine (
19
19
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 ,
23
22
)
24
23
print ("real frequency" , sm .frequency )
25
24
You can’t perform that action at this time.
0 commit comments