Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: GR-PEACH_Azure_Speech
Fork of TLV320_RBSP by
TLV320_RBSP.cpp
- Committer:
- dkato
- Date:
- 2015-06-01
- Revision:
- 0:bb69b79a1e05
- Child:
- 2:b1fab4a2b59d
File content as of revision 0:bb69b79a1e05:
/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer*
* Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
*******************************************************************************/
#include "mbed.h"
#include "TLV320_RBSP.h"
#include "pinmap.h"
static const PinMap PinMap_AUDIO_XOUT[] = {
// pin ch func
{P5_5 , 0 , 5},
{P3_13 , 0 , 4},
{P8_7 , 0 , 3},
{NC , NC , 0}
};
TLV320_RBSP::TLV320_RBSP(PinName xout, PinName cs, PinName sda, PinName scl, PinName sck, PinName ws, PinName tx, PinName rx, uint8_t int_level, int32_t max_write_num, int32_t max_read_num)
: mI2c_(sda, scl), mI2s_(sck, ws, tx, rx, int_level, max_write_num, max_read_num), audio_cs_(cs) {
audio_cs_ = 0;
mAddr = 0x34;
pinmap_pinout(xout, PinMap_AUDIO_XOUT);
audio_path_control = 0;
ssif_cfg.enabled = true;
ssif_cfg.int_level = 0x78;
ssif_cfg.slave_mode = true;
ssif_cfg.sample_freq = 44100u;
ssif_cfg.clk_select = SSIF_CFG_CKS_AUDIO_X1;
ssif_cfg.multi_ch = SSIF_CFG_MULTI_CH_1;
ssif_cfg.data_word = SSIF_CFG_DATA_WORD_16;
ssif_cfg.system_word = SSIF_CFG_SYSTEM_WORD_32;
ssif_cfg.bclk_pol = SSIF_CFG_FALLING;
ssif_cfg.ws_pol = SSIF_CFG_WS_LOW;
ssif_cfg.padding_pol = SSIF_CFG_PADDING_LOW;
ssif_cfg.serial_alignment = SSIF_CFG_DATA_FIRST;
ssif_cfg.parallel_alignment = SSIF_CFG_LEFT;
ssif_cfg.ws_delay = SSIF_CFG_DELAY;
ssif_cfg.noise_cancel = SSIF_CFG_DISABLE_NOISE_CANCEL;
ssif_cfg.tdm_mode = SSIF_CFG_DISABLE_TDM;
ssif_cfg.romdec_direct.mode = SSIF_CFG_DISABLE_ROMDEC_DIRECT;
ssif_cfg.romdec_direct.p_cbfunc = NULL;
mI2s_.ConfigChannel(&ssif_cfg);
mI2c_.frequency(150000);
reset(); // TLV resets
power(0x80); // Power off
format(16); // 16Bit I2S protocol format
frequency(44100); // Default sample frequency is 44.1kHz
bypass(false); // Do not bypass device
mic(false); // Input select for ADC is line
mute(false); // Not muted
activateDigitalInterface_(); // The digital part of the chip is active
}
// Public Functions
bool TLV320_RBSP::inputVolume(float leftVolumeIn, float rightVolumeIn) {
// check values are in range
if ((leftVolumeIn < 0.0) || (leftVolumeIn > 1.0)
|| (rightVolumeIn < 0.0) || (rightVolumeIn > 1.0)) {
return false;
}
// convert float to encoded char
char left = (char)(31 * leftVolumeIn);
char right = (char)(31 * rightVolumeIn);
// Left Channel
cmd[0] = LEFT_LINE_INPUT_CHANNEL_VOLUME_CONTROL;
cmd[1] = left | (0 << 7); // set volume, mute off
mI2c_.write(mAddr, cmd, 2);
// Right Channel
cmd[0] = RIGHT_LINE_INPUT_CHANNEL_VOLUME_CONTROL;
cmd[1] = right | (0 << 7); // set volume, mute off
mI2c_.write(mAddr, cmd, 2);
return true;
}
bool TLV320_RBSP::outputVolume(float leftVolumeOut, float rightVolumeOut) {
// check values are in range
if ((leftVolumeOut < 0.0) || (leftVolumeOut > 1.0)
|| (rightVolumeOut < 0.0) || (rightVolumeOut > 1.0)) {
return false;
}
// convert float to encoded char
char left = (char)((79 * leftVolumeOut) + 0x30);
char right = (char)((79 * rightVolumeOut) + 0x30);
// Left Channel
cmd[0] = LEFT_CHANNEL_HEADPHONE_VOLUME_CONTROL;
cmd[1] = left | (1 << 7); // set volume, Zero-cross detect
mI2c_.write(mAddr, cmd, 2);
// Right Channel
cmd[0] = RIGHT_CHANNEL_HEADPHONE_VOLUME_CONTROL;
cmd[1] = right | (1 << 7); // set volume, Zero-cross detect
mI2c_.write(mAddr, cmd, 2);
return true;
}
void TLV320_RBSP::bypass(bool bypassVar) {
cmd[0] = ANALOG_AUDIO_PATH_CONTROL;
if (bypassVar != false) {
audio_path_control |= (1 << 3); // bypass enabled
audio_path_control &= ~(1 << 4); // DAC disable
} else {
audio_path_control &= ~(1 << 3); // bypass disable
audio_path_control |= (1 << 4); // DAC enabled
}
cmd[1] = audio_path_control;
mI2c_.write(mAddr, cmd, 2);
}
void TLV320_RBSP::mic(bool micVar) {
cmd[0] = ANALOG_AUDIO_PATH_CONTROL;
if (micVar != false) {
audio_path_control |= (1 << 2); // INSEL Microphone
} else {
audio_path_control &= ~(1 << 2); // INSEL Line
}
cmd[1] = audio_path_control;
mI2c_.write(mAddr, cmd, 2);
}
void TLV320_RBSP::mute(bool softMute) {
cmd[0] = DIGITAL_AUDIO_PATH_CONTROL;
if (softMute != false) {
cmd[1] = 0x08; // set instruction to mute
} else {
cmd[1] = 0x00; // set instruction to NOT mute
}
mI2c_.write(mAddr, cmd, 2);
}
void TLV320_RBSP::power(int device) {
cmd[0] = POWER_DOWN_CONTROL;
cmd[1] = (char)device; // set user defined commands
mI2c_.write(mAddr, cmd, 2);
}
void TLV320_RBSP::format(char length) {
cmd[0] = DIGITAL_AUDIO_INTERFACE_FORMAT;
cmd[1] = (1 << 6); // Master
switch (length) { // input data into instruction byte
case 16:
ssif_cfg.data_word = SSIF_CFG_DATA_WORD_16;
cmd[1] |= 0x02;
break;
case 20:
ssif_cfg.data_word = SSIF_CFG_DATA_WORD_20;
cmd[1] |= 0x06;
break;
case 24:
ssif_cfg.data_word = SSIF_CFG_DATA_WORD_24;
cmd[1] |= 0x0A;
break;
case 32:
ssif_cfg.data_word = SSIF_CFG_DATA_WORD_32;
cmd[1] |= 0x0E;
break;
default:
return;
}
mI2s_.ConfigChannel(&ssif_cfg);
mI2c_.write(mAddr, cmd, 2);
}
bool TLV320_RBSP::frequency(int hz) {
char rate;
switch (hz) {
case 8021:
rate = 0x0B;
break;
case 44100:
rate = 0x08;
break;
case 88200:
rate = 0x0F;
break;
default:
return false;
}
cmd[0] = SAMPLE_RATE_CONTROL;
cmd[1] = (rate << 2); // Sampling rate control
mI2c_.write(mAddr, cmd, 2);
return true;
}
void TLV320_RBSP::reset(void) {
cmd[0] = RESET_REGISTER;
cmd[1] = 0x00; // Write 000000000 to this register triggers reset
mI2c_.write(mAddr, cmd, 2);
}
// Private Functions
void TLV320_RBSP::activateDigitalInterface_(void) {
cmd[0] = DIGITAL_INTERFACE_ACTIVATION;
cmd[1] = 0x01; // Activate
mI2c_.write(mAddr, cmd, 2);
}
