Example program for EVAL-ADMX2001

Dependencies:   ADMX2001

Committer:
nsheth
Date:
Wed Nov 17 18:15:37 2021 +0000
Revision:
15:fca7551aaf0a
Parent:
10:186e097fb1e3
Updating default .lib file for eval platform

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nsheth 5:746e78113d7d 1 /* Copyright (c) 2021 Analog Devices, Inc. All rights reserved.
Kjansen45 0:a74ad5b8dc7b 2
Kjansen45 0:a74ad5b8dc7b 3 Redistribution and use in source and binary forms, with or without modification,
Kjansen45 0:a74ad5b8dc7b 4 are permitted provided that the following conditions are met:
Kjansen45 0:a74ad5b8dc7b 5 - Redistributions of source code must retain the above copyright notice,
Kjansen45 0:a74ad5b8dc7b 6 this list of conditions and the following disclaimer.
Kjansen45 0:a74ad5b8dc7b 7 - Redistributions in binary form must reproduce the above copyright notice,
Kjansen45 0:a74ad5b8dc7b 8 this list of conditions and the following disclaimer in the documentation
Kjansen45 0:a74ad5b8dc7b 9 and/or other materials provided with the distribution.
Kjansen45 0:a74ad5b8dc7b 10 - Modified versions of the software must be conspicuously marked as such.
Kjansen45 0:a74ad5b8dc7b 11 - This software is licensed solely and exclusively for use with processors/products
Kjansen45 0:a74ad5b8dc7b 12 manufactured by or for Analog Devices, Inc.
Kjansen45 0:a74ad5b8dc7b 13 - This software may not be combined or merged with other code in any manner
Kjansen45 0:a74ad5b8dc7b 14 that would cause the software to become subject to terms and conditions which
Kjansen45 0:a74ad5b8dc7b 15 differ from those listed here.
Kjansen45 0:a74ad5b8dc7b 16 - Neither the name of Analog Devices, Inc. nor the names of its contributors
Kjansen45 0:a74ad5b8dc7b 17 may be used to endorse or promote products derived from this software without
Kjansen45 0:a74ad5b8dc7b 18 specific prior written permission.
Kjansen45 0:a74ad5b8dc7b 19 - The use of this software may or may not infringe the patent rights of one or
Kjansen45 0:a74ad5b8dc7b 20 more patent holders. This license does not release you from the requirement
Kjansen45 0:a74ad5b8dc7b 21 that you obtain separate licenses from these patent holders to use this software.
Kjansen45 0:a74ad5b8dc7b 22
Kjansen45 0:a74ad5b8dc7b 23 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND
Kjansen45 0:a74ad5b8dc7b 24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
Kjansen45 0:a74ad5b8dc7b 25 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
Kjansen45 0:a74ad5b8dc7b 26 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Kjansen45 0:a74ad5b8dc7b 27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
Kjansen45 0:a74ad5b8dc7b 28 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
Kjansen45 0:a74ad5b8dc7b 29 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Kjansen45 0:a74ad5b8dc7b 30 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Kjansen45 0:a74ad5b8dc7b 31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Kjansen45 0:a74ad5b8dc7b 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
Kjansen45 0:a74ad5b8dc7b 33 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kjansen45 0:a74ad5b8dc7b 34
nsheth 5:746e78113d7d 35 2021-01-10-7CBSD SLA
nsheth 5:746e78113d7d 36 */
nsheth 3:7954489d9f8a 37
Kjansen45 0:a74ad5b8dc7b 38
nsheth 3:7954489d9f8a 39 /*============= I N C L U D E S =============*/
nsheth 4:e7e194f58f65 40 #include "ADMX2001.h"
nsheth 5:746e78113d7d 41 #include "ADMX2001_commands.h"
nsheth 3:7954489d9f8a 42 #include "platform_drivers.h"
nsheth 4:e7e194f58f65 43 #include "app_config.h"
nsheth 3:7954489d9f8a 44 #include "spi_extra.h"
nsheth 5:746e78113d7d 45 #include "message.h"
nsheth 5:746e78113d7d 46 #include "gpio_config.h"
nsheth 5:746e78113d7d 47 #include "measure.h"
nsheth 5:746e78113d7d 48 #include "calibrate.h"
nsheth 3:7954489d9f8a 49 #include <stdint.h>
nsheth 3:7954489d9f8a 50 #include <stdio.h>
nsheth 3:7954489d9f8a 51 #include <stdlib.h>
Kjansen45 0:a74ad5b8dc7b 52
nsheth 9:29db35656fcb 53 /*============= D A T A =============*/
nsheth 9:29db35656fcb 54
nsheth 10:186e097fb1e3 55 /** This is kept global and non-static as the test code requires this*/
nsheth 3:7954489d9f8a 56 Admx200xDev admx200xDev;
nsheth 3:7954489d9f8a 57
nsheth 3:7954489d9f8a 58 static int32_t SPIInit(spi_desc **pSpiDesc);
Kjansen45 0:a74ad5b8dc7b 59
nsheth 8:bd0b93e35392 60 static int32_t ResetModule();
nsheth 8:bd0b93e35392 61
nsheth 9:29db35656fcb 62 static int32_t SetGain(uint32_t voltageGain, uint32_t currentGain);
nsheth 9:29db35656fcb 63
nsheth 10:186e097fb1e3 64 /*============= C O D E =============*/
nsheth 10:186e097fb1e3 65
nsheth 8:bd0b93e35392 66 /**
nsheth 8:bd0b93e35392 67 * @brief Function to initialize SPI
nsheth 8:bd0b93e35392 68 * @param pSpiDesc - Pointer to the spi descriptor
nsheth 8:bd0b93e35392 69 * @return Returns 0 for success or error code.
nsheth 3:7954489d9f8a 70 */
nsheth 3:7954489d9f8a 71 int32_t SPIInit(spi_desc **pSpiDesc)
nsheth 3:7954489d9f8a 72 {
nsheth 3:7954489d9f8a 73 int32_t status;
Kjansen45 0:a74ad5b8dc7b 74
nsheth 3:7954489d9f8a 75 // Init SPI extra parameters structure
nsheth 3:7954489d9f8a 76 mbed_spi_init_param spiInitExtraParams = {.spi_clk_pin = SPI_SCK,
nsheth 3:7954489d9f8a 77 .spi_miso_pin = SPI_MISO,
nsheth 3:7954489d9f8a 78 .spi_mosi_pin = SPI_MOSI};
nsheth 3:7954489d9f8a 79
nsheth 3:7954489d9f8a 80 spi_init_param spiInit = {
nsheth 3:7954489d9f8a 81 25000000, // Max SPI Speed
nsheth 3:7954489d9f8a 82 SPI_SS, // Chip Select pin
nsheth 3:7954489d9f8a 83 SPI_MODE_0,
nsheth 3:7954489d9f8a 84 &spiInitExtraParams, // SPI extra configurations
nsheth 3:7954489d9f8a 85 };
nsheth 3:7954489d9f8a 86
nsheth 3:7954489d9f8a 87 status = spi_init(pSpiDesc, &spiInit);
nsheth 3:7954489d9f8a 88
nsheth 3:7954489d9f8a 89 return status;
Kjansen45 0:a74ad5b8dc7b 90 }
Kjansen45 0:a74ad5b8dc7b 91
nsheth 5:746e78113d7d 92 /**
nsheth 10:186e097fb1e3 93 * @brief Resets measurement
nsheth 10:186e097fb1e3 94 * @return Returns 0 for success or error code.
nsheth 5:746e78113d7d 95 */
nsheth 8:bd0b93e35392 96 int32_t ResetModule()
nsheth 5:746e78113d7d 97 {
nsheth 8:bd0b93e35392 98 int32_t status = 0;
nsheth 8:bd0b93e35392 99 uint8_t cmdId;
nsheth 8:bd0b93e35392 100 uint16_t addr;
nsheth 8:bd0b93e35392 101 uint32_t data;
nsheth 8:bd0b93e35392 102 uint32_t statReg;
nsheth 5:746e78113d7d 103
nsheth 8:bd0b93e35392 104 Admx200xDev *pAdmx200x = &admx200xDev;
nsheth 5:746e78113d7d 105
nsheth 8:bd0b93e35392 106 /* Send reset command */
nsheth 8:bd0b93e35392 107 cmdId = CMD_RESET;
nsheth 8:bd0b93e35392 108 addr = 0;
nsheth 8:bd0b93e35392 109 data = 0;
nsheth 8:bd0b93e35392 110 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 5:746e78113d7d 111
nsheth 5:746e78113d7d 112 return status;
nsheth 5:746e78113d7d 113 }
nsheth 5:746e78113d7d 114
nsheth 9:29db35656fcb 115 /**
nsheth 10:186e097fb1e3 116 * @brief Set the voltage and current gain
nsheth 10:186e097fb1e3 117 * @param voltageGain - Voltage gain value
nsheth 10:186e097fb1e3 118 * @param currentGain - Current gain value
nsheth 10:186e097fb1e3 119 * @return Returns 0 for success or error code.
nsheth 9:29db35656fcb 120 */
nsheth 9:29db35656fcb 121 int32_t SetGain(uint32_t voltageGain, uint32_t currentGain)
nsheth 9:29db35656fcb 122 {
nsheth 9:29db35656fcb 123 int32_t status = ADMX_STATUS_SUCCESS;
nsheth 9:29db35656fcb 124 uint16_t addr = 0;
nsheth 9:29db35656fcb 125 uint8_t cmdId;
nsheth 9:29db35656fcb 126 uint32_t statReg;
nsheth 9:29db35656fcb 127 Admx200xDev *pAdmx200x = &admx200xDev;
nsheth 9:29db35656fcb 128 uint32_t data;
nsheth 9:29db35656fcb 129
nsheth 9:29db35656fcb 130 data = voltageGain;
nsheth 9:29db35656fcb 131 cmdId = CMD_VOLTAGE_GAIN;
nsheth 9:29db35656fcb 132 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 9:29db35656fcb 133 if (status == ADMX_STATUS_SUCCESS)
nsheth 9:29db35656fcb 134 {
nsheth 9:29db35656fcb 135 status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
nsheth 9:29db35656fcb 136 }
nsheth 9:29db35656fcb 137
nsheth 9:29db35656fcb 138 if (status == ADMX_STATUS_SUCCESS)
nsheth 9:29db35656fcb 139 {
nsheth 9:29db35656fcb 140 cmdId = CMD_CURRENT_GAIN;
nsheth 9:29db35656fcb 141 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 9:29db35656fcb 142 if (status == ADMX_STATUS_SUCCESS)
nsheth 9:29db35656fcb 143 {
nsheth 9:29db35656fcb 144 status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
nsheth 9:29db35656fcb 145 }
nsheth 9:29db35656fcb 146 }
nsheth 9:29db35656fcb 147
nsheth 9:29db35656fcb 148 return status;
nsheth 9:29db35656fcb 149 }
nsheth 9:29db35656fcb 150
Kjansen45 0:a74ad5b8dc7b 151 int main()
Kjansen45 0:a74ad5b8dc7b 152 {
nsheth 3:7954489d9f8a 153 int32_t status = 0;
nsheth 3:7954489d9f8a 154 static spi_desc spiDesc;
nsheth 5:746e78113d7d 155 float stdLoad = 51;
nsheth 5:746e78113d7d 156 float Xt = 0;
nsheth 5:746e78113d7d 157 CAL_TYPE calType;
nsheth 3:7954489d9f8a 158 spi_desc *pSpi = &spiDesc;
nsheth 3:7954489d9f8a 159 Admx200xDev *pADmx2001 = &admx200xDev;
nsheth 3:7954489d9f8a 160 status |= SPIInit(&pSpi);
nsheth 3:7954489d9f8a 161 status |= Admx200xInit(pADmx2001, pSpi);
nsheth 3:7954489d9f8a 162
nsheth 3:7954489d9f8a 163 mdelay(100);
nsheth 3:7954489d9f8a 164
nsheth 3:7954489d9f8a 165 /* Initialize the ADMX200x application */
nsheth 3:7954489d9f8a 166 if (status != 0)
nsheth 3:7954489d9f8a 167 {
nsheth 8:bd0b93e35392 168 ERROR_MSG("Setting up ADMX200X failed");
Kjansen45 0:a74ad5b8dc7b 169 }
nsheth 3:7954489d9f8a 170 else
nsheth 3:7954489d9f8a 171 {
nsheth 8:bd0b93e35392 172 INFO_MSG("Resetting module");
nsheth 8:bd0b93e35392 173 status = ResetModule();
nsheth 8:bd0b93e35392 174 if (status != ADMX_STATUS_SUCCESS)
nsheth 8:bd0b93e35392 175 {
nsheth 8:bd0b93e35392 176 ERROR_MSG("Failed to reset module");
nsheth 8:bd0b93e35392 177 }
nsheth 8:bd0b93e35392 178 else
nsheth 8:bd0b93e35392 179 {
nsheth 8:bd0b93e35392 180 INFO_MSG("------ Initialising measurement ------");
nsheth 8:bd0b93e35392 181 INFO_MSG("Setting test load to 1k ohms");
nsheth 8:bd0b93e35392 182 // Selects test load 3 - 1000 ohm resistor
nsheth 8:bd0b93e35392 183 EnableMuxLine(3);
nsheth 8:bd0b93e35392 184 // Performing measurement
nsheth 8:bd0b93e35392 185 status = MeasureAndDisplay();
nsheth 8:bd0b93e35392 186 INFO_MSG("------ Measurement completed ------");
nsheth 8:bd0b93e35392 187
nsheth 8:bd0b93e35392 188 INFO_MSG("------ Starting Calibration ------");
nsheth 9:29db35656fcb 189 INFO_MSG("Setting test load to short circuit");
nsheth 9:29db35656fcb 190 // Short the terminals across the lcr meter
nsheth 9:29db35656fcb 191 EnableMuxLine(1);
nsheth 9:29db35656fcb 192 // Setting the gain for calibration
nsheth 9:29db35656fcb 193 status = SetGain(0, 0);
nsheth 9:29db35656fcb 194 INFO_MSG("Setting voltage gain to zero");
nsheth 9:29db35656fcb 195 INFO_MSG("Setting current gain to zero");
nsheth 9:29db35656fcb 196 // Running short calibration
nsheth 9:29db35656fcb 197 calType = CAL_TYPE_SHORT;
nsheth 8:bd0b93e35392 198 status = Calibrate(calType, stdLoad, Xt);
nsheth 10:186e097fb1e3 199 if (status != ADMX_STATUS_SUCCESS)
nsheth 10:186e097fb1e3 200 {
nsheth 10:186e097fb1e3 201 ERROR_MSG("Calibration Failed");
nsheth 10:186e097fb1e3 202 }
nsheth 8:bd0b93e35392 203 INFO_MSG("------ Calibration completed ------");
nsheth 8:bd0b93e35392 204 }
nsheth 3:7954489d9f8a 205 }
nsheth 3:7954489d9f8a 206
nsheth 3:7954489d9f8a 207 status = spi_remove(pSpi);
nsheth 3:7954489d9f8a 208
nsheth 3:7954489d9f8a 209 return status;
Kjansen45 0:a74ad5b8dc7b 210 }