A fairly well tested TLV320 library for I2C configuration and control of the TI TLV320 part

Dependents:   TLV320_Write_test

Committer:
p07gbar
Date:
Tue Sep 25 15:22:02 2012 +0000
Revision:
1:3ee50344f8f4
Parent:
0:a68f7c573e8c
Added header to cpp file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p07gbar 0:a68f7c573e8c 1 /**
p07gbar 0:a68f7c573e8c 2 * @author Giles Barton-Owen
p07gbar 0:a68f7c573e8c 3 *
p07gbar 0:a68f7c573e8c 4 * @section LICENSE
p07gbar 0:a68f7c573e8c 5 *
p07gbar 0:a68f7c573e8c 6 * Copyright (c) 2012 mbed
p07gbar 0:a68f7c573e8c 7 *
p07gbar 0:a68f7c573e8c 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
p07gbar 0:a68f7c573e8c 9 * of this software and associated documentation files (the "Software"), to deal
p07gbar 0:a68f7c573e8c 10 * in the Software without restriction, including without limitation the rights
p07gbar 0:a68f7c573e8c 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
p07gbar 0:a68f7c573e8c 12 * copies of the Software, and to permit persons to whom the Software is
p07gbar 0:a68f7c573e8c 13 * furnished to do so, subject to the following conditions:
p07gbar 0:a68f7c573e8c 14 *
p07gbar 0:a68f7c573e8c 15 * The above copyright notice and this permission notice shall be included in
p07gbar 0:a68f7c573e8c 16 * all copies or substantial portions of the Software.
p07gbar 0:a68f7c573e8c 17 *
p07gbar 0:a68f7c573e8c 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
p07gbar 0:a68f7c573e8c 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
p07gbar 0:a68f7c573e8c 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
p07gbar 0:a68f7c573e8c 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
p07gbar 0:a68f7c573e8c 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
p07gbar 0:a68f7c573e8c 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
p07gbar 0:a68f7c573e8c 24 * THE SOFTWARE.
p07gbar 0:a68f7c573e8c 25 *
p07gbar 0:a68f7c573e8c 26 * @section DESCRIPTION
p07gbar 0:a68f7c573e8c 27 * A Driver set for the I2C half of the TLV320
p07gbar 0:a68f7c573e8c 28 *
p07gbar 0:a68f7c573e8c 29 */
p07gbar 0:a68f7c573e8c 30
p07gbar 0:a68f7c573e8c 31
p07gbar 0:a68f7c573e8c 32 #ifndef TLV320_H
p07gbar 0:a68f7c573e8c 33 #define TLV320_H
p07gbar 0:a68f7c573e8c 34 #include "mbed.h"
p07gbar 0:a68f7c573e8c 35
p07gbar 0:a68f7c573e8c 36 #define TLV320_CS_HIGH true
p07gbar 0:a68f7c573e8c 37 #define TLV320_CS_LOW false
p07gbar 0:a68f7c573e8c 38
p07gbar 0:a68f7c573e8c 39 #define TLV320_ON true
p07gbar 0:a68f7c573e8c 40 #define TLV320_OFF false
p07gbar 0:a68f7c573e8c 41
p07gbar 0:a68f7c573e8c 42 #define TLV320_MUTE true
p07gbar 0:a68f7c573e8c 43 #define TLV320_UNMUTE false
p07gbar 0:a68f7c573e8c 44
p07gbar 0:a68f7c573e8c 45 #define TLV320_MASTER true
p07gbar 0:a68f7c573e8c 46 #define TLV320_SLAVE false
p07gbar 0:a68f7c573e8c 47
p07gbar 0:a68f7c573e8c 48 #define TLV320_LINE 0
p07gbar 0:a68f7c573e8c 49 #define TLV320_MIC 1
p07gbar 0:a68f7c573e8c 50 #define TLV320_NO_IN -1
p07gbar 0:a68f7c573e8c 51
p07gbar 0:a68f7c573e8c 52 #define TLV320_DE_EMPH_DISABLED 0
p07gbar 0:a68f7c573e8c 53 #define TLV320_DE_EMPH_32KHZ 1
p07gbar 0:a68f7c573e8c 54 #define TLV320_DE_EMPH_44KHZ 2
p07gbar 0:a68f7c573e8c 55 #define TLV320_DE_EMPH_48KHZ 3
p07gbar 0:a68f7c573e8c 56
p07gbar 0:a68f7c573e8c 57 /** A class to control the I2C part of the TLV320
p07gbar 0:a68f7c573e8c 58 *
p07gbar 0:a68f7c573e8c 59 */
p07gbar 0:a68f7c573e8c 60 class TLV320
p07gbar 0:a68f7c573e8c 61 {
p07gbar 0:a68f7c573e8c 62 public:
p07gbar 0:a68f7c573e8c 63 /** Create an instance of the TLV320 class
p07gbar 0:a68f7c573e8c 64 *
p07gbar 0:a68f7c573e8c 65 * @param i2c_sda The SDA pin of the I2C
p07gbar 0:a68f7c573e8c 66 * @param i2c_scl The SCL pin of the I2C
p07gbar 0:a68f7c573e8c 67 */
p07gbar 0:a68f7c573e8c 68 TLV320(PinName i2c_sda, PinName i2c_scl);
p07gbar 0:a68f7c573e8c 69
p07gbar 0:a68f7c573e8c 70 /** Create an instance of the TLV320 class
p07gbar 0:a68f7c573e8c 71 *
p07gbar 0:a68f7c573e8c 72 * @param i2c_sda The SDA pin of the I2C
p07gbar 0:a68f7c573e8c 73 * @param i2c_scl The SCL pin of the I2C
p07gbar 0:a68f7c573e8c 74 * @param cs_level The level of the CS pin on the TLV320
p07gbar 0:a68f7c573e8c 75 */
p07gbar 0:a68f7c573e8c 76 TLV320(PinName i2c_sda, PinName i2c_scl, bool cs_level);
p07gbar 0:a68f7c573e8c 77
p07gbar 0:a68f7c573e8c 78 /** Control the power of the device
p07gbar 0:a68f7c573e8c 79 *
p07gbar 0:a68f7c573e8c 80 * @param on_off The power state
p07gbar 0:a68f7c573e8c 81 */
p07gbar 0:a68f7c573e8c 82 void power(bool on_off);
p07gbar 0:a68f7c573e8c 83
p07gbar 0:a68f7c573e8c 84 /** Control the input source of the device
p07gbar 0:a68f7c573e8c 85 *
p07gbar 0:a68f7c573e8c 86 * @param input Select the source of the input of the device: TLV320_LINE, TLV320_MIC, TLV320_NO_IN
p07gbar 0:a68f7c573e8c 87 */
p07gbar 0:a68f7c573e8c 88 void input_select(int input);
p07gbar 0:a68f7c573e8c 89
p07gbar 0:a68f7c573e8c 90 /** Set the headphone volume
p07gbar 0:a68f7c573e8c 91 *
p07gbar 0:a68f7c573e8c 92 * @param h_volume The desired headphone volume: 0->1
p07gbar 0:a68f7c573e8c 93 */
p07gbar 0:a68f7c573e8c 94 void headphone_volume(float h_volume);
p07gbar 0:a68f7c573e8c 95
p07gbar 0:a68f7c573e8c 96 /** Set the line in pre-amp volume
p07gbar 0:a68f7c573e8c 97 *
p07gbar 0:a68f7c573e8c 98 * @param li_volume The desired line in volume: 0->1
p07gbar 0:a68f7c573e8c 99 */
p07gbar 0:a68f7c573e8c 100 void linein_volume(float li_volume);
p07gbar 0:a68f7c573e8c 101
p07gbar 0:a68f7c573e8c 102 /** Turn on/off the microphone pre-amp boost
p07gbar 0:a68f7c573e8c 103 *
p07gbar 0:a68f7c573e8c 104 * @param mic_boost Boost on or off
p07gbar 0:a68f7c573e8c 105 */
p07gbar 0:a68f7c573e8c 106 void microphone_boost(bool mic_boost);
p07gbar 0:a68f7c573e8c 107
p07gbar 0:a68f7c573e8c 108 /** Mute the input
p07gbar 0:a68f7c573e8c 109 *
p07gbar 0:a68f7c573e8c 110 * @param mute Mute on/off
p07gbar 0:a68f7c573e8c 111 */
p07gbar 0:a68f7c573e8c 112 void input_mute(bool mute);
p07gbar 0:a68f7c573e8c 113
p07gbar 0:a68f7c573e8c 114 /** Mute the output
p07gbar 0:a68f7c573e8c 115 *
p07gbar 0:a68f7c573e8c 116 * @param mute Mute on/off
p07gbar 0:a68f7c573e8c 117 */
p07gbar 0:a68f7c573e8c 118 void output_mute(bool mute);
p07gbar 0:a68f7c573e8c 119
p07gbar 0:a68f7c573e8c 120 /** Turn on/off the input stage
p07gbar 0:a68f7c573e8c 121 *
p07gbar 0:a68f7c573e8c 122 * @param on_off Input stage on(true)/off(false)
p07gbar 0:a68f7c573e8c 123 */
p07gbar 0:a68f7c573e8c 124 void input_power(bool on_off);
p07gbar 0:a68f7c573e8c 125
p07gbar 0:a68f7c573e8c 126 /** Turn on/off the output stage
p07gbar 0:a68f7c573e8c 127 *
p07gbar 0:a68f7c573e8c 128 * @param on_off Output stage on(true)/off(false)
p07gbar 0:a68f7c573e8c 129 */
p07gbar 0:a68f7c573e8c 130 void output_power(bool on_off);
p07gbar 0:a68f7c573e8c 131
p07gbar 0:a68f7c573e8c 132 /** Select the word size
p07gbar 0:a68f7c573e8c 133 *
p07gbar 0:a68f7c573e8c 134 * @param words 16/20/24/32 bits
p07gbar 0:a68f7c573e8c 135 */
p07gbar 0:a68f7c573e8c 136 void wordsize(int words);
p07gbar 0:a68f7c573e8c 137
p07gbar 0:a68f7c573e8c 138 /** Select interface mode: Master or Slave
p07gbar 0:a68f7c573e8c 139 *
p07gbar 0:a68f7c573e8c 140 * @param master Interface mode: master(true)/slave
p07gbar 0:a68f7c573e8c 141 */
p07gbar 0:a68f7c573e8c 142 void master(bool master);
p07gbar 0:a68f7c573e8c 143
p07gbar 0:a68f7c573e8c 144 /** Select the sample rate
p07gbar 0:a68f7c573e8c 145 *
p07gbar 0:a68f7c573e8c 146 * @param freq Frequency: 96/48/32/8 kHz
p07gbar 0:a68f7c573e8c 147 */
p07gbar 0:a68f7c573e8c 148 void frequency(int freq);
p07gbar 0:a68f7c573e8c 149
p07gbar 0:a68f7c573e8c 150 /** Enable the input highpass filter
p07gbar 0:a68f7c573e8c 151 *
p07gbar 0:a68f7c573e8c 152 * @param enabled Input highpass filter enabled
p07gbar 0:a68f7c573e8c 153 */
p07gbar 0:a68f7c573e8c 154 void input_highpass(bool enabled);
p07gbar 0:a68f7c573e8c 155
p07gbar 0:a68f7c573e8c 156 /** Enable the output soft mute
p07gbar 0:a68f7c573e8c 157 *
p07gbar 0:a68f7c573e8c 158 * @param enabled Output soft mute enabled
p07gbar 0:a68f7c573e8c 159 */
p07gbar 0:a68f7c573e8c 160 void output_softmute(bool enabled);
p07gbar 0:a68f7c573e8c 161
p07gbar 0:a68f7c573e8c 162 /** Turn on and off the I2S
p07gbar 0:a68f7c573e8c 163 *
p07gbar 0:a68f7c573e8c 164 * @param on_off Switch the I2S interface on(true)/off(false)
p07gbar 0:a68f7c573e8c 165 */
p07gbar 0:a68f7c573e8c 166 void interface_switch(bool on_off);
p07gbar 0:a68f7c573e8c 167
p07gbar 0:a68f7c573e8c 168 /** Reset the device and settings
p07gbar 0:a68f7c573e8c 169 *
p07gbar 0:a68f7c573e8c 170 */
p07gbar 0:a68f7c573e8c 171 void reset();
p07gbar 0:a68f7c573e8c 172
p07gbar 0:a68f7c573e8c 173 /** Set the microphone sidetone volume
p07gbar 0:a68f7c573e8c 174 *
p07gbar 0:a68f7c573e8c 175 * @param sidetone_volume The volume of the sidetone: 0->1
p07gbar 0:a68f7c573e8c 176 */
p07gbar 0:a68f7c573e8c 177 void sidetone(float sidetone_vol);
p07gbar 0:a68f7c573e8c 178
p07gbar 0:a68f7c573e8c 179 /** Set the analog bypass
p07gbar 0:a68f7c573e8c 180 *
p07gbar 0:a68f7c573e8c 181 * @param bypass_en Enable the bypass: enabled(true)
p07gbar 0:a68f7c573e8c 182 */
p07gbar 0:a68f7c573e8c 183 void bypass(bool bypass_en);
p07gbar 0:a68f7c573e8c 184
p07gbar 0:a68f7c573e8c 185 /** Set the deemphasis frequency
p07gbar 0:a68f7c573e8c 186 *
p07gbar 0:a68f7c573e8c 187 * @param code The deemphasis code: TLV320_DE_EMPH_DISABLED, TLV320_DE_EMPH_32KHZ, TLV320_DE_EMPH_44KHZ, TLV320_DE_EMPH_48KHZ
p07gbar 0:a68f7c573e8c 188 */
p07gbar 0:a68f7c573e8c 189 void deemphasis(char code);
p07gbar 0:a68f7c573e8c 190
p07gbar 0:a68f7c573e8c 191 /** Enable the input highpass filter
p07gbar 0:a68f7c573e8c 192 *
p07gbar 0:a68f7c573e8c 193 * @param enable Enable the input highpass filter enabled(true)
p07gbar 0:a68f7c573e8c 194 */
p07gbar 0:a68f7c573e8c 195
p07gbar 0:a68f7c573e8c 196 void adc_highpass(bool enable);
p07gbar 0:a68f7c573e8c 197
p07gbar 0:a68f7c573e8c 198 /** Start the device sending/recieving etc
p07gbar 0:a68f7c573e8c 199 */
p07gbar 0:a68f7c573e8c 200 void start();
p07gbar 0:a68f7c573e8c 201
p07gbar 0:a68f7c573e8c 202 /** Stop the device sending/recieving etc
p07gbar 0:a68f7c573e8c 203 */
p07gbar 0:a68f7c573e8c 204 void stop();
p07gbar 0:a68f7c573e8c 205
p07gbar 0:a68f7c573e8c 206 private:
p07gbar 0:a68f7c573e8c 207
p07gbar 0:a68f7c573e8c 208 enum reg_address {
p07gbar 0:a68f7c573e8c 209 line_in_vol_left = 0x00,
p07gbar 0:a68f7c573e8c 210 line_in_vol_right = 0x01,
p07gbar 0:a68f7c573e8c 211 headphone_vol_left = 0x02,
p07gbar 0:a68f7c573e8c 212 headphone_vol_right = 0x03,
p07gbar 0:a68f7c573e8c 213 path_analog = 0x04,
p07gbar 0:a68f7c573e8c 214 path_digital = 0x05,
p07gbar 0:a68f7c573e8c 215 power_control = 0x06,
p07gbar 0:a68f7c573e8c 216 interface_format = 0x07,
p07gbar 0:a68f7c573e8c 217 sample_rate = 0x08,
p07gbar 0:a68f7c573e8c 218 interface_activation = 0x09,
p07gbar 0:a68f7c573e8c 219 reset_reg = 0x0A,
p07gbar 0:a68f7c573e8c 220 all = 0xFF
p07gbar 0:a68f7c573e8c 221 };
p07gbar 0:a68f7c573e8c 222
p07gbar 0:a68f7c573e8c 223 enum tlv320_defaults{
p07gbar 0:a68f7c573e8c 224 df_bypass_ = 0,
p07gbar 0:a68f7c573e8c 225 df_ADC_source = TLV320_LINE,
p07gbar 0:a68f7c573e8c 226 df_mic_mute = TLV320_UNMUTE,
p07gbar 0:a68f7c573e8c 227 df_li_mute_left = 0,
p07gbar 0:a68f7c573e8c 228 df_li_mute_right = 0,
p07gbar 0:a68f7c573e8c 229 df_mic_boost_ = 0,
p07gbar 0:a68f7c573e8c 230 df_out_mute = TLV320_UNMUTE,
p07gbar 0:a68f7c573e8c 231
p07gbar 0:a68f7c573e8c 232 df_de_emph_code = 0x00,
p07gbar 0:a68f7c573e8c 233 df_ADC_highpass_enable = 0,
p07gbar 0:a68f7c573e8c 234
p07gbar 0:a68f7c573e8c 235 df_device_all_pwr = 1,
p07gbar 0:a68f7c573e8c 236 df_device_clk_pwr = 1,
p07gbar 0:a68f7c573e8c 237 df_device_osc_pwr = 1,
p07gbar 0:a68f7c573e8c 238 df_device_out_pwr = 1,
p07gbar 0:a68f7c573e8c 239 df_device_dac_pwr = 1,
p07gbar 0:a68f7c573e8c 240 df_device_adc_pwr = 1,
p07gbar 0:a68f7c573e8c 241 df_device_mic_pwr = 0,
p07gbar 0:a68f7c573e8c 242 df_device_lni_pwr = 1,
p07gbar 0:a68f7c573e8c 243
p07gbar 0:a68f7c573e8c 244 df_device_master = 0,
p07gbar 0:a68f7c573e8c 245 df_device_lrswap = 0,
p07gbar 0:a68f7c573e8c 246 df_device_lrws = 0,
p07gbar 0:a68f7c573e8c 247 df_device_bitlength = 32,
p07gbar 0:a68f7c573e8c 248
p07gbar 0:a68f7c573e8c 249 df_ADC_rate = 32000,
p07gbar 0:a68f7c573e8c 250 df_DAC_rate = 32000,
p07gbar 0:a68f7c573e8c 251
p07gbar 0:a68f7c573e8c 252 df_device_interface_active = 0
p07gbar 0:a68f7c573e8c 253 };
p07gbar 0:a68f7c573e8c 254
p07gbar 0:a68f7c573e8c 255
p07gbar 0:a68f7c573e8c 256 I2C i2c;
p07gbar 0:a68f7c573e8c 257 uint8_t address;
p07gbar 0:a68f7c573e8c 258 void command(reg_address add, uint16_t byte);
p07gbar 0:a68f7c573e8c 259 void form_cmd(reg_address add);
p07gbar 0:a68f7c573e8c 260 void defaulter();
p07gbar 0:a68f7c573e8c 261
p07gbar 0:a68f7c573e8c 262 char gen_samplerate();
p07gbar 0:a68f7c573e8c 263
p07gbar 0:a68f7c573e8c 264 //I2S i2s_tx(I2S_TRANSMIT, p5, p6 , p7);
p07gbar 0:a68f7c573e8c 265 //I2S i2s_rx(I2S_RECIEVE , p8, p29, p30);
p07gbar 0:a68f7c573e8c 266
p07gbar 0:a68f7c573e8c 267 float hp_vol_left, hp_vol_right;
p07gbar 0:a68f7c573e8c 268 float li_vol_left, li_vol_right;
p07gbar 0:a68f7c573e8c 269 float sdt_vol;
p07gbar 0:a68f7c573e8c 270 bool li_mute_left, li_mute_right;
p07gbar 0:a68f7c573e8c 271 bool bypass_;
p07gbar 0:a68f7c573e8c 272 bool ADC_source;
p07gbar 0:a68f7c573e8c 273 bool ADC_source_old;
p07gbar 0:a68f7c573e8c 274 bool mic_mute;
p07gbar 0:a68f7c573e8c 275 bool mic_boost_;
p07gbar 0:a68f7c573e8c 276 bool out_mute;
p07gbar 0:a68f7c573e8c 277 char de_emph_code;
p07gbar 0:a68f7c573e8c 278 bool ADC_highpass_enable;
p07gbar 0:a68f7c573e8c 279
p07gbar 0:a68f7c573e8c 280 bool device_all_pwr;
p07gbar 0:a68f7c573e8c 281 bool device_clk_pwr;
p07gbar 0:a68f7c573e8c 282 bool device_osc_pwr;
p07gbar 0:a68f7c573e8c 283 bool device_out_pwr;
p07gbar 0:a68f7c573e8c 284 bool device_dac_pwr;
p07gbar 0:a68f7c573e8c 285 bool device_adc_pwr;
p07gbar 0:a68f7c573e8c 286 bool device_mic_pwr;
p07gbar 0:a68f7c573e8c 287 bool device_lni_pwr;
p07gbar 0:a68f7c573e8c 288
p07gbar 0:a68f7c573e8c 289 bool device_master;
p07gbar 0:a68f7c573e8c 290 bool device_lrswap;
p07gbar 0:a68f7c573e8c 291 bool device_lrws;
p07gbar 0:a68f7c573e8c 292 char device_bitlength;
p07gbar 0:a68f7c573e8c 293 static const char device_data_form = 0x02;
p07gbar 0:a68f7c573e8c 294
p07gbar 0:a68f7c573e8c 295 int ADC_rate;
p07gbar 0:a68f7c573e8c 296 int DAC_rate;
p07gbar 0:a68f7c573e8c 297 static const bool device_usb_mode = false;
p07gbar 0:a68f7c573e8c 298 static const bool device_clk_in_div = false;
p07gbar 0:a68f7c573e8c 299 static const bool device_clk_out_div = false;
p07gbar 0:a68f7c573e8c 300 bool device_interface_active;
p07gbar 0:a68f7c573e8c 301
p07gbar 0:a68f7c573e8c 302 };
p07gbar 0:a68f7c573e8c 303
p07gbar 0:a68f7c573e8c 304
p07gbar 0:a68f7c573e8c 305 #endif