⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlv320aic23b.c

📁 基于TI公司Cortex-M3的uart超级通信开发
💻 C
📖 第 1 页 / 共 2 页
字号:
    //
    // Continue the transfer.
    //
    I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);

    //
    // Wait until the current byte has been transferred.
    //
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0)
    {
    }

    if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE)
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
        return(false);
    }

    //
    // Wait until the current byte has been transferred.
    //
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false))
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
    }

    //
    // Write the next byte to the controller.
    //
    I2CMasterDataPut(DAC_I2C_MASTER_BASE, ulData);

    //
    // End the transfer.
    //
    I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

    //
    // Wait until the current byte has been transferred.
    //
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0)
    {
    }

    if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE)
    {
        return(false);
    }

    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false))
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
    }

    return(true);
}

//*****************************************************************************
//
//! Initialize the TLV320AIC23B DAC.
//!
//! This function initializes the I2C interface and the TLV320AIC23B DAC.
//!
//! \return Returns \b true on success of \b false if the I2S daughter board
//! is not present.
//
//*****************************************************************************
tBoolean
TLV320AIC23BInit(void)
{
    tBoolean bRetcode;

    //
    // Enable the GPIO port containing the I2C pins and set the SDA pin as a
    // GPIO input for now and engage a weak pull-down.  If the daughter board
    // is present, the pull-up on the board should easily overwhelm
    // the pull-down and we should read the line state as high.
    //
    SysCtlPeripheralEnable(DAC_I2CSCL_GPIO_PERIPH);
    GPIOPinTypeGPIOInput(DAC_I2CSCL_GPIO_PORT, DAC_I2CSDA_PIN);
    GPIOPadConfigSet(DAC_I2CSCL_GPIO_PORT, DAC_I2CSDA_PIN, GPIO_STRENGTH_2MA,
                     GPIO_PIN_TYPE_STD_WPD);

    //
    // Enable the I2C peripheral.
    //
    SysCtlPeripheralEnable(DAC_I2C_PERIPH);

    //
    // Delay a while to ensure that we read a stable value from the SDA
    // GPIO pin.  If we read too quickly, the result is unpredictable.
    // This delay is around 2mS.
    //
    SysCtlDelay(SysCtlClockGet() / (3 * 500));

    //
    // Read the current state of the I2C1SDA line.  If it is low, the
    // daughter board must not be present since it should be pulled high.
    //
    if(!(GPIOPinRead(DAC_I2CSCL_GPIO_PORT, DAC_I2CSDA_PIN) & DAC_I2CSDA_PIN))
    {
        return(false);
    }

    //
    // Configure the I2C SCL and SDA pins for I2C operation.
    //
    GPIOPinTypeI2C(DAC_I2CSCL_GPIO_PORT, DAC_I2CSCL_PIN | DAC_I2CSDA_PIN);

    //
    // Initialize the I2C master.
    //
    I2CMasterInitExpClk(DAC_I2C_MASTER_BASE, SysCtlClockGet(), 0);

    //
    // Allow the rest of the public APIs to make hardware changes.
    //
    g_ucEnabled = 1;

    //
    // Reset the DAC.  Check the return code on this call since we use it to
    // indicate whether or not the DAC is present.  If the register write
    // fails, we assume the I2S daughter board and DAC are not present and
    // return false.
    //
    bRetcode = TLV320AIC23BWriteRegister(TI_RESET, 0);
    if(!bRetcode)
    {
        return(bRetcode);
    }

    //
    // Power up the device and the DAC.
    //
    TLV320AIC23BWriteRegister(TI_POWER_DOWN, TI_POWER_DOWN_CLK |
                                             TI_POWER_DOWN_OSC);

    //
    // Set the sample rate.
    //
    TLV320AIC23BWriteRegister(TI_SRC, TI_SRC_SR_48000);

    //
    // Unmute the DAC.
    //
    TLV320AIC23BWriteRegister(TI_DIGITAL_AP, TI_DIGITAL_AP_DEEMP_48K |
                              TI_DIGITAL_AP_ADCHP);

    //
    // Enable the DAC path and insure the Mic input stays muted.
    //
    TLV320AIC23BWriteRegister(TI_ANALOG_AP, TI_ANALOG_AP_DAC |
                                            TI_ANALOG_AP_MICM);

    //
    // 16 bit I2S slave mode.
    //
    TLV320AIC23BWriteRegister(TI_DIGITAL_AI, TI_DIGITAL_AI_IWL_16 |
                              TI_DIGITAL_AI_FOR_I2S | TI_DIGITAL_AI_SLAVE);

    //
    // Set the Headphone volume.
    //
    TLV320AIC23BHeadPhoneVolumeSet(100);

    //
    // Unmute the Line input to the ADC.
    //
    TLV320AIC23BLineInVolumeSet(TLV_LINEIN_VC_0DB);

    //
    // Turn on the digital interface.
    //
    TLV320AIC23BWriteRegister(TI_DIGITAL_ACTIVATE, TI_DIGITAL_ACTIVATE_EN);

    return(true);
}

//*****************************************************************************
//
//! Sets the Line In volume.
//!
//! \param ucVolume is the volume to set for the line input.
//!
//! This function adjusts the audio output up by the specified percentage.  The
//! TI_LEFT_LINEIN_* values should be used for the \e ucVolume parameter.
//!
//! \return None
//
//*****************************************************************************
void
TLV320AIC23BLineInVolumeSet(unsigned char ucVolume)
{
    //
    // Unmute the line inputs and set the mixer to 0db.
    //
    TLV320AIC23BWriteRegister(TI_LEFT_LINEIN_VC, ucVolume);
    TLV320AIC23BWriteRegister(TI_RIGHT_LINEIN_VC, ucVolume);
}

//*****************************************************************************
//
//! Sets the Headphone volume on the DAC.
//!
//! \param ulVolume is the volume to set, specified as a percentage between 0%
//! (silence) and 100% (full volume), inclusive.
//!
//! This function adjusts the audio output up by the specified percentage.  The
//! adjusted volume will not go above 100% (full volume).
//!
//! \return None
//
//*****************************************************************************
void
TLV320AIC23BHeadPhoneVolumeSet(unsigned long ulVolume)
{
    g_ucHPVolume = (unsigned char)ulVolume;

    //
    // Cap the volume at 100%
    //
    if(g_ucHPVolume >= 100)
    {
        g_ucHPVolume = 100;
    }

    if(g_ucEnabled == 1)
    {
        //
        // Set the left and right volumes with zero cross detect.
        //
        TLV320AIC23BWriteRegister(TI_LEFT_HP_VC,
                                  (TI_LEFT_HP_VC_LZC |
                                   pucVolumeTable[ulVolume >> 3]));
        TLV320AIC23BWriteRegister(TI_RIGHT_HP_VC,
                                  (TI_LEFT_HP_VC_LZC |
                                   pucVolumeTable[ulVolume >> 3]));
    }
}

//*****************************************************************************
//
//! Returns the Headphone volume on the DAC.
//!
//! This function returns the current volume, specified as a percentage between
//! 0% (silence) and 100% (full volume), inclusive.
//!
//! \return Returns the current volume.
//
//*****************************************************************************
unsigned long
TLV320AIC23BHeadPhoneVolumeGet(void)
{
    return(g_ucHPVolume);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -