From f5db1f6348450900a1bd7180c198cf0bd1cd3d34 Mon Sep 17 00:00:00 2001 From: BlitzCityDIY Date: Wed, 19 Mar 2025 11:27:57 -0400 Subject: [PATCH] remove todos, tested with fruitjam rev b --- README.rst | 38 ++++++++++------ adafruit_tlv320.py | 86 ++++++++++++++++++++++------------- docs/conf.py | 1 - docs/examples.rst | 9 ++++ docs/index.rst | 6 --- examples/tlv320_fulltest.py | 15 ++---- examples/tlv320_simpletest.py | 2 +- requirements.txt | 1 - 8 files changed, 93 insertions(+), 65 deletions(-) diff --git a/README.rst b/README.rst index c4fb62d..4b08231 100644 --- a/README.rst +++ b/README.rst @@ -30,7 +30,6 @@ This driver depends on: * `Adafruit CircuitPython `_ * `Bus Device `_ -* `Register `_ Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading @@ -38,19 +37,8 @@ This is easily achieved by downloading or individual libraries can be installed using `circup `_. - - -.. todo:: Describe the Adafruit product this library works with. For PCBs, you can also add the -image from the assets folder in the PCB's GitHub repo. - -`Purchase one from the Adafruit shop `_ - Installing from PyPI ===================== -.. note:: This library is not available on PyPI yet. Install documentation is included - as a standard element. Stay tuned for PyPI availability! - -.. todo:: Remove the above note if PyPI version is/will be available at time of release. On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from PyPI `_. @@ -101,8 +89,30 @@ Or the following command to update an existing version: Usage Example ============= -.. todo:: Add a quick, simple example. It and other examples should live in the -examples folder and be included in docs/examples.rst. +.. code-block:: python + + import audiobusio + import audiocore + import board + + import adafruit_tlv320 + + i2c = board.I2C() + dac = adafruit_tlv320.TLV320DAC3100(i2c) + + # set mclk, sample rate & bit depth + dac.configure_clocks(mclk_freq=12000000, sample_rate=44100, bit_depth=16) + + # use headphones + # helper function for default settings + dac.headphone_output = True + dac.headphone_volume = -20 # dB + # or use speaker + # helper function for default settings + # dac.speaker_output = True + # dac.speaker_volume = -15 # dB + + audio = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN) Documentation ============= diff --git a/adafruit_tlv320.py b/adafruit_tlv320.py index 305bb6c..2be7ab5 100644 --- a/adafruit_tlv320.py +++ b/adafruit_tlv320.py @@ -517,12 +517,13 @@ class Page0Registers(PagedRegisterBase): self._write_register(_CODEC_IF_CTRL1, value) - def _configure_clocks_for_sample_rate(self, mclk_freq, sample_rate, bit_depth): + def _configure_clocks_for_sample_rate(self, mclk_freq: int, sample_rate: int, bit_depth: int): """Configure clock settings for the specified sample rate. - :param mclk_freq: The main clock frequency in Hz + :param mclk_freq: The main clock frequency in Hz, or 0 to use BCLK as PLL input :param sample_rate: The desired sample rate in Hz :param bit_depth: The bit depth (16, 20, 24, or 32) + :return: True if successful, False otherwise """ if bit_depth == 16: data_len = DATA_LEN_16 @@ -532,7 +533,26 @@ class Page0Registers(PagedRegisterBase): data_len = DATA_LEN_24 else: data_len = DATA_LEN_32 - if mclk_freq % (128 * sample_rate) == 0: + if mclk_freq == 0: + self._set_codec_interface(FORMAT_I2S, data_len) + self._set_bits(_CLOCK_MUX1, 0x03, 2, 0b01) + self._set_bits(_CLOCK_MUX1, 0x03, 0, 0b11) + p, r, j, d = 1, 2, 32, 0 + ndac = 8 + mdac = 2 + dosr = 128 + pr_value = ((p & 0x07) << 4) | (r & 0x0F) + self._write_register(_PLL_PROG_PR, pr_value & 0x7F) + self._write_register(_PLL_PROG_J, j & 0x3F) + self._write_register(_PLL_PROG_D_MSB, (d >> 8) & 0xFF) + self._write_register(_PLL_PROG_D_LSB, d & 0xFF) + self._write_register(_NDAC, 0x80 | (ndac & 0x7F)) + self._write_register(_MDAC, 0x80 | (mdac & 0x7F)) + self._write_register(_DOSR_MSB, (dosr >> 8) & 0xFF) + self._write_register(_DOSR_LSB, dosr & 0xFF) + self._set_bits(_PLL_PROG_PR, 0x01, 7, 1) + time.sleep(0.01) + elif mclk_freq % (128 * sample_rate) == 0: div_ratio = mclk_freq // (128 * sample_rate) self._set_bits(_CLOCK_MUX1, 0x03, 0, 0b00) self._set_bits(_PLL_PROG_PR, 0x01, 7, 0) @@ -542,8 +562,7 @@ class Page0Registers(PagedRegisterBase): self._write_register(_DOSR_MSB, 0) self._write_register(_DOSR_LSB, 128) self._set_codec_interface(FORMAT_I2S, data_len) - - if mclk_freq == 12000000: + elif mclk_freq == 12000000: if sample_rate == 22050: p, r, j, d = 1, 1, 7, 6144 ndac = 8 @@ -565,7 +584,7 @@ class Page0Registers(PagedRegisterBase): mdac = 1 dosr = 128 else: - return False + raise ValueError("Need a valid sample rate: 22050, 44100, 48000 or 96000") elif mclk_freq == 24000000: if sample_rate == 44100: p, r, j, d = 1, 2, 7, 6144 @@ -585,23 +604,22 @@ class Page0Registers(PagedRegisterBase): else: raise ValueError("Need a valid sample rate: 44100, 48000 or 96000") else: - raise ValueError("Need a valid sample rate: 22050, 44100, 48000 or 96000") - - self._set_bits(_CLOCK_MUX1, 0x03, 2, 0b00) - self._set_bits(_CLOCK_MUX1, 0x03, 0, 0b11) - pr_value = ((p & 0x07) << 4) | (r & 0x0F) - self._write_register(_PLL_PROG_PR, pr_value & 0x7F) - self._write_register(_PLL_PROG_J, j & 0x3F) - self._write_register(_PLL_PROG_D_MSB, (d >> 8) & 0xFF) - self._write_register(_PLL_PROG_D_LSB, d & 0xFF) - self._write_register(_NDAC, 0x80 | (ndac & 0x7F)) - self._write_register(_MDAC, 0x80 | (mdac & 0x7F)) - self._write_register(_DOSR_MSB, (dosr >> 8) & 0xFF) - self._write_register(_DOSR_LSB, dosr & 0xFF) - self._set_codec_interface(FORMAT_I2S, data_len) - self._set_bits(_PLL_PROG_PR, 0x01, 7, 1) - time.sleep(0.01) - + raise ValueError("Need a valid MCLK frequency: 12MHz or 24MHz") + if mclk_freq != 0: + self._set_bits(_CLOCK_MUX1, 0x03, 2, 0b00) + self._set_bits(_CLOCK_MUX1, 0x03, 0, 0b11) + pr_value = ((p & 0x07) << 4) | (r & 0x0F) + self._write_register(_PLL_PROG_PR, pr_value & 0x7F) + self._write_register(_PLL_PROG_J, j & 0x3F) + self._write_register(_PLL_PROG_D_MSB, (d >> 8) & 0xFF) + self._write_register(_PLL_PROG_D_LSB, d & 0xFF) + self._write_register(_NDAC, 0x80 | (ndac & 0x7F)) + self._write_register(_MDAC, 0x80 | (mdac & 0x7F)) + self._write_register(_DOSR_MSB, (dosr >> 8) & 0xFF) + self._write_register(_DOSR_LSB, dosr & 0xFF) + self._set_codec_interface(FORMAT_I2S, data_len) + self._set_bits(_PLL_PROG_PR, 0x01, 7, 1) + time.sleep(0.01) class Page1Registers(PagedRegisterBase): """Page 1 registers containing analog output settings, HP/SPK controls, etc.""" @@ -839,7 +857,7 @@ class TLV320DAC3100: # Initialize configuration tracking variables self._sample_rate: int = 44100 # Default self._bit_depth: int = 16 # Default - self._mclk_freq: int = 12000000 # Default + self._mclk_freq: int = 0 # Default blck # Reset the device if not self.reset(): @@ -1709,22 +1727,26 @@ class TLV320DAC3100: headset_detect, button_press, dac_drc, agc_noise, over_current, multiple_pulse ) - def configure_clocks(self, mclk_freq: int, sample_rate: int, bit_depth: int = 16) -> bool: + def configure_clocks(self, sample_rate: int, bit_depth: int = 16, mclk_freq: Optional[int] = None): """Configure the TLV320DAC3100 clock settings. - This function configures all necessary clock settings including MCLK, dividers, - and interface settings to achieve the requested sample rate with the provided - main clock frequency. + This function configures all necessary clock settings including PLL, dividers, + and interface settings to achieve the requested sample rate. - :param mclk_freq: The main clock frequency in Hz (e.g., 12000000 for 12MHz) :param sample_rate: The desired sample rate in Hz (e.g., 44100, 48000) - :param bit_depth: The bit depth (16, 20, 24, or 32) + :param bit_depth: The bit depth (16, 20, 24, or 32), defaults to 16 + :param mclk_freq: The main clock frequency in Hz (e.g., 12000000 for 12MHz) + If None (default), BCLK will be used as the PLL input source :return: True if successful, False otherwise """ - self._mclk_freq = mclk_freq self._sample_rate = sample_rate self._bit_depth = bit_depth - return self._page0._configure_clocks_for_sample_rate(mclk_freq, sample_rate, bit_depth) + if mclk_freq is not None: + self._mclk_freq = mclk_freq + else: + self._mclk_freq = 0 # Internally use 0 to indicate BCLK mode + + return self._page0._configure_clocks_for_sample_rate(self._mclk_freq, sample_rate, bit_depth) @property def headphone_output(self) -> bool: diff --git a/docs/conf.py b/docs/conf.py index 07359f1..42b1ab3 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -32,7 +32,6 @@ autodoc_preserve_defaults = True intersphinx_mapping = { "python": ("https://docs.python.org/3", None), "BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None), - "Register": ("https://docs.circuitpython.org/projects/register/en/latest/", None), "CircuitPython": ("https://docs.circuitpython.org/en/latest/", None), } diff --git a/docs/examples.rst b/docs/examples.rst index 3115c4b..aac2b00 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -6,3 +6,12 @@ Ensure your device works with this simple test. .. literalinclude:: ../examples/tlv320_simpletest.py :caption: examples/tlv320_simpletest.py :linenos: + +Full test +---------- + +Demos advanced features of the library. + +.. literalinclude:: ../examples/tlv320_fulltest.py + :caption: examples/tlv320_fulltest.py + :linenos: \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index e826ea3..806537f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -24,15 +24,9 @@ Table of Contents .. toctree:: :caption: Tutorials -.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave - the toctree above for use later. - .. toctree:: :caption: Related Products -.. todo:: Add any product links here. If there are none, then simply delete this todo and leave - the toctree above for use later. - .. toctree:: :caption: Other Links diff --git a/examples/tlv320_fulltest.py b/examples/tlv320_fulltest.py index 65f2c70..df57ca8 100644 --- a/examples/tlv320_fulltest.py +++ b/examples/tlv320_fulltest.py @@ -5,6 +5,9 @@ """ Full TLV320DAC3100 Test Demo all features in the library +Shows advanced control for DAC, headphone and speaker +beyond basic headphone_output & speaker_output helpers +in simpletest. """ import time @@ -51,19 +54,16 @@ time.sleep(0.1) # Give device time to reset print("\n=== Basic Information ===") print(f"Sample rate: {dac.sample_rate} Hz") print(f"Bit depth: {dac.bit_depth}-bit") -print(f"MCLK frequency: {dac.mclk_freq} Hz") print(f"Overtemperature condition: {dac.overtemperature}") # I2S Config -dac.configure_clocks(mclk_freq=12000000, sample_rate=22050, bit_depth=16) +dac.configure_clocks(sample_rate=22050, bit_depth=16) print(f"Sample rate: {dac.sample_rate} Hz") print(f"Bit depth: {dac.bit_depth}-bit") -print(f"MCLK frequency: {dac.mclk_freq} Hz") time.sleep(0.2) -dac.configure_clocks(mclk_freq=12000000, sample_rate=48000, bit_depth=32) +dac.configure_clocks(sample_rate=48000, bit_depth=32) print(f"Sample rate: {dac.sample_rate} Hz") print(f"Bit depth: {dac.bit_depth}-bit") -print(f"MCLK frequency: {dac.mclk_freq} Hz") time.sleep(0.2) # Headphone Output Setup @@ -241,11 +241,6 @@ print(f"New GPIO1 mode: {dac.gpio1_mode}") dac.gpio1_mode = GPIO1_DISABLED # Disable GPIO1 print(f"Disabled GPIO1 mode: {dac.gpio1_mode}") -# Headset Detection -print("\n=== Headset Detection ===") -print(f"Current headset status: {dac.headset_status}") -# 0 = none, 1 = without mic, 3 = with mic - # Volume Control ADC print("\n=== Volume Control ADC ===") print(f"Volume ADC reading: {dac.vol_adc_db} dB") diff --git a/examples/tlv320_simpletest.py b/examples/tlv320_simpletest.py index 9754462..d4a6732 100644 --- a/examples/tlv320_simpletest.py +++ b/examples/tlv320_simpletest.py @@ -17,7 +17,7 @@ i2c = busio.I2C(board.SCL, board.SDA) dac = adafruit_tlv320.TLV320DAC3100(i2c) # set mclk, sample rate & bit depth -dac.configure_clocks(mclk_freq=12000000, sample_rate=44100, bit_depth=16) +dac.configure_clocks(sample_rate=44100, bit_depth=16) # use headphones dac.headphone_output = True diff --git a/requirements.txt b/requirements.txt index 7284723..c1ad2a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,3 @@ Adafruit-Blinka adafruit-circuitpython-busdevice -adafruit-circuitpython-register