Qubit 2020 / presensfirmwareupdate

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers adc.cpp Source File

adc.cpp

00001 #include "cisme.h"
00002 #include "adc.h"
00003 #include "debug.h"
00004 
00005 #define SPI_WRITE_DEBUG_RESPONSE(_value)\
00006 {\
00007     int _spiResponse;\
00008     _spiResponse = AtoD.write(_value);\
00009     DEBUG3("SPI returned response %d on ## _value", _spiResponse);\
00010 }
00011 
00012 static SPI AtoD(p5,p6,p7);
00013 
00014 static void spiWaitPin8(unsigned char maxChecks)
00015 {
00016     int pin8State;
00017     int noPinChecks = 0;
00018     
00019     do {
00020         /* Looks like SD0Flag returns(operator (int)) 0 or 1 - pin state. */
00021         pin8State = SDOFlag;
00022         noPinChecks++;
00023         
00024         if (noPinChecks > maxChecks) {
00025             DEBUG1("All pin checks are failed. Continue working anyway");
00026             pin8State = 0;
00027         }
00028     }
00029     while (pin8State == 1);
00030 }
00031 
00032 void adcInit()
00033 {
00034     int syncIx;
00035     int i;
00036 
00037     AtoD.format(SPI_NO_BITS_PER_FRAME, SPI_CLOCK_PHASE);
00038     AtoD.frequency(SPI_FREQUENCY);
00039     
00040     // Initialize the AtoD to the command mode
00041     for (syncIx = 0; syncIx < SPI_MAX_SYNC_TRYS; syncIx++) {
00042         AtoD.write(0xFF);
00043     }
00044 
00045     SPI_WRITE_DEBUG_RESPONSE(0xFE);
00046     wait(SPI_CONF_DELAY);
00047 
00048     // Reset the configuration register
00049     SPI_WRITE_DEBUG_RESPONSE(0x03);
00050     SPI_WRITE_DEBUG_RESPONSE(0x20);
00051     SPI_WRITE_DEBUG_RESPONSE(0x00);
00052     SPI_WRITE_DEBUG_RESPONSE(0x00);
00053     SPI_WRITE_DEBUG_RESPONSE(0x00);
00054 
00055     wait(SPI_CONF_DELAY);
00056 
00057     // Read the configuration register
00058     AtoD.write(0x0B);
00059     SPI_WRITE_DEBUG_RESPONSE(0x00);
00060     SPI_WRITE_DEBUG_RESPONSE(0x00);
00061     SPI_WRITE_DEBUG_RESPONSE(0x00);
00062     SPI_WRITE_DEBUG_RESPONSE(0x00);
00063     wait(SPI_CONF_DELAY);
00064 
00065     // Enable the device
00066     AtoD.write(0x03);
00067     AtoD.write(0x00);
00068     AtoD.write(0x00);
00069     AtoD.write(0x00);
00070     AtoD.write(0x00);
00071     wait(SPI_CONF_DELAY);
00072     
00073     // Read the configuration register
00074     AtoD.write(0x0B);
00075     SPI_WRITE_DEBUG_RESPONSE(0x00);
00076     SPI_WRITE_DEBUG_RESPONSE(0x00);
00077     SPI_WRITE_DEBUG_RESPONSE(0x00);
00078     SPI_WRITE_DEBUG_RESPONSE(0x00);
00079     wait(SPI_CONF_DELAY);
00080 
00081     // Setup the Channel Setup Registers
00082     AtoD.write(0x05);
00083     AtoD.write(0x00);
00084     AtoD.write(0x00);
00085     AtoD.write(0x40);
00086     AtoD.write(0x00);
00087 
00088     wait(SPI_CONF_DELAY);
00089 
00090     for (i = 0; i < 4; i++) {
00091         if (i < 2) {
00092             AtoD.write(0x80);
00093         } else {
00094             AtoD.write(0x88);
00095         }
00096 
00097         spiWaitPin8(MAX_CONF_PIN_CHECKS);
00098         wait (0.05);
00099 
00100         SPI_WRITE_DEBUG_RESPONSE(0x00);
00101         SPI_WRITE_DEBUG_RESPONSE(0xFE);
00102         SPI_WRITE_DEBUG_RESPONSE(0xFE);
00103         SPI_WRITE_DEBUG_RESPONSE(0xFE);
00104         SPI_WRITE_DEBUG_RESPONSE(0xFE);
00105     }
00106 }
00107 
00108 void adcGetData()
00109 {
00110     int spiResponse;
00111     int Polarity;
00112 
00113     /* Temporary math variables. */
00114     int pHInt;
00115     int pHTInt;
00116     double Rtherm;
00117     double E1SINGLEPT, S1SINGLEPT;
00118 
00119     L1 = 1;
00120 
00121     pH = 0;
00122     pHCorrected = 0;
00123 
00124     NVIC_DisableIRQ(UART2_IRQn);
00125 
00126     AtoD.write(0x80);
00127 
00128     spiWaitPin8(MAX_PIN_CHECKS);
00129     wait(0.1);
00130 
00131     SPI_WRITE_DEBUG_RESPONSE(0x00);
00132 
00133     spiResponse = AtoD.write(0xFE);
00134     Polarity    = spiResponse & 0x80;
00135     pHTInt      = spiResponse & 0x0000007F;
00136 
00137     spiResponse = AtoD.write(0xFE);
00138     pHTInt = (pHTInt << 8) + (spiResponse & 0xFF);
00139 
00140     spiResponse = AtoD.write(0xFE);
00141     pHTInt = (pHTInt << 8) + (spiResponse &  0x000000FF);
00142 
00143     SPI_WRITE_DEBUG_RESPONSE(0xFE);
00144 
00145     if (Polarity == 0x80) {
00146         pHTInt = -((~pHTInt) & 0x007FFFFF) - 1;
00147     }
00148 
00149     DEBUG1("Received: Polarity=0x%x phTInt=0x%x phTInt=%d", Polarity, pHTInt, pHTInt);
00150 
00151     pHT = (double)(pHTInt/8388608. * 3.0);
00152 
00153     Rtherm = 20000.0 / ((2.5 / pHT) - 1.0);
00154     DEBUG1("Calculated Rtherm=%1.1f", Rtherm);
00155 
00156     PHTEMP = A + (B * Rtherm) + (C * log10(Rtherm)) + (D * pow(log10(Rtherm), 3));
00157     DEBUG1("pHT=%1.5f PHTEMP=%1.5f", pHT, PHTEMP);
00158 
00159     L2=1;
00160 
00161     AtoD.write(0x88);
00162 
00163     spiWaitPin8(MAX_PIN_CHECKS);
00164     wait(0.1);
00165 
00166     SPI_WRITE_DEBUG_RESPONSE(0x00);
00167 
00168     spiResponse = AtoD.write(0xFE);
00169     Polarity    = spiResponse & 0x80;
00170     pHInt       = spiResponse & 0x0000007F;
00171 
00172     spiResponse = AtoD.write(0xFE);
00173     pHInt = (pHInt << 8) + (spiResponse & 0x000000FF);
00174 
00175     spiResponse = AtoD.write(0xFE);
00176     pHInt = (pHInt << 8) + (spiResponse &  0x000000FF);
00177 
00178     SPI_WRITE_DEBUG_RESPONSE(0xFE);
00179 
00180     if (Polarity==0x80) {
00181         pHInt = -((~pHInt) & 0x007FFFFF) - 1;
00182     }
00183 
00184     DEBUG1("Received: Polarity=0x%x pHInt=0x%x pHInt=%d", Polarity, pHInt, pHInt);
00185 
00186     pH = (double)(pHInt/8388608. * 3.0);
00187 
00188     pHNoGain = pH;
00189 
00190     E1SINGLEPT = (double)EOSINGLEPT + (double)ADC_dEdT * ((double)PHTEMP-(double)PHTEMPF);
00191     S1SINGLEPT = (double)ADC_R * ((double)PHTEMP + 273.15) / (double)ADC_F * 2.302585;
00192 
00193     DEBUG1("E1SINGLEPT=%f S1SINGLEPT=%f", E1SINGLEPT, S1SINGLEPT);
00194 
00195     pHCorrected = ((double)pHNoGain - (double)E1SINGLEPT) / (double)S1SINGLEPT;
00196 
00197     DEBUG1("pHNoGain=%1.6f pHCorrected=%2.3f", pHNoGain, pHCorrected);
00198 
00199     L1=0;
00200     L2=0;
00201     
00202     NVIC_EnableIRQ(UART1_IRQn);
00203     NVIC_EnableIRQ(UART2_IRQn);
00204 }