ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Revision:
5:dbb2b71a59ed
Parent:
3:3796776e2c27
Child:
7:4dbae381f693
--- a/main.cpp	Fri Aug 25 12:53:49 2017 +0000
+++ b/main.cpp	Mon Sep 25 14:45:20 2017 +0000
@@ -1,7 +1,6 @@
-/*!
+/*
  ******************************************************************************
- * @file:   main.cpp
- * @brief:
+ * file:   main.cpp
  *-----------------------------------------------------------------------------
  *
 Copyright (c) 2017 Emutex Ltd. / Analog Devices, Inc.
@@ -48,25 +47,50 @@
 #include "mbed.h"
 #include "inc/adisense1000.h"
 
-#define BITP_VALID_DATA (7)
-#define SAMPLE_COUNT (10)
+/*!
+ ******************************************************************************
+ * @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
+ */
 
-void setupCJC0(void);
-void setupSENSOR0(void);
-void setVoltage(void);
-void printCalTable(void);
+#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       temperature     = 0;
+    float       processed        = 0;
     bool        validSample     = 0;
+    bool        waitUntilReady  = true;
     ADI_CORE_Status_t devStatus;
     ADI_SENSE_RESULT retValue;
 
@@ -75,49 +99,109 @@
     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);
 
-    retValue = ADISense1000_ConfigureModule();
+    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);
 
-    printCalTable();
+    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);
 
-    retValue = ADISense1000_StartMeasurement(CORE_COMMAND_CONVERT_WITH_RAW);
+    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) {
 
-    while(!ADISense1000_SampleReady()) {
-    }
+        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(uint8_t i=0; i<(SAMPLE_COUNT*2); i++)
-    {
-        /* Read data from the enabled channels */
-        retValue = ADISense1000_GetData(&rawSample, &temperature, &chStatus);
-        validSample = (chStatus >> BITP_VALID_DATA)&0x01;
-        pc.printf("-%s- :: Sample # %2d Channel # %2d :: -%s- :: Raw %8d  :: Temperature %.7f\r\n",
-          ((retValue>0) ? "ERROR" : "OK") , (i+1), (chStatus&0x0f), (validSample ? "VALID" : "INVALID"), rawSample, temperature);
-    }
-
-    while (true) {
-        pc.printf("()\r");
-        wait(1);
-        pc.printf("[]\r");
-        wait(1);
+        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);
+        }
     }
 
 }
@@ -125,12 +209,11 @@
 
 void setupCJC0(void)
 {
-    CJC0.Count.Channel_Count        =   SAMPLE_COUNT - 1;
+    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.Type.Sensor_Load_Defaults  =   0;
 
     CJC0.Details.CJC_Publish                =   1;
     CJC0.Details.Vbias                      =   0;
@@ -141,53 +224,155 @@
     CJC0.Excitation.IOUT_Excitation_Current     =   4; /* 500uA */
     CJC0.Excitation.IOUT0_Disable               =   0;
 
-    CJC0.DigitalCoding.VALUE16  =   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        =   SAMPLE_COUNT - 1;
+    SENSOR0.Count.Channel_Count        =   SAMPLES_PER_CHANNEL - 1;
     SENSOR0.Count.Channel_Enable       =   1;
 
-    SENSOR0.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_K;
+    SENSOR0.Type.Sensor_Type           =   CORE_SENSOR_TYPE_SENSOR_RTD_2W_PT100;
     SENSOR0.Type.Sensor_Category       =   CORE_SENSOR_TYPE_ANALOG;
-    SENSOR0.Type.Sensor_Load_Defaults  =   0;
 
-    SENSOR0.Details.Vbias                      =   1;
-    SENSOR0.Details.Reference_Buffer_Disable   =   1;
-    SENSOR0.Details.Compensation_Channel       =   0; /* CJC0 */
-    SENSOR0.Details.Reference_Select           =   CORE_SENSOR_DETAILS_REF_INT;
-    SENSOR0.Details.PGA_Gain                   =   5; /* G=32 */
+    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.VALUE8      =   0;
-    SENSOR0.DigitalCoding.VALUE16  =   0;
+    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 */;
 }
 
-static float calDataBuffer[56 * 3];
-void printCalTable(void)
+
+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)
 {
-    ADI_SENSE_RESULT retValue;
-    unsigned dataLen, nRows, nColumns;
+    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 */
 
-    retValue = ADISense1000_ReadCalTable(calDataBuffer, sizeof(calDataBuffer),
-                                         &dataLen, &nRows, &nColumns);
-    pc.printf("Cal table read complete with return: %d\r\n", retValue);
+    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;
 
-    pc.printf("Calibration Table:\r\n");
-    pc.printf("%6s| %10s | %10s | %10s |\r\n", "index", "25", "-40", "85");
-    for (unsigned row = 0; row < nRows; row++)
-    {
-        pc.printf("%6d", row);
-        for (unsigned col = 0; col < nColumns; col++)
-            pc.printf("| %10f ", calDataBuffer[(row * nColumns) + col]);
-        pc.printf("|\r\n");
-    }
+    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;
 }