|
| 1 | +// Copyright 2020-2023 XMOS LIMITED. |
| 2 | +// This Software is subject to the terms of the XMOS Public Licence: Version 1. |
| 3 | + |
| 4 | +/* App headers */ |
| 5 | +#include "aic3204.h" |
| 6 | + |
| 7 | +#include "i2c.h" |
| 8 | +extern i2c_master_t i2c_master_ctx; |
| 9 | + |
| 10 | +/* |
| 11 | + * Example configuration of the TLV320AIC3204 DAC using i2c. |
| 12 | + * |
| 13 | + * For details on the TLV320AIC3204 registers and configuration sequence, |
| 14 | + * see chapters 4 and 5 here: https://www.ti.com/lit/ml/slaa557/slaa557.pdf |
| 15 | + * |
| 16 | + * Must be called after the RTOS scheduler is started. |
| 17 | + */ |
| 18 | +int aic3204_init(void) |
| 19 | +{ |
| 20 | + if ( |
| 21 | + // Set register page to 0 |
| 22 | + aic3204_reg_write(AIC3204_PAGE_CTRL, 0x00) == 0 && |
| 23 | + |
| 24 | + // Initiate SW reset (PLL is powered off as part of reset) |
| 25 | + aic3204_reg_write(AIC3204_SW_RST, 0x01) == 0 && |
| 26 | + |
| 27 | + // Program clock settings |
| 28 | + |
| 29 | + // Default is CODEC_CLKIN is from MCLK pin. Don't need to change this. |
| 30 | + // Power up NDAC and set to 1 |
| 31 | + aic3204_reg_write(AIC3204_NDAC, 0x81) == 0 && |
| 32 | + // Power up MDAC and set to 4 |
| 33 | + aic3204_reg_write(AIC3204_MDAC, 0x84) == 0 && |
| 34 | + // Power up NADC and set to 1 |
| 35 | + aic3204_reg_write(AIC3204_NADC, 0x81) == 0 && |
| 36 | + // Power up MADC and set to 4 |
| 37 | + aic3204_reg_write(AIC3204_MADC, 0x84) == 0 && |
| 38 | + // Program DOSR = 128 |
| 39 | + aic3204_reg_write(AIC3204_DOSR, 0x80) == 0 && |
| 40 | + // Program AOSR = 128 |
| 41 | + aic3204_reg_write(AIC3204_AOSR, 0x80) == 0 && |
| 42 | + // Set Audio Interface Config: I2S, 24 bits, slave mode, DOUT always driving. |
| 43 | + aic3204_reg_write(AIC3204_CODEC_IF, 0x20) == 0 && |
| 44 | + // Program the DAC processing block to be used - PRB_P1 |
| 45 | + aic3204_reg_write(AIC3204_DAC_SIG_PROC, 0x01) == 0 && |
| 46 | + // Program the ADC processing block to be used - PRB_R1 |
| 47 | + aic3204_reg_write(AIC3204_ADC_SIG_PROC, 0x01) == 0 && |
| 48 | + // Select Page 1 |
| 49 | + aic3204_reg_write(AIC3204_PAGE_CTRL, 0x01) == 0 && |
| 50 | + // Enable the internal AVDD_LDO: |
| 51 | + aic3204_reg_write(AIC3204_LDO_CTRL, 0x09) == 0 && |
| 52 | + |
| 53 | + // |
| 54 | + // Program Analog Blocks |
| 55 | + // --------------------- |
| 56 | + // |
| 57 | + // Disable Internal Crude AVdd in presence of external AVdd supply or before powering up internal AVdd LDO |
| 58 | + aic3204_reg_write(AIC3204_PWR_CFG, 0x08) == 0 && |
| 59 | + // Enable Master Analog Power Control |
| 60 | + aic3204_reg_write(AIC3204_LDO_CTRL, 0x01) == 0 && |
| 61 | + // Set Common Mode voltages: Full Chip CM to 0.9V and Output Common Mode for Headphone to 1.65V and HP powered from LDOin @ 3.3V. |
| 62 | + aic3204_reg_write(AIC3204_CM_CTRL, 0x33) == 0 && |
| 63 | + // Set PowerTune Modes |
| 64 | + // Set the Left & Right DAC PowerTune mode to PTM_P3/4. Use Class-AB driver. |
| 65 | + aic3204_reg_write(AIC3204_PLAY_CFG1, 0x00) == 0 && |
| 66 | + aic3204_reg_write(AIC3204_PLAY_CFG2, 0x00) == 0 && |
| 67 | + // Set ADC PowerTune mode PTM_R4. |
| 68 | + aic3204_reg_write(AIC3204_ADC_PTM, 0x00) == 0 && |
| 69 | + // Set MicPGA startup delay to 3.1ms |
| 70 | + aic3204_reg_write(AIC3204_AN_IN_CHRG, 0x31) == 0 && |
| 71 | + // Set the REF charging time to 40ms |
| 72 | + aic3204_reg_write(AIC3204_REF_STARTUP, 0x01) == 0 && |
| 73 | + // HP soft stepping settings for optimal pop performance at power up |
| 74 | + // Rpop used is 6k with N = 6 and soft step = 20usec. This should work with 47uF coupling |
| 75 | + // capacitor. Can try N=5,6 or 7 time constants as well. Trade-off delay vs “pop” sound. |
| 76 | + aic3204_reg_write(AIC3204_HP_START, 0x25) == 0 && |
| 77 | + // Route Left DAC to HPL |
| 78 | + aic3204_reg_write(AIC3204_HPL_ROUTE, 0x08) == 0 && |
| 79 | + // Route Right DAC to HPR |
| 80 | + aic3204_reg_write(AIC3204_HPR_ROUTE, 0x08) == 0 && |
| 81 | + // We are using Line input with low gain for PGA so can use 40k input R but lets stick to 20k for now. |
| 82 | + // Route IN2_L to LEFT_P with 20K input impedance |
| 83 | + aic3204_reg_write(AIC3204_LPGA_P_ROUTE, 0x20) == 0 && |
| 84 | + // Route IN2_R to LEFT_M with 20K input impedance |
| 85 | + aic3204_reg_write(AIC3204_LPGA_N_ROUTE, 0x20) == 0 && |
| 86 | + // Route IN1_R to RIGHT_P with 20K input impedance |
| 87 | + aic3204_reg_write(AIC3204_RPGA_P_ROUTE, 0x80) == 0 && |
| 88 | + // Route IN1_L to RIGHT_M with 20K input impedance |
| 89 | + aic3204_reg_write(AIC3204_RPGA_N_ROUTE, 0x20) == 0 && |
| 90 | + // Unmute HPL and set gain to 0dB |
| 91 | + aic3204_reg_write(AIC3204_HPL_GAIN, 0x00) == 0 && |
| 92 | + // Unmute HPR and set gain to 0dB |
| 93 | + aic3204_reg_write(AIC3204_HPR_GAIN, 0x00) == 0 && |
| 94 | + // Unmute Left MICPGA, Set Gain to 0dB. |
| 95 | + aic3204_reg_write(AIC3204_LPGA_VOL, 0x00) == 0 && |
| 96 | + // Unmute Right MICPGA, Set Gain to 0dB. |
| 97 | + aic3204_reg_write(AIC3204_RPGA_VOL, 0x00) == 0 && |
| 98 | + // Power up HPL and HPR drivers |
| 99 | + aic3204_reg_write(AIC3204_OP_PWR_CTRL, 0x30) == 0 |
| 100 | + ) |
| 101 | + { |
| 102 | + // Wait for 2.5 sec for soft stepping to take effect |
| 103 | + aic3204_wait(2500); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + return -1; |
| 108 | + } |
| 109 | + |
| 110 | + if ( |
| 111 | + // |
| 112 | + // Power Up DAC/ADC |
| 113 | + // ---------------- |
| 114 | + // |
| 115 | + // Select Page 0 |
| 116 | + aic3204_reg_write(AIC3204_PAGE_CTRL, 0x00) == 0 && |
| 117 | + // Power up the Left and Right DAC Channels. Route Left data to Left DAC and Right data to Right DAC. |
| 118 | + // DAC Vol control soft step 1 step per DAC word clock. |
| 119 | + aic3204_reg_write(AIC3204_DAC_CH_SET1, 0xd4) == 0 && |
| 120 | + // Power up Left and Right ADC Channels, ADC vol ctrl soft step 1 step per ADC word clock. |
| 121 | + aic3204_reg_write(AIC3204_ADC_CH_SET, 0xc0) == 0 && |
| 122 | + // Unmute Left and Right DAC digital volume control |
| 123 | + aic3204_reg_write(AIC3204_DAC_CH_SET2, 0x00) == 0 && |
| 124 | + // Unmute Left and Right ADC Digital Volume Control. |
| 125 | + aic3204_reg_write(AIC3204_ADC_FGA_MUTE, 0x00) == 0 |
| 126 | + ) |
| 127 | + { |
| 128 | + return 0; |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + return -1; |
| 133 | + } |
| 134 | +} |
0 commit comments