Example program for EVAL-ADMX2001

Dependencies:   ADMX2001

Committer:
nsheth
Date:
Tue Nov 02 10:14:25 2021 +0000
Revision:
9:29db35656fcb
Parent:
8:bd0b93e35392
Child:
10:186e097fb1e3
Added platform drivers, documentation updated, short calibration completed

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 8:bd0b93e35392 55 /** This is kept global and non-static as 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 3:7954489d9f8a 64 /**
nsheth 8:bd0b93e35392 65 *
nsheth 8:bd0b93e35392 66 */
nsheth 8:bd0b93e35392 67 /**
nsheth 8:bd0b93e35392 68 * @brief Function to initialize SPI
nsheth 8:bd0b93e35392 69 *
nsheth 8:bd0b93e35392 70 * @param pSpiDesc - Pointer to the spi descriptor
nsheth 8:bd0b93e35392 71 * @return Returns 0 for success or error code.
nsheth 3:7954489d9f8a 72 */
nsheth 3:7954489d9f8a 73 int32_t SPIInit(spi_desc **pSpiDesc)
nsheth 3:7954489d9f8a 74 {
nsheth 3:7954489d9f8a 75 int32_t status;
Kjansen45 0:a74ad5b8dc7b 76
nsheth 3:7954489d9f8a 77 // Init SPI extra parameters structure
nsheth 3:7954489d9f8a 78 mbed_spi_init_param spiInitExtraParams = {.spi_clk_pin = SPI_SCK,
nsheth 3:7954489d9f8a 79 .spi_miso_pin = SPI_MISO,
nsheth 3:7954489d9f8a 80 .spi_mosi_pin = SPI_MOSI};
nsheth 3:7954489d9f8a 81
nsheth 3:7954489d9f8a 82 spi_init_param spiInit = {
nsheth 3:7954489d9f8a 83 25000000, // Max SPI Speed
nsheth 3:7954489d9f8a 84 SPI_SS, // Chip Select pin
nsheth 3:7954489d9f8a 85 SPI_MODE_0,
nsheth 3:7954489d9f8a 86 &spiInitExtraParams, // SPI extra configurations
nsheth 3:7954489d9f8a 87 };
nsheth 3:7954489d9f8a 88
nsheth 3:7954489d9f8a 89 status = spi_init(pSpiDesc, &spiInit);
nsheth 3:7954489d9f8a 90
nsheth 3:7954489d9f8a 91 return status;
Kjansen45 0:a74ad5b8dc7b 92 }
Kjansen45 0:a74ad5b8dc7b 93
nsheth 5:746e78113d7d 94 /**
nsheth 8:bd0b93e35392 95 * Resets measurement
nsheth 5:746e78113d7d 96 */
nsheth 8:bd0b93e35392 97 int32_t ResetModule()
nsheth 5:746e78113d7d 98 {
nsheth 8:bd0b93e35392 99 int32_t status = 0;
nsheth 8:bd0b93e35392 100 uint8_t cmdId;
nsheth 8:bd0b93e35392 101 uint16_t addr;
nsheth 8:bd0b93e35392 102 uint32_t data;
nsheth 8:bd0b93e35392 103 uint32_t statReg;
nsheth 5:746e78113d7d 104
nsheth 8:bd0b93e35392 105 Admx200xDev *pAdmx200x = &admx200xDev;
nsheth 5:746e78113d7d 106
nsheth 8:bd0b93e35392 107 /* Send reset command */
nsheth 8:bd0b93e35392 108 cmdId = CMD_RESET;
nsheth 8:bd0b93e35392 109 addr = 0;
nsheth 8:bd0b93e35392 110 data = 0;
nsheth 8:bd0b93e35392 111 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 5:746e78113d7d 112
nsheth 5:746e78113d7d 113 return status;
nsheth 5:746e78113d7d 114 }
nsheth 5:746e78113d7d 115
nsheth 9:29db35656fcb 116 /**
nsheth 9:29db35656fcb 117 * Set the voltage/current gain
nsheth 9:29db35656fcb 118 */
nsheth 9:29db35656fcb 119 int32_t SetGain(uint32_t voltageGain, uint32_t currentGain)
nsheth 9:29db35656fcb 120 {
nsheth 9:29db35656fcb 121 int32_t status = ADMX_STATUS_SUCCESS;
nsheth 9:29db35656fcb 122 uint16_t addr = 0;
nsheth 9:29db35656fcb 123 uint8_t cmdId;
nsheth 9:29db35656fcb 124 uint32_t statReg;
nsheth 9:29db35656fcb 125 Admx200xDev *pAdmx200x = &admx200xDev;
nsheth 9:29db35656fcb 126 uint32_t data;
nsheth 9:29db35656fcb 127
nsheth 9:29db35656fcb 128 data = voltageGain;
nsheth 9:29db35656fcb 129 cmdId = CMD_VOLTAGE_GAIN;
nsheth 9:29db35656fcb 130 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 9:29db35656fcb 131 if (status == ADMX_STATUS_SUCCESS)
nsheth 9:29db35656fcb 132 {
nsheth 9:29db35656fcb 133 status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
nsheth 9:29db35656fcb 134 }
nsheth 9:29db35656fcb 135
nsheth 9:29db35656fcb 136 if (status == ADMX_STATUS_SUCCESS)
nsheth 9:29db35656fcb 137 {
nsheth 9:29db35656fcb 138 cmdId = CMD_CURRENT_GAIN;
nsheth 9:29db35656fcb 139 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 9:29db35656fcb 140 if (status == ADMX_STATUS_SUCCESS)
nsheth 9:29db35656fcb 141 {
nsheth 9:29db35656fcb 142 status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
nsheth 9:29db35656fcb 143 }
nsheth 9:29db35656fcb 144 }
nsheth 9:29db35656fcb 145
nsheth 9:29db35656fcb 146 return status;
nsheth 9:29db35656fcb 147 }
nsheth 9:29db35656fcb 148
Kjansen45 0:a74ad5b8dc7b 149 int main()
Kjansen45 0:a74ad5b8dc7b 150 {
nsheth 3:7954489d9f8a 151 int32_t status = 0;
nsheth 3:7954489d9f8a 152 static spi_desc spiDesc;
nsheth 5:746e78113d7d 153 float stdLoad = 51;
nsheth 5:746e78113d7d 154 float Xt = 0;
nsheth 5:746e78113d7d 155 CAL_TYPE calType;
nsheth 3:7954489d9f8a 156 spi_desc *pSpi = &spiDesc;
nsheth 3:7954489d9f8a 157 Admx200xDev *pADmx2001 = &admx200xDev;
nsheth 3:7954489d9f8a 158 status |= SPIInit(&pSpi);
nsheth 3:7954489d9f8a 159 status |= Admx200xInit(pADmx2001, pSpi);
nsheth 3:7954489d9f8a 160
nsheth 3:7954489d9f8a 161 mdelay(100);
nsheth 3:7954489d9f8a 162
nsheth 3:7954489d9f8a 163 /* Initialize the ADMX200x application */
nsheth 3:7954489d9f8a 164 if (status != 0)
nsheth 3:7954489d9f8a 165 {
nsheth 8:bd0b93e35392 166 ERROR_MSG("Setting up ADMX200X failed");
Kjansen45 0:a74ad5b8dc7b 167 }
nsheth 3:7954489d9f8a 168 else
nsheth 3:7954489d9f8a 169 {
nsheth 8:bd0b93e35392 170 INFO_MSG("Resetting module");
nsheth 8:bd0b93e35392 171 status = ResetModule();
nsheth 8:bd0b93e35392 172 if (status != ADMX_STATUS_SUCCESS)
nsheth 8:bd0b93e35392 173 {
nsheth 8:bd0b93e35392 174 ERROR_MSG("Failed to reset module");
nsheth 8:bd0b93e35392 175 }
nsheth 8:bd0b93e35392 176 else
nsheth 8:bd0b93e35392 177 {
nsheth 8:bd0b93e35392 178 INFO_MSG("------ Initialising measurement ------");
nsheth 8:bd0b93e35392 179 INFO_MSG("Setting test load to 1k ohms");
nsheth 8:bd0b93e35392 180 // Selects test load 3 - 1000 ohm resistor
nsheth 8:bd0b93e35392 181 EnableMuxLine(3);
nsheth 8:bd0b93e35392 182 // Performing measurement
nsheth 8:bd0b93e35392 183 status = MeasureAndDisplay();
nsheth 8:bd0b93e35392 184 INFO_MSG("------ Measurement completed ------");
nsheth 8:bd0b93e35392 185
nsheth 8:bd0b93e35392 186 INFO_MSG("------ Starting Calibration ------");
nsheth 9:29db35656fcb 187 INFO_MSG("Setting test load to short circuit");
nsheth 9:29db35656fcb 188 // Short the terminals across the lcr meter
nsheth 9:29db35656fcb 189 EnableMuxLine(1);
nsheth 9:29db35656fcb 190 // Setting the gain for calibration
nsheth 9:29db35656fcb 191 status = SetGain(0, 0);
nsheth 9:29db35656fcb 192 INFO_MSG("Setting voltage gain to zero");
nsheth 9:29db35656fcb 193 INFO_MSG("Setting current gain to zero");
nsheth 9:29db35656fcb 194 // Running short calibration
nsheth 9:29db35656fcb 195 calType = CAL_TYPE_SHORT;
nsheth 8:bd0b93e35392 196 status = Calibrate(calType, stdLoad, Xt);
nsheth 8:bd0b93e35392 197 INFO_MSG("------ Calibration completed ------");
nsheth 8:bd0b93e35392 198 }
nsheth 3:7954489d9f8a 199 }
nsheth 3:7954489d9f8a 200
nsheth 3:7954489d9f8a 201 status = spi_remove(pSpi);
nsheth 3:7954489d9f8a 202
nsheth 3:7954489d9f8a 203 return status;
Kjansen45 0:a74ad5b8dc7b 204 }