Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
TDC7200.cpp
00001 //TDC7200.cpp 00002 //Functions for the TDC7200 chip class 00003 //Creaetd on 11 December 2015 00004 //Eric Lindholm 00005 00006 #include "mbed.h" 00007 #include "TI_Registers.h" 00008 #include "TDC7200.h" 00009 00010 //The constructor start up the interrupt and the enable pin. 00011 TDC7200::TDC7200(PinName EN, PinName INTRPT, PinName MISO, PinName MOSI, PinName SCLK, PinName SPICS) : 00012 en(EN), int_7200(INTRPT), tdc7200_registers(MOSI, MISO, SCLK, SPICS) { 00013 00014 //The enable pin has to see a positive edge after the device is powered up. 00015 set_EN(false); 00016 wait_us(5); 00017 set_EN(true); 00018 00019 //Attach the interrupt handler 00020 int_7200.rise(this, &TDC7200::interruptReceived); 00021 00022 //Set default values 00023 setClockFrequencyIn(8000000); 00024 currentmode = 0; //Mode 1 00025 parityBit = false; 00026 calibrationPeriods = 10; 00027 averageCycles = 1; 00028 numberStops = 1; 00029 for(int q = 0; q < 6; q++) 00030 timeOfFlight[q] = 0; 00031 calibrationCount = 0; 00032 normLSB = 0; 00033 } 00034 00035 //Read a toggle setting 00036 bool TDC7200::readToggleSetting(SettingChoice7200 choice) { 00037 int registerInt = tdc7200_registers.registerRead8(settingAddress7200[choice]); 00038 bool stateOfSetting = (bool)(stateOfSetting & settingLookUp7200[choice]); 00039 return stateOfSetting; 00040 } 00041 //Toggle a setting 00042 void TDC7200::setToggle(SettingChoice7200 choice) { 00043 int registerInt = tdc7200_registers.registerRead8(settingAddress7200[choice]); 00044 int newRegisterSetting = (registerInt ^ settingLookUp7200[choice]); 00045 tdc7200_registers.registerWrite(settingAddress7200[choice], newRegisterSetting); 00046 } 00047 00048 //MODE_SELECT 00049 int TDC7200::readMODE_SELECT() { 00050 //the mode is the 2 thru 1 bits of the CONFIG1 register 00051 int registerInt = tdc7200_registers.registerRead8(0x00); 00052 currentmode = (registerInt & 0x06)>>1; 00053 return currentmode; //I feel like I should add one to this number. 00054 } 00055 void TDC7200::setMODE_SELECT(int newMode) { 00056 int registerInt = tdc7200_registers.registerRead8(0x00); 00057 int registerPut; 00058 if((newMode < 2) && ( newMode >= 0)) { 00059 registerPut = (registerInt & 0xF9) + (newMode<<1); 00060 currentmode = newMode; 00061 } 00062 else { 00063 registerPut = (registerInt & 0xF9); 00064 currentmode = 0; 00065 } 00066 tdc7200_registers.registerWrite(0x00, registerPut); 00067 } 00068 00069 //CALIBRATION2_PERIODS 00070 int TDC7200::readCALIBRATION2_PERIODS() { 00071 //the calibration2 periods is the 7 thru 6 bits of the CONFIG2 register 00072 int registerInt = tdc7200_registers.registerRead8(0x01); 00073 int calib2_periods = (registerInt & 0xC0)>>6; 00074 if(calib2_periods == 0) 00075 calibrationPeriods = 2; 00076 else if(calib2_periods == 1) 00077 calibrationPeriods = 10; 00078 else if(calib2_periods == 2) 00079 calibrationPeriods = 20; 00080 else if(calib2_periods == 3) 00081 calibrationPeriods = 40; 00082 else 00083 calibrationPeriods = 10; 00084 return calib2_periods; 00085 } 00086 void TDC7200::setCALIBRATION2_PERIODS(int newCalibPeriods) { 00087 int registerInt = tdc7200_registers.registerRead8(0x01); 00088 int registerPut; 00089 if((newCalibPeriods < 4) && ( newCalibPeriods >= 0)) { 00090 registerPut = (registerInt & 0x3F) + (newCalibPeriods<<6); 00091 if(newCalibPeriods == 0) 00092 calibrationPeriods = 2; 00093 else if(newCalibPeriods == 1) 00094 calibrationPeriods = 10; 00095 else if(newCalibPeriods == 2) 00096 calibrationPeriods = 20; 00097 else if(newCalibPeriods == 3); 00098 calibrationPeriods = 40; 00099 } 00100 else { 00101 registerPut = (registerInt & 0x3F); 00102 calibrationPeriods = 10; 00103 } 00104 tdc7200_registers.registerWrite(0x01, registerPut); 00105 } 00106 00107 //AVG_CYCLES 00108 int TDC7200::readAVG_CYCLES() { 00109 //the average cycles is the 5 thru 3 bits of the CONFIG2 register 00110 int registerInt = tdc7200_registers.registerRead8(0x01); 00111 int numcycles = (registerInt & 0x38)>>3; 00112 averageCycles = 1<<(numcycles); 00113 return numcycles; 00114 } 00115 void TDC7200::setAVG_CYCLES(int newCycles) { 00116 int registerInt = tdc7200_registers.registerRead8(0x01); 00117 int registerPut; 00118 if((newCycles < 8) && ( newCycles >= 0)) { 00119 registerPut = (registerInt & 0xC7) + (newCycles<<3); 00120 averageCycles = 1<<(newCycles); 00121 } 00122 else { 00123 registerPut = (registerInt & 0xC7); 00124 averageCycles = 1; 00125 } 00126 tdc7200_registers.registerWrite(0x01, registerPut); 00127 } 00128 00129 //NUM_STOP 00130 int TDC7200::readNUM_STOP() { 00131 //the number of stops is the 2 thru 0 bits of the CONFIG2 register 00132 int registerInt = tdc7200_registers.registerRead8(0x01); 00133 int numstops = (registerInt & 0x07); 00134 numberStops = numstops + 1; 00135 return numstops; 00136 } 00137 void TDC7200::setNUM_STOP(int newStops) { 00138 int registerInt = tdc7200_registers.registerRead8(0x01); 00139 int registerPut; 00140 if((newStops < 8) && ( newStops >= 0)) { 00141 registerPut = (registerInt & 0xF8) + (newStops); 00142 numberStops = newStops + 1; 00143 } 00144 else { 00145 registerPut = (registerInt & 0xF8) ; 00146 numberStops = 1; 00147 } 00148 tdc7200_registers.registerWrite(0x01, registerPut); 00149 } 00150 00151 //Enable Pin 00152 bool TDC7200::read_EN() { 00153 return en; 00154 } 00155 void TDC7200::set_EN(bool onOff) { 00156 if(onOff) 00157 en = 1; 00158 else 00159 en = 0; 00160 } 00161 00162 //Called when the interrupt happens 00163 void TDC7200::interruptReceived() { 00164 //First, check what interrupt event happened. 00165 int eventInt = tdc7200_registers.registerRead8(0x02); 00166 //bit 0: a new measurement has been completed. 00167 //bit 1: a coarse counter overflow happened. 00168 //bit 2: a clock counter overflow happened. 00169 //bit 3: a measurement was started (START pin triggered) 00170 //bit 4: a measurement has been completed. 00171 //Bits 5, 6, and 7 do not have anything. 00172 int clearBits = 0; 00173 if(eventInt & 0x01) { 00174 //New measurement completed 00175 forceMeasurementRead(); 00176 clearBits += 1; 00177 } 00178 if(eventInt & 0x02) { 00179 //coarse counter overflow 00180 clearBits += 2; 00181 } 00182 if(eventInt & 0x04) { 00183 //clock counter overflow 00184 clearBits += 4; 00185 } 00186 if(eventInt & 0x08) { 00187 //measurement started 00188 clearBits += 8; 00189 } 00190 if(eventInt & 0x10) { 00191 //Measurement completed 00192 forceMeasurementRead(); 00193 clearBits += 16; 00194 } 00195 //clear the interrupted flags 00196 tdc7200_registers.registerWrite(0x02, clearBits); 00197 } 00198 00199 //Clock frequency 00200 int TDC7200::readClockFrequencyIn() { 00201 return fclkin; 00202 } 00203 void TDC7200::setClockFrequencyIn(int newFreq) { 00204 if(newFreq > 0) 00205 fclkin = newFreq; 00206 else 00207 fclkin = 8000000; 00208 } 00209 00210 //other 00211 int TDC7200::readCalibrationPeriods() { 00212 return calibrationPeriods; 00213 } 00214 int TDC7200::readAverageCycles() { 00215 return averageCycles; 00216 } 00217 int TDC7200::readNumberStops() { 00218 return numberStops; 00219 } 00220 double TDC7200::readNormLSB() { 00221 return normLSB; 00222 } 00223 double TDC7200::readCalibrationCount() { 00224 return calibrationCount; 00225 } 00226 //The Time of flight data 00227 double TDC7200::readTimeOfFlight(int index = 0) { 00228 if((index < 5) && (index >= 0)) 00229 return timeOfFlight[index]; 00230 else 00231 return timeOfFlight[0]; 00232 } 00233 00234 //Different kinds of functions 00235 //Read all the measurement registers and do calculations 00236 void TDC7200::forceMeasurementRead() { 00237 //Read in calibration values 00238 int calib1 = tdc7200_registers.registerRead24(0x1B); 00239 int calib2 = tdc7200_registers.registerRead24(0x1C); 00240 //Read in time1 through time6 as well as the clock counts. 00241 int timeregister[numberStops + 1]; 00242 int clockcount[numberStops]; 00243 int regaddress = 0x10; 00244 for(int j = 0; j < numberStops; j++) { 00245 timeregister[j] = tdc7200_registers.registerRead24(regaddress); 00246 clockcount[j] = tdc7200_registers.registerRead24(regaddress + 1); 00247 regaddress += 2; 00248 } 00249 timeregister[numberStops] = tdc7200_registers.registerRead24(0x10 + 2 * numberStops); 00250 //If the parity bit is enabled... 00251 if(parityBit) { 00252 int timeparity[numberStops + 1]; 00253 int clockparity[numberStops]; 00254 bool paritybits[2 * numberStops + 3]; 00255 //Go through each measurement and check the parity (even parity). 00256 //Then re-request every measurement that didn't match parity. 00257 int parityselection = 0x800000; 00258 int calib1parity = calib1 & 0x7FFFFF; 00259 paritybits[2 * numberStops + 1] = false; 00260 while(calib1parity) { 00261 paritybits[2 * numberStops + 1] = !paritybits[2 * numberStops + 1]; 00262 calib1parity = calib1parity & (calib1parity - 1); 00263 } 00264 if(((calib1 & parityselection)>>23) != paritybits[2 * numberStops + 1]) 00265 calib1 = tdc7200_registers.registerRead24(0x1B); 00266 00267 int calib2parity = calib2 & 0x7FFFFF; 00268 paritybits[2 * numberStops + 2] = false; 00269 while(calib2parity) { 00270 paritybits[2 * numberStops + 2] = !paritybits[2 * numberStops + 2]; 00271 calib2parity = calib2parity & (calib2parity - 1); 00272 } 00273 if(((calib1 & parityselection)>>23) != paritybits[2 * numberStops + 1]) 00274 calib1 = tdc7200_registers.registerRead24(0x1B); 00275 00276 timeparity[0] = timeregister[0] & 0x7FFFFF; 00277 paritybits[0] = false; 00278 while(timeparity[0]) { 00279 paritybits[0] = !paritybits[0]; 00280 timeparity[0] = timeparity[0] & (timeparity[0] - 1); 00281 } 00282 if(((timeregister[0] & parityselection)>>23) != paritybits[0]) 00283 timeregister[0] = tdc7200_registers.registerRead24(0x10); 00284 00285 for(int k = 0; k < numberStops; k++) { 00286 timeparity[k+1] = timeregister[k+1] & 0x7FFFFF; 00287 paritybits[k+1] = false; 00288 while(timeparity[k+1]) { 00289 paritybits[k+1] = !paritybits[k+1]; 00290 timeparity[k+1] = timeparity[k+1] & (timeparity[k+1] - 1); 00291 } 00292 if(((timeregister[k+1] & parityselection)>>23) != paritybits[k+1]) 00293 timeregister[k+1] = tdc7200_registers.registerRead24(0x10 + 2 * (k + 1)); 00294 00295 clockparity[k] = clockcount[k] & 0x7FFFFF; 00296 paritybits[numberStops + k] = false; 00297 while(clockparity[k]) { 00298 paritybits[numberStops + k] = !paritybits[numberStops + k]; 00299 clockparity[k] = clockparity[k] & (clockparity[k] - 1); 00300 } 00301 if(((clockcount[k] & parityselection)>>23) != paritybits[numberStops + k]) 00302 clockcount[k] = tdc7200_registers.registerRead24(0x11 + 2 * k); 00303 } 00304 } //This was all just for the parity bit calculations 00305 00306 //Calculate the normLSB and CalibrationCount. 00307 calibrationCount = ((double)calib2 - (double)calib1) / ((double)calibrationPeriods - 1); 00308 normLSB = 1/ ((double)fclkin * calibrationCount); 00309 //Mode 1 00310 if(currentmode == 0) { 00311 for(int f = 0; f < numberStops; f++) 00312 timeOfFlight[f] = (double)(timeregister[f]) * normLSB; 00313 } 00314 //Mode 2 00315 else if(currentmode == 1) { 00316 for(int n = 0; n < numberStops; n++) 00317 timeOfFlight[n] = ((double)timeregister[n] * normLSB) + (double)clockcount[n] / ((double)fclkin) - ((double)timeregister[n+1] * normLSB); 00318 } 00319 00320 } 00321 //Begin a new measurement 00322 void TDC7200::startMeasurement() { 00323 //read current state 00324 int registerInt = tdc7200_registers.registerRead8(0x00); 00325 //Add the 'start measurement' bit 00326 registerInt = (registerInt & 0xFE) + 1; 00327 //put that back into the chip 00328 tdc7200_registers.registerWrite(0x00, registerInt); 00329 00330 }
Generated on Tue Jul 12 2022 23:44:08 by
1.7.2