Analog Devices / Mbed OS EVAL-ADMX2001

Dependencies:   ADMX2001

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2021 Analog Devices, Inc.  All rights reserved.
00002 
00003 Redistribution and use in source and binary forms, with or without modification, 
00004 are permitted provided that the following conditions are met:
00005   - Redistributions of source code must retain the above copyright notice, 
00006   this list of conditions and the following disclaimer.
00007   - Redistributions in binary form must reproduce the above copyright notice, 
00008   this list of conditions and the following disclaimer in the documentation 
00009   and/or other materials provided with the distribution.  
00010   - Modified versions of the software must be conspicuously marked as such.
00011   - This software is licensed solely and exclusively for use with processors/products 
00012   manufactured by or for Analog Devices, Inc.
00013   - This software may not be combined or merged with other code in any manner 
00014   that would cause the software to become subject to terms and conditions which 
00015   differ from those listed here.
00016   - Neither the name of Analog Devices, Inc. nor the names of its contributors 
00017   may be used to endorse or promote products derived from this software without 
00018   specific prior written permission.
00019   - The use of this software may or may not infringe the patent rights of one or 
00020   more patent holders.  This license does not release you from the requirement 
00021   that you obtain separate licenses from these patent holders to use this software.
00022 
00023 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND 
00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 
00025 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
00026 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
00027 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES 
00028 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL 
00029 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
00030 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00031 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 
00035 2021-01-10-7CBSD SLA
00036 */
00037 
00038 
00039 /*=============  I N C L U D E S   =============*/
00040 #include "ADMX2001.h"
00041 #include "ADMX2001_commands.h"
00042 #include "platform_drivers.h"
00043 #include "app_config.h"
00044 #include "spi_extra.h"
00045 #include "message.h"
00046 #include "gpio_config.h"
00047 #include "measure.h"
00048 #include "calibrate.h"
00049 #include <stdint.h>
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 
00053 /*=============  D A T A  =============*/
00054 
00055 /** This is kept global and non-static as the test code requires this*/
00056 Admx200xDev admx200xDev;
00057 
00058 static int32_t SPIInit(spi_desc **pSpiDesc);
00059 
00060 static int32_t ResetModule();
00061 
00062 static int32_t SetGain(uint32_t voltageGain, uint32_t currentGain);
00063 
00064 /*=============  C O D E  =============*/
00065 
00066  /**
00067  * @brief Function to initialize SPI
00068  * @param pSpiDesc - Pointer to the spi descriptor
00069  * @return Returns 0 for success or error code.
00070  */
00071 int32_t SPIInit(spi_desc **pSpiDesc)
00072 {
00073     int32_t status;
00074 
00075     // Init SPI extra parameters structure
00076     mbed_spi_init_param spiInitExtraParams = {.spi_clk_pin = SPI_SCK,
00077                                               .spi_miso_pin = SPI_MISO,
00078                                               .spi_mosi_pin = SPI_MOSI};
00079 
00080     spi_init_param spiInit = {
00081         25000000, // Max SPI Speed
00082         SPI_SS,  // Chip Select pin
00083         SPI_MODE_0,
00084         &spiInitExtraParams, // SPI extra configurations
00085     };
00086 
00087     status = spi_init(pSpiDesc, &spiInit);
00088 
00089     return status;
00090 }
00091 
00092 /**
00093  * @brief Resets measurement 
00094  * @return Returns 0 for success or error code.
00095  */
00096 int32_t ResetModule()
00097 {
00098     int32_t status = 0;
00099     uint8_t cmdId;
00100     uint16_t addr;
00101     uint32_t data;
00102     uint32_t statReg;
00103 
00104     Admx200xDev *pAdmx200x = &admx200xDev;
00105 
00106     /* Send reset command */
00107     cmdId = CMD_RESET;
00108     addr = 0;
00109     data = 0;
00110     status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
00111 
00112     return status;
00113 }
00114 
00115 /**
00116  * @brief Set the voltage and current gain 
00117  * @param voltageGain - Voltage gain value
00118  * @param currentGain - Current gain value
00119  * @return Returns 0 for success or error code.
00120  */
00121 int32_t SetGain(uint32_t voltageGain, uint32_t currentGain)
00122 {
00123     int32_t status = ADMX_STATUS_SUCCESS;
00124     uint16_t addr = 0;
00125     uint8_t cmdId;
00126     uint32_t statReg;
00127     Admx200xDev *pAdmx200x = &admx200xDev;
00128     uint32_t data;
00129     
00130     data = voltageGain;
00131     cmdId = CMD_VOLTAGE_GAIN;
00132     status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
00133     if (status == ADMX_STATUS_SUCCESS)
00134     {
00135         status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
00136     }
00137     
00138     if (status == ADMX_STATUS_SUCCESS)
00139     {
00140         cmdId = CMD_CURRENT_GAIN;
00141         status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
00142         if (status == ADMX_STATUS_SUCCESS)
00143         {
00144             status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
00145         }
00146     }
00147 
00148     return status;
00149 }
00150 
00151 int main()
00152 {
00153     int32_t status = 0;
00154     static spi_desc spiDesc;
00155     float stdLoad = 51;
00156     float Xt = 0;
00157     CAL_TYPE calType;
00158     spi_desc *pSpi = &spiDesc;
00159     Admx200xDev *pADmx2001 = &admx200xDev;
00160     status |= SPIInit(&pSpi);
00161     status |= Admx200xInit(pADmx2001, pSpi);
00162 
00163     mdelay(100);
00164 
00165     /* Initialize the ADMX200x application */
00166     if (status != 0)
00167     {
00168         ERROR_MSG("Setting up ADMX200X failed");
00169     }
00170     else
00171     {
00172         INFO_MSG("Resetting module");
00173         status = ResetModule();
00174         if (status != ADMX_STATUS_SUCCESS)
00175         {
00176             ERROR_MSG("Failed to reset module");
00177         }
00178         else
00179         {
00180             INFO_MSG("------ Initialising measurement ------");
00181             INFO_MSG("Setting test load to 1k ohms");
00182             // Selects test load 3 - 1000 ohm resistor
00183             EnableMuxLine (3);
00184             // Performing measurement
00185             status = MeasureAndDisplay();
00186             INFO_MSG("------ Measurement completed ------");
00187             
00188             INFO_MSG("------ Starting Calibration ------");
00189             INFO_MSG("Setting test load to short circuit");
00190             // Short the terminals across the lcr meter
00191             EnableMuxLine (1);
00192             // Setting the gain for calibration
00193             status = SetGain(0, 0);
00194             INFO_MSG("Setting voltage gain to zero");
00195             INFO_MSG("Setting current gain to zero");
00196             // Running short calibration
00197             calType = CAL_TYPE_SHORT;
00198             status = Calibrate(calType, stdLoad, Xt);
00199             if (status != ADMX_STATUS_SUCCESS)
00200             {
00201                 ERROR_MSG("Calibration Failed");
00202             }
00203             INFO_MSG("------ Calibration completed ------");
00204         }
00205     }
00206 
00207     status = spi_remove(pSpi);
00208 
00209     return status;
00210 }