Sam Grove / TLV320AIC1110

TLV320AIC1110.cpp

Committer:
sam_grove
Date:
2013-05-21
Revision:
5:174f94df7624
Parent:
4:470f89e786f9

File content as of revision 5:174f94df7624:

/**
 * @file    TLV320AIC1110.cpp
 * @brief   Device driver - TLV320AIC1110 CODEC
 * @author  sam grove
 * @version 1.0
 * @see     http://www.ti.com/product/tlv320aic1110
 *
 * Copyright (c) 2013
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "TLV320AIC1110.h"
#include "LogUtil.h"

#define W_ADDRESS   0xE2
#define R_ADDRESS   0xE3

TLV320AIC1110::TLV320AIC1110(I2C &i2c)
{
    _i2c = &i2c;
    _i2c->frequency(400000);
    
    return;
}

TLV320AIC1110::~TLV320AIC1110()
{
    return;
}

void TLV320AIC1110::init(void)
{
    writeRegister(POWER_CONTROL, 0x9B); // reg 0
    writeRegister(MODE_CONTROL, 0x00);  // reg 1
    writeRegister(AUX, 0x81);           // reg 6
    
    return;
}

void TLV320AIC1110::regDump(void)
{
    for(int i=0; i<7; i++)
    {
        LOG(" Register 0x%02x 0x%02x\n", i, readRegister(i));
    }
    
    return;
}

void TLV320AIC1110::writeRegister(const TLV320AIC1110_REGISTERS reg, const uint8_t value)
{
    char data[2] = {reg, value};
    
    __disable_irq();
    if (0 != _i2c->write(W_ADDRESS, data, 2))
    {
        ERROR("write failed\n");
    }
    __enable_irq();
        
    return;
}

uint8_t TLV320AIC1110::readRegister(const uint8_t reg)
{
    char data[1] = {reg};

    __disable_irq();
    if (0 != _i2c->write(W_ADDRESS, data, 1, 1))
    {
        ERROR("read failed\n");
    }
    if (0 != _i2c->read (R_ADDRESS, data, 1))
    {
        ERROR("read failed\n");
    }
    __enable_irq();
    
    return data[0];
}

void TLV320AIC1110::mute(const CHANNEL_T ch)
{
    uint8_t reg_val = readRegister(POWER_CONTROL);
    reg_val |= ch;  // write 1's
    writeRegister(POWER_CONTROL, reg_val);
    
    return;    
}

void TLV320AIC1110::unmute(const CHANNEL_T ch)
{
    uint8_t reg_val = readRegister(POWER_CONTROL);
    reg_val &= ~ch; // write 0's
    writeRegister(POWER_CONTROL, reg_val);
    
    return; 
}

uint32_t TLV320AIC1110::txGain(const uint8_t gain)
{
    uint32_t retval = 0;
    uint8_t normalize = (gain >= 31) ? 42 : 30;
    // validate the parameter
    if( (gain <= 42) && (gain >= 31) )
    {
        retval = 1;
        // normalize & format the parameter
        uint8_t fmt_gain = (normalize - gain) >> 2;    // divide by 2 (2db increment)
        // read the current register setting (retain sidetone gain)
        uint8_t reg = readRegister(TXPGA) & 0x07;
        fmt_gain |= (30 == normalize) ? (reg | 0x40) : reg;
        writeRegister(TXPGA, fmt_gain);
    }
    
    return retval;
}

uint32_t TLV320AIC1110::sidetoneGain(const int8_t gain)
{
    uint32_t retval = 0;
    // validate the parameter
    if( (gain <= -12) && (gain >= -24) )
    {
        retval = 1;
        // normalize & format the parameter
        uint8_t fmt_gain = (24 - (gain+36)) >> 2;    // divide by 2 (2db increment)
        // read the current register setting (retain tx gain)
        uint8_t reg = readRegister(TXPGA) & 0xf8;
        fmt_gain |= (reg << 3);
        writeRegister(TXPGA, fmt_gain);
    }
    
    return retval;
}

uint32_t TLV320AIC1110::rxGain(const int8_t gain)
{
    uint32_t retval = 0;
    // validate the parameter
    if( (gain <= 6) && (gain >= -6) )
    {
        retval = 1;
        // normalize & format the parameter
        uint8_t fmt_gain = (12 - (gain+6)) << 4;
        // read the current register setting (retain rx volume control)
        uint8_t reg = readRegister(RXPGA) & 0x0f;
        fmt_gain |= reg;
        writeRegister(RXPGA, fmt_gain);
    }
    
    return retval;
}

uint32_t TLV320AIC1110::rxVolume(const int8_t gain)
{
    uint32_t retval = 0;
    // validate the parameter
    if( (gain <= 0) && (gain >= -18) )
    {
        retval = 1;
        // normalize & format the parameter
        uint8_t fmt_gain = (18 - (gain+18)) >> 2;    // divide by 2 (2db increment)
        // read the current register setting (retain rx gain)
        uint8_t reg = readRegister(RXPGA) & 0xf0;
        fmt_gain |= (reg << 4);
        writeRegister(RXPGA, fmt_gain);
    }
    
    return retval;
}