This library controls a ST TDA7419 audio control IC. This is part of a project to implement an mbed controlled car stereo. The TDA7419 will take in stereo and output four channels of audio plus a subwoofer channel.

Dependents:   car_stereo

Committer:
danielashercohen
Date:
Mon Oct 20 05:54:52 2014 +0000
Revision:
1:69c37f1ab7df
Parent:
0:86ea14016b10
Child:
2:34a58356394c
Volume, input and treble controls working.; Moving all the functionality into the library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danielashercohen 0:86ea14016b10 1 /** PreampTDA7419 Library
danielashercohen 0:86ea14016b10 2 *
danielashercohen 0:86ea14016b10 3 * @Author: Dan Cohen
danielashercohen 0:86ea14016b10 4 */
danielashercohen 0:86ea14016b10 5
danielashercohen 0:86ea14016b10 6 #include "mbed.h"
danielashercohen 0:86ea14016b10 7 #include "PreampTDA7419.h"
danielashercohen 0:86ea14016b10 8 #include <stdio.h>
danielashercohen 0:86ea14016b10 9 #include <string.h>
danielashercohen 0:86ea14016b10 10 #include <inttypes.h>
danielashercohen 0:86ea14016b10 11
danielashercohen 0:86ea14016b10 12 PreampTDA7419::PreampTDA7419(PinName sda, PinName scl):
danielashercohen 0:86ea14016b10 13 _device(sda, scl)
danielashercohen 0:86ea14016b10 14 {
danielashercohen 0:86ea14016b10 15 _address = (TDA7419_ADDRESS<<1);
danielashercohen 1:69c37f1ab7df 16
danielashercohen 1:69c37f1ab7df 17 _volume = 6;
danielashercohen 1:69c37f1ab7df 18 _input = 1;
danielashercohen 1:69c37f1ab7df 19
danielashercohen 1:69c37f1ab7df 20 _mute = 0;
danielashercohen 1:69c37f1ab7df 21 _mix = 0;
danielashercohen 1:69c37f1ab7df 22
danielashercohen 1:69c37f1ab7df 23 // for register 4 Treble Filter
danielashercohen 1:69c37f1ab7df 24 _referenceInE = 0;
danielashercohen 1:69c37f1ab7df 25 _trebleCenterFreq = 0;
danielashercohen 1:69c37f1ab7df 26 _treble = 0;
danielashercohen 1:69c37f1ab7df 27
danielashercohen 1:69c37f1ab7df 28 // for middle frequecy filter
danielashercohen 1:69c37f1ab7df 29 _middleSoftStep = 0;
danielashercohen 1:69c37f1ab7df 30 _middleQ = 0;
danielashercohen 1:69c37f1ab7df 31 _middle = 0;
danielashercohen 0:86ea14016b10 32 }
danielashercohen 0:86ea14016b10 33
danielashercohen 0:86ea14016b10 34 void PreampTDA7419::setI2CAddress(uint8_t add)
danielashercohen 0:86ea14016b10 35 {
danielashercohen 0:86ea14016b10 36 _address = (add<<1);
danielashercohen 0:86ea14016b10 37 }
danielashercohen 0:86ea14016b10 38
danielashercohen 1:69c37f1ab7df 39 int PreampTDA7419::i2c_write(int command, int value) {
danielashercohen 0:86ea14016b10 40 int transmissionSuccessful;
danielashercohen 0:86ea14016b10 41 _device.start();
danielashercohen 1:69c37f1ab7df 42 transmissionSuccessful = _device.write(_address);
danielashercohen 1:69c37f1ab7df 43 transmissionSuccessful |= _device.write(command);
danielashercohen 1:69c37f1ab7df 44 transmissionSuccessful |= _device.write(value);
danielashercohen 0:86ea14016b10 45 _device.stop();
danielashercohen 0:86ea14016b10 46 return (transmissionSuccessful);
danielashercohen 0:86ea14016b10 47 }
danielashercohen 0:86ea14016b10 48
danielashercohen 1:69c37f1ab7df 49 void PreampTDA7419::writeToTDA7419 (int address, int value) {
danielashercohen 1:69c37f1ab7df 50 i2c_write(address, value);
danielashercohen 1:69c37f1ab7df 51 }
danielashercohen 1:69c37f1ab7df 52
danielashercohen 1:69c37f1ab7df 53 /////////////////////////////////
danielashercohen 1:69c37f1ab7df 54 // set the speaker attenuators //
danielashercohen 1:69c37f1ab7df 55 /////////////////////////////////
danielashercohen 1:69c37f1ab7df 56 // attenuation can be set from 0 to 11 and this is mapped to the
danielashercohen 1:69c37f1ab7df 57 // values that the TDA7419 uses for it's attenuation (0->h60)
danielashercohen 1:69c37f1ab7df 58
danielashercohen 1:69c37f1ab7df 59 //
danielashercohen 1:69c37f1ab7df 60 // (FL/FR/RL/RR/SWL/SWR) (13-18)
danielashercohen 1:69c37f1ab7df 61 int PreampTDA7419::calcAttenuationReg(int attenuation) {
danielashercohen 1:69c37f1ab7df 62 int regAtten;
danielashercohen 1:69c37f1ab7df 63 if (attenuation == 11) {
danielashercohen 1:69c37f1ab7df 64 regAtten = 13;
danielashercohen 1:69c37f1ab7df 65 } else if (attenuation == 10) {
danielashercohen 1:69c37f1ab7df 66 regAtten = 6;
danielashercohen 1:69c37f1ab7df 67 } else {
danielashercohen 1:69c37f1ab7df 68 regAtten = (99-(attenuation*9));
danielashercohen 1:69c37f1ab7df 69 }
danielashercohen 1:69c37f1ab7df 70 return (regAtten);
danielashercohen 1:69c37f1ab7df 71 }
danielashercohen 1:69c37f1ab7df 72
danielashercohen 1:69c37f1ab7df 73 int PreampTDA7419::calcToneAttenuationReg(int attenuation) {
danielashercohen 1:69c37f1ab7df 74 int regAtten;
danielashercohen 1:69c37f1ab7df 75 if (attenuation > 0) {
danielashercohen 1:69c37f1ab7df 76 regAtten = 16 + (attenuation * 3);
danielashercohen 1:69c37f1ab7df 77 } else if (attenuation == 0) {
danielashercohen 1:69c37f1ab7df 78 regAtten = 0;
danielashercohen 1:69c37f1ab7df 79 } else if (attenuation < 0) {
danielashercohen 1:69c37f1ab7df 80 regAtten = 0 - (attenuation * 3);
danielashercohen 1:69c37f1ab7df 81 }
danielashercohen 1:69c37f1ab7df 82 return (regAtten);
danielashercohen 1:69c37f1ab7df 83 }
danielashercohen 1:69c37f1ab7df 84
danielashercohen 1:69c37f1ab7df 85 // update all of the registers in the TDA7419
danielashercohen 1:69c37f1ab7df 86 void PreampTDA7419::updateTDA7419Reg() {
danielashercohen 1:69c37f1ab7df 87
danielashercohen 1:69c37f1ab7df 88 int regVolume;
danielashercohen 1:69c37f1ab7df 89 int regTreble;
danielashercohen 1:69c37f1ab7df 90 int regMiddle;
danielashercohen 1:69c37f1ab7df 91 int regBass;
danielashercohen 1:69c37f1ab7df 92
danielashercohen 1:69c37f1ab7df 93 int s_main_source = 0;
danielashercohen 1:69c37f1ab7df 94 int s_main_loud = 1 | 0x40;
danielashercohen 1:69c37f1ab7df 95 int s_softmute = 2 | 0x40;
danielashercohen 1:69c37f1ab7df 96 int s_volume = 3 | 0x40;
danielashercohen 1:69c37f1ab7df 97 int s_treble = 4 | 0x40;
danielashercohen 1:69c37f1ab7df 98 int s_middle = 5 | 0x40;
danielashercohen 1:69c37f1ab7df 99 int s_bass = 6 | 0x40;
danielashercohen 1:69c37f1ab7df 100 int s_second_source = 7 | 0x40;
danielashercohen 1:69c37f1ab7df 101 int s_sub_mid_bass = 8 | 0x40;
danielashercohen 1:69c37f1ab7df 102 int s_mix_gain = 9 | 0x40;
danielashercohen 1:69c37f1ab7df 103 int s_atten_lf = 10 | 0x40;
danielashercohen 1:69c37f1ab7df 104 int s_atten_rf = 11 | 0x40;
danielashercohen 1:69c37f1ab7df 105 int s_atten_lr = 12 | 0x40;
danielashercohen 1:69c37f1ab7df 106 int s_atten_rr = 13 | 0x40;
danielashercohen 1:69c37f1ab7df 107 int s_atten_mix = 14 | 0x40;
danielashercohen 1:69c37f1ab7df 108 int s_atten_sub = 15 | 0x40;
danielashercohen 1:69c37f1ab7df 109 int s_spectrum = 16 | 0x40;
danielashercohen 1:69c37f1ab7df 110 int s_test = 17 | 0x40;
danielashercohen 1:69c37f1ab7df 111
danielashercohen 1:69c37f1ab7df 112 //////////////////////////////////////////////////////////////////
danielashercohen 1:69c37f1ab7df 113 // Calculate actual register values from the variables that the //
danielashercohen 1:69c37f1ab7df 114 // buttons control //
danielashercohen 1:69c37f1ab7df 115 //////////////////////////////////////////////////////////////////
danielashercohen 1:69c37f1ab7df 116
danielashercohen 1:69c37f1ab7df 117
danielashercohen 1:69c37f1ab7df 118
danielashercohen 1:69c37f1ab7df 119 /*
danielashercohen 1:69c37f1ab7df 120 // set the tone controls //
danielashercohen 1:69c37f1ab7df 121 // Expect treble to have the values -5 to +5 //
danielashercohen 1:69c37f1ab7df 122 // Expect trebleCenterFreq to be 0 to 3 //
danielashercohen 1:69c37f1ab7df 123 // Expect referenceInE to be 0 or 1 //
danielashercohen 1:69c37f1ab7df 124 // we define treble as -5 to +5 the TDA7419 register value is more complex
danielashercohen 1:69c37f1ab7df 125 if (treble > 0) {
danielashercohen 1:69c37f1ab7df 126 regTreble = 16 + (treble * 3);
danielashercohen 1:69c37f1ab7df 127 } else if (treble == 0) {
danielashercohen 1:69c37f1ab7df 128 regTreble = 0;
danielashercohen 1:69c37f1ab7df 129 } else if (treble < 0) {
danielashercohen 1:69c37f1ab7df 130 regTreble = 0 - (treble * 3);
danielashercohen 1:69c37f1ab7df 131 }
danielashercohen 1:69c37f1ab7df 132
danielashercohen 1:69c37f1ab7df 133 if (middle > 0) {
danielashercohen 1:69c37f1ab7df 134 regMiddle = 16 + (middle * 3);
danielashercohen 1:69c37f1ab7df 135 } else if (middle == 0) {
danielashercohen 1:69c37f1ab7df 136 regMiddle = 0;
danielashercohen 1:69c37f1ab7df 137 } else if (middle < 0) {
danielashercohen 1:69c37f1ab7df 138 regMiddle = 0 - (middle * 3);
danielashercohen 1:69c37f1ab7df 139 }
danielashercohen 1:69c37f1ab7df 140
danielashercohen 1:69c37f1ab7df 141 if (bass > 0) {
danielashercohen 1:69c37f1ab7df 142 regBass = 16 + (bass * 3);
danielashercohen 1:69c37f1ab7df 143 } else if (bass == 0) {
danielashercohen 1:69c37f1ab7df 144 regBass = 0;
danielashercohen 1:69c37f1ab7df 145 } else if (bass < 0) {
danielashercohen 1:69c37f1ab7df 146 regBass = 0 - (bass * 3);
danielashercohen 1:69c37f1ab7df 147 }
danielashercohen 1:69c37f1ab7df 148 */
danielashercohen 1:69c37f1ab7df 149 //////////////////////////
danielashercohen 1:69c37f1ab7df 150 // update the registers //
danielashercohen 1:69c37f1ab7df 151 //////////////////////////
danielashercohen 1:69c37f1ab7df 152 writeToTDA7419(s_main_source, ( (0x78) | (_input & 0x3) ) );
danielashercohen 1:69c37f1ab7df 153 writeToTDA7419(s_main_loud, (0xc0));
danielashercohen 1:69c37f1ab7df 154 writeToTDA7419(s_softmute, (0xa7));
danielashercohen 1:69c37f1ab7df 155 writeToTDA7419(s_volume, calcAttenuationReg(_volume));
danielashercohen 1:69c37f1ab7df 156
danielashercohen 1:69c37f1ab7df 157 // tone register attenuation isn't simple so moving that
danielashercohen 1:69c37f1ab7df 158 // calculation to a separate function
danielashercohen 1:69c37f1ab7df 159 // locking softwtep as '0' because that is on and I think we
danielashercohen 1:69c37f1ab7df 160 // want soft step!
danielashercohen 1:69c37f1ab7df 161 writeToTDA7419(s_treble,
danielashercohen 1:69c37f1ab7df 162 ( (0 & 0x1 ) << 7 ) |
danielashercohen 1:69c37f1ab7df 163 ( (1 & 0x3 ) << 5 ) |
danielashercohen 1:69c37f1ab7df 164 ( (calcToneAttenuationReg(_treble) & 0x1f ) ) );
danielashercohen 1:69c37f1ab7df 165
danielashercohen 1:69c37f1ab7df 166 writeToTDA7419(s_middle,
danielashercohen 1:69c37f1ab7df 167 ( (0 & 0x1 ) << 7 ) |
danielashercohen 1:69c37f1ab7df 168 ( (1 & 0x3 ) << 5 ) |
danielashercohen 1:69c37f1ab7df 169 ( (calcToneAttenuationReg(_middle) & 0x1f ) ) );
danielashercohen 1:69c37f1ab7df 170
danielashercohen 1:69c37f1ab7df 171 writeToTDA7419(s_bass,
danielashercohen 1:69c37f1ab7df 172 ( (0 & 0x1 ) << 7 ) |
danielashercohen 1:69c37f1ab7df 173 ( (1 & 0x3 ) << 5 ) |
danielashercohen 1:69c37f1ab7df 174 ( (calcToneAttenuationReg(_bass) & 0x1f ) ) );
danielashercohen 1:69c37f1ab7df 175
danielashercohen 1:69c37f1ab7df 176 // this register allows the second source to be routed to the rear speakers
danielashercohen 1:69c37f1ab7df 177 // not useful in the context of this project
danielashercohen 1:69c37f1ab7df 178 writeToTDA7419(s_second_source, (0x07));
danielashercohen 1:69c37f1ab7df 179
danielashercohen 1:69c37f1ab7df 180 // this is the subwoofer cut-off frequency
danielashercohen 1:69c37f1ab7df 181 // 11 which is 160Khz)
danielashercohen 1:69c37f1ab7df 182 writeToTDA7419(s_sub_mid_bass, (0x63));
danielashercohen 1:69c37f1ab7df 183
danielashercohen 1:69c37f1ab7df 184 // mix to the front speakers, enable the sub, no gain
danielashercohen 1:69c37f1ab7df 185 if (_mix == 1) {
danielashercohen 1:69c37f1ab7df 186 writeToTDA7419(s_mix_gain, (0xf7));
danielashercohen 1:69c37f1ab7df 187 } else {
danielashercohen 1:69c37f1ab7df 188 writeToTDA7419(s_mix_gain, (0xf0));
danielashercohen 1:69c37f1ab7df 189 }
danielashercohen 1:69c37f1ab7df 190
danielashercohen 1:69c37f1ab7df 191 s_atten_lf = calcAttenuationReg(_atten_lf );
danielashercohen 1:69c37f1ab7df 192 s_atten_rf = calcAttenuationReg(_atten_rf );
danielashercohen 1:69c37f1ab7df 193 s_atten_lr = calcAttenuationReg(_atten_lr );
danielashercohen 1:69c37f1ab7df 194 s_atten_rr = calcAttenuationReg(_atten_rr );
danielashercohen 1:69c37f1ab7df 195
danielashercohen 1:69c37f1ab7df 196 s_atten_mix = calcAttenuationReg(_atten_mix );
danielashercohen 1:69c37f1ab7df 197 s_atten_sub = calcAttenuationReg(_atten_sub );
danielashercohen 1:69c37f1ab7df 198
danielashercohen 1:69c37f1ab7df 199 writeToTDA7419 (s_spectrum, (0x09));
danielashercohen 1:69c37f1ab7df 200
danielashercohen 1:69c37f1ab7df 201 }
danielashercohen 1:69c37f1ab7df 202
danielashercohen 1:69c37f1ab7df 203 /* setVolume: This sets the volume within the valid range of 0->11
danielashercohen 1:69c37f1ab7df 204 return indicates it was successfully set */
danielashercohen 1:69c37f1ab7df 205 void PreampTDA7419::setVolume(int volume) {
danielashercohen 1:69c37f1ab7df 206 if (volume > 11) {
danielashercohen 1:69c37f1ab7df 207 _volume = 11;
danielashercohen 1:69c37f1ab7df 208 }
danielashercohen 1:69c37f1ab7df 209 else if (volume < 0) {
danielashercohen 1:69c37f1ab7df 210 volume = 0;
danielashercohen 1:69c37f1ab7df 211 }
danielashercohen 1:69c37f1ab7df 212 else {
danielashercohen 1:69c37f1ab7df 213 _volume = volume;
danielashercohen 1:69c37f1ab7df 214 }
danielashercohen 1:69c37f1ab7df 215 updateTDA7419Reg();
danielashercohen 1:69c37f1ab7df 216 }
danielashercohen 1:69c37f1ab7df 217
danielashercohen 1:69c37f1ab7df 218 /* readVolume: return the volume level that is currently set */
danielashercohen 1:69c37f1ab7df 219 int PreampTDA7419::readVolume() {
danielashercohen 1:69c37f1ab7df 220 return (_volume);
danielashercohen 1:69c37f1ab7df 221 }
danielashercohen 1:69c37f1ab7df 222 /* readVolume: return the volume level that is currently set */
danielashercohen 1:69c37f1ab7df 223 int PreampTDA7419::increaseVolume() {
danielashercohen 1:69c37f1ab7df 224 _volume++;
danielashercohen 1:69c37f1ab7df 225 setVolume(_volume);
danielashercohen 1:69c37f1ab7df 226 return (_volume);
danielashercohen 1:69c37f1ab7df 227 }
danielashercohen 1:69c37f1ab7df 228 /* readVolume: return the volume level that is currently set */
danielashercohen 1:69c37f1ab7df 229 int PreampTDA7419::decreaseVolume() {
danielashercohen 1:69c37f1ab7df 230 _volume--;
danielashercohen 1:69c37f1ab7df 231 setVolume(_volume);
danielashercohen 1:69c37f1ab7df 232 return (_volume);
danielashercohen 1:69c37f1ab7df 233 }
danielashercohen 1:69c37f1ab7df 234
danielashercohen 1:69c37f1ab7df 235 void PreampTDA7419::setInput(int input) {
danielashercohen 1:69c37f1ab7df 236 if (input > 3) {
danielashercohen 1:69c37f1ab7df 237 _input = 3;
danielashercohen 1:69c37f1ab7df 238 }
danielashercohen 1:69c37f1ab7df 239 else if (input < 0) {
danielashercohen 1:69c37f1ab7df 240 input = 0;
danielashercohen 1:69c37f1ab7df 241 }
danielashercohen 1:69c37f1ab7df 242 else {
danielashercohen 1:69c37f1ab7df 243 _input = input;
danielashercohen 1:69c37f1ab7df 244 }
danielashercohen 1:69c37f1ab7df 245 updateTDA7419Reg();
danielashercohen 1:69c37f1ab7df 246 }
danielashercohen 1:69c37f1ab7df 247
danielashercohen 1:69c37f1ab7df 248 int PreampTDA7419::readInput() {
danielashercohen 1:69c37f1ab7df 249 return (_input);
danielashercohen 1:69c37f1ab7df 250 }
danielashercohen 1:69c37f1ab7df 251
danielashercohen 1:69c37f1ab7df 252 int PreampTDA7419::increaseTreble() {
danielashercohen 1:69c37f1ab7df 253 if (_treble < 5) {
danielashercohen 1:69c37f1ab7df 254 _treble++;
danielashercohen 1:69c37f1ab7df 255 }
danielashercohen 1:69c37f1ab7df 256 updateTDA7419Reg();
danielashercohen 1:69c37f1ab7df 257 return(_treble);
danielashercohen 1:69c37f1ab7df 258 }
danielashercohen 1:69c37f1ab7df 259
danielashercohen 1:69c37f1ab7df 260 int PreampTDA7419::decreaseTreble() {
danielashercohen 1:69c37f1ab7df 261 if (_treble > -5) {
danielashercohen 1:69c37f1ab7df 262 _treble--;
danielashercohen 1:69c37f1ab7df 263 }
danielashercohen 1:69c37f1ab7df 264 updateTDA7419Reg();
danielashercohen 1:69c37f1ab7df 265 return(_treble);
danielashercohen 1:69c37f1ab7df 266 }
danielashercohen 1:69c37f1ab7df 267
danielashercohen 1:69c37f1ab7df 268 int PreampTDA7419::readTreble() {
danielashercohen 1:69c37f1ab7df 269 return (_treble);
danielashercohen 1:69c37f1ab7df 270 }
danielashercohen 1:69c37f1ab7df 271
danielashercohen 1:69c37f1ab7df 272