EVAL-AD593x Mbed Project.

Dependencies:   platform_drivers

Committer:
ssmith73
Date:
Tue Oct 29 21:42:57 2019 +0000
Revision:
4:953fb3148f25
Parent:
1:bd8aea0d51e0
Added AD593x AnalogDevices Lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ssmith73 1:bd8aea0d51e0 1 /*************************************************************************//**
ssmith73 1:bd8aea0d51e0 2 * @file main.cpp
ssmith73 1:bd8aea0d51e0 3 * @brief Main application code for AD5686 firmware example program
ssmith73 1:bd8aea0d51e0 4 * @author ssmith (sean.smith@analog.com)
ssmith73 1:bd8aea0d51e0 5 ******************************************************************************
ssmith73 1:bd8aea0d51e0 6 * Copyright (c) 2019 Analog Devices, Inc.
ssmith73 1:bd8aea0d51e0 7 *
ssmith73 1:bd8aea0d51e0 8 * All rights reserved.
ssmith73 1:bd8aea0d51e0 9 *
ssmith73 1:bd8aea0d51e0 10 * Redistribution and use in source and binary forms, with or without
ssmith73 1:bd8aea0d51e0 11 * modification, are permitted provided that the following conditions are met:
ssmith73 1:bd8aea0d51e0 12 * - Redistributions of source code must retain the above copyright notice,
ssmith73 1:bd8aea0d51e0 13 * this list of conditions and the following disclaimer.
ssmith73 1:bd8aea0d51e0 14 * - Redistributions in binary form must reproduce the above copyright notice,
ssmith73 1:bd8aea0d51e0 15 * this list of conditions and the following disclaimer in the documentation
ssmith73 1:bd8aea0d51e0 16 * and/or other materials provided with the distribution.
ssmith73 1:bd8aea0d51e0 17 * - Modified versions of the software must be conspicuously marked as such.
ssmith73 1:bd8aea0d51e0 18 * - This software is licensed solely and exclusively for use with
ssmith73 1:bd8aea0d51e0 19 * processors/products manufactured by or for Analog Devices, Inc.
ssmith73 1:bd8aea0d51e0 20 * - This software may not be combined or merged with other code in any manner
ssmith73 1:bd8aea0d51e0 21 * that would cause the software to become subject to terms and
ssmith73 1:bd8aea0d51e0 22 * conditions which differ from those listed here.
ssmith73 1:bd8aea0d51e0 23 * - Neither the name of Analog Devices, Inc. nor the names of its
ssmith73 1:bd8aea0d51e0 24 * contributors may be used to endorse or promote products derived
ssmith73 1:bd8aea0d51e0 25 * from this software without specific prior written permission.
ssmith73 1:bd8aea0d51e0 26 * - The use of this software may or may not infringe the patent rights
ssmith73 1:bd8aea0d51e0 27 * of one or more patent holders. This license does not release you from
ssmith73 1:bd8aea0d51e0 28 * the requirement that you obtain separate licenses from these patent
ssmith73 1:bd8aea0d51e0 29 * holders to use this software.
ssmith73 1:bd8aea0d51e0 30 *
ssmith73 1:bd8aea0d51e0 31 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS"
ssmith73 1:bd8aea0d51e0 32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ssmith73 1:bd8aea0d51e0 33 * NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A
ssmith73 1:bd8aea0d51e0 34 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES,
ssmith73 1:bd8aea0d51e0 35 * INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ssmith73 1:bd8aea0d51e0 36 * SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
ssmith73 1:bd8aea0d51e0 37 * (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF
ssmith73 1:bd8aea0d51e0 38 * INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE
ssmith73 1:bd8aea0d51e0 39 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ssmith73 1:bd8aea0d51e0 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
ssmith73 1:bd8aea0d51e0 41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
ssmith73 1:bd8aea0d51e0 42 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
ssmith73 1:bd8aea0d51e0 43 * POSSIBILITY OF SUCH DAMAGE.
ssmith73 1:bd8aea0d51e0 44 *
ssmith73 1:bd8aea0d51e0 45 * 20180927-7CBSD SLA
ssmith73 1:bd8aea0d51e0 46 *****************************************************************************/
malavikasaji 0:86671e9c8db0 47
ssmith73 1:bd8aea0d51e0 48 #include <mbed.h>
ssmith73 1:bd8aea0d51e0 49 #include <ctype.h>
ssmith73 1:bd8aea0d51e0 50 #include "app_config.h"
ssmith73 1:bd8aea0d51e0 51
ssmith73 1:bd8aea0d51e0 52 #define NOT_USED 0
ssmith73 1:bd8aea0d51e0 53 //Lower this value if storage becomes a problem
ssmith73 1:bd8aea0d51e0 54 #define MAX_FREQ_INCREMENTS 511
ssmith73 1:bd8aea0d51e0 55 #define TEMP_LIMIT_MIN -40
ssmith73 1:bd8aea0d51e0 56 #define TEMP_LIMIT_MAX 125
ssmith73 1:bd8aea0d51e0 57
malavikasaji 0:86671e9c8db0 58
ssmith73 1:bd8aea0d51e0 59 static void print_title(void);
ssmith73 1:bd8aea0d51e0 60 static void getMenuSelect(uint8_t *menuSelect);
ssmith73 1:bd8aea0d51e0 61 static void print_prompt();
ssmith73 1:bd8aea0d51e0 62 static uint8_t read_temperature();
ssmith73 1:bd8aea0d51e0 63 static uint8_t set_system_clock();
ssmith73 1:bd8aea0d51e0 64 static uint8_t set_vrange_and_pga_gain();
ssmith73 1:bd8aea0d51e0 65 static int32_t configure_system();
ssmith73 1:bd8aea0d51e0 66 static uint8_t calculate_gain_factor();
ssmith73 1:bd8aea0d51e0 67 static uint8_t guide();
ssmith73 1:bd8aea0d51e0 68 static uint8_t impedance_sweep();
malavikasaji 0:86671e9c8db0 69
ssmith73 1:bd8aea0d51e0 70 typedef struct ad5933_config_data {
ssmith73 1:bd8aea0d51e0 71 float start_freq;
ssmith73 1:bd8aea0d51e0 72 uint8_t pga_gain;
ssmith73 1:bd8aea0d51e0 73 float output_voltage_range;
ssmith73 1:bd8aea0d51e0 74 int32_t start_frequency;
ssmith73 1:bd8aea0d51e0 75 int32_t frequency_increment;
ssmith73 1:bd8aea0d51e0 76 int32_t number_increments;
ssmith73 1:bd8aea0d51e0 77 int32_t number_settling_cycles;
ssmith73 1:bd8aea0d51e0 78 }ad5933_config_data;
malavikasaji 0:86671e9c8db0 79
ssmith73 1:bd8aea0d51e0 80 ad5933_config_data config_data;
ssmith73 1:bd8aea0d51e0 81
ssmith73 1:bd8aea0d51e0 82 static double gain_factor = 0;
ssmith73 1:bd8aea0d51e0 83 static double temperature;
ssmith73 1:bd8aea0d51e0 84
ssmith73 1:bd8aea0d51e0 85
ssmith73 1:bd8aea0d51e0 86 /******************************************************************************/
ssmith73 1:bd8aea0d51e0 87 /************************** Variables Declarations ****************************/
ssmith73 1:bd8aea0d51e0 88 /******************************************************************************/
malavikasaji 0:86671e9c8db0 89
ssmith73 1:bd8aea0d51e0 90 /**
ssmith73 1:bd8aea0d51e0 91 The following definitions are a requirement for the platform_driver
ssmith73 1:bd8aea0d51e0 92 Pin are changed if required in the app_config.h file
ssmith73 1:bd8aea0d51e0 93 ***/
ssmith73 1:bd8aea0d51e0 94 mbed::SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
ssmith73 1:bd8aea0d51e0 95 mbed::I2C i2c(I2C_SDA, I2C_SCL);
ssmith73 1:bd8aea0d51e0 96 PinName slave_selects[MAX_SLAVE_SELECTS];
malavikasaji 0:86671e9c8db0 97
ssmith73 1:bd8aea0d51e0 98 i2c_init_param i2c_params =
ssmith73 1:bd8aea0d51e0 99 {
ssmith73 1:bd8aea0d51e0 100 GENERIC_I2C, // i2c type
ssmith73 1:bd8aea0d51e0 101 0, // i2c id
ssmith73 1:bd8aea0d51e0 102 100000, // i2c max speed (hz)
ssmith73 1:bd8aea0d51e0 103 AD5933_ADDRESS << 1, // i2c slave address //A0 tied high
ssmith73 1:bd8aea0d51e0 104 };
malavikasaji 0:86671e9c8db0 105
malavikasaji 0:86671e9c8db0 106
ssmith73 1:bd8aea0d51e0 107 ad5933_init_param init_params = {
ssmith73 1:bd8aea0d51e0 108 i2c_params, // I2C parameters
ssmith73 1:bd8aea0d51e0 109 AD5933_INTERNAL_SYS_CLK, //current_sys_clk frequency (16MHz)
ssmith73 1:bd8aea0d51e0 110 AD5933_CONTROL_INT_SYSCLK, //current_clock_source
ssmith73 1:bd8aea0d51e0 111 AD5933_RANGE_1000mVpp, //current_gain
ssmith73 1:bd8aea0d51e0 112 AD5933_GAIN_X1, //current_range
ssmith73 1:bd8aea0d51e0 113 AD5933_15_CYCLES, //current_settling
ssmith73 1:bd8aea0d51e0 114 };
ssmith73 1:bd8aea0d51e0 115
ssmith73 1:bd8aea0d51e0 116 ad5933_dev *device;
ssmith73 1:bd8aea0d51e0 117 int32_t connected = -1;
ssmith73 1:bd8aea0d51e0 118 Serial pc(USBTX, USBRX, 115200);
ssmith73 1:bd8aea0d51e0 119
malavikasaji 0:86671e9c8db0 120
malavikasaji 0:86671e9c8db0 121 int main()
malavikasaji 0:86671e9c8db0 122 {
ssmith73 1:bd8aea0d51e0 123 uint8_t menu_select = 0;
ssmith73 1:bd8aea0d51e0 124 print_title();
ssmith73 1:bd8aea0d51e0 125 connected = ad5933_init(&device, init_params);
malavikasaji 0:86671e9c8db0 126
malavikasaji 0:86671e9c8db0 127
ssmith73 1:bd8aea0d51e0 128 //Do a quick check to ensure basic connectivity is ok
ssmith73 1:bd8aea0d51e0 129 temperature = ad5933_get_temperature(device);
ssmith73 1:bd8aea0d51e0 130 if (temperature >= TEMP_LIMIT_MIN && temperature <= TEMP_LIMIT_MAX) {
ssmith73 1:bd8aea0d51e0 131 pc.printf("\nTemperature: %f, AD5933 initialisation successful!\n",temperature);
ssmith73 1:bd8aea0d51e0 132 }
ssmith73 1:bd8aea0d51e0 133 else {
ssmith73 1:bd8aea0d51e0 134 pc.printf("AD5933 initialisation reported a bad temperature - recommend debug :\n");
ssmith73 1:bd8aea0d51e0 135 }
ssmith73 1:bd8aea0d51e0 136
ssmith73 1:bd8aea0d51e0 137 while (connected == SUCCESS) {
ssmith73 1:bd8aea0d51e0 138 print_prompt();
ssmith73 1:bd8aea0d51e0 139 getMenuSelect(&menu_select);
ssmith73 1:bd8aea0d51e0 140 config_data.start_freq = 10000;
malavikasaji 0:86671e9c8db0 141
ssmith73 1:bd8aea0d51e0 142 if (menu_select > 12)
ssmith73 1:bd8aea0d51e0 143 print_prompt();
ssmith73 1:bd8aea0d51e0 144 else switch (menu_select)
ssmith73 1:bd8aea0d51e0 145 {
ssmith73 1:bd8aea0d51e0 146 case 0:
ssmith73 1:bd8aea0d51e0 147 guide();
ssmith73 1:bd8aea0d51e0 148 wait(2);
ssmith73 1:bd8aea0d51e0 149 break;
malavikasaji 0:86671e9c8db0 150
ssmith73 1:bd8aea0d51e0 151 case 1:
ssmith73 1:bd8aea0d51e0 152 read_temperature();
ssmith73 1:bd8aea0d51e0 153 break;
ssmith73 1:bd8aea0d51e0 154 case 2:
ssmith73 1:bd8aea0d51e0 155 configure_system();
ssmith73 1:bd8aea0d51e0 156 break;
ssmith73 1:bd8aea0d51e0 157 case 3:
ssmith73 1:bd8aea0d51e0 158 calculate_gain_factor();
ssmith73 1:bd8aea0d51e0 159 break;
ssmith73 1:bd8aea0d51e0 160 case 4:
ssmith73 1:bd8aea0d51e0 161 impedance_sweep();
ssmith73 1:bd8aea0d51e0 162 break;
malavikasaji 0:86671e9c8db0 163
ssmith73 1:bd8aea0d51e0 164 default:
ssmith73 1:bd8aea0d51e0 165 pc.printf("Invalid option: Ignored.");
ssmith73 1:bd8aea0d51e0 166 break;
ssmith73 1:bd8aea0d51e0 167 }
ssmith73 1:bd8aea0d51e0 168
ssmith73 1:bd8aea0d51e0 169 wait(0.1);
ssmith73 1:bd8aea0d51e0 170 }
ssmith73 1:bd8aea0d51e0 171
ssmith73 1:bd8aea0d51e0 172 return 0;
malavikasaji 0:86671e9c8db0 173 }
malavikasaji 0:86671e9c8db0 174
ssmith73 1:bd8aea0d51e0 175 //! Prints the title block
ssmith73 1:bd8aea0d51e0 176 void print_title()
ssmith73 1:bd8aea0d51e0 177 {
ssmith73 1:bd8aea0d51e0 178 pc.printf("*****************************************************************\n");
ssmith73 1:bd8aea0d51e0 179 pc.printf("* AD5933 Demonstration Program -- (mbed 1.0) *\n");
ssmith73 1:bd8aea0d51e0 180 pc.printf("* *\n");
ssmith73 1:bd8aea0d51e0 181 pc.printf("* This program demonstrates communication with the AD5933 *\n");
ssmith73 1:bd8aea0d51e0 182 pc.printf("* *\n");
ssmith73 1:bd8aea0d51e0 183 pc.printf("* 1 MSPS, 12-Bit Impedance Converter, Network analyser *\n");
ssmith73 1:bd8aea0d51e0 184 pc.printf("* *\n");
ssmith73 1:bd8aea0d51e0 185 pc.printf("* Set the baud rate to 115200 select the newline terminator. *\n");
ssmith73 1:bd8aea0d51e0 186 pc.printf("*****************************************************************\n");
ssmith73 1:bd8aea0d51e0 187 }
malavikasaji 0:86671e9c8db0 188
ssmith73 1:bd8aea0d51e0 189 void print_prompt()
malavikasaji 0:86671e9c8db0 190 {
ssmith73 1:bd8aea0d51e0 191 pc.printf("\n\n\rCommand Summary:\n\n");
ssmith73 1:bd8aea0d51e0 192 pc.printf(" 0 -Software Guide\n");
ssmith73 1:bd8aea0d51e0 193 pc.printf(" 1 -Read temperature\n");
ssmith73 1:bd8aea0d51e0 194 pc.printf(" 2 -Configure voltage-range, PGA-Gain and sweep parameters\n");
ssmith73 1:bd8aea0d51e0 195 pc.printf(" 3 -Calculate Gain-Factor\n");
ssmith73 1:bd8aea0d51e0 196 pc.printf(" 4 -Do an impedance sweep\n");
ssmith73 1:bd8aea0d51e0 197 pc.printf("\n\rMake a selection...\n");
ssmith73 1:bd8aea0d51e0 198
malavikasaji 0:86671e9c8db0 199 }
malavikasaji 0:86671e9c8db0 200
malavikasaji 0:86671e9c8db0 201
ssmith73 1:bd8aea0d51e0 202 static void getMenuSelect(uint8_t *menuSelect) {
ssmith73 1:bd8aea0d51e0 203 pc.scanf("%d", (int *)menuSelect);
ssmith73 1:bd8aea0d51e0 204 }
ssmith73 1:bd8aea0d51e0 205
ssmith73 1:bd8aea0d51e0 206 static uint8_t read_temperature()
ssmith73 1:bd8aea0d51e0 207 {
ssmith73 1:bd8aea0d51e0 208 temperature = ad5933_get_temperature(device);
ssmith73 1:bd8aea0d51e0 209
ssmith73 1:bd8aea0d51e0 210 pc.printf("Current temperature:%.3f C", temperature);
ssmith73 1:bd8aea0d51e0 211 return SUCCESS;
ssmith73 1:bd8aea0d51e0 212 }
ssmith73 1:bd8aea0d51e0 213
ssmith73 1:bd8aea0d51e0 214 static uint8_t set_system_clock()
ssmith73 1:bd8aea0d51e0 215 {
ssmith73 1:bd8aea0d51e0 216 pc.printf(" Select Internal (1) or external clock (2): ");
ssmith73 1:bd8aea0d51e0 217
ssmith73 1:bd8aea0d51e0 218 int input = 0;
ssmith73 1:bd8aea0d51e0 219 pc.scanf("%d", &input);
ssmith73 1:bd8aea0d51e0 220 if (isdigit(input) == 0 && (input == 1 || input == 2))
ssmith73 1:bd8aea0d51e0 221 {
ssmith73 1:bd8aea0d51e0 222 input == 1 ? pc.printf("\n You selected Internal clock source\n") :
ssmith73 1:bd8aea0d51e0 223 pc.printf(" You selected external Source clock source\n");
ssmith73 1:bd8aea0d51e0 224 }
ssmith73 1:bd8aea0d51e0 225 else
ssmith73 1:bd8aea0d51e0 226 {
ssmith73 1:bd8aea0d51e0 227 pc.printf("Invalid entry\n");
ssmith73 1:bd8aea0d51e0 228 wait(2);
ssmith73 1:bd8aea0d51e0 229 return FAILURE;
ssmith73 1:bd8aea0d51e0 230 }
ssmith73 1:bd8aea0d51e0 231
ssmith73 1:bd8aea0d51e0 232 if (input == 2)
ssmith73 1:bd8aea0d51e0 233 {
ssmith73 1:bd8aea0d51e0 234
ssmith73 1:bd8aea0d51e0 235 pc.printf(" Enter external clock frequency in Hz ");
ssmith73 1:bd8aea0d51e0 236 pc.scanf("%d", &input);
ssmith73 1:bd8aea0d51e0 237 if (isdigit(input) == 0 && input > 0 && input < 20000000)
ssmith73 1:bd8aea0d51e0 238 {
ssmith73 1:bd8aea0d51e0 239 pc.printf(" External clk-source frequency set to %d \n", input);
ssmith73 1:bd8aea0d51e0 240 }
ssmith73 1:bd8aea0d51e0 241 else
ssmith73 1:bd8aea0d51e0 242 {
ssmith73 1:bd8aea0d51e0 243 pc.printf("Invalid entry\n");
ssmith73 1:bd8aea0d51e0 244 wait(2);
ssmith73 1:bd8aea0d51e0 245 return FAILURE;
ssmith73 1:bd8aea0d51e0 246 }
ssmith73 1:bd8aea0d51e0 247 }
ssmith73 1:bd8aea0d51e0 248
ssmith73 1:bd8aea0d51e0 249 ad5933_set_system_clk(device,
ssmith73 1:bd8aea0d51e0 250 input == 1 ? AD5933_CONTROL_INT_SYSCLK :
ssmith73 1:bd8aea0d51e0 251 AD5933_CONTROL_EXT_SYSCLK,
ssmith73 1:bd8aea0d51e0 252 input);
ssmith73 1:bd8aea0d51e0 253
ssmith73 1:bd8aea0d51e0 254 return 0;
ssmith73 1:bd8aea0d51e0 255 }
ssmith73 1:bd8aea0d51e0 256
ssmith73 1:bd8aea0d51e0 257 static uint8_t set_vrange_and_pga_gain()
malavikasaji 0:86671e9c8db0 258 {
ssmith73 1:bd8aea0d51e0 259 int input;
ssmith73 1:bd8aea0d51e0 260 uint8_t v_range = AD5933_RANGE_1000mVpp;
ssmith73 1:bd8aea0d51e0 261
ssmith73 1:bd8aea0d51e0 262 pc.printf(" Select output voltage range:\n");
ssmith73 1:bd8aea0d51e0 263 pc.printf(" 0: 2Vpp typical:\n");
ssmith73 1:bd8aea0d51e0 264 pc.printf(" 1: 200mVpp typical:\n");
ssmith73 1:bd8aea0d51e0 265 pc.printf(" 2: 400mVpp typical:\n");
ssmith73 1:bd8aea0d51e0 266 pc.printf(" 3: 1Vpp typical:\n");
ssmith73 1:bd8aea0d51e0 267
ssmith73 1:bd8aea0d51e0 268
ssmith73 1:bd8aea0d51e0 269 pc.scanf("%d", &input);
ssmith73 1:bd8aea0d51e0 270 if (input >= 0 && input < 4)
ssmith73 1:bd8aea0d51e0 271 {
ssmith73 1:bd8aea0d51e0 272 switch (input)
ssmith73 1:bd8aea0d51e0 273 {
ssmith73 1:bd8aea0d51e0 274 case AD5933_RANGE_2000mVpp: {
ssmith73 1:bd8aea0d51e0 275 pc.printf(" Selected 2V pp typical.\n");
ssmith73 1:bd8aea0d51e0 276 break;
ssmith73 1:bd8aea0d51e0 277 }
ssmith73 1:bd8aea0d51e0 278 case AD5933_RANGE_200mVpp: {
ssmith73 1:bd8aea0d51e0 279 pc.printf(" Selected 200mV pp typical.\n");
ssmith73 1:bd8aea0d51e0 280 break;
ssmith73 1:bd8aea0d51e0 281 }
ssmith73 1:bd8aea0d51e0 282 case AD5933_RANGE_400mVpp: {
ssmith73 1:bd8aea0d51e0 283 pc.printf(" Selected 400mV pp typical.\n");
ssmith73 1:bd8aea0d51e0 284 break;
ssmith73 1:bd8aea0d51e0 285 }
ssmith73 1:bd8aea0d51e0 286 case AD5933_RANGE_1000mVpp: {
ssmith73 1:bd8aea0d51e0 287 pc.printf(" Selected 1V pp typical.\n");
ssmith73 1:bd8aea0d51e0 288 break;
ssmith73 1:bd8aea0d51e0 289 }
ssmith73 1:bd8aea0d51e0 290 }
ssmith73 1:bd8aea0d51e0 291 v_range = input;
ssmith73 1:bd8aea0d51e0 292 }
ssmith73 1:bd8aea0d51e0 293 else
ssmith73 1:bd8aea0d51e0 294 {
ssmith73 1:bd8aea0d51e0 295 pc.printf("Invalid entry\n");
ssmith73 1:bd8aea0d51e0 296 wait(2);
ssmith73 1:bd8aea0d51e0 297 return FAILURE;
ssmith73 1:bd8aea0d51e0 298 }
ssmith73 1:bd8aea0d51e0 299
ssmith73 1:bd8aea0d51e0 300 pc.printf("\n Select PGA Gain (0=X5, 1=X1)\n");
ssmith73 1:bd8aea0d51e0 301 pc.scanf("%d", &input);
ssmith73 1:bd8aea0d51e0 302 if (input >= 0 && input < 2)
ssmith73 1:bd8aea0d51e0 303 {
ssmith73 1:bd8aea0d51e0 304 config_data.pga_gain = input;
ssmith73 1:bd8aea0d51e0 305 config_data.output_voltage_range = v_range;
ssmith73 1:bd8aea0d51e0 306
ssmith73 1:bd8aea0d51e0 307 pc.printf("PGA gain set to : ");
ssmith73 1:bd8aea0d51e0 308 input == AD5933_GAIN_X5 ? printf("X5\n\n") : printf("X1\n\n");
ssmith73 1:bd8aea0d51e0 309 ad5933_set_range_and_gain(device,
ssmith73 1:bd8aea0d51e0 310 config_data.output_voltage_range,
ssmith73 1:bd8aea0d51e0 311 config_data.pga_gain);
ssmith73 1:bd8aea0d51e0 312 }
ssmith73 1:bd8aea0d51e0 313 else
ssmith73 1:bd8aea0d51e0 314 {
ssmith73 1:bd8aea0d51e0 315 pc.printf("Invalid entry: write aborted\n");
ssmith73 1:bd8aea0d51e0 316 wait(2);
ssmith73 1:bd8aea0d51e0 317 return FAILURE;
ssmith73 1:bd8aea0d51e0 318 }
ssmith73 1:bd8aea0d51e0 319
ssmith73 1:bd8aea0d51e0 320
ssmith73 1:bd8aea0d51e0 321 return SUCCESS;
malavikasaji 0:86671e9c8db0 322 }
malavikasaji 0:86671e9c8db0 323
ssmith73 1:bd8aea0d51e0 324 static int32_t configure_system()
ssmith73 1:bd8aea0d51e0 325 {
ssmith73 1:bd8aea0d51e0 326
ssmith73 1:bd8aea0d51e0 327 pc.printf("Configure the impedance meter\n\n");
ssmith73 1:bd8aea0d51e0 328 set_vrange_and_pga_gain();
ssmith73 1:bd8aea0d51e0 329 set_system_clock();
ssmith73 1:bd8aea0d51e0 330
ssmith73 1:bd8aea0d51e0 331 int start_freq;
ssmith73 1:bd8aea0d51e0 332 int freq_inc;
ssmith73 1:bd8aea0d51e0 333 int num_increments;
ssmith73 1:bd8aea0d51e0 334 int num_settling_cycles;
ssmith73 1:bd8aea0d51e0 335 int multiplier = AD5933_SETTLING_X1;
ssmith73 1:bd8aea0d51e0 336
ssmith73 1:bd8aea0d51e0 337 pc.printf("\n Enter start-frequency as a decimal number: ");
ssmith73 1:bd8aea0d51e0 338 if (pc.scanf("%d", &start_freq) == 1)
ssmith73 1:bd8aea0d51e0 339 {
ssmith73 1:bd8aea0d51e0 340 if (start_freq <= 0)
ssmith73 1:bd8aea0d51e0 341 {
ssmith73 1:bd8aea0d51e0 342 pc.printf(" Invalid entry, write aborted: \n");
ssmith73 1:bd8aea0d51e0 343 return FAILURE;
ssmith73 1:bd8aea0d51e0 344 }
ssmith73 1:bd8aea0d51e0 345 }
ssmith73 1:bd8aea0d51e0 346
ssmith73 1:bd8aea0d51e0 347
ssmith73 1:bd8aea0d51e0 348 pc.printf("\n Enter frequency-increment as a decimal number: ");
ssmith73 1:bd8aea0d51e0 349 pc.scanf("%d", &freq_inc);
ssmith73 1:bd8aea0d51e0 350 if (isdigit(freq_inc) != 0 || freq_inc <= 0)
ssmith73 1:bd8aea0d51e0 351 {
ssmith73 1:bd8aea0d51e0 352 pc.printf(" Invalid entry, write aborted: \n");
ssmith73 1:bd8aea0d51e0 353 return FAILURE;
ssmith73 1:bd8aea0d51e0 354 }
ssmith73 1:bd8aea0d51e0 355
ssmith73 1:bd8aea0d51e0 356 pc.printf("\n Enter the number of increments as a decimal number: ");
ssmith73 1:bd8aea0d51e0 357 pc.printf("\n Number of increments must be less than %d\n", MAX_FREQ_INCREMENTS);
ssmith73 1:bd8aea0d51e0 358 pc.scanf("%d", &num_increments);
ssmith73 1:bd8aea0d51e0 359 if (isdigit(num_increments) != 0 || num_increments > MAX_FREQ_INCREMENTS)
ssmith73 1:bd8aea0d51e0 360 {
ssmith73 1:bd8aea0d51e0 361 pc.printf(" Invalid entry, write aborted: \n");
ssmith73 1:bd8aea0d51e0 362 return FAILURE;
ssmith73 1:bd8aea0d51e0 363 }
ssmith73 1:bd8aea0d51e0 364
ssmith73 1:bd8aea0d51e0 365 pc.printf("Enter the number of settling-time cycles before ADC is triggered.\n");
ssmith73 1:bd8aea0d51e0 366 pc.scanf("%d", &num_settling_cycles);
ssmith73 1:bd8aea0d51e0 367 if (num_settling_cycles > AD5933_MAX_SETTLING_CYCLES )
ssmith73 1:bd8aea0d51e0 368 {
ssmith73 1:bd8aea0d51e0 369 pc.printf(" Invalid entry, write aborted: \n");
ssmith73 1:bd8aea0d51e0 370 return FAILURE;
ssmith73 1:bd8aea0d51e0 371 }
ssmith73 1:bd8aea0d51e0 372
ssmith73 1:bd8aea0d51e0 373 pc.printf("Set the settling time multiplier (X1=0, X2=1, X4=2).\n");
ssmith73 1:bd8aea0d51e0 374 pc.scanf("%d", &multiplier);
ssmith73 1:bd8aea0d51e0 375 if (multiplier > 2)
ssmith73 1:bd8aea0d51e0 376 {
ssmith73 1:bd8aea0d51e0 377 pc.printf(" Invalid entry, write aborted: \n");
ssmith73 1:bd8aea0d51e0 378 return FAILURE;
ssmith73 1:bd8aea0d51e0 379 }
ssmith73 1:bd8aea0d51e0 380 else
ssmith73 1:bd8aea0d51e0 381 { //adjust X4 option to match memory map
ssmith73 1:bd8aea0d51e0 382 if (multiplier == 2)
ssmith73 1:bd8aea0d51e0 383 multiplier = AD5933_SETTLING_X4;
ssmith73 1:bd8aea0d51e0 384 }
ssmith73 1:bd8aea0d51e0 385
ssmith73 1:bd8aea0d51e0 386 pc.printf("\n Setting start frequency to %d\n\r", (unsigned int)start_freq);
ssmith73 1:bd8aea0d51e0 387 pc.printf(" Setting frequency increment to %d\n\r", (unsigned int)freq_inc);
ssmith73 1:bd8aea0d51e0 388 pc.printf(" Setting the number of increments to %d\n\r", (unsigned int)num_increments);
ssmith73 1:bd8aea0d51e0 389 pc.printf(" Setting the number of settling-cycles to %d\n\r", (unsigned int)num_settling_cycles);
ssmith73 1:bd8aea0d51e0 390 pc.printf(" The multiplier for the settling-cycles %d\n\r", (unsigned int)multiplier+1);
ssmith73 1:bd8aea0d51e0 391
ssmith73 1:bd8aea0d51e0 392 //update device state
ssmith73 1:bd8aea0d51e0 393 config_data.start_freq = start_freq;
ssmith73 1:bd8aea0d51e0 394 config_data.frequency_increment = freq_inc;
ssmith73 1:bd8aea0d51e0 395 config_data.number_increments = num_increments;
ssmith73 1:bd8aea0d51e0 396 config_data.number_settling_cycles = num_settling_cycles;
ssmith73 1:bd8aea0d51e0 397
ssmith73 1:bd8aea0d51e0 398 ad5933_set_settling_time(device,multiplier,num_settling_cycles);
ssmith73 1:bd8aea0d51e0 399 ad5933_set_range_and_gain(device, device->current_range, device->current_gain);
ssmith73 1:bd8aea0d51e0 400 ad5933_config_sweep(device, start_freq, freq_inc, num_increments);
ssmith73 1:bd8aea0d51e0 401
ssmith73 1:bd8aea0d51e0 402 return SUCCESS;
ssmith73 1:bd8aea0d51e0 403
ssmith73 1:bd8aea0d51e0 404 }
ssmith73 1:bd8aea0d51e0 405
ssmith73 1:bd8aea0d51e0 406 static uint8_t calculate_gain_factor()
ssmith73 1:bd8aea0d51e0 407 {
ssmith73 1:bd8aea0d51e0 408
ssmith73 1:bd8aea0d51e0 409 double calibration_impedance;
ssmith73 1:bd8aea0d51e0 410
ssmith73 1:bd8aea0d51e0 411 pc.printf("\n\nCalculate the gain-factor (see datasheet for information)\n");
ssmith73 1:bd8aea0d51e0 412 pc.printf("Calcualted gain-factor will be stored for impedance measurements and\n");
ssmith73 1:bd8aea0d51e0 413 pc.printf("displayed on the terminal screen.\n");
ssmith73 1:bd8aea0d51e0 414 pc.printf("Ensure that the system has been configured before\n");
ssmith73 1:bd8aea0d51e0 415 pc.printf("calculating the gain factor\n");
ssmith73 1:bd8aea0d51e0 416
ssmith73 1:bd8aea0d51e0 417 ad5933_config_sweep(device,
ssmith73 1:bd8aea0d51e0 418 config_data.start_freq,
ssmith73 1:bd8aea0d51e0 419 config_data.frequency_increment,
ssmith73 1:bd8aea0d51e0 420 config_data.number_increments);
ssmith73 1:bd8aea0d51e0 421
ssmith73 1:bd8aea0d51e0 422 // Do standby, init-start freq, start the sweep, and wait for valid data
ssmith73 1:bd8aea0d51e0 423 ad5933_start_sweep(device);
ssmith73 1:bd8aea0d51e0 424
ssmith73 1:bd8aea0d51e0 425 pc.printf("\nEnter calibration resistance in Ohms: ");
ssmith73 1:bd8aea0d51e0 426 pc.scanf("%le", &calibration_impedance);
ssmith73 1:bd8aea0d51e0 427
ssmith73 1:bd8aea0d51e0 428
ssmith73 1:bd8aea0d51e0 429 pc.printf("Calculating gain factor\n\r");
ssmith73 1:bd8aea0d51e0 430
ssmith73 1:bd8aea0d51e0 431 gain_factor = ad5933_calculate_gain_factor(device,
ssmith73 1:bd8aea0d51e0 432 calibration_impedance,
ssmith73 1:bd8aea0d51e0 433 AD5933_FUNCTION_REPEAT_FREQ);
ssmith73 1:bd8aea0d51e0 434 pc.printf("\n\r Calculated gain factor %e\n\r", gain_factor);
ssmith73 1:bd8aea0d51e0 435
ssmith73 1:bd8aea0d51e0 436
ssmith73 1:bd8aea0d51e0 437 return SUCCESS;
ssmith73 1:bd8aea0d51e0 438 }
ssmith73 1:bd8aea0d51e0 439
ssmith73 1:bd8aea0d51e0 440
ssmith73 1:bd8aea0d51e0 441
ssmith73 1:bd8aea0d51e0 442
ssmith73 1:bd8aea0d51e0 443 static uint8_t guide()
ssmith73 1:bd8aea0d51e0 444 {
ssmith73 1:bd8aea0d51e0 445
ssmith73 1:bd8aea0d51e0 446 pc.printf("\n\rAD5933-Demo quick-start guide: \n\n");
ssmith73 1:bd8aea0d51e0 447 pc.printf("This program can be used both as a demo of the AD5933 impedance \n");
ssmith73 1:bd8aea0d51e0 448 pc.printf("measurement system and as a starting point for developing a \n");
ssmith73 1:bd8aea0d51e0 449 pc.printf("more advanced program for prototyping. This program is not \n");
ssmith73 1:bd8aea0d51e0 450 pc.printf("provided as production-quality code, but as a helpful starting point.\n\n");
ssmith73 1:bd8aea0d51e0 451
ssmith73 1:bd8aea0d51e0 452 pc.printf("As a quick start, the following steps can be implemented to ensure\n");
ssmith73 1:bd8aea0d51e0 453 pc.printf("firmware is communcating with the board and measurements taking place.\n\n");
ssmith73 1:bd8aea0d51e0 454
ssmith73 1:bd8aea0d51e0 455 pc.printf("Firstly - use menu option 1 to read the on-chip temperature.\n");
ssmith73 1:bd8aea0d51e0 456 pc.printf("If a realistic temperature comes back - you are good to go :)\n\n");
ssmith73 1:bd8aea0d51e0 457
ssmith73 1:bd8aea0d51e0 458 pc.printf("Step 1\tConnect a 200k Resistor across the SMA terminals of the PMOD 1A\n");
ssmith73 1:bd8aea0d51e0 459 pc.printf("Step 2\tSelect the 100k feedback resistor by pulling the SEL pin high\n");
ssmith73 1:bd8aea0d51e0 460 pc.printf("Step 2\tConfigure the impedance system with Menu Option 2\n");
ssmith73 1:bd8aea0d51e0 461 pc.printf("Step 3\tCalculate the gain factor with menu-item 3\n");
ssmith73 1:bd8aea0d51e0 462 pc.printf("Step 3\tReplace the 200k impedance across the SMA terminals with a \n");
ssmith73 1:bd8aea0d51e0 463 pc.printf("different 'unknown' impedance (300K perhaps)\n");
ssmith73 1:bd8aea0d51e0 464 pc.printf("Step 4\tRun the impedance measurement with menu-item 4\n");
ssmith73 1:bd8aea0d51e0 465 pc.printf("\tresults are dispayed on the terminal\n");
ssmith73 1:bd8aea0d51e0 466
ssmith73 1:bd8aea0d51e0 467 return SUCCESS;
malavikasaji 0:86671e9c8db0 468 }
malavikasaji 0:86671e9c8db0 469
malavikasaji 0:86671e9c8db0 470
ssmith73 1:bd8aea0d51e0 471
ssmith73 1:bd8aea0d51e0 472 static uint8_t impedance_sweep() {
ssmith73 1:bd8aea0d51e0 473
ssmith73 1:bd8aea0d51e0 474 ad5933_result results;
ssmith73 1:bd8aea0d51e0 475
ssmith73 1:bd8aea0d51e0 476 pc.printf("\nPerform a sweep to calculate an unknown impedance (see datasheet for information)\n");
ssmith73 1:bd8aea0d51e0 477 pc.printf("System should have been previously configured (Menu Option 2)\n");
ssmith73 1:bd8aea0d51e0 478 pc.printf("Impedance will be caculated and results shown.\n\r");
ssmith73 1:bd8aea0d51e0 479
ssmith73 1:bd8aea0d51e0 480 int32_t status = FAILURE;
ssmith73 1:bd8aea0d51e0 481 double impedance;
ssmith73 1:bd8aea0d51e0 482 float frequency = config_data.start_freq;
ssmith73 1:bd8aea0d51e0 483
ssmith73 1:bd8aea0d51e0 484
ssmith73 1:bd8aea0d51e0 485 ad5933_config_sweep(device,
ssmith73 1:bd8aea0d51e0 486 config_data.start_freq,
ssmith73 1:bd8aea0d51e0 487 config_data.frequency_increment,
ssmith73 1:bd8aea0d51e0 488 config_data.number_increments);
ssmith73 1:bd8aea0d51e0 489
ssmith73 1:bd8aea0d51e0 490 /*
ssmith73 1:bd8aea0d51e0 491 > program frequency sweep parameters into relevant registerS
ssmith73 1:bd8aea0d51e0 492 > place the ad5933 into standby mode.
ssmith73 1:bd8aea0d51e0 493 > start frequency register
ssmith73 1:bd8aea0d51e0 494 > number of increments register
ssmith73 1:bd8aea0d51e0 495 */
ssmith73 1:bd8aea0d51e0 496 ad5933_start_sweep(device);
ssmith73 1:bd8aea0d51e0 497 pc.printf("\n FREQUENCY MAGNITUDE PHASE IMPEDANCE\n");
ssmith73 1:bd8aea0d51e0 498
ssmith73 1:bd8aea0d51e0 499 do {
ssmith73 1:bd8aea0d51e0 500
ssmith73 1:bd8aea0d51e0 501 //Fill up the results struct with data
ssmith73 1:bd8aea0d51e0 502 results = ad5933_calculate_impedance(device,
ssmith73 1:bd8aea0d51e0 503 gain_factor,
ssmith73 1:bd8aea0d51e0 504 AD5933_FUNCTION_INC_FREQ);
ssmith73 1:bd8aea0d51e0 505
ssmith73 1:bd8aea0d51e0 506 impedance = 1 / (results.magnitude * gain_factor);
ssmith73 1:bd8aea0d51e0 507
ssmith73 1:bd8aea0d51e0 508 pc.printf(" %.2f,", frequency);
ssmith73 1:bd8aea0d51e0 509 pc.printf(" %.2f", results.magnitude);
ssmith73 1:bd8aea0d51e0 510 pc.printf(" %.2f", results.phase);
ssmith73 1:bd8aea0d51e0 511 pc.printf(" %.2f\n", impedance);
ssmith73 1:bd8aea0d51e0 512
ssmith73 1:bd8aea0d51e0 513 frequency += config_data.frequency_increment;
ssmith73 1:bd8aea0d51e0 514
ssmith73 1:bd8aea0d51e0 515 //poll the status register to check if frequency sweep is complete.
ssmith73 1:bd8aea0d51e0 516 status = ad5933_get_register_value(device, AD5933_REG_STATUS, 1);
ssmith73 1:bd8aea0d51e0 517
ssmith73 1:bd8aea0d51e0 518 } while ((status & AD5933_STAT_SWEEP_DONE) == 0);
ssmith73 1:bd8aea0d51e0 519
ssmith73 1:bd8aea0d51e0 520 return status;
malavikasaji 0:86671e9c8db0 521 }
malavikasaji 0:86671e9c8db0 522
malavikasaji 0:86671e9c8db0 523
malavikasaji 0:86671e9c8db0 524
ssmith73 1:bd8aea0d51e0 525
ssmith73 1:bd8aea0d51e0 526