Skip to content

Commit a42efe6

Browse files
authored
Merge pull request #136 from Neotron-Compute/add-circuitpython-tests
2 parents 2c87ae5 + 1af7d24 commit a42efe6

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

python-test/audio_sd.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
Tests an SD Card in the slot, the debug LEDs on the IO chip, and WAV playback
3+
on the audio codec.
4+
"""
5+
6+
import board
7+
import sdcardio
8+
import storage
9+
import digitalio
10+
import busio
11+
import time
12+
from adafruit_bus_device.i2c_device import I2CDevice
13+
import audiobusio
14+
import audiocore
15+
import math
16+
import array
17+
import os
18+
19+
led = digitalio.DigitalInOut(board.LED)
20+
led.direction = digitalio.Direction.OUTPUT
21+
i2c = busio.I2C(board.GP15, board.GP14)
22+
# BCLK, LRC, DATA
23+
audio = audiobusio.I2SOut(board.GP27, board.GP28, board.GP26)
24+
25+
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
26+
27+
class Codec:
28+
CODEC_ADDR = 0x1A
29+
30+
LeftLineInputChannelVolumeControl = 0
31+
RightLineInputChannelVolumeControl = 1
32+
LeftChannelHeadphoneVolumeControl = 2
33+
RightChannelHeadphoneVolumeControl = 3
34+
AnalogAudioPathControl = 4
35+
DigitalAudioPathControl = 5
36+
PowerDownControl = 6
37+
DigitalAudioInterfaceFormat = 7
38+
SampleRateControl = 8
39+
DigitalInterfaceActivation = 9
40+
Reset = 15
41+
42+
def __init__(self, i2c):
43+
self.dev = I2CDevice(i2c, self.CODEC_ADDR)
44+
45+
46+
def write(self, reg_addr, value):
47+
with self.dev:
48+
data = bytearray([(reg_addr << 1) | (value >> 8) & 1, value & 0xFF])
49+
self.dev.write(data)
50+
51+
print("Configuring CODEC")
52+
codec = Codec(i2c)
53+
codec.write(codec.Reset, 0x00)
54+
codec.write(codec.LeftLineInputChannelVolumeControl, 0x1F) # 0x17 = 0dB, not muted
55+
codec.write(codec.RightLineInputChannelVolumeControl, 0x1F) # 0x17 = 0dB, not muted
56+
codec.write(codec.LeftChannelHeadphoneVolumeControl, 0xFF)
57+
codec.write(codec.RightChannelHeadphoneVolumeControl, 0xFF)
58+
# STA2 STA1 STA0 STE DAC BYP INSEL MICM MICB
59+
# 0 0 0 0 1 1 0 1 0 - Line in with bypass (18)
60+
# 1 0 0 1 1 0 0 1 0 - Line in with sidetone (132)
61+
# 0 0 0 1 1 0 1 0 1 - Microphone in sidetone (35)
62+
codec.write(codec.AnalogAudioPathControl, 0x35)
63+
codec.write(codec.DigitalAudioPathControl, 0x00)
64+
codec.write(codec.PowerDownControl, 0x00)
65+
# x x MS LRSWAP LRP IWL1 IWL0 FOR1 FOR0
66+
# 0 0 0 0 0 0 0 1 0
67+
codec.write(codec.DigitalAudioInterfaceFormat, 0x02)
68+
# x CLKOUT CLKIN SR3 SR2 SR1 SR0 BOSR USB/Normal
69+
# 0 0 0 1 0 0 0 1 1
70+
codec.write(codec.SampleRateControl, 0x23)
71+
codec.write(codec.DigitalInterfaceActivation, 0x01)
72+
print("CODEC configured")
73+
74+
while not spi.try_lock():
75+
print(".")
76+
77+
print("Setting up MCP23S17")
78+
# Set up MCP23S17 to select CS1
79+
io_cs = digitalio.DigitalInOut(board.GP17)
80+
io_cs.direction = digitalio.Direction.OUTPUT
81+
spi.configure(baudrate=8000000, phase=0, polarity=0)
82+
io_cs.value = False
83+
spi.write(bytes([0x40, 0x00, 0x00])) # GPIOA is outputs
84+
io_cs.value = True
85+
86+
io_cs.value = False
87+
spi.write(bytes([0x40, 0x0D, 0xFF])) # GPIOB has pull-ups
88+
io_cs.value = True
89+
90+
io_cs.value = False
91+
cs = 1
92+
debug_leds = 0x15
93+
spi.write(bytes([0x40, 0x12, debug_leds << 3 | cs]));
94+
io_cs.value = True
95+
96+
wave_file = open("CantinaBand3.wav", "rb")
97+
wav = audiocore.WaveFile(wave_file)
98+
print("Playing wav file!")
99+
audio.play(wav)
100+
while audio.playing:
101+
pass
102+
print("Done!")
103+
104+
spi.unlock()
105+
106+
# nOUTPUT_EN will now activate CS1
107+
cs = board.GP21
108+
109+
110+
print("Mounting SD card")
111+
sdcard = sdcardio.SDCard(spi, cs, baudrate=25000000)
112+
vfs = storage.VfsFat(sdcard)
113+
storage.mount(vfs, "/sd")
114+
print("SD card is {} MiB".format(sdcard.count() * 512 // (1024 * 1024)))

python-test/card-test.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
Tests a Neotron Expansion card in Slot 3.
3+
"""
4+
5+
import board
6+
import sdcardio
7+
import storage
8+
import digitalio
9+
import busio
10+
import os
11+
import time
12+
13+
led = digitalio.DigitalInOut(board.LED)
14+
led.direction = digitalio.Direction.OUTPUT
15+
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
16+
17+
while not spi.try_lock():
18+
print(".")
19+
20+
print("Setting up MCP23S17")
21+
# Set up MCP23S17 to select CS1
22+
io_cs = digitalio.DigitalInOut(board.GP17)
23+
io_cs.direction = digitalio.Direction.OUTPUT
24+
spi.configure(baudrate=8000000, phase=0, polarity=0)
25+
io_cs.value = False
26+
spi.write(bytes([0x40, 0x00, 0x00])) # GPIOA is outputs
27+
io_cs.value = True
28+
29+
io_cs.value = False
30+
spi.write(bytes([0x40, 0x0D, 0xFF])) # GPIOB has pull-ups
31+
io_cs.value = True
32+
33+
def set_cs_and_leds(cs, debug_leds, hdd_led):
34+
io_cs.value = False
35+
debug_leds = (debug_leds ^ 0xF) & 0x0F
36+
if hdd_led:
37+
hdd_led = 0
38+
else:
39+
hdd_led = 1
40+
spi.write(bytes([0x40, 0x12, (debug_leds << 4) | (hdd_led << 3) | cs]));
41+
io_cs.value = True
42+
43+
set_cs_and_leds(3, 0x00, 0)
44+
45+
# nOUTPUT_EN will now activate CS3
46+
noutput_en = digitalio.DigitalInOut(board.GP21)
47+
noutput_en.direction = digitalio.Direction.OUTPUT
48+
49+
50+
print("Setting up Expansion Card")
51+
noutput_en.value = False
52+
spi.write(bytes([0x40, 0x00, 0x00])) # GPIOA is outputs
53+
noutput_en.value = True
54+
55+
scanner = [
56+
0b00000000,
57+
0b10000000,
58+
0b11000000,
59+
0b11100000,
60+
0b01110000,
61+
0b00111000,
62+
0b00011100,
63+
0b00001110,
64+
0b00000111,
65+
0b00000011,
66+
0b00000001,
67+
0b00000000,
68+
]
69+
debug_leds = 0
70+
while True:
71+
for leds in scanner:
72+
noutput_en.value = False
73+
# LEDs are active low, so invert the counter
74+
spi.write(bytes([0x40, 0x00, leds ^ 0xFF]));
75+
noutput_en.value = True
76+
time.sleep(0.05)
77+
for leds in reversed(scanner):
78+
noutput_en.value = False
79+
# LEDs are active low, so invert the counter
80+
spi.write(bytes([0x40, 0x00, leds ^ 0xFF]));
81+
noutput_en.value = True
82+
time.sleep(0.05)
83+
debug_leds = (debug_leds + 1) & 0x0F
84+
set_cs_and_leds(3, debug_leds, 0)
85+
time.sleep(0.2)
86+

0 commit comments

Comments
 (0)