TLV320_RBSP class, defined on the I2C master bus.

Dependents:   MovPlayer GR-PEACH_Audio_Playback_Sample GR-PEACH_Audio_Playback_7InchLCD_Sample RGA-MJPEG_VideoDemo ... more

Fork of TLV320_RBSP by Daiki Kato

Files at this revision

API Documentation at this revision

Comitter:
dkato
Date:
Mon Jun 01 08:33:42 2015 +0000
Child:
1:4b34a496efac
Commit message:
first commit

Changed in this revision

TLV320_RBSP.cpp Show annotated file Show diff for this revision Revisions of this file
TLV320_RBSP.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TLV320_RBSP.cpp	Mon Jun 01 08:33:42 2015 +0000
@@ -0,0 +1,224 @@
+/*******************************************************************************
+* 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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TLV320_RBSP.h	Mon Jun 01 08:33:42 2015 +0000
@@ -0,0 +1,175 @@
+/*******************************************************************************
+* 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.
+*******************************************************************************/
+
+#ifndef MBED_TLV320_RBSP_H
+#define MBED_TLV320_RBSP_H
+
+#include "mbed.h"
+#include "R_BSP_Ssif.h"
+
+/** TLV320_RBSP class, defined on the I2C master bus
+*
+*/
+class TLV320_RBSP {
+public:
+
+    /** Create a TLV320_RBSP object defined on the I2C port
+     * 
+     * @param xout AUDIO_X1 clock output
+     * @param cs  Control port input latch/address select (codec pin)
+     * @param sda I2C data line pin
+     * @param scl I2C clock line pin
+     * @param sck SSIF serial bit clock
+     * @param ws  SSIF word selection
+     * @param tx  SSIF serial data output
+     * @param rx  SSIF serial data input
+     * @param int_level     Interupt priority (SSIF)
+     * @param max_write_num The upper limit of write buffer (SSIF)
+     * @param max_read_num  The upper limit of read buffer (SSIF)
+     */
+    TLV320_RBSP(PinName xout, PinName cs, PinName sda, PinName scl, PinName sck, PinName ws, PinName tx, PinName rx, uint8_t int_level = 0x80, int32_t max_write_num = 16, int32_t max_read_num = 16);
+
+    /** Overloaded power() function default = 0x80, record requires 0x02
+     *
+     * @param device Call individual devices to power up/down
+     * Device power      0x00 = On 0x80 = Off
+     * Clock             0x00 = On 0x40 = Off
+     * Oscillator        0x00 = On 0x20 = Off
+     * Outputs           0x00 = On 0x10 = Off
+     * DAC               0x00 = On 0x08 = Off
+     * ADC               0x00 = On 0x04 = Off
+     * Microphone input  0x00 = On 0x02 = Off
+     * Line input        0x00 = On 0x01 = Off
+     */
+    void power(int device = 0x07);
+
+    /** Set I2S interface bit length and mode
+     *
+     * @param length Set bit length to 16, 20, 24 or 32 bits
+     */
+    void format(char length);
+
+    /** Set sample frequency
+     *
+     * @param frequency Sample frequency of data in Hz
+     * @return true = success, false = failure
+     * 
+     * The TLV320 supports the following frequencies: 8.021kHz, 44.1kHz, 88.2kHz
+     * Default is 44.1kHz
+     */
+    bool frequency(int hz);
+
+    /** Reset TLV320
+     *
+     */
+    void reset(void);
+
+    /** Enqueue asynchronous write request
+     *
+     * @param p_data Location of the data
+     * @param data_size Number of bytes to write
+     * @param p_data_conf Asynchronous control block structure
+     * @return Number of bytes written on success. negative number on error.
+     */
+    int write(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf = NULL) {
+        return mI2s_.write(p_data, data_size, p_data_conf);
+    };
+
+    /** Enqueue asynchronous read request
+     *
+     * @param p_data Location of the data
+     * @param data_size Number of bytes to read
+     * @param p_data_conf Asynchronous control block structure
+     * @return Number of bytes read on success. negative number on error.
+     */
+    int read(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf = NULL) {
+        return mI2s_.read(p_data, data_size, p_data_conf);
+    };
+
+    /** Line in volume control i.e. record volume
+     *
+     * @param leftVolumeIn Left line-in volume 
+     * @param rightVolumeIn Right line-in volume
+     * @return Returns "true" for success, "false" if parameters are out of range
+     * Parameters accept a value, where 0.0 < parameter < 1.0 and where 0.0 maps to -34.5dB 
+     * and 1.0 maps to +12dB (0.74 = 0 dB default).
+     */
+    bool inputVolume(float leftVolumeIn, float rightVolumeIn);
+
+    /** Headphone out volume control
+     *
+     * @param leftVolumeOut Left headphone-out volume
+     * @param rightVolumeOut Right headphone-out volume
+     * @return Returns "true" for success, "false" if parameters are out of range
+     * Parameters accept a value, where 0.0 < parameter < 1.0 and where 0.0 maps to -73dB (mute) 
+     * and 1.0 maps to +6dB (0.5 = default)
+     */
+    bool outputVolume(float leftVolumeOut, float rightVolumeOut);
+
+    /** Analog audio path control (Bypass) function default = false
+     *
+     * @param bypassVar Route analogue audio direct from line in to headphone out
+     */
+    void bypass(bool bypassVar);
+
+    /** Analog audio path control (Input select for ADC) function default = false
+     *
+     * @param micVar Input select for ADC. true : Microphone , false : Line
+     */
+    void mic(bool micVar);
+
+    /** Digital audio path control
+     *
+     * @param softMute Mute output
+     */
+    void mute(bool softMute);
+
+protected:
+    char cmd[2];    // the address and command for TLV320 internal registers
+    int mAddr;      // register write address
+private:
+    DigitalOut audio_cs_;
+    I2C mI2c_;      // MUST use the I2C port
+    SSIF mI2s_;
+    ssif_channel_cfg_t  ssif_cfg;
+    char audio_path_control;
+    /** Digital interface activation
+     *
+     */
+    void activateDigitalInterface_(void);
+
+    // TLV320AIC23B register addresses as defined in the TLV320AIC23B datasheet
+    #define LEFT_LINE_INPUT_CHANNEL_VOLUME_CONTROL  (0x00 << 1)
+    #define RIGHT_LINE_INPUT_CHANNEL_VOLUME_CONTROL (0x01 << 1)
+    #define LEFT_CHANNEL_HEADPHONE_VOLUME_CONTROL   (0x02 << 1)
+    #define RIGHT_CHANNEL_HEADPHONE_VOLUME_CONTROL  (0x03 << 1)
+    #define ANALOG_AUDIO_PATH_CONTROL               (0x04 << 1)
+    #define DIGITAL_AUDIO_PATH_CONTROL              (0x05 << 1)
+    #define POWER_DOWN_CONTROL                      (0x06 << 1)
+    #define DIGITAL_AUDIO_INTERFACE_FORMAT          (0x07 << 1)
+    #define SAMPLE_RATE_CONTROL                     (0x08 << 1)
+    #define DIGITAL_INTERFACE_ACTIVATION            (0x09 << 1)
+    #define RESET_REGISTER                          (0x0F << 1)
+};
+
+#endif