Mercurial > hg > beaglert
comparison core/I2c_Codec.cpp @ 134:09a32ca96255
Fixed and improved I2c_codec. Copied from the scope-refactoring branch
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Thu, 27 Aug 2015 03:39:11 +0100 |
parents | c44fa102d02b |
children | d064234468cd |
comparison
equal
deleted
inserted
replaced
107:836052c86e1e | 134:09a32ca96255 |
---|---|
42 { | 42 { |
43 if(writeRegister(0x02, 0x00)) // Codec sample rate register: fs_ref / 1 | 43 if(writeRegister(0x02, 0x00)) // Codec sample rate register: fs_ref / 1 |
44 return 1; | 44 return 1; |
45 if(writeRegister(0x03, 0x91)) // PLL register A: enable | 45 if(writeRegister(0x03, 0x91)) // PLL register A: enable |
46 return 1; | 46 return 1; |
47 if(writeRegister(0x04, 0x1C)) // PLL register B | 47 // if(writeRegister(0x04, 0x1C)) // PLL register B |
48 return 1; | 48 // return 1; |
49 if(writeRegister(0x05, 0x52)) // PLL register C | 49 // if(writeRegister(0x05, 0x52)) // PLL register C |
50 return 1; | 50 // return 1; |
51 if(writeRegister(0x06, 0x40)) // PLL register D | 51 // if(writeRegister(0x06, 0x40)) // PLL register D |
52 // return 1; | |
53 if(setPllD(5264)) //7.5264 gives 44.1kHz nominal value with a 12MHz master clock | |
54 return 1; | |
55 if(setPllJ(7)) | |
52 return 1; | 56 return 1; |
53 if(dual_rate) { | 57 if(dual_rate) { |
54 if(writeRegister(0x07, 0xEA)) // Codec datapath register: 44.1kHz; dual rate; standard datapath | 58 if(writeRegister(0x07, 0xEA)) // Codec datapath register: 44.1kHz; dual rate; standard datapath |
55 return 1; | 59 return 1; |
56 } | 60 } |
118 } | 122 } |
119 | 123 |
120 //set the numerator multiplier for the PLL | 124 //set the numerator multiplier for the PLL |
121 int I2c_Codec::setPllK(float k){ | 125 int I2c_Codec::setPllK(float k){ |
122 short unsigned int j=(int)k; | 126 short unsigned int j=(int)k; |
123 unsigned int d=(k-j+0.5)*10000; //fractionary part, between 0 and 9999 | 127 unsigned int d=(int)(0.5+(k-j)*10000); //fractional part, between 0 and 9999 |
124 if(setPllJ(j)>0) | 128 if(setPllJ(j)>0) |
125 return 1; | 129 return 1; |
126 if(setPllD(d)>0) | 130 if(setPllD(d)>0) |
127 return 2; | 131 return 2; |
128 return 0; | 132 return 0; |
136 } | 140 } |
137 if(writeRegister(0x04, j<<2)){ // PLL register B: j<<2 | 141 if(writeRegister(0x04, j<<2)){ // PLL register B: j<<2 |
138 printf("I2C error while writing PLL j: %d", j); | 142 printf("I2C error while writing PLL j: %d", j); |
139 return 1; | 143 return 1; |
140 } | 144 } |
145 pllJ=j; | |
141 return 0; | 146 return 0; |
142 } | 147 } |
143 | 148 |
144 //set fractional part(between 0 and 9999) of the numerator mutliplier of the PLL | 149 //set fractional part(between 0 and 9999) of the numerator mutliplier of the PLL |
145 int I2c_Codec::setPllD(unsigned int d){ | 150 int I2c_Codec::setPllD(unsigned int d){ |
151 } | 156 } |
152 if(writeRegister(0x06, (d<<2)&255)){ // PLL register D: D=5264, part 2 | 157 if(writeRegister(0x06, (d<<2)&255)){ // PLL register D: D=5264, part 2 |
153 printf("I2C error while writing PLL d part 2 : %d", d); | 158 printf("I2C error while writing PLL d part 2 : %d", d); |
154 return 1; | 159 return 1; |
155 } | 160 } |
156 return 0; | 161 pllD=d; |
162 return 0; | |
163 } | |
164 | |
165 int I2c_Codec::setAudioSamplingRate(float newSamplingRate){ | |
166 int pllP=1; //TODO: create get/set for pllP and pllR | |
167 int pllR=1; | |
168 long int PLLCLK_IN=12000000; | |
169 // f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P) | |
170 float k = ((double)(newSamplingRate * pllP * 2048.0f/(float)pllR)) / PLLCLK_IN ; | |
171 return (setPllK(k)); | |
172 } | |
173 | |
174 short unsigned int I2c_Codec::getPllJ(){ | |
175 return pllJ; | |
176 } | |
177 unsigned int I2c_Codec::getPllD(){ | |
178 return pllD; | |
179 } | |
180 float I2c_Codec::getPllK(){ | |
181 float j=getPllJ(); | |
182 float d=getPllD(); | |
183 float k=j+d/10000.0f; | |
184 return k; | |
185 } | |
186 | |
187 float I2c_Codec::getAudioSamplingRate(){ | |
188 int pllP=1; //TODO: create get/set for pllP and pllR | |
189 int pllR=1; | |
190 long int PLLCLK_IN=12000000; | |
191 // f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P) | |
192 float fs = (PLLCLK_IN/2048.0f) * getPllK()*pllR/(float)pllP; | |
193 return fs; | |
157 } | 194 } |
158 // Set the volume of the DAC output | 195 // Set the volume of the DAC output |
159 int I2c_Codec::setDACVolume(int halfDbSteps) | 196 int I2c_Codec::setDACVolume(int halfDbSteps) |
160 { | 197 { |
161 dacVolumeHalfDbs = halfDbSteps; | 198 dacVolumeHalfDbs = halfDbSteps; |