Test, please delete

main.cpp

Committer:
kevin1990
Date:
2017-09-25
Revision:
5:dbb2b71a59ed
Parent:
3:3796776e2c27
Child:
7:4dbae381f693

File content as of revision 5:dbb2b71a59ed:

/*
 ******************************************************************************
 * file:   main.cpp
 *-----------------------------------------------------------------------------
 *
Copyright (c) 2017 Emutex Ltd. / Analog Devices, Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
  - Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  - Modified versions of the software must be conspicuously marked as such.
  - This software is licensed solely and exclusively for use with processors
    manufactured by or for Analog Devices, Inc.
  - This software may not be combined or merged with other code in any manner
    that would cause the software to become subject to terms and conditions
    which differ from those listed here.
  - Neither the name of Analog Devices, Inc. nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.
  - The use of this software may or may not infringe the patent rights of one
    or more patent holders.  This license does not release you from the
    requirement that you obtain separate licenses from these patent holders
    to use this software.

THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/
/******************************************************************************/
/* Include Files                                                              */
/******************************************************************************/
#include "mbed.h"
#include "inc/adisense1000.h"

/*!
 ******************************************************************************
 * @file:   main.cpp
 * @brief:  Setup the following channels listed below to configure a burst of 5
 *          cycles to occur every 30 seconds.
 *          The time allocated to each individual cycle is 3 seconds
 */

#define BITP_VALID_DATA     (7)
#define SAMPLES_PER_CHANNEL (10)
#define NUM_CHANNELS        (9)
#define CYCLE_TIME (3)
#define NUM_CYCLES_PER_FILL (5)
#define MULTI_CYCLE_FILL_INTERVAL (30)

void setupCJC0(void);    /* RTD 2-wire PT100 */
void setupCJC1(void);    /* RTD 2-wire PT100 */
void setupSENSOR0(void); /* RTD 2-wire PT100 */
void setupSENSOR1(void); /* Thermocouple Type-K (compensation: CJC1) */
void setupSENSOR2(void); /* Thermocouple Type-T (compensation: CJC0) */
void setupSENSOR3(void); /* Thermocouple Type-J (compensation: CJC0) */
void setupVOLTAGE(void); /* Honeywell Pressure (HSCMRNN1.6BAAA3) */
void setupCURRENT(void); /* Honeywell Pressure (PX2CN2XX100PACH) */
void setupI2C0(void);    /* Honeywell Humidity (HIH8000 Series) */

Serial pc(PA_11, PA_12, 115200);

ADI_Channel_Config_t CJC0;
ADI_Channel_Config_t CJC1;
ADI_Channel_Config_t SENSOR0;
ADI_Channel_Config_t SENSOR1;
ADI_Channel_Config_t SENSOR2;
ADI_Channel_Config_t SENSOR3;
ADI_Channel_Config_t VOLTAGE;
ADI_Channel_Config_t CURRENT;
ADI_Channel_Config_t I2C0;

int main() {
    uint8_t     chStatus        = 0;
    uint16_t    deviceID        = 0;
    uint32_t    rawSample       = 0;
    float       processed        = 0;
    bool        validSample     = 0;
    bool        waitUntilReady  = true;
    ADI_CORE_Status_t devStatus;
    ADI_SENSE_RESULT retValue;

    pc.printf("\r\n\r\n\r\n\r\n");

    retValue = ADISense1000_Open();
    pc.printf("Host - Device Comms Opened with return:  %d\r\n", retValue);

    pc.printf("Resetting ADI Sense 1000 device, please wait...\r\n");
    retValue = ADISense1000_Reset(waitUntilReady);
    pc.printf("Reset device with return:  %d\r\n", retValue);

    retValue = ADISense1000_GetID(&deviceID);
    pc.printf("Read device ID 0x%lx with return %d\r\n", deviceID, retValue);

    retValue = ADISense1000_GetStatus(&devStatus);
    pc.printf("Read Status 0x%lx with return %d\r\n", devStatus, retValue);

    ADI_CORE_Mode_t modeReg;
    modeReg.Conversion_Mode = CORE_MODE_MULTICYCLE;
    modeReg.Drdy_Mode = CORE_MODE_DRDY_PER_FIFO_FILL;

    retValue = ADISense1000_ConfigureModule(modeReg);
    pc.printf("Configure Module with return %d\r\n", retValue);

    ADI_CORE_Cycle_Control_t cycleControlReg;
    cycleControlReg.Cycle_Time = CYCLE_TIME;
    cycleControlReg.Cycle_Time_Units = CORE_CYCLE_CONTROL_SECONDS;

    ADI_CORE_Fifo_Num_Cycles_t fifoNumCyclesReg;
    fifoNumCyclesReg.Fifo_Num_Cycles = NUM_CYCLES_PER_FILL;

    ADI_CORE_Multi_Cycle_Rate_t multiCycleRateReg;
    multiCycleRateReg.Multi_Cycle_Rate = MULTI_CYCLE_FILL_INTERVAL;

    retValue = ADISense1000_ConfigureCycleIntervals(cycleControlReg,
                                                    fifoNumCyclesReg,
                                                    multiCycleRateReg);
    pc.printf("Configure Cycle Intervals with return %d\r\n", retValue);

    setupCJC0();
    pc.printf("\r\nStart CJC0 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_CJC0, &CJC0);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupCJC1();
    pc.printf("\r\nStart CJC1 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_CJC1, &CJC1);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupSENSOR0();
    pc.printf("\r\nStart SENSOR0 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_SENSOR_0, &SENSOR0);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupSENSOR1();
    pc.printf("\r\nStart SENSOR1 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_SENSOR_1, &SENSOR1);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupSENSOR2();
    pc.printf("\r\nStart SENSOR2 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_SENSOR_2, &SENSOR2);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupSENSOR3();
    pc.printf("\r\nStart SENSOR3 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_SENSOR_3, &SENSOR3);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupVOLTAGE();
    pc.printf("\r\nStart VOLTAGE Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_V_MEASURE, &VOLTAGE);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupCURRENT();
    pc.printf("\r\nStart CURRENT Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_I_MEASURE, &CURRENT);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    setupI2C0();
    pc.printf("\r\nStart I2C0 Channel Config \r\n");
    retValue = ADISense1000_ConfigureChannel(ADI_SENSE_DIG_SENSOR_0, &I2C0);
    pc.printf("Channel setup complete with return: %d\r\n", retValue);

    ADI_CORE_Power_Config_t powerConfig;

    powerConfig.Power_Mode_ADC = 3;  /* Full power */
    powerConfig.Power_Mode_MCU = 0;  /* not yet defined */

    retValue = ADISense1000_StartMeasurement(powerConfig, CORE_COMMAND_CONVERT_WITH_RAW);
    pc.printf("Measurement started with return: %d\r\n\r\n", retValue);

    /* In multi-cycle mode, a burst of measurement cycles will be executed at cycle intervals,
       and then the burst is repeated at the multi-cycle interval */
    while (true) {

        pc.printf("\r\n\r\nWaiting for next multi-cycle fill to complete...\r\n\r\n");
        /* Wait for the next cycle to complete */
        while(!ADISense1000_SampleReady()) {
        }


        for(unsigned i=0; i<(SAMPLES_PER_CHANNEL*NUM_CHANNELS*NUM_CYCLES_PER_FILL); i++)
        {
            /* Read data from the enabled channels */
            retValue = ADISense1000_GetData(&rawSample, &processed, &chStatus);
            validSample = (chStatus >> BITP_VALID_DATA)&0x01;
            pc.printf("-%s- :: Sample # %2d Channel # %2d :: -%s- :: Raw %8d  :: Processed %.7f\r\n",
                      ((retValue>0) ? "ERROR" : "OK") , (i+1), (chStatus&0x0f), (validSample ? "VALID" : "INVALID"), rawSample, processed);
        }
    }

}


void setupCJC0(void)
{
    CJC0.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    CJC0.Count.Channel_Enable       =   1;

    CJC0.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_RTD_2W_PT100;
    CJC0.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    CJC0.Details.CJC_Publish                =   1;
    CJC0.Details.Vbias                      =   0;
    CJC0.Details.Reference_Buffer_Disable   =   0;
    CJC0.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_RINT1;
    CJC0.Details.PGA_Gain                   =   3; /* G=8 */

    CJC0.Excitation.IOUT_Excitation_Current     =   4; /* 500uA */
    CJC0.Excitation.IOUT0_Disable               =   0;

    CJC0.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    CJC0.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupCJC1(void)
{
    CJC1.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    CJC1.Count.Channel_Enable       =   1;

    CJC1.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_RTD_2W_PT100;
    CJC1.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    CJC1.Details.CJC_Publish                =   1;
    CJC1.Details.Vbias                      =   0;
    CJC1.Details.Reference_Buffer_Disable   =   0;
    CJC1.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_RINT1;
    CJC1.Details.PGA_Gain                   =   3; /* G=8 */

    CJC1.Excitation.IOUT_Excitation_Current =   4; /* 500uA */
    CJC1.Excitation.IOUT0_Disable           =   0;

    CJC1.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    CJC1.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupSENSOR0(void)
{
    SENSOR0.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    SENSOR0.Count.Channel_Enable       =   1;

    SENSOR0.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_RTD_2W_PT100;
    SENSOR0.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    SENSOR0.Details.Vbias                      =   0;
    SENSOR0.Details.Reference_Buffer_Disable   =   0;
    SENSOR0.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_RINT1;
    SENSOR0.Details.PGA_Gain                   =   3; /* G=8 */

    SENSOR0.Excitation.IOUT_Excitation_Current =   4; /* 500uA */
    SENSOR0.Excitation.IOUT0_Disable           =   0;

    SENSOR0.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    SENSOR0.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupSENSOR1(void)
{
    SENSOR1.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    SENSOR1.Count.Channel_Enable       =   1;

    SENSOR1.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_K;
    SENSOR1.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    SENSOR1.Details.Vbias                      =   1;
    SENSOR1.Details.Reference_Buffer_Disable   =   1;
    SENSOR1.Details.Compensation_Channel       =   1; /* CJC1 */
    SENSOR1.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_INT;
    SENSOR1.Details.PGA_Gain                   =   5; /* G=32 */

    SENSOR1.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    SENSOR1.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupSENSOR2(void)
{
    SENSOR2.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    SENSOR2.Count.Channel_Enable       =   1;

    SENSOR2.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_T;
    SENSOR2.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    SENSOR2.Details.Vbias                      =   1;
    SENSOR2.Details.Reference_Buffer_Disable   =   1;
    SENSOR2.Details.Compensation_Channel       =   0; /* CJC0 */
    SENSOR2.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_INT;
    SENSOR2.Details.PGA_Gain                   =   5; /* G=32 */

    SENSOR2.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    SENSOR2.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupSENSOR3(void)
{
    SENSOR3.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    SENSOR3.Count.Channel_Enable       =   1;

    SENSOR3.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_J;
    SENSOR3.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    SENSOR3.Details.Vbias                      =   1;
    SENSOR3.Details.Reference_Buffer_Disable   =   1;
    SENSOR3.Details.Compensation_Channel       =   0; /* CJC0 */
    SENSOR3.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_INT;
    SENSOR3.Details.PGA_Gain                   =   5; /* G=32 */

    SENSOR3.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    SENSOR3.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupVOLTAGE(void)
{
    VOLTAGE.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    VOLTAGE.Count.Channel_Enable       =   1;

    VOLTAGE.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_VOLTAGE_PRESSURE1;
    VOLTAGE.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    VOLTAGE.Details.Vbias                      =   0;
    VOLTAGE.Details.Reference_Buffer_Disable   =   1;
    VOLTAGE.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_INT;
    VOLTAGE.Details.PGA_Gain                   =   1; /* G=2 */

    VOLTAGE.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    VOLTAGE.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupCURRENT(void)
{
    CURRENT.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    CURRENT.Count.Channel_Enable       =   1;

    CURRENT.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_CURRENT_PRESSURE1;
    CURRENT.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;

    CURRENT.Details.Vbias                      =   0;
    CURRENT.Details.Reference_Buffer_Disable   =   1;
    CURRENT.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_INT;
    CURRENT.Details.PGA_Gain                   =   1; /* G=2 */

    CURRENT.FilterSelect.ADC_Filter_Type       = CORE_FILTER_SELECT_FIR;
    CURRENT.FilterSelect.ADC_FIR_Sel           = 3; /* 25 SPS */;
}


void setupI2C0(void)
{
    I2C0.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
    I2C0.Count.Channel_Enable       =   1;

    I2C0.Type.Sensor_Type           =   CORE_SENSOR_TYPE_I2C_PRESSURE1;
    I2C0.Type.Sensor_Category       =   CORE_SENSOR_TYPE_I2C;

    I2C0.DigitalAddress.Digital_Sensor_Address = 0x27;
}