|
| 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))) |
0 commit comments