CJ Shaw / HY3116
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HY3116.cpp Source File

HY3116.cpp

00001 #include "HY3116.h"
00002 
00003 /**
00004  * Default Constructor.
00005  * Sets default I2C pins with default frequency
00006  */
00007 HY3116::HY3116() : 
00008     i2c(p22, p21)
00009 {
00010     
00011     // Set the I2C clock frequency
00012     i2c.frequency(250000);
00013 } // End constructor
00014 
00015 /**
00016  * Constructor #2
00017  * Sets I2C pins with default frequency
00018  */
00019 HY3116::HY3116(PinName sda,
00020                PinName scl) : 
00021     i2c(sda, scl)
00022 {
00023     
00024     // Set the I2C clock frequency
00025     i2c.frequency(250000);
00026 } // End constructor
00027 
00028 /**
00029  * Constructor #3
00030  * Sets I2C pins and non-default frequency
00031  */
00032 HY3116::HY3116(PinName sda,
00033                PinName scl,
00034                int freq   ) : 
00035     i2c(sda, scl)
00036 {
00037     
00038     // Set the I2C clock frequency
00039     i2c.frequency(freq);
00040 } // End constructor
00041 
00042 /**
00043  * Destructor.
00044  */
00045 HY3116::~HY3116()
00046 {
00047 
00048 }
00049 
00050 /**
00051  * Helper function to write correct data to HY3116 registers
00052  */
00053 int HY3116::writeRegister(uint8_t regAddress, uint8_t writeData)
00054 {
00055     int retval = 1;
00056     char writeBuffer[2];
00057     writeBuffer[0]=regAddress;
00058     writeBuffer[1]=writeData;
00059     retval = i2c.write(HY3116_ADDRESS,writeBuffer,sizeof(writeBuffer),0);
00060     if (retval != 0) {
00061         return retval;
00062     }
00063     return 0;
00064 }
00065 
00066 /**
00067  * Helper function to write correct data to HY3116 registers
00068  */
00069 int HY3116::readRegister(uint8_t regAddress, uint8_t byteNum, uint8_t* dest)
00070 {
00071     int retval = 1;
00072     if (byteNum > sizeof(dest)) {
00073         return retval;
00074     }
00075     char writeBuffer[1];
00076     writeBuffer[0] = regAddress;
00077     char readBuffer[byteNum];
00078     retval = i2c.write(HY3116_ADDRESS,writeBuffer,sizeof(writeBuffer),1);
00079     if (retval != 0) {
00080         return retval;
00081     }
00082     retval = i2c.read(HY3116_ADDRESS,readBuffer,sizeof(readBuffer),0);
00083     if (retval != 0) {
00084         return retval;
00085     }
00086     for(int i=0; i<byteNum; i++) {
00087         dest[i]=readBuffer[i];
00088     }
00089     return 0;
00090 }
00091 
00092 // Function to send the reset command
00093 int HY3116::resetChip()
00094 {
00095     int retval = 0;
00096     char writeBuffer[1];
00097     writeBuffer[0] = RESET;
00098     retval = i2c.write(RESET_ADDRESS,writeBuffer,sizeof(writeBuffer),0);
00099     if (retval != 0) {
00100         return 1;
00101     }
00102     return  0;
00103 }
00104 
00105 // Dedicated ADC-output read, check & format function
00106 uint8_t HY3116::readAdc(int32_t *_adcReading)
00107 {
00108     // Initialise function variables
00109     uint8_t rawData[3];
00110     bool newReading = 0;
00111     int32_t adcReading = 0;
00112     uint8_t adc_error = 0;
00113     uint8_t returnError = 0;
00114     
00115     // Read in the raw ADO bytes
00116     adc_error = readRegister(ADO, 3, &rawData[0]);
00117     if (adc_error != 0) {
00118             returnError = 2;
00119             return returnError;
00120     }
00121     
00122     // Test if there is new data (polling mode)
00123     if (rawData[2] & 0b00000001) {
00124         
00125         // Set the newReading flag
00126         returnError = 0;
00127         
00128         // Shift the raw bytes into the 32-bit variable
00129         adcReading += rawData[0] << 15;
00130         adcReading += rawData[1] << 7;
00131         adcReading += rawData[2] >> 1;
00132         
00133         // Account for twos complement polarity
00134         if (rawData[0] & 0b10000000) {
00135             adcReading ^= 0xFF800000;
00136         }
00137     }
00138     else {
00139         
00140         // Set the newReading flag
00141         returnError = 1;
00142     }
00143     
00144     *_adcReading = adcReading;
00145     
00146     return returnError;
00147 }
00148 
00149 // Initialise the HY3116 with the following config.:
00150 //
00151 bool HY3116::init()
00152 {
00153     int adc_error = 0;
00154     
00155     // Reset the chip
00156     adc_error = resetChip();
00157     if (adc_error != 0) {
00158             return 0;
00159     }
00160     wait_ms(1);
00161     
00162     // Set-up the SYS register
00163     adc_error = writeRegister(SYS, 0b00011100); // Enable the ADC & LDO
00164     if (adc_error != 0) {
00165             return 0;
00166     }
00167     wait_ms(1); // wait 100 ms to stabilize 
00168         
00169     // Set-up the ADC1 register
00170     adc_error = writeRegister(ADC1, 0b00001000); // Set inputs to AIN1 & AIN2
00171     if (adc_error != 0) {
00172             return 0;
00173     }
00174     wait_ms(1); // wait 100 ms to stabilize
00175     
00176     // Set-up the ADC2 register
00177     adc_error = writeRegister(ADC2, 0b01010000); // Set pos. ref. voltage to VDDA, neg. ref. to VSSA, DC offset to 0VRef
00178     if (adc_error != 0) {
00179             return 0;
00180     }
00181     wait_ms(1); // wait 100 ms to stabilize
00182     
00183     // Set-up the ADC3 register
00184     adc_error = writeRegister(ADC3, 0b00111111); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 2x
00185     if (adc_error != 0) {
00186             return 0;
00187     }
00188     wait_ms(1); // wait 100 ms to stabilize
00189     
00190     // Set-up the ADC4 register
00191     // [0:1] = LDO voltage select, 01 = 3.0V
00192     // [2] = REFO voltage, 0 = 1.2V
00193     // [3] = HS conversion rate, 0 = slow (327kHz)
00194     // [4:6] = OSR ADC output rate, 110 = 40SPS (when HS = 0)
00195     // [7] = N/A
00196     adc_error = writeRegister(ADC4, 0b01001100);
00197     if (adc_error != 0) {
00198             return 0;
00199     }
00200     wait_ms(1); // Wait to stabilise
00201     
00202     return 1;
00203 }