Use the TLV320 with in-built I2S object to stream audio data from an SD Card and send it to the TLV320 CODEC for audio playback

Dependencies:   I2SSlave mbed TLV320

Committer:
d_worrall
Date:
Fri Aug 05 10:07:47 2011 +0000
Revision:
0:3d6892f6384f
version 2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
d_worrall 0:3d6892f6384f 1 /******************************************************
d_worrall 0:3d6892f6384f 2 * TLV320.h: Texas instruments TLV320AIC23B library NXP LPC1768
d_worrall 0:3d6892f6384f 3 *
d_worrall 0:3d6892f6384f 4 * Written by:
d_worrall 0:3d6892f6384f 5 * mbed Team
d_worrall 0:3d6892f6384f 6 * Ioannis Kedros - 10th January 2011
d_worrall 0:3d6892f6384f 7 * (basic functionality incurred)
d_worrall 0:3d6892f6384f 8 * Daniel Worrall - 15th July 2011
d_worrall 0:3d6892f6384f 9 * (I2SSlave implementation and API restructuring)
d_worrall 0:3d6892f6384f 10 *
d_worrall 0:3d6892f6384f 11 * History
d_worrall 0:3d6892f6384f 12 * version 2.29
d_worrall 0:3d6892f6384f 13 ******************************************************/
d_worrall 0:3d6892f6384f 14
d_worrall 0:3d6892f6384f 15 #ifndef MBED_TLV320_H
d_worrall 0:3d6892f6384f 16 #define MBED_TLV320_H
d_worrall 0:3d6892f6384f 17
d_worrall 0:3d6892f6384f 18 #include "mbed.h"
d_worrall 0:3d6892f6384f 19 #include "I2SSlave.h"
d_worrall 0:3d6892f6384f 20
d_worrall 0:3d6892f6384f 21 /** TLV320 class, defined on the I2C master bus
d_worrall 0:3d6892f6384f 22 *
d_worrall 0:3d6892f6384f 23 */
d_worrall 0:3d6892f6384f 24
d_worrall 0:3d6892f6384f 25 class TLV320
d_worrall 0:3d6892f6384f 26 {
d_worrall 0:3d6892f6384f 27 public:
d_worrall 0:3d6892f6384f 28 //constructor
d_worrall 0:3d6892f6384f 29 /** Create a TLV320 object defined on the I2C port
d_worrall 0:3d6892f6384f 30 *
d_worrall 0:3d6892f6384f 31 * @param sda Serial data pin (p9 or p28)
d_worrall 0:3d6892f6384f 32 * @param scl Serial clock pin (p10 or p27)
d_worrall 0:3d6892f6384f 33 * @param addr Object address
d_worrall 0:3d6892f6384f 34 */
d_worrall 0:3d6892f6384f 35 TLV320(PinName sda, PinName scl, int addr, PinName tx_sda, PinName tx_ws, PinName clk, PinName rx_sda, PinName rx_ws);
d_worrall 0:3d6892f6384f 36 /** Power up/down
d_worrall 0:3d6892f6384f 37 *
d_worrall 0:3d6892f6384f 38 * @param powerUp 0 = power down, 1 = power up
d_worrall 0:3d6892f6384f 39 */
d_worrall 0:3d6892f6384f 40 void power(bool powerUp);
d_worrall 0:3d6892f6384f 41 /** Overloaded power() function
d_worrall 0:3d6892f6384f 42 *
d_worrall 0:3d6892f6384f 43 * @param device Call individual devices to power up/down
d_worrall 0:3d6892f6384f 44 * Device power 0x00 = On 0x80 = Off
d_worrall 0:3d6892f6384f 45 * Clock 0x00 = On 0x40 = Off
d_worrall 0:3d6892f6384f 46 * Oscillator 0x00 = On 0x20 = Off
d_worrall 0:3d6892f6384f 47 * Outputs 0x00 = On 0x10 = Off
d_worrall 0:3d6892f6384f 48 * DAC 0x00 = On 0x08 = Off
d_worrall 0:3d6892f6384f 49 * ADC 0x00 = On 0x04 = Off
d_worrall 0:3d6892f6384f 50 * Microphone input 0x00 = On 0x02 = Off
d_worrall 0:3d6892f6384f 51 * Line input 0x00 = On 0x01 = Off
d_worrall 0:3d6892f6384f 52 */
d_worrall 0:3d6892f6384f 53 void power(int device);
d_worrall 0:3d6892f6384f 54 /** Set I2S interface bit length and mode
d_worrall 0:3d6892f6384f 55 *
d_worrall 0:3d6892f6384f 56 * @param length Set bit length to 16, 20, 24 or 32 bits
d_worrall 0:3d6892f6384f 57 * @param mode Set STEREO (0), MONO (1)
d_worrall 0:3d6892f6384f 58 */
d_worrall 0:3d6892f6384f 59 void format(char length, bool mode);
d_worrall 0:3d6892f6384f 60 /** Set sample frequency
d_worrall 0:3d6892f6384f 61 *
d_worrall 0:3d6892f6384f 62 * @param frequency Sample frequency of data in Hz
d_worrall 0:3d6892f6384f 63 * @return Returns an integer 0 = success, -1 = unrecognnised frequency
d_worrall 0:3d6892f6384f 64 *
d_worrall 0:3d6892f6384f 65 * The TLV320 supports the following frequencies: 8kHz, 8.021kHz, 32kHz, 44.1kHz, 48kHz, 88.2kHz, 96kHz
d_worrall 0:3d6892f6384f 66 * Default is 44.1kHz
d_worrall 0:3d6892f6384f 67 */
d_worrall 0:3d6892f6384f 68 int frequency(int hz);
d_worrall 0:3d6892f6384f 69 /** Reset TLV320
d_worrall 0:3d6892f6384f 70 *
d_worrall 0:3d6892f6384f 71 */
d_worrall 0:3d6892f6384f 72 void reset(void);
d_worrall 0:3d6892f6384f 73 /** Start streaming i.e. enable interrupts
d_worrall 0:3d6892f6384f 74 *
d_worrall 0:3d6892f6384f 75 * @param mode Enable interrupts for NONE, TRANSMIT only, RECEIVE only, BOTH
d_worrall 0:3d6892f6384f 76 */
d_worrall 0:3d6892f6384f 77 void start(int mode);
d_worrall 0:3d6892f6384f 78 /** Stop streaming i.e. disable all interrupts
d_worrall 0:3d6892f6384f 79 *
d_worrall 0:3d6892f6384f 80 */
d_worrall 0:3d6892f6384f 81 void stop(void);
d_worrall 0:3d6892f6384f 82 /** Write [length] 32 bit words in buffer to I2S port
d_worrall 0:3d6892f6384f 83 *
d_worrall 0:3d6892f6384f 84 * @param *buffer Address of buffer to be written
d_worrall 0:3d6892f6384f 85 * @param from Start position in buffer to read from
d_worrall 0:3d6892f6384f 86 * @param length Number of words to be written (MUST not exceed 4)
d_worrall 0:3d6892f6384f 87 */
d_worrall 0:3d6892f6384f 88 void write(int *buffer, int from, int length);
d_worrall 0:3d6892f6384f 89 /** Read 4 x (32bit) words into rxBuffer
d_worrall 0:3d6892f6384f 90 *
d_worrall 0:3d6892f6384f 91 */
d_worrall 0:3d6892f6384f 92 void read(void);
d_worrall 0:3d6892f6384f 93 /** Attach a void/void function or void/void static member funciton to an interrupt generated by the I2SxxFIFOs
d_worrall 0:3d6892f6384f 94 *
d_worrall 0:3d6892f6384f 95 * @param function Function to attach
d_worrall 0:3d6892f6384f 96 *
d_worrall 0:3d6892f6384f 97 * e.g. myTlv320Object.attach(&myfunction);
d_worrall 0:3d6892f6384f 98 * OR myTlv320Object.attach(&myClass::myStaticMemberFunction);
d_worrall 0:3d6892f6384f 99 */
d_worrall 0:3d6892f6384f 100 void attach(void(*fptr)(void));
d_worrall 0:3d6892f6384f 101 /** Attach a nonstatic void/void member function to an interrupt generated by the I2SxxFIFOs
d_worrall 0:3d6892f6384f 102 *
d_worrall 0:3d6892f6384f 103 * @param tptr Object pointer
d_worrall 0:3d6892f6384f 104 * @param mptr Member function pointer
d_worrall 0:3d6892f6384f 105 *
d_worrall 0:3d6892f6384f 106 * e.g. myTlv320Object.attach(&myObject, &myClass::myNonstaticMemberFunction); where myObject is an object of myClass
d_worrall 0:3d6892f6384f 107 */
d_worrall 0:3d6892f6384f 108 template<typename T>
d_worrall 0:3d6892f6384f 109 void attach(T *tptr, void(T::*mptr)(void)){
d_worrall 0:3d6892f6384f 110 mI2s_.attach(tptr, mptr);
d_worrall 0:3d6892f6384f 111 }
d_worrall 0:3d6892f6384f 112 /** Line in volume control i.e. record volume
d_worrall 0:3d6892f6384f 113 *
d_worrall 0:3d6892f6384f 114 * @param leftVolumeIn Left line-in volume
d_worrall 0:3d6892f6384f 115 * @param rightVolumeIn Right line-in volume
d_worrall 0:3d6892f6384f 116 * @return Returns 0 for success, -1 if parameters are out of range
d_worrall 0:3d6892f6384f 117 * Parameters accept a value, where 0.0 < parameter < 1.0 and where 0.0 maps to -34.5dB
d_worrall 0:3d6892f6384f 118 * and 1.0 maps to +12dB (0.74 = 0 dB default).
d_worrall 0:3d6892f6384f 119 */
d_worrall 0:3d6892f6384f 120 int inputVolume(float leftVolumeIn, float rightVolumeIn);
d_worrall 0:3d6892f6384f 121 /** Headphone out volume control
d_worrall 0:3d6892f6384f 122 *
d_worrall 0:3d6892f6384f 123 * @param leftVolumeOut Left line-out volume
d_worrall 0:3d6892f6384f 124 * @param rightVolumeOut Right line-out volume
d_worrall 0:3d6892f6384f 125 * @return Returns 0 for success, -1 if parameters are out of range
d_worrall 0:3d6892f6384f 126 * Parameters accept a value, where 0.0 < parameter < 1.0 and where 0.0 maps to -73dB (mute)
d_worrall 0:3d6892f6384f 127 * and 1.0 maps to +6dB (0.5 = default)
d_worrall 0:3d6892f6384f 128 */
d_worrall 0:3d6892f6384f 129 int outputVolume(float leftVolumeOut, float rightVolumeOut);
d_worrall 0:3d6892f6384f 130 /** Analog audio path control
d_worrall 0:3d6892f6384f 131 *
d_worrall 0:3d6892f6384f 132 * @param bypassVar Route analogue audio direct from line in to headphone out
d_worrall 0:3d6892f6384f 133 */
d_worrall 0:3d6892f6384f 134 void bypass(bool bypassVar);
d_worrall 0:3d6892f6384f 135 /**Digital audio path control
d_worrall 0:3d6892f6384f 136 *
d_worrall 0:3d6892f6384f 137 * @param softMute Mute output
d_worrall 0:3d6892f6384f 138 */
d_worrall 0:3d6892f6384f 139 void mute(bool softMute);
d_worrall 0:3d6892f6384f 140 //Receive buffer
d_worrall 0:3d6892f6384f 141
d_worrall 0:3d6892f6384f 142 int *rxBuffer;
d_worrall 0:3d6892f6384f 143
d_worrall 0:3d6892f6384f 144 protected:
d_worrall 0:3d6892f6384f 145 char cmd[2]; //the address and command for TLV320 internal registers
d_worrall 0:3d6892f6384f 146 int mAddr; //register write address
d_worrall 0:3d6892f6384f 147 private:
d_worrall 0:3d6892f6384f 148 I2C mI2c_; //MUST use the I2C port
d_worrall 0:3d6892f6384f 149 I2SSlave mI2s_;
d_worrall 0:3d6892f6384f 150 Ticker I2sTick;
d_worrall 0:3d6892f6384f 151 void io(void);
d_worrall 0:3d6892f6384f 152 /** Sample rate control
d_worrall 0:3d6892f6384f 153 *
d_worrall 0:3d6892f6384f 154 * @param rate Set the sampling rate as per datasheet section 3.3.2
d_worrall 0:3d6892f6384f 155 * @param clockIn Set the clock in divider MCLK, MCLK_DIV2
d_worrall 0:3d6892f6384f 156 * @param clockMode Set clock mode CLOCK_NORMAL, CLOCK_USB
d_worrall 0:3d6892f6384f 157 */
d_worrall 0:3d6892f6384f 158 void setSampleRate_(char rate, bool clockIn, bool mode, bool bOSR);
d_worrall 0:3d6892f6384f 159 /** Digital interface activation
d_worrall 0:3d6892f6384f 160 *
d_worrall 0:3d6892f6384f 161 */
d_worrall 0:3d6892f6384f 162 void activateDigitalInterface_(void);
d_worrall 0:3d6892f6384f 163 /** Digital interface deactivation
d_worrall 0:3d6892f6384f 164 *
d_worrall 0:3d6892f6384f 165 */
d_worrall 0:3d6892f6384f 166 void deactivateDigitalInterface_(void);
d_worrall 0:3d6892f6384f 167
d_worrall 0:3d6892f6384f 168 //TLV320AIC23B register addresses as defined in the TLV320AIC23B datasheet
d_worrall 0:3d6892f6384f 169 #define LEFT_LINE_INPUT_CHANNEL_VOLUME_CONTROL (0x00 << 1)
d_worrall 0:3d6892f6384f 170 #define RIGHT_LINE_INPUT_CHANNEL_VOLUME_CONTROL (0x01 << 1)
d_worrall 0:3d6892f6384f 171 #define LEFT_CHANNEL_HEADPHONE_VOLUME_CONTROL (0x02 << 1)
d_worrall 0:3d6892f6384f 172 #define RIGHT_CHANNEL_HEADPHONE_VOLUME_CONTROL (0x03 << 1)
d_worrall 0:3d6892f6384f 173 #define ANALOG_AUDIO_PATH_CONTROL (0x04 << 1)
d_worrall 0:3d6892f6384f 174 #define DIGITAL_AUDIO_PATH_CONTROL (0x05 << 1)
d_worrall 0:3d6892f6384f 175 #define POWER_DOWN_CONTROL (0x06 << 1)
d_worrall 0:3d6892f6384f 176 #define DIGITAL_AUDIO_INTERFACE_FORMAT (0x07 << 1)
d_worrall 0:3d6892f6384f 177 #define SAMPLE_RATE_CONTROL (0x08 << 1)
d_worrall 0:3d6892f6384f 178 #define DIGITAL_INTERFACE_ACTIVATION (0x09 << 1)
d_worrall 0:3d6892f6384f 179 #define RESET_REGISTER (0x0F << 1)
d_worrall 0:3d6892f6384f 180
d_worrall 0:3d6892f6384f 181 #define CLOCK_NORMAL 0
d_worrall 0:3d6892f6384f 182 #define CLOCK_USB 1
d_worrall 0:3d6892f6384f 183 #define MCLK 0
d_worrall 0:3d6892f6384f 184 #define MCLK_DIV2 1
d_worrall 0:3d6892f6384f 185 };
d_worrall 0:3d6892f6384f 186
d_worrall 0:3d6892f6384f 187 #endif