Mercurial > hg > beaglert
comparison core/I2c_Codec.cpp @ 149:134bff10e561 ClockSync
Added simple one-variable one-measurement Kalman filter, Pid controller(which output is not used). Virtual clock is now much more precise and reactive for period. Still it is lagging behind a bit on the overall offset.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 21 Sep 2015 03:12:21 +0100 |
parents | e9a2f31dff7b |
children | e9c9404e3d1f |
comparison
equal
deleted
inserted
replaced
148:6cd38e261027 | 149:134bff10e561 |
---|---|
45 return 1; | 45 return 1; |
46 // The sampling frequency is given as f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P) | 46 // The sampling frequency is given as f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P) |
47 // The master clock PLLCLK_IN is 12MHz | 47 // The master clock PLLCLK_IN is 12MHz |
48 // K can be varied in intervals of resolution of 0.0001 up to 63.9999 | 48 // K can be varied in intervals of resolution of 0.0001 up to 63.9999 |
49 // using P=8 and R=1 gives a resolution of 0.0732421875Hz ( 0.000166% at 44.1kHz) | 49 // using P=8 and R=1 gives a resolution of 0.0732421875Hz ( 0.000166% at 44.1kHz) |
50 // for whatever reason, P=8 does not work (i.e.: setting the sampling rate works fine, | |
51 // but when changing K by a small-ish value (0.0040), the clock does not seem to change). | |
52 // to obtain Fs=44100 we need to have K=60.2112 | 50 // to obtain Fs=44100 we need to have K=60.2112 |
53 | 51 |
54 if(setPllP(7)) | 52 if(setPllP(7)) |
55 return 1; | 53 return 1; |
56 if(setPllR(1)) | 54 if(setPllR(1)) |
218 int I2c_Codec::setAudioSamplingRate(float newSamplingRate){ | 216 int I2c_Codec::setAudioSamplingRate(float newSamplingRate){ |
219 long int PLLCLK_IN=12000000; | 217 long int PLLCLK_IN=12000000; |
220 // f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P) | 218 // f_{S(ref)} = (PLLCLK_IN × K × R)/(2048 × P) |
221 float k = ((double)(newSamplingRate * pllP * 2048.0f/(float)pllR)) / PLLCLK_IN ; | 219 float k = ((double)(newSamplingRate * pllP * 2048.0f/(float)pllR)) / PLLCLK_IN ; |
222 int ret = setPllK(k); | 220 int ret = setPllK(k); |
223 // printf("P: %d, R: %d, J: %d, D: %d\n", pllP, pllR, pllJ, pllD); | 221 float actualSamplingRate = PLLCLK_IN * (pllJ + pllD/10000.0) * pllR / (2048.0 * pllP); |
222 printf("P: %d, R: %d, J: %d, D: %d, samplingRate= %f\n", pllP, pllR, pllJ, pllD, actualSamplingRate ); | |
223 // printf("fs_ref=%f;\n",actualSamplingRate); | |
224 return ret; | 224 return ret; |
225 } | 225 } |
226 | 226 |
227 short unsigned int I2c_Codec::getPllJ(){ | 227 short unsigned int I2c_Codec::getPllJ(){ |
228 return pllJ; | 228 return pllJ; |