ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

main.cpp

Committer:
kevin1990
Date:
2017-10-20
Revision:
7:4dbae381f693
Parent:
5:dbb2b71a59ed

File content as of revision 7:4dbae381f693:

/*
 ******************************************************************************
 * 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 "mbed.h"
#include "inc/adi_sense_api.h"
#include "inc/adi_sense_log.h"

/* Define an arbitrary number of measurement cycles to execute */
#define MAX_MEASUREMENT_CYCLES 10

#define ADI_SENSE_DEVICE_INDEX (0)

#define ASSERT(x)                                                       \
do {                                                                    \
    if (!(x))                                                           \
    {                                                                   \
        ADI_SENSE_LOG_ERROR("Assertion failed on line %d of %s",        \
                            __LINE__, __FILE__);                        \
        exit(1);                                                        \
    }                                                                   \
} while(0)

extern ADI_SENSE_CONFIG adi_sense_config;
extern ADI_SENSE_DSP_LUT_RAW adi_sense_dsp_lut;

static ADI_SENSE_CONNECTION connectionInfo = {
    .type = ADI_SENSE_CONNECTION_TYPE_SPI,
    .spi = {
        .mosiPin    = SPI_MOSI,
        .misoPin    = SPI_MISO,
        .sckPin     = SPI_SCK,
        .csPin      = D10,
        .maxSpeedHz = 2000000,
    },
    .gpio = {
        .resetPin     = D6,
        .errorPin     = D3,
        .alertPin     = D4,
        .datareadyPin = D5,
    },
};


static void printSamples(ADI_SENSE_DATA_SAMPLE *pSampleBuffer, unsigned count)
{
    for (unsigned i = 0; i < count; i++)
    {
        ADI_SENSE_LOG_DEBUG("sample %3u/%u: channel=%2u, value=%10f, raw=%10u, flags: %s %s",
                            i+1, count,
                            pSampleBuffer[i].channelId,
                            pSampleBuffer[i].processedValue,
                            pSampleBuffer[i].rawValue,
                            pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ERROR ? "ERROR" : "",
                            pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ALERT ? "ALERT" : "");
    }
}

static void datareadyCallback(
    ADI_SENSE_GPIO_PIN ePinId,
    void *pArg)
{
    volatile bool_t *pbDataready = (volatile bool_t *) pArg;

    *pbDataready = true;
}

int main()
{
    ADI_SENSE_DEVICE_HANDLE hDevice;
    ADI_SENSE_PRODUCT_ID productId;
    ADI_SENSE_DATA_SAMPLE *pSampleBuffer;
    ADI_SENSE_OPERATING_MODE eOperatingMode;
    ADI_SENSE_DATA_PUBLISH_MODE eDataPublishMode;
    uint32_t nSamplesPerDataready;
    uint32_t nSamplesPerCycle;
    bool_t bDeviceReady;

    /*
     * Open an ADI Sense device instance.
     */
    ASSERT(adi_sense_Open(0, &connectionInfo, &hDevice) == ADI_SENSE_SUCCESS);

    /*
     * Reset the given ADI Sense device, and wait until the device is ready.
     */
    ADI_SENSE_LOG_INFO("Resetting ADI Sense device, please wait...");
    ASSERT(adi_sense_Reset(hDevice) == ADI_SENSE_SUCCESS);

    /*
     * Wait until the device is ready.
     */
    do {
        wait_ms(100);
        ASSERT(adi_sense_GetDeviceReadyState(hDevice, &bDeviceReady) == ADI_SENSE_SUCCESS);
    } while (! bDeviceReady);
    ADI_SENSE_LOG_INFO("ADI Sense device ready");

    /*
     * Read the product ID from the device registers.
     */
    ASSERT(adi_sense_GetProductID(hDevice, &productId) == ADI_SENSE_SUCCESS);
    ADI_SENSE_LOG_INFO("Product ID: 0x%04X", productId);

    /*
     * Write configuration settings to the device registers.
     * Settings are not applied until adi_sense_ApplyConfigUpdates() is called.
     */
    ASSERT(adi_sense_SetDeviceConfig(hDevice, &adi_sense_config) == ADI_SENSE_SUCCESS);
    ASSERT(adi_sense_SetDspData(hDevice, &adi_sense_dsp_lut) == ADI_SENSE_SUCCESS);
    ASSERT(adi_sense_ApplyConfigUpdates(hDevice) == ADI_SENSE_SUCCESS);

    /*
     * Allocate a buffer to store the samples retreived on each DATAREADY pulse
     */
    ASSERT(adi_sense_GetDataPublishingInfo(hDevice,
                                           &eOperatingMode,
                                           &eDataPublishMode,
                                           &nSamplesPerDataready,
                                           &nSamplesPerCycle) == ADI_SENSE_SUCCESS);
    pSampleBuffer = new ADI_SENSE_DATA_SAMPLE[nSamplesPerDataready];
    ASSERT(pSampleBuffer != NULL);

    ADI_SENSE_LOG_INFO("Configuration loaded and applied, starting measurement cycles");

    if (eOperatingMode == ADI_SENSE_OPERATING_MODE_SINGLECYCLE)
    {
        uint32_t nTotalCycles = 0;

        /* For demonstration reasons only, we're using interrupt-triggered DATAREADY
         * notifications in this mode */
        volatile bool_t bDataReady = false;
        ASSERT(adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_DATAREADY,
                                              datareadyCallback,
                                              (void *)&bDataReady) == ADI_SENSE_SUCCESS);

        do
        {
            ASSERT(adi_sense_StartMeasurement(hDevice) == ADI_SENSE_SUCCESS);

            for (unsigned i = 0; i < nSamplesPerCycle / nSamplesPerDataready; i++)
            {
                uint32_t nReturned;

                /*
                 * Interrupt method: wait for DATAREADY interrupt callback
                 *
                 * Note that we could potentially enter a sleep mode here if this
                 * GPIO interrupt is capable of waking the CPU...
                 */
                while (! bDataReady)
                    ;
                /* Reset bDataReady flag to detect the next callback */
                bDataReady = false;

                ASSERT(adi_sense_GetData(hDevice, pSampleBuffer, nSamplesPerDataready, &nReturned) == ADI_SENSE_SUCCESS);
                ASSERT(nReturned == nSamplesPerDataready);

                printSamples(pSampleBuffer, nReturned);
            }
            nTotalCycles++;

        } while (nTotalCycles < MAX_MEASUREMENT_CYCLES);
    }
    else
    {
        uint32_t nTotalSamples = 0;
        bool_t bDataReady = false;

        ASSERT(adi_sense_StartMeasurement(hDevice) == ADI_SENSE_SUCCESS);

        do
        {
            uint32_t nReturned;

            /* Polling method: continuously check DATAREADY until it is asserted */
            do {
                ASSERT(adi_sense_GetGpioState(hDevice, ADI_SENSE_GPIO_PIN_DATAREADY, &bDataReady) == ADI_SENSE_SUCCESS);
            } while (! bDataReady);

            ASSERT(adi_sense_GetData(hDevice, pSampleBuffer, nSamplesPerDataready, &nReturned) == ADI_SENSE_SUCCESS);
            ASSERT(nReturned == nSamplesPerDataready);
            nTotalSamples += nReturned;

            printSamples(pSampleBuffer, nReturned);
        } while ((nTotalSamples / nSamplesPerCycle) < MAX_MEASUREMENT_CYCLES);

        ASSERT(adi_sense_StopMeasurement(hDevice) == ADI_SENSE_SUCCESS);
    }
    delete [] pSampleBuffer;

    ASSERT(adi_sense_Close(hDevice) == ADI_SENSE_SUCCESS);

    return 0;
}