Example program for EVAL-ADMX2001
Dependencies: ADMX2001
main.cpp@8:bd0b93e35392, 2021-10-27 (annotated)
- Committer:
- nsheth
- Date:
- Wed Oct 27 21:18:12 2021 +0000
- Revision:
- 8:bd0b93e35392
- Parent:
- 5:746e78113d7d
- Child:
- 9:29db35656fcb
Refactoring and restructuring
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:bd0b93e35392 | 53 | /** This is kept global and non-static as test code requires this*/ |
nsheth | 3:7954489d9f8a | 54 | Admx200xDev admx200xDev; |
nsheth | 3:7954489d9f8a | 55 | |
nsheth | 3:7954489d9f8a | 56 | static int32_t SPIInit(spi_desc **pSpiDesc); |
Kjansen45 | 0:a74ad5b8dc7b | 57 | |
nsheth | 8:bd0b93e35392 | 58 | static int32_t ResetModule(); |
nsheth | 8:bd0b93e35392 | 59 | |
nsheth | 3:7954489d9f8a | 60 | /** |
nsheth | 8:bd0b93e35392 | 61 | * |
nsheth | 8:bd0b93e35392 | 62 | */ |
nsheth | 8:bd0b93e35392 | 63 | /** |
nsheth | 8:bd0b93e35392 | 64 | * @brief Function to initialize SPI |
nsheth | 8:bd0b93e35392 | 65 | * |
nsheth | 8:bd0b93e35392 | 66 | * @param pSpiDesc - Pointer to the spi descriptor |
nsheth | 8:bd0b93e35392 | 67 | * @return Returns 0 for success or error code. |
nsheth | 3:7954489d9f8a | 68 | */ |
nsheth | 3:7954489d9f8a | 69 | int32_t SPIInit(spi_desc **pSpiDesc) |
nsheth | 3:7954489d9f8a | 70 | { |
nsheth | 3:7954489d9f8a | 71 | int32_t status; |
Kjansen45 | 0:a74ad5b8dc7b | 72 | |
nsheth | 3:7954489d9f8a | 73 | // Init SPI extra parameters structure |
nsheth | 3:7954489d9f8a | 74 | mbed_spi_init_param spiInitExtraParams = {.spi_clk_pin = SPI_SCK, |
nsheth | 3:7954489d9f8a | 75 | .spi_miso_pin = SPI_MISO, |
nsheth | 3:7954489d9f8a | 76 | .spi_mosi_pin = SPI_MOSI}; |
nsheth | 3:7954489d9f8a | 77 | |
nsheth | 3:7954489d9f8a | 78 | spi_init_param spiInit = { |
nsheth | 3:7954489d9f8a | 79 | 25000000, // Max SPI Speed |
nsheth | 3:7954489d9f8a | 80 | SPI_SS, // Chip Select pin |
nsheth | 3:7954489d9f8a | 81 | SPI_MODE_0, |
nsheth | 3:7954489d9f8a | 82 | &spiInitExtraParams, // SPI extra configurations |
nsheth | 3:7954489d9f8a | 83 | }; |
nsheth | 3:7954489d9f8a | 84 | |
nsheth | 3:7954489d9f8a | 85 | status = spi_init(pSpiDesc, &spiInit); |
nsheth | 3:7954489d9f8a | 86 | |
nsheth | 3:7954489d9f8a | 87 | return status; |
Kjansen45 | 0:a74ad5b8dc7b | 88 | } |
Kjansen45 | 0:a74ad5b8dc7b | 89 | |
nsheth | 5:746e78113d7d | 90 | /** |
nsheth | 8:bd0b93e35392 | 91 | * Resets measurement |
nsheth | 5:746e78113d7d | 92 | */ |
nsheth | 8:bd0b93e35392 | 93 | int32_t ResetModule() |
nsheth | 5:746e78113d7d | 94 | { |
nsheth | 8:bd0b93e35392 | 95 | int32_t status = 0; |
nsheth | 8:bd0b93e35392 | 96 | uint8_t cmdId; |
nsheth | 8:bd0b93e35392 | 97 | uint16_t addr; |
nsheth | 8:bd0b93e35392 | 98 | uint32_t data; |
nsheth | 8:bd0b93e35392 | 99 | uint32_t statReg; |
nsheth | 5:746e78113d7d | 100 | |
nsheth | 8:bd0b93e35392 | 101 | Admx200xDev *pAdmx200x = &admx200xDev; |
nsheth | 5:746e78113d7d | 102 | |
nsheth | 8:bd0b93e35392 | 103 | /* Send reset command */ |
nsheth | 8:bd0b93e35392 | 104 | cmdId = CMD_RESET; |
nsheth | 8:bd0b93e35392 | 105 | addr = 0; |
nsheth | 8:bd0b93e35392 | 106 | data = 0; |
nsheth | 8:bd0b93e35392 | 107 | status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg); |
nsheth | 5:746e78113d7d | 108 | |
nsheth | 5:746e78113d7d | 109 | return status; |
nsheth | 5:746e78113d7d | 110 | } |
nsheth | 5:746e78113d7d | 111 | |
Kjansen45 | 0:a74ad5b8dc7b | 112 | int main() |
Kjansen45 | 0:a74ad5b8dc7b | 113 | { |
nsheth | 3:7954489d9f8a | 114 | int32_t status = 0; |
nsheth | 3:7954489d9f8a | 115 | static spi_desc spiDesc; |
nsheth | 5:746e78113d7d | 116 | float stdLoad = 51; |
nsheth | 5:746e78113d7d | 117 | float Xt = 0; |
nsheth | 5:746e78113d7d | 118 | CAL_TYPE calType; |
nsheth | 3:7954489d9f8a | 119 | spi_desc *pSpi = &spiDesc; |
nsheth | 3:7954489d9f8a | 120 | Admx200xDev *pADmx2001 = &admx200xDev; |
nsheth | 3:7954489d9f8a | 121 | status |= SPIInit(&pSpi); |
nsheth | 3:7954489d9f8a | 122 | status |= Admx200xInit(pADmx2001, pSpi); |
nsheth | 3:7954489d9f8a | 123 | |
nsheth | 3:7954489d9f8a | 124 | mdelay(100); |
nsheth | 3:7954489d9f8a | 125 | |
nsheth | 3:7954489d9f8a | 126 | /* Initialize the ADMX200x application */ |
nsheth | 3:7954489d9f8a | 127 | if (status != 0) |
nsheth | 3:7954489d9f8a | 128 | { |
nsheth | 8:bd0b93e35392 | 129 | ERROR_MSG("Setting up ADMX200X failed"); |
Kjansen45 | 0:a74ad5b8dc7b | 130 | } |
nsheth | 3:7954489d9f8a | 131 | else |
nsheth | 3:7954489d9f8a | 132 | { |
nsheth | 8:bd0b93e35392 | 133 | INFO_MSG("Resetting module"); |
nsheth | 8:bd0b93e35392 | 134 | status = ResetModule(); |
nsheth | 8:bd0b93e35392 | 135 | if (status != ADMX_STATUS_SUCCESS) |
nsheth | 8:bd0b93e35392 | 136 | { |
nsheth | 8:bd0b93e35392 | 137 | ERROR_MSG("Failed to reset module"); |
nsheth | 8:bd0b93e35392 | 138 | } |
nsheth | 8:bd0b93e35392 | 139 | else |
nsheth | 8:bd0b93e35392 | 140 | { |
nsheth | 8:bd0b93e35392 | 141 | INFO_MSG("------ Initialising measurement ------"); |
nsheth | 8:bd0b93e35392 | 142 | INFO_MSG("Setting test load to 1k ohms"); |
nsheth | 8:bd0b93e35392 | 143 | // Selects test load 3 - 1000 ohm resistor |
nsheth | 8:bd0b93e35392 | 144 | EnableMuxLine(3); |
nsheth | 8:bd0b93e35392 | 145 | // Performing measurement |
nsheth | 8:bd0b93e35392 | 146 | status = MeasureAndDisplay(); |
nsheth | 8:bd0b93e35392 | 147 | INFO_MSG("------ Measurement completed ------"); |
nsheth | 8:bd0b93e35392 | 148 | |
nsheth | 8:bd0b93e35392 | 149 | INFO_MSG("------ Starting Calibration ------"); |
nsheth | 8:bd0b93e35392 | 150 | INFO_MSG("Setting test load to open circuit"); |
nsheth | 8:bd0b93e35392 | 151 | // Open the terminals across the lcr meter |
nsheth | 8:bd0b93e35392 | 152 | EnableMuxLine(0); |
nsheth | 8:bd0b93e35392 | 153 | // Running open calibration |
nsheth | 8:bd0b93e35392 | 154 | calType = CAL_TYPE_OPEN; |
nsheth | 8:bd0b93e35392 | 155 | status = Calibrate(calType, stdLoad, Xt); |
nsheth | 8:bd0b93e35392 | 156 | INFO_MSG("------ Calibration completed ------"); |
nsheth | 8:bd0b93e35392 | 157 | } |
nsheth | 3:7954489d9f8a | 158 | } |
nsheth | 3:7954489d9f8a | 159 | |
nsheth | 3:7954489d9f8a | 160 | status = spi_remove(pSpi); |
nsheth | 3:7954489d9f8a | 161 | |
nsheth | 3:7954489d9f8a | 162 | return status; |
Kjansen45 | 0:a74ad5b8dc7b | 163 | } |