A fairly well tested TLV320 library for I2C configuration and control of the TI TLV320 part
Embed:
(wiki syntax)
Show/hide line numbers
TLV320.cpp
00001 /** 00002 * @author Giles Barton-Owen 00003 * 00004 * @section LICENSE 00005 * 00006 * Copyright (c) 2012 mbed 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * 00026 * @section DESCRIPTION 00027 * A Driver set for the I2C half of the TLV320 00028 * 00029 */ 00030 00031 #include "TLV320.h" 00032 00033 #define TLV320_HP_VOL_DF_MASK 0x80 00034 00035 00036 #define TLV320_DF_hp_vol_left 0.5 00037 #define TLV320_DF_hp_vol_right 0.5 00038 #define TLV320_DF_li_vol_left 0.5 00039 #define TLV320_DF_li_vol_right 0.5 00040 #define TLV320_DF_sdt_vol 0 00041 00042 const uint8_t base_address = 0x1A; 00043 00044 00045 TLV320::TLV320(PinName i2c_sda, PinName i2c_scl): i2c(i2c_sda,i2c_scl) { 00046 address = base_address; 00047 defaulter(); 00048 form_cmd(all); 00049 } 00050 00051 TLV320::TLV320(PinName i2c_sda, PinName i2c_scl, bool cs_level): i2c(i2c_sda,i2c_scl) { 00052 address = base_address + (1*cs_level); 00053 defaulter(); 00054 form_cmd(all); 00055 } 00056 00057 void TLV320::power(bool on_off) { 00058 device_all_pwr = on_off; 00059 form_cmd(power_control); 00060 } 00061 00062 void TLV320::input_select(int input) { 00063 00064 switch(input) 00065 { 00066 case TLV320_NO_IN: 00067 device_adc_pwr = false; 00068 device_mic_pwr = false; 00069 device_lni_pwr = false; 00070 form_cmd(power_control); 00071 break; 00072 case TLV320_LINE: 00073 device_adc_pwr = true; 00074 device_lni_pwr = true; 00075 device_mic_pwr = false; 00076 ADC_source = TLV320_LINE; 00077 form_cmd(power_control); 00078 form_cmd(path_analog); 00079 break; 00080 case TLV320_MIC: 00081 device_adc_pwr = true; 00082 device_lni_pwr = false; 00083 device_mic_pwr = true; 00084 ADC_source = TLV320_MIC; 00085 form_cmd(power_control); 00086 form_cmd(path_analog); 00087 break; 00088 default: 00089 device_adc_pwr = df_device_adc_pwr; 00090 device_mic_pwr = df_device_mic_pwr; 00091 device_lni_pwr = df_device_lni_pwr; 00092 ADC_source = df_ADC_source; 00093 form_cmd(power_control); 00094 form_cmd(path_analog); 00095 break; 00096 } 00097 ADC_source_old = ADC_source; 00098 } 00099 00100 void TLV320::headphone_volume(float h_volume) { 00101 hp_vol_left = h_volume; 00102 hp_vol_right = h_volume; 00103 form_cmd(headphone_vol_left); 00104 form_cmd(headphone_vol_right); 00105 } 00106 00107 void TLV320::linein_volume(float li_volume) { 00108 li_vol_left = li_volume; 00109 li_vol_right = li_volume; 00110 form_cmd(line_in_vol_left); 00111 form_cmd(line_in_vol_right); 00112 } 00113 00114 void TLV320::microphone_boost(bool mic_boost) { 00115 mic_boost_ = mic_boost; 00116 } 00117 00118 void TLV320::input_mute(bool mute) { 00119 if(ADC_source == TLV320_MIC) 00120 { 00121 mic_mute = mute; 00122 form_cmd(path_analog); 00123 } 00124 else 00125 { 00126 li_mute_left = mute; 00127 li_mute_right = mute; 00128 form_cmd(line_in_vol_left); 00129 form_cmd(line_in_vol_right); 00130 } 00131 } 00132 00133 void TLV320::output_mute(bool mute) { 00134 out_mute = mute; 00135 form_cmd(path_digital); 00136 } 00137 00138 void TLV320::input_power(bool on_off) { 00139 00140 device_adc_pwr = on_off; 00141 00142 if(ADC_source == TLV320_MIC) 00143 { 00144 device_mic_pwr = on_off; 00145 device_lni_pwr = false; 00146 } 00147 else 00148 { 00149 device_mic_pwr = false; 00150 device_lni_pwr = on_off; 00151 } 00152 00153 form_cmd(power_control); 00154 } 00155 00156 void TLV320::output_power(bool on_off) { 00157 device_dac_pwr = on_off; 00158 device_out_pwr = on_off; 00159 00160 form_cmd(power_control); 00161 } 00162 00163 void TLV320::wordsize(int words) { 00164 device_bitlength = words; 00165 form_cmd(interface_format); 00166 } 00167 00168 void TLV320::master(bool master) { 00169 device_master = master; 00170 form_cmd(interface_format); 00171 } 00172 00173 void TLV320::frequency(int freq) { 00174 ADC_rate = freq; 00175 DAC_rate = freq; 00176 form_cmd(sample_rate); 00177 } 00178 00179 void TLV320::input_highpass(bool enabled) { 00180 ADC_highpass_enable = enabled; 00181 form_cmd(path_digital); 00182 } 00183 00184 void TLV320::output_softmute(bool enabled) { 00185 out_mute = enabled; 00186 form_cmd(path_digital); 00187 } 00188 00189 void TLV320::interface_switch(bool on_off) { 00190 device_interface_active = on_off; 00191 form_cmd(interface_activation); 00192 } 00193 00194 void TLV320::sidetone(float sidetone_vol) { 00195 sdt_vol = sidetone_vol; 00196 form_cmd(path_analog); 00197 } 00198 00199 void TLV320::deemphasis(char code) { 00200 de_emph_code = code & 0x03; 00201 form_cmd(path_digital); 00202 } 00203 00204 void TLV320::reset() { 00205 form_cmd(reset_reg); 00206 } 00207 00208 void TLV320::start() { 00209 interface_switch(true); 00210 } 00211 00212 void TLV320::bypass(bool enable) { 00213 bypass_ = enable; 00214 form_cmd(path_analog); 00215 } 00216 00217 void TLV320::stop() { 00218 interface_switch(false); 00219 } 00220 00221 void TLV320::command(reg_address add, uint16_t cmd) { 00222 char temp[2]; 00223 temp[0] = (char(add)<<1) | ((cmd >> 6) & 0x01); 00224 temp[1] = (cmd & 0xFF); 00225 i2c.write((address<<1), temp, 2); 00226 } 00227 00228 void TLV320::form_cmd(reg_address add) { 00229 uint16_t cmd = 0; 00230 int temp = 0; 00231 bool mute; 00232 switch(add) 00233 { 00234 case line_in_vol_left: 00235 temp = int(li_vol_left * 32) - 1; 00236 mute = li_mute_left; 00237 00238 if(temp < 0) 00239 { 00240 temp = 0; 00241 mute = true; 00242 } 00243 cmd = temp & 0x1F; 00244 cmd |= mute << 7; 00245 break; 00246 case line_in_vol_right: 00247 temp = int(li_vol_right * 32) - 1; 00248 mute = li_mute_right; 00249 if(temp < 0) 00250 { 00251 temp = 0; 00252 mute = true; 00253 } 00254 cmd = temp & 0x1F; 00255 cmd |= mute << 7; 00256 break; 00257 00258 case headphone_vol_left: 00259 temp = int(hp_vol_left * 80) + 47; 00260 cmd = TLV320_HP_VOL_DF_MASK; 00261 cmd |= temp & 0x7F; 00262 break; 00263 case headphone_vol_right: 00264 temp = int(hp_vol_right * 80) + 47; 00265 cmd = TLV320_HP_VOL_DF_MASK; 00266 cmd |= temp & 0x7F; 00267 break; 00268 00269 case path_analog: 00270 temp = int(sdt_vol * 5); 00271 char vol_code = 0; 00272 switch(temp) 00273 { 00274 case 5: 00275 vol_code = 0x0C; 00276 break; 00277 case 0: 00278 vol_code = 0x00; 00279 break; 00280 default: 00281 vol_code = ((0x04 - temp)&0x07) | 0x08; 00282 break; 00283 } 00284 cmd = vol_code << 5; 00285 cmd |= 1 << 4; 00286 cmd |= bypass_ << 3; 00287 cmd |= ADC_source << 2; 00288 cmd |= mic_mute << 1; 00289 cmd |= mic_boost_; 00290 break; 00291 00292 case path_digital: 00293 cmd |= out_mute << 3; 00294 cmd |= ((de_emph_code & 0x3) << 1); 00295 cmd |= ADC_highpass_enable; 00296 break; 00297 00298 case power_control: 00299 cmd |= !device_all_pwr << 7; 00300 cmd |= !device_clk_pwr << 6; 00301 cmd |= !device_osc_pwr << 5; 00302 cmd |= !device_out_pwr << 4; 00303 cmd |= !device_dac_pwr << 3; 00304 cmd |= !device_adc_pwr << 2; 00305 cmd |= !device_mic_pwr << 1; 00306 cmd |= !device_lni_pwr << 0; 00307 break; 00308 00309 case interface_format: 00310 cmd |= device_master << 6; 00311 cmd |= device_lrswap << 5; 00312 cmd |= device_lrws << 4; 00313 temp = 0; 00314 switch(device_bitlength) 00315 { 00316 case 16: 00317 temp = 0; 00318 break; 00319 case 20: 00320 temp = 1; 00321 break; 00322 case 24: 00323 temp = 2; 00324 break; 00325 case 32: 00326 temp = 3; 00327 break; 00328 } 00329 cmd |= (temp & 0x03) << 2; 00330 cmd |= (device_data_form & 0x03); 00331 break; 00332 00333 case sample_rate: 00334 temp = gen_samplerate(); 00335 cmd = device_usb_mode; 00336 cmd |= (temp & 0x03) << 1; 00337 cmd |= device_clk_in_div << 6; 00338 cmd |= device_clk_out_div << 7; 00339 break; 00340 00341 case interface_activation: 00342 cmd = device_interface_active; 00343 break; 00344 00345 case reset_reg: 00346 cmd = 0; 00347 break; 00348 00349 case all: 00350 for( int i = line_in_vol_left; i <= reset_reg; i++) 00351 { 00352 form_cmd((reg_address)i); 00353 } 00354 break; 00355 } 00356 if(add != all) command(add , cmd); 00357 } 00358 00359 void TLV320::defaulter() { 00360 hp_vol_left = TLV320_DF_hp_vol_left; 00361 hp_vol_right = TLV320_DF_hp_vol_right; 00362 li_vol_left = TLV320_DF_li_vol_left; 00363 li_vol_right = TLV320_DF_li_vol_right; 00364 sdt_vol = TLV320_DF_sdt_vol; 00365 bypass_ = df_bypass_; 00366 00367 ADC_source = df_ADC_source; 00368 ADC_source_old = df_ADC_source; 00369 00370 mic_mute = df_mic_mute; 00371 li_mute_left = df_li_mute_left; 00372 li_mute_right = df_li_mute_right; 00373 00374 00375 mic_boost_ = df_mic_boost_; 00376 out_mute = df_out_mute; 00377 de_emph_code = df_de_emph_code; 00378 ADC_highpass_enable = df_ADC_highpass_enable; 00379 00380 device_all_pwr = df_device_all_pwr; 00381 device_clk_pwr = df_device_clk_pwr; 00382 device_osc_pwr = df_device_osc_pwr; 00383 device_out_pwr = df_device_out_pwr; 00384 device_dac_pwr = df_device_dac_pwr; 00385 device_adc_pwr = df_device_dac_pwr; 00386 device_mic_pwr = df_device_mic_pwr; 00387 device_lni_pwr = df_device_lni_pwr; 00388 00389 device_master = df_device_master; 00390 device_lrswap = df_device_lrswap; 00391 device_lrws = df_device_lrws; 00392 device_bitlength = df_device_bitlength; 00393 00394 00395 ADC_rate = df_ADC_rate; 00396 DAC_rate = df_DAC_rate; 00397 00398 device_interface_active = df_device_interface_active; 00399 } 00400 00401 char TLV320::gen_samplerate() { 00402 char temp = 0; 00403 switch(ADC_rate) 00404 { 00405 case 96000: 00406 temp = 0x0E; 00407 break; 00408 case 48000: 00409 temp = 0x00; 00410 if(DAC_rate == 8000) temp = 0x02; 00411 break; 00412 case 32000: 00413 temp = 0x0C; 00414 break; 00415 case 8000: 00416 temp = 0x03; 00417 if(DAC_rate == 48000) temp = 0x04; 00418 break; 00419 default: 00420 temp = 0x00; 00421 break; 00422 } 00423 return temp; 00424 } 00425 00426
Generated on Fri Jul 15 2022 00:36:04 by 1.7.2