EVAL-AD7124 Mbed Example Program.

Dependencies:   adi_console_menu platform_drivers

Committer:
mahphalke
Date:
Mon Mar 02 17:04:20 2020 +0000
Revision:
3:779bb1e55f1a
Child:
5:ca3854efb1e9
Updated firmware with more functionality, latest console menu structure and latest platform drivers.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 3:779bb1e55f1a 1 /*!
mahphalke 3:779bb1e55f1a 2 *****************************************************************************
mahphalke 3:779bb1e55f1a 3 @file: ad7124_console_app.c
mahphalke 3:779bb1e55f1a 4
mahphalke 3:779bb1e55f1a 5 @brief: Implementation for the menu functions that handle the AD7124
mahphalke 3:779bb1e55f1a 6
mahphalke 3:779bb1e55f1a 7 @details: This file is specific to ad7124 console menu application handle.
mahphalke 3:779bb1e55f1a 8 The functions defined in this file performs the action
mahphalke 3:779bb1e55f1a 9 based on user selected console menu.
mahphalke 3:779bb1e55f1a 10 -----------------------------------------------------------------------------
mahphalke 3:779bb1e55f1a 11 Copyright (c) 2019, 2020 Analog Devices, Inc.
mahphalke 3:779bb1e55f1a 12 All rights reserved.
mahphalke 3:779bb1e55f1a 13
mahphalke 3:779bb1e55f1a 14 This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 3:779bb1e55f1a 15 By using this software you agree to the terms of the associated
mahphalke 3:779bb1e55f1a 16 Analog Devices Software License Agreement.
mahphalke 3:779bb1e55f1a 17 *****************************************************************************/
mahphalke 3:779bb1e55f1a 18
mahphalke 3:779bb1e55f1a 19 /* includes */
mahphalke 3:779bb1e55f1a 20 #include <stdio.h>
mahphalke 3:779bb1e55f1a 21 #include <string.h>
mahphalke 3:779bb1e55f1a 22 #include <stdbool.h>
mahphalke 3:779bb1e55f1a 23
mahphalke 3:779bb1e55f1a 24 #include "platform_support.h"
mahphalke 3:779bb1e55f1a 25 #include "platform_drivers.h"
mahphalke 3:779bb1e55f1a 26 #include "spi_extra.h"
mahphalke 3:779bb1e55f1a 27
mahphalke 3:779bb1e55f1a 28 #include "ad7124.h"
mahphalke 3:779bb1e55f1a 29 #include "ad7124_regs.h"
mahphalke 3:779bb1e55f1a 30 #include "ad7124_support.h"
mahphalke 3:779bb1e55f1a 31 #include "ad7124_regs_configs.h"
mahphalke 3:779bb1e55f1a 32
mahphalke 3:779bb1e55f1a 33 #include "ad7124_console_app.h"
mahphalke 3:779bb1e55f1a 34 #include "app_config.h"
mahphalke 3:779bb1e55f1a 35
mahphalke 3:779bb1e55f1a 36
mahphalke 3:779bb1e55f1a 37 /* defines */
mahphalke 3:779bb1e55f1a 38 #define AD7124_CHANNEL_COUNT 16
mahphalke 3:779bb1e55f1a 39
mahphalke 3:779bb1e55f1a 40 #define SHOW_ALL_CHANNELS false
mahphalke 3:779bb1e55f1a 41 #define SHOW_ENABLED_CHANNELS true
mahphalke 3:779bb1e55f1a 42
mahphalke 3:779bb1e55f1a 43 #define DISPLAY_DATA_TABULAR 0
mahphalke 3:779bb1e55f1a 44 #define DISPLAY_DATA_STREAM 1
mahphalke 3:779bb1e55f1a 45
mahphalke 3:779bb1e55f1a 46 #define AD7124_MAX_SETUPS 8
mahphalke 3:779bb1e55f1a 47 #define AD7124_MAX_CHANNELS 16
mahphalke 3:779bb1e55f1a 48 #define NUM_OF_FILTERS 5
mahphalke 3:779bb1e55f1a 49 #define MAX_FILTER_DATA_RATE_FS 2047
mahphalke 3:779bb1e55f1a 50 #define MIN_FILTER_DATA_RATE 1
mahphalke 3:779bb1e55f1a 51 #define MAX_GAIN_BITS_VALUE 7
mahphalke 3:779bb1e55f1a 52 #define MIN_PROGRAMMABLE_GAIN 1
mahphalke 3:779bb1e55f1a 53 #define MAX_PROGRAMMABLE_GAIN 128
mahphalke 3:779bb1e55f1a 54 #define MAX_ANALOG_INPUTS 32
mahphalke 3:779bb1e55f1a 55
mahphalke 3:779bb1e55f1a 56
mahphalke 3:779bb1e55f1a 57 /* Private Variables */
mahphalke 3:779bb1e55f1a 58 /*
mahphalke 3:779bb1e55f1a 59 * This is the 'live' AD7124 register map that is used by the driver
mahphalke 3:779bb1e55f1a 60 * the other 'default' configs are used to populate this at init time
mahphalke 3:779bb1e55f1a 61 */
mahphalke 3:779bb1e55f1a 62 static struct ad7124_st_reg ad7124_register_map[AD7124_REG_NO];
mahphalke 3:779bb1e55f1a 63
mahphalke 3:779bb1e55f1a 64 // Pointer to the struct representing the AD7124 device
mahphalke 3:779bb1e55f1a 65 static struct ad7124_dev * pAd7124_dev = NULL;
mahphalke 3:779bb1e55f1a 66
mahphalke 3:779bb1e55f1a 67 // GPIO descriptor and init parameters for the activity LED pin
mahphalke 3:779bb1e55f1a 68 static gpio_desc *activity_led_desc = NULL;
mahphalke 3:779bb1e55f1a 69 static gpio_init_param activity_led_init_param = { LED_GREEN, NULL };
mahphalke 3:779bb1e55f1a 70
mahphalke 3:779bb1e55f1a 71 // Last Sampled values for All ADC channels
mahphalke 3:779bb1e55f1a 72 static uint32_t channel_samples[AD7124_CHANNEL_COUNT] = {0};
mahphalke 3:779bb1e55f1a 73 // How many times a given channel is sampled in total for one sample run
mahphalke 3:779bb1e55f1a 74 static uint32_t channel_samples_count[AD7124_CHANNEL_COUNT] = {0};
mahphalke 3:779bb1e55f1a 75
mahphalke 3:779bb1e55f1a 76 static ad7124_setup_config setup[AD7124_MAX_SETUPS]; // AD7124 setup
mahphalke 3:779bb1e55f1a 77
mahphalke 3:779bb1e55f1a 78 static float filter_data_rate_raw; // filter data rate value
mahphalke 3:779bb1e55f1a 79 static uint32_t gain_raw; // Gain value
mahphalke 3:779bb1e55f1a 80 static uint8_t power_mode; // power mode of ADC
mahphalke 3:779bb1e55f1a 81
mahphalke 3:779bb1e55f1a 82 typedef enum {
mahphalke 3:779bb1e55f1a 83 CHN_DISABLE,
mahphalke 3:779bb1e55f1a 84 CHN_ENABLE
mahphalke 3:779bb1e55f1a 85 } Chn_EnbleDisable_action;
mahphalke 3:779bb1e55f1a 86
mahphalke 3:779bb1e55f1a 87 /* Filter names */
mahphalke 3:779bb1e55f1a 88 static char *filter_name[] = {
mahphalke 3:779bb1e55f1a 89 "SINC4",
mahphalke 3:779bb1e55f1a 90 "Reserved",
mahphalke 3:779bb1e55f1a 91 "SINC3",
mahphalke 3:779bb1e55f1a 92 "Reserved",
mahphalke 3:779bb1e55f1a 93 "FS SINC4",
mahphalke 3:779bb1e55f1a 94 "FS SINC3",
mahphalke 3:779bb1e55f1a 95 "Reserved",
mahphalke 3:779bb1e55f1a 96 "POST SINC3"
mahphalke 3:779bb1e55f1a 97 };
mahphalke 3:779bb1e55f1a 98
mahphalke 3:779bb1e55f1a 99 static char *reference_name[] = {
mahphalke 3:779bb1e55f1a 100 "REFIN1",
mahphalke 3:779bb1e55f1a 101 "REFIN2",
mahphalke 3:779bb1e55f1a 102 "INTERNAL",
mahphalke 3:779bb1e55f1a 103 "AVDD"
mahphalke 3:779bb1e55f1a 104 };
mahphalke 3:779bb1e55f1a 105
mahphalke 3:779bb1e55f1a 106 static char *enable_disable_status[] = {
mahphalke 3:779bb1e55f1a 107 "DISABLED",
mahphalke 3:779bb1e55f1a 108 "ENABLED"
mahphalke 3:779bb1e55f1a 109 };
mahphalke 3:779bb1e55f1a 110
mahphalke 3:779bb1e55f1a 111 static char *polarity_status[] = {
mahphalke 3:779bb1e55f1a 112 "UNIPOLAR",
mahphalke 3:779bb1e55f1a 113 "BIPOLAR"
mahphalke 3:779bb1e55f1a 114 };
mahphalke 3:779bb1e55f1a 115
mahphalke 3:779bb1e55f1a 116 static char *power_modes_str[] = {
mahphalke 3:779bb1e55f1a 117 "Low Power",
mahphalke 3:779bb1e55f1a 118 "Medium Power",
mahphalke 3:779bb1e55f1a 119 "Full Power",
mahphalke 3:779bb1e55f1a 120 "Full Power"
mahphalke 3:779bb1e55f1a 121 };
mahphalke 3:779bb1e55f1a 122
mahphalke 3:779bb1e55f1a 123 /* Programmable gain values */
mahphalke 3:779bb1e55f1a 124 static uint8_t p_gain[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
mahphalke 3:779bb1e55f1a 125
mahphalke 3:779bb1e55f1a 126 /* Temperature sensor data structure */
mahphalke 3:779bb1e55f1a 127 typedef struct {
mahphalke 3:779bb1e55f1a 128 int16_t temp; // Temperature
mahphalke 3:779bb1e55f1a 129 uint32_t adc_sample; // ADC conversion sample
mahphalke 3:779bb1e55f1a 130 } temp_loopup;
mahphalke 3:779bb1e55f1a 131
mahphalke 3:779bb1e55f1a 132
mahphalke 3:779bb1e55f1a 133 /* Function protoypes */
mahphalke 3:779bb1e55f1a 134 static int32_t menu_continuous_conversion_tabular(void);
mahphalke 3:779bb1e55f1a 135 static int32_t menu_continuous_conversion_stream(void);
mahphalke 3:779bb1e55f1a 136 static int32_t menu_single_conversion(void);
mahphalke 3:779bb1e55f1a 137 static int32_t menu_read_status(void);
mahphalke 3:779bb1e55f1a 138 static int32_t menu_read_id(void);
mahphalke 3:779bb1e55f1a 139 static int32_t menu_reset(void);
mahphalke 3:779bb1e55f1a 140 static int32_t menu_reset_to_configuration(uint32_t config_type);
mahphalke 3:779bb1e55f1a 141 static int32_t menu_read_temperature(void);
mahphalke 3:779bb1e55f1a 142 static int32_t menu_select_power_mode(void);
mahphalke 3:779bb1e55f1a 143 static int32_t menu_power_modes_selection(uint32_t power_mode);
mahphalke 3:779bb1e55f1a 144 static int32_t menu_rw_ad7124_register(uint32_t rw_id);
mahphalke 3:779bb1e55f1a 145 static int32_t menu_sample_channels(void);
mahphalke 3:779bb1e55f1a 146 static int32_t menu_enable_disable_channels(void);
mahphalke 3:779bb1e55f1a 147 static int32_t menu_config_and_assign_setup(void);
mahphalke 3:779bb1e55f1a 148 static int32_t menu_connect_input_to_channel(void);
mahphalke 3:779bb1e55f1a 149 static int32_t menu_calibrate_adc(void);
mahphalke 3:779bb1e55f1a 150
mahphalke 3:779bb1e55f1a 151 static bool was_escape_key_pressed(void);
mahphalke 3:779bb1e55f1a 152 static void toggle_activity_led(void);
mahphalke 3:779bb1e55f1a 153 static void read_status_register(void);
mahphalke 3:779bb1e55f1a 154 static void dislay_channel_samples(bool showOnlyEnabledChannels,
mahphalke 3:779bb1e55f1a 155 uint8_t console_mode);
mahphalke 3:779bb1e55f1a 156 static void clear_channel_samples(void);
mahphalke 3:779bb1e55f1a 157 static int32_t do_continuous_conversion(uint8_t display_mode);
mahphalke 3:779bb1e55f1a 158 static int16_t scan_temperature(uint32_t value);
mahphalke 3:779bb1e55f1a 159 static void init_with_configuration(uint8_t configID);
mahphalke 3:779bb1e55f1a 160 static uint8_t get_setup_selection(void);
mahphalke 3:779bb1e55f1a 161 static uint8_t get_channel_selection(void);
mahphalke 3:779bb1e55f1a 162 static void config_analog_inputs(ad7124_setup_config *psetup);
mahphalke 3:779bb1e55f1a 163 static float calculate_data_rate(filter_type filter, uint8_t power_mode,
mahphalke 3:779bb1e55f1a 164 float data_rate);
mahphalke 3:779bb1e55f1a 165 static void config_filter_parameters(ad7124_setup_config *psetup);
mahphalke 3:779bb1e55f1a 166 static void assign_setup_to_channel(uint8_t setup);
mahphalke 3:779bb1e55f1a 167
mahphalke 3:779bb1e55f1a 168
mahphalke 3:779bb1e55f1a 169 /* The temperature lookup table is formed using below formula:
mahphalke 3:779bb1e55f1a 170 * temp = ((adc_conv_reading - 8388608) / 13584) - 272.5;
mahphalke 3:779bb1e55f1a 171 *
mahphalke 3:779bb1e55f1a 172 * Note: Refer datasheet page 71 (TEMPERATURE SENSOR) for using internal
mahphalke 3:779bb1e55f1a 173 * temperature sensor of AD7124, which monitors the die temperature.
mahphalke 3:779bb1e55f1a 174 * This method of temperature sensing is not accurate and might have deviations
mahphalke 3:779bb1e55f1a 175 * of +/-1C due to non-floating calculations. In order to get more precise result,
mahphalke 3:779bb1e55f1a 176 * use floating point calculations (using above formula).
mahphalke 3:779bb1e55f1a 177 **/
mahphalke 3:779bb1e55f1a 178
mahphalke 3:779bb1e55f1a 179 /* Temperature Range: -20C to +50C */
mahphalke 3:779bb1e55f1a 180 static temp_loopup temperature_lookup[] = {
mahphalke 3:779bb1e55f1a 181 { -20, 11818568 },
mahphalke 3:779bb1e55f1a 182 { -19, 11832152 },
mahphalke 3:779bb1e55f1a 183 { -18, 11845736 },
mahphalke 3:779bb1e55f1a 184 { -17, 11859320 },
mahphalke 3:779bb1e55f1a 185 { -16, 11872904 },
mahphalke 3:779bb1e55f1a 186 { -15, 11886488 },
mahphalke 3:779bb1e55f1a 187 { -14, 11900072 },
mahphalke 3:779bb1e55f1a 188 { -13, 11913656 },
mahphalke 3:779bb1e55f1a 189 { -12, 11927240 },
mahphalke 3:779bb1e55f1a 190 { -11, 11940824 },
mahphalke 3:779bb1e55f1a 191 { -10, 11954408 },
mahphalke 3:779bb1e55f1a 192 { -9, 11967992 },
mahphalke 3:779bb1e55f1a 193 { -8, 11981576 },
mahphalke 3:779bb1e55f1a 194 { -7, 11995160 },
mahphalke 3:779bb1e55f1a 195 { -6, 12008744 },
mahphalke 3:779bb1e55f1a 196 { -5, 12022328 },
mahphalke 3:779bb1e55f1a 197 { -4, 12035912 },
mahphalke 3:779bb1e55f1a 198 { -3, 12049496 },
mahphalke 3:779bb1e55f1a 199 { -2, 12063080 },
mahphalke 3:779bb1e55f1a 200 { -1, 12076664 },
mahphalke 3:779bb1e55f1a 201 { 0, 12090248 },
mahphalke 3:779bb1e55f1a 202 { 1, 12103832 },
mahphalke 3:779bb1e55f1a 203 { 2, 12117416 },
mahphalke 3:779bb1e55f1a 204 { 3, 12131000 },
mahphalke 3:779bb1e55f1a 205 { 4, 12144584 },
mahphalke 3:779bb1e55f1a 206 { 5, 12158168 },
mahphalke 3:779bb1e55f1a 207 { 6, 12171752 },
mahphalke 3:779bb1e55f1a 208 { 7, 12185336 },
mahphalke 3:779bb1e55f1a 209 { 8, 12198920 },
mahphalke 3:779bb1e55f1a 210 { 9, 12212504 },
mahphalke 3:779bb1e55f1a 211 { 10, 12226088 },
mahphalke 3:779bb1e55f1a 212 { 11, 12239672 },
mahphalke 3:779bb1e55f1a 213 { 12, 12253256 },
mahphalke 3:779bb1e55f1a 214 { 13, 12266840 },
mahphalke 3:779bb1e55f1a 215 { 14, 12280424 },
mahphalke 3:779bb1e55f1a 216 { 15, 12294008 },
mahphalke 3:779bb1e55f1a 217 { 16, 12307592 },
mahphalke 3:779bb1e55f1a 218 { 17, 12321176 },
mahphalke 3:779bb1e55f1a 219 { 18, 12334760 },
mahphalke 3:779bb1e55f1a 220 { 19, 12348344 },
mahphalke 3:779bb1e55f1a 221 { 20, 12361928 },
mahphalke 3:779bb1e55f1a 222 { 21, 12375512 },
mahphalke 3:779bb1e55f1a 223 { 22, 12389096 },
mahphalke 3:779bb1e55f1a 224 { 23, 12402680 },
mahphalke 3:779bb1e55f1a 225 { 24, 12416264 },
mahphalke 3:779bb1e55f1a 226 { 25, 12429848 },
mahphalke 3:779bb1e55f1a 227 { 26, 12443432 },
mahphalke 3:779bb1e55f1a 228 { 27, 12457016 },
mahphalke 3:779bb1e55f1a 229 { 28, 12470600 },
mahphalke 3:779bb1e55f1a 230 { 29, 12484184 },
mahphalke 3:779bb1e55f1a 231 { 30, 12497768 },
mahphalke 3:779bb1e55f1a 232 { 31, 12511352 },
mahphalke 3:779bb1e55f1a 233 { 32, 12524936 },
mahphalke 3:779bb1e55f1a 234 { 33, 12538520 },
mahphalke 3:779bb1e55f1a 235 { 34, 12552104 },
mahphalke 3:779bb1e55f1a 236 { 35, 12565688 },
mahphalke 3:779bb1e55f1a 237 { 36, 12579272 },
mahphalke 3:779bb1e55f1a 238 { 37, 12592856 },
mahphalke 3:779bb1e55f1a 239 { 38, 12606440 },
mahphalke 3:779bb1e55f1a 240 { 39, 12620024 },
mahphalke 3:779bb1e55f1a 241 { 40, 12633608 },
mahphalke 3:779bb1e55f1a 242 { 41, 12647192 },
mahphalke 3:779bb1e55f1a 243 { 42, 12660776 },
mahphalke 3:779bb1e55f1a 244 { 43, 12674360 },
mahphalke 3:779bb1e55f1a 245 { 44, 12687944 },
mahphalke 3:779bb1e55f1a 246 { 45, 12701528 },
mahphalke 3:779bb1e55f1a 247 { 46, 12715112 },
mahphalke 3:779bb1e55f1a 248 { 47, 12728696 },
mahphalke 3:779bb1e55f1a 249 { 48, 12742280 },
mahphalke 3:779bb1e55f1a 250 { 49, 12755864 },
mahphalke 3:779bb1e55f1a 251 { 50, 12769448 }
mahphalke 3:779bb1e55f1a 252 };
mahphalke 3:779bb1e55f1a 253
mahphalke 3:779bb1e55f1a 254
mahphalke 3:779bb1e55f1a 255 // Public Functions
mahphalke 3:779bb1e55f1a 256
mahphalke 3:779bb1e55f1a 257 /*!
mahphalke 3:779bb1e55f1a 258 * @brief Initialize the AD7124 device and the SPI port as required
mahphalke 3:779bb1e55f1a 259 *
mahphalke 3:779bb1e55f1a 260 * @details This resets and then writes the default register map value to
mahphalke 3:779bb1e55f1a 261 * the device. A call to init the SPI port is made, but may not
mahphalke 3:779bb1e55f1a 262 * actually do very much, depending on the platform
mahphalke 3:779bb1e55f1a 263 */
mahphalke 3:779bb1e55f1a 264 int32_t ad7124_app_initialize(uint8_t configID)
mahphalke 3:779bb1e55f1a 265 {
mahphalke 3:779bb1e55f1a 266 // Init SPI extra parameters structure
mahphalke 3:779bb1e55f1a 267 mbed_spi_init_param spi_init_extra_params = {
mahphalke 3:779bb1e55f1a 268 .spi_clk_pin = SPI_SCK,
mahphalke 3:779bb1e55f1a 269 .spi_miso_pin = SPI_MISO,
mahphalke 3:779bb1e55f1a 270 .spi_mosi_pin = SPI_MOSI
mahphalke 3:779bb1e55f1a 271 };
mahphalke 3:779bb1e55f1a 272
mahphalke 3:779bb1e55f1a 273 // Create a new descriptor for activity led
mahphalke 3:779bb1e55f1a 274 if (gpio_get(&activity_led_desc, &activity_led_init_param) == FAILURE) {
mahphalke 3:779bb1e55f1a 275 return FAILURE;
mahphalke 3:779bb1e55f1a 276 }
mahphalke 3:779bb1e55f1a 277
mahphalke 3:779bb1e55f1a 278 // Set the direction of activity LED
mahphalke 3:779bb1e55f1a 279 if ((gpio_direction_output(activity_led_desc, GPIO_HIGH)) == FAILURE) {
mahphalke 3:779bb1e55f1a 280 return FAILURE;
mahphalke 3:779bb1e55f1a 281 }
mahphalke 3:779bb1e55f1a 282
mahphalke 3:779bb1e55f1a 283 /*
mahphalke 3:779bb1e55f1a 284 * Copy one of the default/user configs to the live register memory map
mahphalke 3:779bb1e55f1a 285 * Requirement, not checked here, is that all the configs are the same size
mahphalke 3:779bb1e55f1a 286 */
mahphalke 3:779bb1e55f1a 287 switch (configID) {
mahphalke 3:779bb1e55f1a 288 case AD7124_CONFIG_A: {
mahphalke 3:779bb1e55f1a 289 memcpy(ad7124_register_map, ad7124_regs_config_a, sizeof(ad7124_register_map));
mahphalke 3:779bb1e55f1a 290 break;
mahphalke 3:779bb1e55f1a 291 }
mahphalke 3:779bb1e55f1a 292 case AD7124_CONFIG_B: {
mahphalke 3:779bb1e55f1a 293 memcpy(ad7124_register_map, ad7124_regs_config_b, sizeof(ad7124_register_map));
mahphalke 3:779bb1e55f1a 294 break;
mahphalke 3:779bb1e55f1a 295 }
mahphalke 3:779bb1e55f1a 296 default:
mahphalke 3:779bb1e55f1a 297 // Not a defined configID
mahphalke 3:779bb1e55f1a 298 return(FAILURE);
mahphalke 3:779bb1e55f1a 299 }
mahphalke 3:779bb1e55f1a 300
mahphalke 3:779bb1e55f1a 301 // Used to create the ad7124 device
mahphalke 3:779bb1e55f1a 302 struct ad7124_init_param sAd7124_init = {
mahphalke 3:779bb1e55f1a 303 // spi_init_param type
mahphalke 3:779bb1e55f1a 304 {
mahphalke 3:779bb1e55f1a 305 2500000, // Max SPI Speed
mahphalke 3:779bb1e55f1a 306 SPI_SS_A, // Chip Select pin
mahphalke 3:779bb1e55f1a 307 SPI_MODE_3, // CPOL = 1, CPHA =1
mahphalke 3:779bb1e55f1a 308 &spi_init_extra_params, // SPI extra configurations
mahphalke 3:779bb1e55f1a 309 },
mahphalke 3:779bb1e55f1a 310 ad7124_register_map,
mahphalke 3:779bb1e55f1a 311
mahphalke 3:779bb1e55f1a 312 10000 // Retry count for polling
mahphalke 3:779bb1e55f1a 313 };
mahphalke 3:779bb1e55f1a 314
mahphalke 3:779bb1e55f1a 315 return (ad7124_setup(&pAd7124_dev, sAd7124_init));
mahphalke 3:779bb1e55f1a 316 }
mahphalke 3:779bb1e55f1a 317
mahphalke 3:779bb1e55f1a 318 // Private Functions
mahphalke 3:779bb1e55f1a 319
mahphalke 3:779bb1e55f1a 320 /*!
mahphalke 3:779bb1e55f1a 321 * @brief determines if the Escape key was pressed
mahphalke 3:779bb1e55f1a 322 *
mahphalke 3:779bb1e55f1a 323 * @return bool- key press status
mahphalke 3:779bb1e55f1a 324 */
mahphalke 3:779bb1e55f1a 325 static bool was_escape_key_pressed(void)
mahphalke 3:779bb1e55f1a 326 {
mahphalke 3:779bb1e55f1a 327 char rxChar;
mahphalke 3:779bb1e55f1a 328 bool wasPressed = false;
mahphalke 3:779bb1e55f1a 329
mahphalke 3:779bb1e55f1a 330 // Check for Escape key pressed
mahphalke 3:779bb1e55f1a 331 if ((rxChar = getchar_noblock()) > 0) {
mahphalke 3:779bb1e55f1a 332 if (rxChar == ESCAPE_KEY_CODE) {
mahphalke 3:779bb1e55f1a 333 wasPressed = true;
mahphalke 3:779bb1e55f1a 334 }
mahphalke 3:779bb1e55f1a 335 }
mahphalke 3:779bb1e55f1a 336 return (wasPressed);
mahphalke 3:779bb1e55f1a 337 }
mahphalke 3:779bb1e55f1a 338
mahphalke 3:779bb1e55f1a 339
mahphalke 3:779bb1e55f1a 340 /**
mahphalke 3:779bb1e55f1a 341 * @brief toggles an LED to show something has happened
mahphalke 3:779bb1e55f1a 342 * @param None
mahphalke 3:779bb1e55f1a 343 * @return None
mahphalke 3:779bb1e55f1a 344 */
mahphalke 3:779bb1e55f1a 345 static void toggle_activity_led(void)
mahphalke 3:779bb1e55f1a 346 {
mahphalke 3:779bb1e55f1a 347 static int led_state = GPIO_HIGH;
mahphalke 3:779bb1e55f1a 348
mahphalke 3:779bb1e55f1a 349 /* Toggle the LED state */
mahphalke 3:779bb1e55f1a 350 if (led_state == GPIO_LOW) {
mahphalke 3:779bb1e55f1a 351 led_state = GPIO_HIGH;
mahphalke 3:779bb1e55f1a 352 } else {
mahphalke 3:779bb1e55f1a 353 led_state = GPIO_LOW;
mahphalke 3:779bb1e55f1a 354 }
mahphalke 3:779bb1e55f1a 355
mahphalke 3:779bb1e55f1a 356 gpio_set_value(activity_led_desc, led_state);
mahphalke 3:779bb1e55f1a 357 }
mahphalke 3:779bb1e55f1a 358
mahphalke 3:779bb1e55f1a 359
mahphalke 3:779bb1e55f1a 360 /*!
mahphalke 3:779bb1e55f1a 361 * @brief reads and displays the status register on the AD7124
mahphalke 3:779bb1e55f1a 362 *
mahphalke 3:779bb1e55f1a 363 * @details
mahphalke 3:779bb1e55f1a 364 */
mahphalke 3:779bb1e55f1a 365 static void read_status_register(void)
mahphalke 3:779bb1e55f1a 366 {
mahphalke 3:779bb1e55f1a 367 if (ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 368 &ad7124_register_map[AD7124_Status]) < 0) {
mahphalke 3:779bb1e55f1a 369 printf("\r\nError Encountered reading Status register\r\n");
mahphalke 3:779bb1e55f1a 370 } else {
mahphalke 3:779bb1e55f1a 371 uint32_t status_value = (uint32_t)ad7124_register_map[AD7124_Status].value;
mahphalke 3:779bb1e55f1a 372 printf("\r\nRead Status Register = 0x%02lx\r\n", status_value);
mahphalke 3:779bb1e55f1a 373 }
mahphalke 3:779bb1e55f1a 374 }
mahphalke 3:779bb1e55f1a 375
mahphalke 3:779bb1e55f1a 376
mahphalke 3:779bb1e55f1a 377 /*!
mahphalke 3:779bb1e55f1a 378 * @brief displays the current sample value for a ADC channels
mahphalke 3:779bb1e55f1a 379 *
mahphalke 3:779bb1e55f1a 380 * @details
mahphalke 3:779bb1e55f1a 381 *
mahphalke 3:779bb1e55f1a 382 * @param showOnlyEnabledChannels only channels that are enabled are displayed
mahphalke 3:779bb1e55f1a 383 *
mahphalke 3:779bb1e55f1a 384 */
mahphalke 3:779bb1e55f1a 385 static void dislay_channel_samples(bool showOnlyEnabledChannels,
mahphalke 3:779bb1e55f1a 386 uint8_t console_mode)
mahphalke 3:779bb1e55f1a 387 {
mahphalke 3:779bb1e55f1a 388 switch (console_mode) {
mahphalke 3:779bb1e55f1a 389 case DISPLAY_DATA_TABULAR: {
mahphalke 3:779bb1e55f1a 390 printf("\tCh\tValue\t\tCount\t\tVoltage\r\n");
mahphalke 3:779bb1e55f1a 391 for (uint8_t i = 0; i < AD7124_CHANNEL_COUNT; i++) {
mahphalke 3:779bb1e55f1a 392 // if showing all channels, or channel is enabled
mahphalke 3:779bb1e55f1a 393 if ((showOnlyEnabledChannels == false)
mahphalke 3:779bb1e55f1a 394 || (ad7124_register_map[AD7124_Channel_0 + i].value &
mahphalke 3:779bb1e55f1a 395 AD7124_CH_MAP_REG_CH_ENABLE)) {
mahphalke 3:779bb1e55f1a 396 printf("\t%-2d\t%-10ld\t%ld\t\t% .6f\r\n", \
mahphalke 3:779bb1e55f1a 397 i,
mahphalke 3:779bb1e55f1a 398 channel_samples[i],
mahphalke 3:779bb1e55f1a 399 channel_samples_count[i],
mahphalke 3:779bb1e55f1a 400 ad7124_convert_sample_to_voltage(pAd7124_dev, i, channel_samples[i]));
mahphalke 3:779bb1e55f1a 401 }
mahphalke 3:779bb1e55f1a 402 }
mahphalke 3:779bb1e55f1a 403 break;
mahphalke 3:779bb1e55f1a 404 }
mahphalke 3:779bb1e55f1a 405 case DISPLAY_DATA_STREAM: {
mahphalke 3:779bb1e55f1a 406 // Output a CSV list of the sampled channels as voltages on a single line
mahphalke 3:779bb1e55f1a 407 bool channel_printed = false;
mahphalke 3:779bb1e55f1a 408
mahphalke 3:779bb1e55f1a 409 for (uint8_t i = 0; i < AD7124_CHANNEL_COUNT; i++) {
mahphalke 3:779bb1e55f1a 410 // if showing all channels, or channel is enabled
mahphalke 3:779bb1e55f1a 411 if ((showOnlyEnabledChannels == false)
mahphalke 3:779bb1e55f1a 412 || (ad7124_register_map[AD7124_Channel_0 + i].value &
mahphalke 3:779bb1e55f1a 413 AD7124_CH_MAP_REG_CH_ENABLE)) {
mahphalke 3:779bb1e55f1a 414 /*
mahphalke 3:779bb1e55f1a 415 * add the comma before we output the next channel but
mahphalke 3:779bb1e55f1a 416 * only if at least one channel has been printed
mahphalke 3:779bb1e55f1a 417 */
mahphalke 3:779bb1e55f1a 418 if (channel_printed) {
mahphalke 3:779bb1e55f1a 419 printf(", ");
mahphalke 3:779bb1e55f1a 420 }
mahphalke 3:779bb1e55f1a 421 printf("%.6f", \
mahphalke 3:779bb1e55f1a 422 ad7124_convert_sample_to_voltage(pAd7124_dev, i, channel_samples[i]));
mahphalke 3:779bb1e55f1a 423 channel_printed = true;
mahphalke 3:779bb1e55f1a 424 }
mahphalke 3:779bb1e55f1a 425 }
mahphalke 3:779bb1e55f1a 426 printf("\r\n");
mahphalke 3:779bb1e55f1a 427 break;
mahphalke 3:779bb1e55f1a 428 }
mahphalke 3:779bb1e55f1a 429 default: {
mahphalke 3:779bb1e55f1a 430 // ASSERT(false);
mahphalke 3:779bb1e55f1a 431 }
mahphalke 3:779bb1e55f1a 432 }
mahphalke 3:779bb1e55f1a 433 }
mahphalke 3:779bb1e55f1a 434
mahphalke 3:779bb1e55f1a 435
mahphalke 3:779bb1e55f1a 436 /*!
mahphalke 3:779bb1e55f1a 437 * @brief resets the channelSampleCounts to zero
mahphalke 3:779bb1e55f1a 438 *
mahphalke 3:779bb1e55f1a 439 * @details
mahphalke 3:779bb1e55f1a 440 */
mahphalke 3:779bb1e55f1a 441 static void clear_channel_samples(void)
mahphalke 3:779bb1e55f1a 442 {
mahphalke 3:779bb1e55f1a 443 for (uint8_t i = 0; i < AD7124_CHANNEL_COUNT; i++) {
mahphalke 3:779bb1e55f1a 444 channel_samples[i] = 0;
mahphalke 3:779bb1e55f1a 445 channel_samples_count[i] = 0;
mahphalke 3:779bb1e55f1a 446 }
mahphalke 3:779bb1e55f1a 447 }
mahphalke 3:779bb1e55f1a 448
mahphalke 3:779bb1e55f1a 449
mahphalke 3:779bb1e55f1a 450 /*!
mahphalke 3:779bb1e55f1a 451 * @brief Continuously acquires samples in Continuous Conversion mode
mahphalke 3:779bb1e55f1a 452 *
mahphalke 3:779bb1e55f1a 453 * @details The ADC is run in continuous mode, and all samples are acquired
mahphalke 3:779bb1e55f1a 454 * and assigned to the channel they come from. Escape key an be used
mahphalke 3:779bb1e55f1a 455 * to exit the loop
mahphalke 3:779bb1e55f1a 456 */
mahphalke 3:779bb1e55f1a 457 static int32_t do_continuous_conversion(uint8_t display_mode)
mahphalke 3:779bb1e55f1a 458 {
mahphalke 3:779bb1e55f1a 459 int32_t error_code;
mahphalke 3:779bb1e55f1a 460 int32_t sample_data;
mahphalke 3:779bb1e55f1a 461
mahphalke 3:779bb1e55f1a 462 // Clear the ADC CTRL MODE bits, has the effect of selecting continuous mode
mahphalke 3:779bb1e55f1a 463 ad7124_register_map[AD7124_ADC_Control].value &= ~(AD7124_ADC_CTRL_REG_MODE(
mahphalke 3:779bb1e55f1a 464 0xf));
mahphalke 3:779bb1e55f1a 465 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 466 ad7124_register_map[AD7124_ADC_Control])) < 0) {
mahphalke 3:779bb1e55f1a 467 printf("Error (%ld) setting AD7124 Continuous conversion mode.\r\n",
mahphalke 3:779bb1e55f1a 468 error_code);
mahphalke 3:779bb1e55f1a 469 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 470 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 471 }
mahphalke 3:779bb1e55f1a 472
mahphalke 3:779bb1e55f1a 473 clear_channel_samples();
mahphalke 3:779bb1e55f1a 474
mahphalke 3:779bb1e55f1a 475 /*
mahphalke 3:779bb1e55f1a 476 * If displaying data in stream form, want to output a channel header
mahphalke 3:779bb1e55f1a 477 */
mahphalke 3:779bb1e55f1a 478 if (display_mode == DISPLAY_DATA_STREAM) {
mahphalke 3:779bb1e55f1a 479 bool channel_printed = false;
mahphalke 3:779bb1e55f1a 480
mahphalke 3:779bb1e55f1a 481 for (uint8_t i = 0; i < AD7124_CHANNEL_COUNT; i++) {
mahphalke 3:779bb1e55f1a 482 // if showing all channels, or channel is enabled
mahphalke 3:779bb1e55f1a 483 if(ad7124_register_map[AD7124_Channel_0 + i].value &
mahphalke 3:779bb1e55f1a 484 AD7124_CH_MAP_REG_CH_ENABLE) {
mahphalke 3:779bb1e55f1a 485 /*
mahphalke 3:779bb1e55f1a 486 * add the comma before we output the next channel but
mahphalke 3:779bb1e55f1a 487 * only if at least one channel has been printed
mahphalke 3:779bb1e55f1a 488 */
mahphalke 3:779bb1e55f1a 489 if (channel_printed) {
mahphalke 3:779bb1e55f1a 490 printf(", ");
mahphalke 3:779bb1e55f1a 491 }
mahphalke 3:779bb1e55f1a 492 printf("%d", i);
mahphalke 3:779bb1e55f1a 493 }
mahphalke 3:779bb1e55f1a 494 channel_printed = true;
mahphalke 3:779bb1e55f1a 495 }
mahphalke 3:779bb1e55f1a 496 printf("\r\n");
mahphalke 3:779bb1e55f1a 497 }
mahphalke 3:779bb1e55f1a 498
mahphalke 3:779bb1e55f1a 499 // Continuously read the channels, and store sample values
mahphalke 3:779bb1e55f1a 500 while(was_escape_key_pressed() != true) {
mahphalke 3:779bb1e55f1a 501 toggle_activity_led();
mahphalke 3:779bb1e55f1a 502
mahphalke 3:779bb1e55f1a 503 if (display_mode == DISPLAY_DATA_TABULAR) {
mahphalke 3:779bb1e55f1a 504 adi_clear_console();
mahphalke 3:779bb1e55f1a 505 printf("Running continuous conversion mode...\r\nPress Escape to stop\r\n\r\n");
mahphalke 3:779bb1e55f1a 506 }
mahphalke 3:779bb1e55f1a 507
mahphalke 3:779bb1e55f1a 508 /*
mahphalke 3:779bb1e55f1a 509 * this polls the status register READY/ bit to determine when conversion is done
mahphalke 3:779bb1e55f1a 510 * this also ensures the STATUS register value is up to date and contains the
mahphalke 3:779bb1e55f1a 511 * channel that was sampled as well.
mahphalke 3:779bb1e55f1a 512 * Generally, no need to read STATUS separately, but for faster sampling
mahphalke 3:779bb1e55f1a 513 * enabling the DATA_STATUS bit means that status is appended to ADC data read
mahphalke 3:779bb1e55f1a 514 * so the channel being sampled is read back (and updated) as part of the same frame
mahphalke 3:779bb1e55f1a 515 */
mahphalke 3:779bb1e55f1a 516
mahphalke 3:779bb1e55f1a 517 if ((error_code = ad7124_wait_for_conv_ready(pAd7124_dev, 10000)) < 0) {
mahphalke 3:779bb1e55f1a 518 printf("Error/Timeout waiting for conversion ready %ld\r\n", error_code);
mahphalke 3:779bb1e55f1a 519 continue;
mahphalke 3:779bb1e55f1a 520 }
mahphalke 3:779bb1e55f1a 521
mahphalke 3:779bb1e55f1a 522 if ((error_code = ad7124_read_data(pAd7124_dev, &sample_data)) < 0) {
mahphalke 3:779bb1e55f1a 523 printf("Error reading ADC Data (%ld).\r\n", error_code);
mahphalke 3:779bb1e55f1a 524 continue;
mahphalke 3:779bb1e55f1a 525 }
mahphalke 3:779bb1e55f1a 526
mahphalke 3:779bb1e55f1a 527 /*
mahphalke 3:779bb1e55f1a 528 * No error, need to process the sample, what channel has been read? update that channelSample
mahphalke 3:779bb1e55f1a 529 */
mahphalke 3:779bb1e55f1a 530 uint8_t channel_read = ad7124_register_map[AD7124_Status].value & 0x0000000F;
mahphalke 3:779bb1e55f1a 531
mahphalke 3:779bb1e55f1a 532 if (channel_read < AD7124_CHANNEL_COUNT) {
mahphalke 3:779bb1e55f1a 533 channel_samples[channel_read] = sample_data;
mahphalke 3:779bb1e55f1a 534 channel_samples_count[channel_read]++;
mahphalke 3:779bb1e55f1a 535 } else {
mahphalke 3:779bb1e55f1a 536 printf("Channel Read was %d, which is not < AD7124_CHANNEL_COUNT\r\n",
mahphalke 3:779bb1e55f1a 537 channel_read);
mahphalke 3:779bb1e55f1a 538 }
mahphalke 3:779bb1e55f1a 539
mahphalke 3:779bb1e55f1a 540 dislay_channel_samples(SHOW_ENABLED_CHANNELS, display_mode);
mahphalke 3:779bb1e55f1a 541 }
mahphalke 3:779bb1e55f1a 542
mahphalke 3:779bb1e55f1a 543 // All done, ADC put into standby mode
mahphalke 3:779bb1e55f1a 544 ad7124_register_map[AD7124_ADC_Control].value &= ~(AD7124_ADC_CTRL_REG_MODE(
mahphalke 3:779bb1e55f1a 545 0xf));
mahphalke 3:779bb1e55f1a 546 // 2 = sleep/standby mode
mahphalke 3:779bb1e55f1a 547 ad7124_register_map[AD7124_ADC_Control].value |= AD7124_ADC_CTRL_REG_MODE(2);
mahphalke 3:779bb1e55f1a 548
mahphalke 3:779bb1e55f1a 549 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 550 ad7124_register_map[AD7124_ADC_Control])) < 0) {
mahphalke 3:779bb1e55f1a 551 printf("Error (%ld) setting AD7124 ADC into standby mode.\r\n", error_code);
mahphalke 3:779bb1e55f1a 552 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 553 }
mahphalke 3:779bb1e55f1a 554
mahphalke 3:779bb1e55f1a 555 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 556 }
mahphalke 3:779bb1e55f1a 557
mahphalke 3:779bb1e55f1a 558
mahphalke 3:779bb1e55f1a 559 /*!
mahphalke 3:779bb1e55f1a 560 * @brief Samples all enabled channels and displays in tabular form
mahphalke 3:779bb1e55f1a 561 *
mahphalke 3:779bb1e55f1a 562 * @details
mahphalke 3:779bb1e55f1a 563 */
mahphalke 3:779bb1e55f1a 564 static int32_t menu_continuous_conversion_tabular(void)
mahphalke 3:779bb1e55f1a 565 {
mahphalke 3:779bb1e55f1a 566 do_continuous_conversion(DISPLAY_DATA_TABULAR);
mahphalke 3:779bb1e55f1a 567
mahphalke 3:779bb1e55f1a 568 adi_clear_console();
mahphalke 3:779bb1e55f1a 569 printf("Continuous Conversion completed...\r\n\r\n");
mahphalke 3:779bb1e55f1a 570 dislay_channel_samples(SHOW_ALL_CHANNELS, DISPLAY_DATA_TABULAR);
mahphalke 3:779bb1e55f1a 571 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 572
mahphalke 3:779bb1e55f1a 573 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 574 }
mahphalke 3:779bb1e55f1a 575
mahphalke 3:779bb1e55f1a 576
mahphalke 3:779bb1e55f1a 577 /*!
mahphalke 3:779bb1e55f1a 578 * @brief Samples all enabled channels and displays on the console
mahphalke 3:779bb1e55f1a 579 *
mahphalke 3:779bb1e55f1a 580 * @details
mahphalke 3:779bb1e55f1a 581 */
mahphalke 3:779bb1e55f1a 582 static int32_t menu_continuous_conversion_stream(void)
mahphalke 3:779bb1e55f1a 583 {
mahphalke 3:779bb1e55f1a 584 do_continuous_conversion(DISPLAY_DATA_STREAM);
mahphalke 3:779bb1e55f1a 585 printf("Continuous Conversion completed...\r\n\r\n");
mahphalke 3:779bb1e55f1a 586 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 587 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 588 }
mahphalke 3:779bb1e55f1a 589
mahphalke 3:779bb1e55f1a 590
mahphalke 3:779bb1e55f1a 591 /*!
mahphalke 3:779bb1e55f1a 592 * @brief Samples all enabled channels once in Single Conversion mode
mahphalke 3:779bb1e55f1a 593 *
mahphalke 3:779bb1e55f1a 594 * @details This stores all channels that are enabled in a bitmask, and then
mahphalke 3:779bb1e55f1a 595 * runs the ADC in single conversion mode, which acquires one channel
mahphalke 3:779bb1e55f1a 596 * of data at a time. After capture, that channel is disabled, and
mahphalke 3:779bb1e55f1a 597 * single conversion run again, until no channels are enabled.
mahphalke 3:779bb1e55f1a 598 * The original enable state of each channel is then restored.
mahphalke 3:779bb1e55f1a 599 */
mahphalke 3:779bb1e55f1a 600 static int32_t menu_single_conversion(void)
mahphalke 3:779bb1e55f1a 601 {
mahphalke 3:779bb1e55f1a 602 int32_t error_code;
mahphalke 3:779bb1e55f1a 603 uint16_t channel_enable_mask = 0;
mahphalke 3:779bb1e55f1a 604 uint8_t channel_count = 0;
mahphalke 3:779bb1e55f1a 605 int32_t sample_data;
mahphalke 3:779bb1e55f1a 606
mahphalke 3:779bb1e55f1a 607 // Need to store which channels are enabled in this config so it can be restored
mahphalke 3:779bb1e55f1a 608 for (uint8_t i = 0 ; i < AD7124_CHANNEL_COUNT ; i++) {
mahphalke 3:779bb1e55f1a 609 if (ad7124_register_map[AD7124_Channel_0 + i].value &
mahphalke 3:779bb1e55f1a 610 AD7124_CH_MAP_REG_CH_ENABLE) {
mahphalke 3:779bb1e55f1a 611 channel_enable_mask |= (1 << i);
mahphalke 3:779bb1e55f1a 612 channel_count++;
mahphalke 3:779bb1e55f1a 613 }
mahphalke 3:779bb1e55f1a 614 }
mahphalke 3:779bb1e55f1a 615
mahphalke 3:779bb1e55f1a 616 clear_channel_samples();
mahphalke 3:779bb1e55f1a 617 adi_clear_console();
mahphalke 3:779bb1e55f1a 618 printf("Running Single conversion mode...\r\nPress Escape to stop\r\n\r\n");
mahphalke 3:779bb1e55f1a 619
mahphalke 3:779bb1e55f1a 620 // Clear the ADC CTRL MODE bits, selecting continuous mode
mahphalke 3:779bb1e55f1a 621 ad7124_register_map[AD7124_ADC_Control].value &= ~(AD7124_ADC_CTRL_REG_MODE(
mahphalke 3:779bb1e55f1a 622 0xf));
mahphalke 3:779bb1e55f1a 623
mahphalke 3:779bb1e55f1a 624 // read the channels, and store sample values
mahphalke 3:779bb1e55f1a 625 for (uint8_t loopCount = 0 ; ((was_escape_key_pressed() != true)
mahphalke 3:779bb1e55f1a 626 && (loopCount < channel_count)) ; loopCount++) {
mahphalke 3:779bb1e55f1a 627 toggle_activity_led();
mahphalke 3:779bb1e55f1a 628
mahphalke 3:779bb1e55f1a 629 // 1 = single conversion mode
mahphalke 3:779bb1e55f1a 630 ad7124_register_map[AD7124_ADC_Control].value |= AD7124_ADC_CTRL_REG_MODE(1);
mahphalke 3:779bb1e55f1a 631
mahphalke 3:779bb1e55f1a 632 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 633 ad7124_register_map[AD7124_ADC_Control])) < 0) {
mahphalke 3:779bb1e55f1a 634 printf("Error (%ld) setting AD7124 Single conversion mode.\r\n", error_code);
mahphalke 3:779bb1e55f1a 635 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 636 continue;
mahphalke 3:779bb1e55f1a 637 }
mahphalke 3:779bb1e55f1a 638
mahphalke 3:779bb1e55f1a 639 /*
mahphalke 3:779bb1e55f1a 640 * this polls the status register READY/ bit to determine when conversion is done
mahphalke 3:779bb1e55f1a 641 * this also ensures the STATUS register value is up to date and contains the
mahphalke 3:779bb1e55f1a 642 * channel that was sampled as well. No need to read STATUS separately
mahphalke 3:779bb1e55f1a 643 */
mahphalke 3:779bb1e55f1a 644 if ((error_code = ad7124_wait_for_conv_ready(pAd7124_dev, 10000)) < 0) {
mahphalke 3:779bb1e55f1a 645 printf("Error/Timeout waiting for conversion ready %ld\r\n", error_code);
mahphalke 3:779bb1e55f1a 646 continue;
mahphalke 3:779bb1e55f1a 647 }
mahphalke 3:779bb1e55f1a 648
mahphalke 3:779bb1e55f1a 649 if ((error_code = ad7124_read_data(pAd7124_dev, &sample_data)) < 0) {
mahphalke 3:779bb1e55f1a 650 printf("Error reading ADC Data (%ld).\r\n", error_code);
mahphalke 3:779bb1e55f1a 651 continue;
mahphalke 3:779bb1e55f1a 652 }
mahphalke 3:779bb1e55f1a 653 /*
mahphalke 3:779bb1e55f1a 654 * No error, need to process the sample, what channel has been read? update that channelSample
mahphalke 3:779bb1e55f1a 655 */
mahphalke 3:779bb1e55f1a 656 uint8_t channelRead = ad7124_register_map[AD7124_Status].value & 0x0000000F;
mahphalke 3:779bb1e55f1a 657
mahphalke 3:779bb1e55f1a 658 if (channelRead < AD7124_CHANNEL_COUNT) {
mahphalke 3:779bb1e55f1a 659 channel_samples[channelRead] = sample_data;
mahphalke 3:779bb1e55f1a 660 channel_samples_count[channelRead]++;
mahphalke 3:779bb1e55f1a 661
mahphalke 3:779bb1e55f1a 662 /* also need to clear the channel enable bit so the next single conversion cycle will sample the next channel */
mahphalke 3:779bb1e55f1a 663 ad7124_register_map[AD7124_Channel_0 + channelRead].value &=
mahphalke 3:779bb1e55f1a 664 ~AD7124_CH_MAP_REG_CH_ENABLE;
mahphalke 3:779bb1e55f1a 665 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 666 ad7124_register_map[AD7124_Channel_0 + channelRead])) < 0) {
mahphalke 3:779bb1e55f1a 667 printf("Error (%ld) Clearing channel %d Enable bit.\r\n", error_code,
mahphalke 3:779bb1e55f1a 668 channelRead);
mahphalke 3:779bb1e55f1a 669 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 670 continue;
mahphalke 3:779bb1e55f1a 671 }
mahphalke 3:779bb1e55f1a 672 } else {
mahphalke 3:779bb1e55f1a 673 printf("Channel Read was %d, which is not < AD7124_CHANNEL_COUNT\r\n",
mahphalke 3:779bb1e55f1a 674 channelRead);
mahphalke 3:779bb1e55f1a 675 }
mahphalke 3:779bb1e55f1a 676 }
mahphalke 3:779bb1e55f1a 677
mahphalke 3:779bb1e55f1a 678 // All done, ADC put into standby mode
mahphalke 3:779bb1e55f1a 679 ad7124_register_map[AD7124_ADC_Control].value &= ~(AD7124_ADC_CTRL_REG_MODE(
mahphalke 3:779bb1e55f1a 680 0xf));
mahphalke 3:779bb1e55f1a 681 // 2 = sleep/standby mode
mahphalke 3:779bb1e55f1a 682 ad7124_register_map[AD7124_ADC_Control].value |= AD7124_ADC_CTRL_REG_MODE(2);
mahphalke 3:779bb1e55f1a 683
mahphalke 3:779bb1e55f1a 684 // Need to restore the channels that were disabled during acquisition
mahphalke 3:779bb1e55f1a 685 for (uint8_t i = 0 ; i < AD7124_CHANNEL_COUNT ; i++) {
mahphalke 3:779bb1e55f1a 686 if (channel_enable_mask & (1 << i)) {
mahphalke 3:779bb1e55f1a 687 ad7124_register_map[AD7124_Channel_0 + i].value |= AD7124_CH_MAP_REG_CH_ENABLE;
mahphalke 3:779bb1e55f1a 688 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 689 ad7124_register_map[AD7124_Channel_0 + i])) < 0) {
mahphalke 3:779bb1e55f1a 690 printf("Error (%ld) Setting channel %d Enable bit.\r\r\n", error_code, i);
mahphalke 3:779bb1e55f1a 691 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 692 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 693 }
mahphalke 3:779bb1e55f1a 694 }
mahphalke 3:779bb1e55f1a 695 }
mahphalke 3:779bb1e55f1a 696
mahphalke 3:779bb1e55f1a 697 printf("Single Conversion completed...\r\n\r\n");
mahphalke 3:779bb1e55f1a 698 dislay_channel_samples(SHOW_ENABLED_CHANNELS, DISPLAY_DATA_TABULAR);
mahphalke 3:779bb1e55f1a 699
mahphalke 3:779bb1e55f1a 700 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 701 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 702 }
mahphalke 3:779bb1e55f1a 703
mahphalke 3:779bb1e55f1a 704
mahphalke 3:779bb1e55f1a 705 /*!
mahphalke 3:779bb1e55f1a 706 * @brief menu item that reads the status register the AD7124
mahphalke 3:779bb1e55f1a 707 *
mahphalke 3:779bb1e55f1a 708 * @details
mahphalke 3:779bb1e55f1a 709 */
mahphalke 3:779bb1e55f1a 710 static int32_t menu_read_status(void)
mahphalke 3:779bb1e55f1a 711 {
mahphalke 3:779bb1e55f1a 712 read_status_register();
mahphalke 3:779bb1e55f1a 713 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 714 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 715 }
mahphalke 3:779bb1e55f1a 716
mahphalke 3:779bb1e55f1a 717
mahphalke 3:779bb1e55f1a 718 /*!
mahphalke 3:779bb1e55f1a 719 * @brief reads the ID register on the AD7124
mahphalke 3:779bb1e55f1a 720 *
mahphalke 3:779bb1e55f1a 721 * @details
mahphalke 3:779bb1e55f1a 722 */
mahphalke 3:779bb1e55f1a 723 static int32_t menu_read_id(void)
mahphalke 3:779bb1e55f1a 724 {
mahphalke 3:779bb1e55f1a 725 if (ad7124_read_register(pAd7124_dev, &ad7124_register_map[AD7124_ID]) < 0) {
mahphalke 3:779bb1e55f1a 726 printf("\r\nError Encountered reading ID register\r\n");
mahphalke 3:779bb1e55f1a 727 } else {
mahphalke 3:779bb1e55f1a 728 printf("\r\nRead ID Register = 0x%02lx\r\n",
mahphalke 3:779bb1e55f1a 729 (uint32_t)ad7124_register_map[AD7124_ID].value);
mahphalke 3:779bb1e55f1a 730 }
mahphalke 3:779bb1e55f1a 731 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 732 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 733 }
mahphalke 3:779bb1e55f1a 734
mahphalke 3:779bb1e55f1a 735
mahphalke 3:779bb1e55f1a 736 /*!
mahphalke 3:779bb1e55f1a 737 * @brief Initialize the part with a specific configuration
mahphalke 3:779bb1e55f1a 738 *
mahphalke 3:779bb1e55f1a 739 * @details
mahphalke 3:779bb1e55f1a 740 */
mahphalke 3:779bb1e55f1a 741 static void init_with_configuration(uint8_t configID)
mahphalke 3:779bb1e55f1a 742 {
mahphalke 3:779bb1e55f1a 743 int32_t status = 0;
mahphalke 3:779bb1e55f1a 744
mahphalke 3:779bb1e55f1a 745 // Free the device resources
mahphalke 3:779bb1e55f1a 746 (void)gpio_remove(activity_led_desc);
mahphalke 3:779bb1e55f1a 747 (void)ad7124_remove(pAd7124_dev);
mahphalke 3:779bb1e55f1a 748
mahphalke 3:779bb1e55f1a 749 status = ad7124_app_initialize(configID);
mahphalke 3:779bb1e55f1a 750 if (status < 0) {
mahphalke 3:779bb1e55f1a 751 printf("\r\n\r\n Error setting Configuration %c \r\n\r\n",
mahphalke 3:779bb1e55f1a 752 (char)(configID + 'A'));
mahphalke 3:779bb1e55f1a 753 } else {
mahphalke 3:779bb1e55f1a 754 printf("\r\n\r\n Configuration %c Set\r\n\r\n", (char)(configID + 'A'));
mahphalke 3:779bb1e55f1a 755 }
mahphalke 3:779bb1e55f1a 756 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 757 }
mahphalke 3:779bb1e55f1a 758
mahphalke 3:779bb1e55f1a 759
mahphalke 3:779bb1e55f1a 760 /*
mahphalke 3:779bb1e55f1a 761 * @brief Sends a reset command on the SPI to reset the AD7124
mahphalke 3:779bb1e55f1a 762 *
mahphalke 3:779bb1e55f1a 763 * @details
mahphalke 3:779bb1e55f1a 764 */
mahphalke 3:779bb1e55f1a 765 static int32_t menu_reset(void)
mahphalke 3:779bb1e55f1a 766 {
mahphalke 3:779bb1e55f1a 767 if (ad7124_reset(pAd7124_dev) < 0) {
mahphalke 3:779bb1e55f1a 768 printf("\r\n\r\n Error performing Reset\r\n\r\n");
mahphalke 3:779bb1e55f1a 769 } else {
mahphalke 3:779bb1e55f1a 770 // Need to set the live register map to defaults as well
mahphalke 3:779bb1e55f1a 771 memcpy(ad7124_register_map, ad7124_regs, sizeof(ad7124_register_map));
mahphalke 3:779bb1e55f1a 772 printf("\r\n\r\n Reset Complete\r\n\r\n");
mahphalke 3:779bb1e55f1a 773 }
mahphalke 3:779bb1e55f1a 774 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 775 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 776 }
mahphalke 3:779bb1e55f1a 777
mahphalke 3:779bb1e55f1a 778
mahphalke 3:779bb1e55f1a 779 /*!
mahphalke 3:779bb1e55f1a 780 * @brief Reset and set the ad7124 with configuration A or B
mahphalke 3:779bb1e55f1a 781 *
mahphalke 3:779bb1e55f1a 782 * @details
mahphalke 3:779bb1e55f1a 783 */
mahphalke 3:779bb1e55f1a 784 static int32_t menu_reset_to_configuration(uint32_t config_type)
mahphalke 3:779bb1e55f1a 785 {
mahphalke 3:779bb1e55f1a 786 if (AD7124_CONFIG_A == config_type) {
mahphalke 3:779bb1e55f1a 787 init_with_configuration(AD7124_CONFIG_A);
mahphalke 3:779bb1e55f1a 788 } else {
mahphalke 3:779bb1e55f1a 789 init_with_configuration(AD7124_CONFIG_B);
mahphalke 3:779bb1e55f1a 790 }
mahphalke 3:779bb1e55f1a 791
mahphalke 3:779bb1e55f1a 792 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 793 }
mahphalke 3:779bb1e55f1a 794
mahphalke 3:779bb1e55f1a 795
mahphalke 3:779bb1e55f1a 796 /* @brief Scan the temparture value from the lookup table using binry search
mahphalke 3:779bb1e55f1a 797 *
mahphalke 3:779bb1e55f1a 798 * @param ADC conversion sample
mahphalke 3:779bb1e55f1a 799 * @return Temperature
mahphalke 3:779bb1e55f1a 800 **/
mahphalke 3:779bb1e55f1a 801 static int16_t scan_temperature(uint32_t value)
mahphalke 3:779bb1e55f1a 802 {
mahphalke 3:779bb1e55f1a 803 uint16_t key=0, start, end;
mahphalke 3:779bb1e55f1a 804 bool found = false;
mahphalke 3:779bb1e55f1a 805
mahphalke 3:779bb1e55f1a 806 start = 0;
mahphalke 3:779bb1e55f1a 807 end = sizeof(temperature_lookup) / sizeof(temperature_lookup[0]) - 1;
mahphalke 3:779bb1e55f1a 808
mahphalke 3:779bb1e55f1a 809 while ((start < end) && !found) {
mahphalke 3:779bb1e55f1a 810 key = (start + end) >> 1;
mahphalke 3:779bb1e55f1a 811
mahphalke 3:779bb1e55f1a 812 if (temperature_lookup[key].adc_sample == value) {
mahphalke 3:779bb1e55f1a 813 found = true;
mahphalke 3:779bb1e55f1a 814 } else if (value > temperature_lookup[key].adc_sample) {
mahphalke 3:779bb1e55f1a 815 start = key + 1;
mahphalke 3:779bb1e55f1a 816 } else if (value < temperature_lookup[key].adc_sample) {
mahphalke 3:779bb1e55f1a 817 end = key - 1;
mahphalke 3:779bb1e55f1a 818 } else {
mahphalke 3:779bb1e55f1a 819 break;
mahphalke 3:779bb1e55f1a 820 }
mahphalke 3:779bb1e55f1a 821 }
mahphalke 3:779bb1e55f1a 822
mahphalke 3:779bb1e55f1a 823 /* Return the scanned temperature value */
mahphalke 3:779bb1e55f1a 824 return temperature_lookup[key].temp;
mahphalke 3:779bb1e55f1a 825 }
mahphalke 3:779bb1e55f1a 826
mahphalke 3:779bb1e55f1a 827
mahphalke 3:779bb1e55f1a 828 /* @brief Console menu to read and display device temperature
mahphalke 3:779bb1e55f1a 829 *
mahphalke 3:779bb1e55f1a 830 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 831 **/
mahphalke 3:779bb1e55f1a 832 static int32_t menu_read_temperature(void)
mahphalke 3:779bb1e55f1a 833 {
mahphalke 3:779bb1e55f1a 834 int32_t error_code; // adc read/write error
mahphalke 3:779bb1e55f1a 835 uint8_t samples; // sample count
mahphalke 3:779bb1e55f1a 836 int32_t temp_readings; // temperature equivalent adc count
mahphalke 3:779bb1e55f1a 837 int16_t temperature = 0; // temperature read
mahphalke 3:779bb1e55f1a 838 uint8_t chn_cnt; // channel counter
mahphalke 3:779bb1e55f1a 839 uint16_t chn_mask = 0; // channels enable mask
mahphalke 3:779bb1e55f1a 840
mahphalke 3:779bb1e55f1a 841 /* Save the previous values of below registers in order to not disturb setup
mahphalke 3:779bb1e55f1a 842 * configured by the user (Note: Channel 0 is used for the temperature sensing) */
mahphalke 3:779bb1e55f1a 843 int32_t prev_adc_reg_values[] = {
mahphalke 3:779bb1e55f1a 844 ad7124_register_map[AD7124_Channel_0].value, // Channel_0 Register previous value
mahphalke 3:779bb1e55f1a 845 ad7124_register_map[AD7124_Config_0].value, // Config_0 Register previous value
mahphalke 3:779bb1e55f1a 846 ad7124_register_map[AD7124_ADC_Control].value // Control Register previous value
mahphalke 3:779bb1e55f1a 847 };
mahphalke 3:779bb1e55f1a 848
mahphalke 3:779bb1e55f1a 849 /* Disable the other enabled channels, to read temperature from only 0th channel */
mahphalke 3:779bb1e55f1a 850 for (chn_cnt = 1; chn_cnt < AD7124_MAX_CHANNELS; chn_cnt++) {
mahphalke 3:779bb1e55f1a 851 if ((error_code = ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 852 &ad7124_register_map[AD7124_Channel_0 + chn_cnt]) < 0)) {
mahphalke 3:779bb1e55f1a 853 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 854 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 855 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 856 }
mahphalke 3:779bb1e55f1a 857
mahphalke 3:779bb1e55f1a 858 if (ad7124_register_map[AD7124_Channel_0 + chn_cnt].value &
mahphalke 3:779bb1e55f1a 859 AD7124_CH_MAP_REG_CH_ENABLE) {
mahphalke 3:779bb1e55f1a 860 // Save enabled channel
mahphalke 3:779bb1e55f1a 861 chn_mask |= (1 << chn_cnt);
mahphalke 3:779bb1e55f1a 862
mahphalke 3:779bb1e55f1a 863 /* Disable the curent channel */
mahphalke 3:779bb1e55f1a 864 ad7124_register_map[AD7124_Channel_0 + chn_cnt].value &=
mahphalke 3:779bb1e55f1a 865 (~AD7124_CH_MAP_REG_CH_ENABLE);
mahphalke 3:779bb1e55f1a 866
mahphalke 3:779bb1e55f1a 867 /* Write to ADC channel register */
mahphalke 3:779bb1e55f1a 868 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 869 ad7124_register_map[AD7124_Channel_0 + chn_cnt]) < 0)) {
mahphalke 3:779bb1e55f1a 870 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 871 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 872 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 873 }
mahphalke 3:779bb1e55f1a 874 }
mahphalke 3:779bb1e55f1a 875 }
mahphalke 3:779bb1e55f1a 876
mahphalke 3:779bb1e55f1a 877 /* Channel 0 Selections: AINP= Temp (16), AINM= AVSS (17), Setup= 0, CHN Enabled= True */
mahphalke 3:779bb1e55f1a 878 ad7124_register_map[AD7124_Channel_0].value = (AD7124_CH_MAP_REG_AINP(
mahphalke 3:779bb1e55f1a 879 16) | AD7124_CH_MAP_REG_AINM(17) |
mahphalke 3:779bb1e55f1a 880 AD7124_CH_MAP_REG_SETUP(0) | AD7124_CH_MAP_REG_CH_ENABLE);
mahphalke 3:779bb1e55f1a 881
mahphalke 3:779bb1e55f1a 882 /* Write to ADC channel 0 register */
mahphalke 3:779bb1e55f1a 883 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 884 ad7124_register_map[AD7124_Channel_0]) < 0)) {
mahphalke 3:779bb1e55f1a 885 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 886 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 887 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 888 }
mahphalke 3:779bb1e55f1a 889
mahphalke 3:779bb1e55f1a 890 /* Setup 0 selections: Bipolar= 1, AINP/M Buffer= Enabled, Ref= EXT1(2.5v), Gain= 1 */
mahphalke 3:779bb1e55f1a 891 ad7124_register_map[AD7124_Config_0].value = (AD7124_CFG_REG_BIPOLAR |
mahphalke 3:779bb1e55f1a 892 AD7124_CFG_REG_AIN_BUFP |
mahphalke 3:779bb1e55f1a 893 AD7124_CFG_REG_AINN_BUFM | AD7124_CFG_REG_REF_SEL(0) |
mahphalke 3:779bb1e55f1a 894 AD7124_CFG_REG_PGA(0));
mahphalke 3:779bb1e55f1a 895
mahphalke 3:779bb1e55f1a 896 /* Write to ADC config 0 register */
mahphalke 3:779bb1e55f1a 897 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 898 ad7124_register_map[AD7124_Config_0]) < 0)) {
mahphalke 3:779bb1e55f1a 899 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 900 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 901 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 902 }
mahphalke 3:779bb1e55f1a 903
mahphalke 3:779bb1e55f1a 904 /* ADC operating mode: Single Conversion (masking off bits 5:2) */
mahphalke 3:779bb1e55f1a 905 ad7124_register_map[AD7124_ADC_Control].value = ((
mahphalke 3:779bb1e55f1a 906 ad7124_register_map[AD7124_ADC_Control].value &
mahphalke 3:779bb1e55f1a 907 ~AD7124_ADC_CTRL_REG_MSK) |
mahphalke 3:779bb1e55f1a 908 AD7124_ADC_CTRL_REG_MODE(1));
mahphalke 3:779bb1e55f1a 909
mahphalke 3:779bb1e55f1a 910 /* Write to ADC control register */
mahphalke 3:779bb1e55f1a 911 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 912 ad7124_register_map[AD7124_ADC_Control]) < 0)) {
mahphalke 3:779bb1e55f1a 913 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 914 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 915 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 916 }
mahphalke 3:779bb1e55f1a 917
mahphalke 3:779bb1e55f1a 918 printf("\r\n\r\n\tReading temperature...\r\n");
mahphalke 3:779bb1e55f1a 919
mahphalke 3:779bb1e55f1a 920 for (samples = 0; samples < 2; samples++) {
mahphalke 3:779bb1e55f1a 921 /* Wait for conversion to complete, then obtain sample */
mahphalke 3:779bb1e55f1a 922 ad7124_wait_for_conv_ready(pAd7124_dev, pAd7124_dev->spi_rdy_poll_cnt);
mahphalke 3:779bb1e55f1a 923
mahphalke 3:779bb1e55f1a 924 if ((error_code = ad7124_read_data(pAd7124_dev, &temp_readings) < 0)) {
mahphalke 3:779bb1e55f1a 925 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 926 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 927 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 928 }
mahphalke 3:779bb1e55f1a 929
mahphalke 3:779bb1e55f1a 930 //temp += ((temp_readings - 8388608) / 13584) - 272.5; // Use this for more precision
mahphalke 3:779bb1e55f1a 931 temperature += scan_temperature(temp_readings);
mahphalke 3:779bb1e55f1a 932 }
mahphalke 3:779bb1e55f1a 933
mahphalke 3:779bb1e55f1a 934 /* Get the averaged temperature value */
mahphalke 3:779bb1e55f1a 935 temperature >>= 1;
mahphalke 3:779bb1e55f1a 936
mahphalke 3:779bb1e55f1a 937 /* Validate temperaure range as specified in look-up table */
mahphalke 3:779bb1e55f1a 938 if (temperature >= -20 || temperature <= 50) {
mahphalke 3:779bb1e55f1a 939 printf("\r\n\tTemperature: %d Celcius\r\n", temperature);
mahphalke 3:779bb1e55f1a 940 } else {
mahphalke 3:779bb1e55f1a 941 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 942 }
mahphalke 3:779bb1e55f1a 943
mahphalke 3:779bb1e55f1a 944 /* Restore the ADC registers with previous values (i.e before modifying them for temperature sensing)
mahphalke 3:779bb1e55f1a 945 * Note: This needs to be done to not disturb the setup configured by user through console menus */
mahphalke 3:779bb1e55f1a 946 ad7124_register_map[AD7124_Channel_0].value = prev_adc_reg_values[0];
mahphalke 3:779bb1e55f1a 947 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_Channel_0]);
mahphalke 3:779bb1e55f1a 948
mahphalke 3:779bb1e55f1a 949 ad7124_register_map[AD7124_Config_0].value = prev_adc_reg_values[1];
mahphalke 3:779bb1e55f1a 950 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_Config_0]);
mahphalke 3:779bb1e55f1a 951
mahphalke 3:779bb1e55f1a 952 ad7124_register_map[AD7124_ADC_Control].value = prev_adc_reg_values[2];
mahphalke 3:779bb1e55f1a 953 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_ADC_Control]);
mahphalke 3:779bb1e55f1a 954
mahphalke 3:779bb1e55f1a 955 /* Enable the previously disabled channels */
mahphalke 3:779bb1e55f1a 956 for (chn_cnt = 1; chn_cnt < AD7124_MAX_CHANNELS; chn_cnt++) {
mahphalke 3:779bb1e55f1a 957 if ((chn_mask >> chn_cnt) & 0x01) {
mahphalke 3:779bb1e55f1a 958 ad7124_register_map[AD7124_Channel_0 + chn_cnt].value |=
mahphalke 3:779bb1e55f1a 959 AD7124_CH_MAP_REG_CH_ENABLE;
mahphalke 3:779bb1e55f1a 960
mahphalke 3:779bb1e55f1a 961 /* Write to ADC channel regiter */
mahphalke 3:779bb1e55f1a 962 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 963 ad7124_register_map[AD7124_Channel_0 + chn_cnt]) < 0)) {
mahphalke 3:779bb1e55f1a 964 printf("\r\n\tError reading temperature!!\r\n");
mahphalke 3:779bb1e55f1a 965 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 966 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 967 }
mahphalke 3:779bb1e55f1a 968 }
mahphalke 3:779bb1e55f1a 969 }
mahphalke 3:779bb1e55f1a 970
mahphalke 3:779bb1e55f1a 971 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 972 adi_clear_console();
mahphalke 3:779bb1e55f1a 973
mahphalke 3:779bb1e55f1a 974 return (MENU_CONTINUE);
mahphalke 3:779bb1e55f1a 975 }
mahphalke 3:779bb1e55f1a 976
mahphalke 3:779bb1e55f1a 977
mahphalke 3:779bb1e55f1a 978 /* @brief Console menu to select the power modes of adc
mahphalke 3:779bb1e55f1a 979 *
mahphalke 3:779bb1e55f1a 980 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 981 **/
mahphalke 3:779bb1e55f1a 982 static int32_t menu_power_modes_selection(uint32_t mode)
mahphalke 3:779bb1e55f1a 983 {
mahphalke 3:779bb1e55f1a 984 int32_t error_code;
mahphalke 3:779bb1e55f1a 985
mahphalke 3:779bb1e55f1a 986 ad7124_register_map[AD7124_ADC_Control].value =
mahphalke 3:779bb1e55f1a 987 ((ad7124_register_map[AD7124_ADC_Control].value &
mahphalke 3:779bb1e55f1a 988 ~AD7124_ADC_CTRL_REG_POWER_MODE_MSK) |
mahphalke 3:779bb1e55f1a 989 AD7124_ADC_CTRL_REG_POWER_MODE(mode));
mahphalke 3:779bb1e55f1a 990
mahphalke 3:779bb1e55f1a 991 /* Write to ADC channel regiter */
mahphalke 3:779bb1e55f1a 992 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 993 ad7124_register_map[AD7124_ADC_Control])) < 0) {
mahphalke 3:779bb1e55f1a 994 printf("\r\n\tError setting %s mode!!\r\n", power_modes_str[mode]);
mahphalke 3:779bb1e55f1a 995 } else {
mahphalke 3:779bb1e55f1a 996 power_mode = mode;
mahphalke 3:779bb1e55f1a 997 printf("\r\n\t%s mode selected...\r\n", power_modes_str[mode]);
mahphalke 3:779bb1e55f1a 998 }
mahphalke 3:779bb1e55f1a 999
mahphalke 3:779bb1e55f1a 1000 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1001 adi_clear_console();
mahphalke 3:779bb1e55f1a 1002
mahphalke 3:779bb1e55f1a 1003 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1004 }
mahphalke 3:779bb1e55f1a 1005
mahphalke 3:779bb1e55f1a 1006
mahphalke 3:779bb1e55f1a 1007 /* @brief Console menu to read the adc register
mahphalke 3:779bb1e55f1a 1008 *
mahphalke 3:779bb1e55f1a 1009 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 1010 **/
mahphalke 3:779bb1e55f1a 1011 static int32_t menu_rw_ad7124_register(uint32_t rw_id)
mahphalke 3:779bb1e55f1a 1012 {
mahphalke 3:779bb1e55f1a 1013 uint32_t reg_address;
mahphalke 3:779bb1e55f1a 1014 uint32_t reg_data;
mahphalke 3:779bb1e55f1a 1015 int32_t error_code;
mahphalke 3:779bb1e55f1a 1016
mahphalke 3:779bb1e55f1a 1017 printf("\r\n\tEnter the register address (in hex): ");
mahphalke 3:779bb1e55f1a 1018 reg_address = adi_get_hex_integer(sizeof(reg_address));
mahphalke 3:779bb1e55f1a 1019
mahphalke 3:779bb1e55f1a 1020 if ((uint32_t)DEVICE_REG_READ_ID == rw_id) {
mahphalke 3:779bb1e55f1a 1021 /* Read from ADC channel register */
mahphalke 3:779bb1e55f1a 1022 if ((reg_address >= AD7124_REG_NO) ||
mahphalke 3:779bb1e55f1a 1023 ((error_code = ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1024 &ad7124_register_map[reg_address])) < 0)) {
mahphalke 3:779bb1e55f1a 1025 printf("\r\n\tError in reading adc register!!\r\n");
mahphalke 3:779bb1e55f1a 1026 } else {
mahphalke 3:779bb1e55f1a 1027 reg_data = ad7124_register_map[reg_address].value;
mahphalke 3:779bb1e55f1a 1028 printf("\r\n\tRead Value: 0x%x", reg_data);
mahphalke 3:779bb1e55f1a 1029 }
mahphalke 3:779bb1e55f1a 1030 } else {
mahphalke 3:779bb1e55f1a 1031 printf("\r\n\tEnter the register data (in hex): ");
mahphalke 3:779bb1e55f1a 1032 reg_data = adi_get_hex_integer(sizeof(reg_data));
mahphalke 3:779bb1e55f1a 1033
mahphalke 3:779bb1e55f1a 1034 ad7124_register_map[reg_address].value = reg_data;
mahphalke 3:779bb1e55f1a 1035
mahphalke 3:779bb1e55f1a 1036 /* Write to ADC channel register */
mahphalke 3:779bb1e55f1a 1037 if ((reg_address >= AD7124_REG_NO) ||
mahphalke 3:779bb1e55f1a 1038 ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1039 ad7124_register_map[reg_address])) < 0)) {
mahphalke 3:779bb1e55f1a 1040 printf("\r\n\tError in writing adc register!!\r\n");
mahphalke 3:779bb1e55f1a 1041 } else {
mahphalke 3:779bb1e55f1a 1042 printf("\r\n\tWrite Successful...\r\n");
mahphalke 3:779bb1e55f1a 1043 }
mahphalke 3:779bb1e55f1a 1044 }
mahphalke 3:779bb1e55f1a 1045
mahphalke 3:779bb1e55f1a 1046 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1047 adi_clear_console();
mahphalke 3:779bb1e55f1a 1048
mahphalke 3:779bb1e55f1a 1049 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1050 }
mahphalke 3:779bb1e55f1a 1051
mahphalke 3:779bb1e55f1a 1052
mahphalke 3:779bb1e55f1a 1053 /* @brief Enable or disable adc channels
mahphalke 3:779bb1e55f1a 1054 *
mahphalke 3:779bb1e55f1a 1055 * @param uint32_t action- channel ENABLE/DISABLE action
mahphalke 3:779bb1e55f1a 1056 **/
mahphalke 3:779bb1e55f1a 1057 static void menu_channels_enable_disable(uint32_t action)
mahphalke 3:779bb1e55f1a 1058 {
mahphalke 3:779bb1e55f1a 1059 char rx_char; // received character from the serial port
mahphalke 3:779bb1e55f1a 1060 int32_t error_code; // data read/write error code
mahphalke 3:779bb1e55f1a 1061 uint8_t current_channel; // channel to be enabled
mahphalke 3:779bb1e55f1a 1062
mahphalke 3:779bb1e55f1a 1063 do {
mahphalke 3:779bb1e55f1a 1064 /* Get the channel selection */
mahphalke 3:779bb1e55f1a 1065 current_channel = get_channel_selection();
mahphalke 3:779bb1e55f1a 1066
mahphalke 3:779bb1e55f1a 1067 if (CHN_ENABLE == action) {
mahphalke 3:779bb1e55f1a 1068 /* Enable the selected channel */
mahphalke 3:779bb1e55f1a 1069 ad7124_register_map[AD7124_Channel_0 + current_channel].value |=
mahphalke 3:779bb1e55f1a 1070 AD7124_CH_MAP_REG_CH_ENABLE;
mahphalke 3:779bb1e55f1a 1071 printf("\tChannel %d is Enabled ", current_channel);
mahphalke 3:779bb1e55f1a 1072 } else {
mahphalke 3:779bb1e55f1a 1073 /* Disable the selected channel */
mahphalke 3:779bb1e55f1a 1074 ad7124_register_map[AD7124_Channel_0 + current_channel].value &=
mahphalke 3:779bb1e55f1a 1075 (~AD7124_CH_MAP_REG_CH_ENABLE);
mahphalke 3:779bb1e55f1a 1076 printf("\tChannel %d is Disabled ", current_channel);
mahphalke 3:779bb1e55f1a 1077 }
mahphalke 3:779bb1e55f1a 1078
mahphalke 3:779bb1e55f1a 1079 /* Write to ADC channel register */
mahphalke 3:779bb1e55f1a 1080 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1081 ad7124_register_map[AD7124_Channel_0 + current_channel]) < 0)) {
mahphalke 3:779bb1e55f1a 1082 printf("\tError in channel Enable/Disable!!\r\n");
mahphalke 3:779bb1e55f1a 1083 break;
mahphalke 3:779bb1e55f1a 1084 }
mahphalke 3:779bb1e55f1a 1085
mahphalke 3:779bb1e55f1a 1086 printf("\r\n\r\n\tDo you want to continue (y/n)?: ");
mahphalke 3:779bb1e55f1a 1087 rx_char = toupper(getchar());
mahphalke 3:779bb1e55f1a 1088
mahphalke 3:779bb1e55f1a 1089 if (rx_char != 'N' && rx_char != 'Y') {
mahphalke 3:779bb1e55f1a 1090 printf("Invalid entry!!\r\n");
mahphalke 3:779bb1e55f1a 1091 } else {
mahphalke 3:779bb1e55f1a 1092 /* Print the entered character back on console window (serial port) */
mahphalke 3:779bb1e55f1a 1093 printf("%c\r\n", rx_char);
mahphalke 3:779bb1e55f1a 1094 }
mahphalke 3:779bb1e55f1a 1095
mahphalke 3:779bb1e55f1a 1096 } while (rx_char != 'N');
mahphalke 3:779bb1e55f1a 1097 }
mahphalke 3:779bb1e55f1a 1098
mahphalke 3:779bb1e55f1a 1099
mahphalke 3:779bb1e55f1a 1100 /* @brief Assign setup to adc channel
mahphalke 3:779bb1e55f1a 1101 *
mahphalke 3:779bb1e55f1a 1102 * @param uint8_t setup- setup to be assigned
mahphalke 3:779bb1e55f1a 1103 **/
mahphalke 3:779bb1e55f1a 1104 static void assign_setup_to_channel(uint8_t setup)
mahphalke 3:779bb1e55f1a 1105 {
mahphalke 3:779bb1e55f1a 1106 uint8_t current_channel; // channel to be assigned with setup
mahphalke 3:779bb1e55f1a 1107 int32_t error_code; // data read/write error code
mahphalke 3:779bb1e55f1a 1108
mahphalke 3:779bb1e55f1a 1109 adi_clear_console();
mahphalke 3:779bb1e55f1a 1110
mahphalke 3:779bb1e55f1a 1111 /* Get the channel selection */
mahphalke 3:779bb1e55f1a 1112 current_channel = get_channel_selection();
mahphalke 3:779bb1e55f1a 1113
mahphalke 3:779bb1e55f1a 1114 /* Load the setup value */
mahphalke 3:779bb1e55f1a 1115 ad7124_register_map[AD7124_Channel_0 + current_channel].value =
mahphalke 3:779bb1e55f1a 1116 ((ad7124_register_map[AD7124_Channel_0 + current_channel].value &
mahphalke 3:779bb1e55f1a 1117 ~AD7124_CH_MAP_REG_SETUP_MSK) |
mahphalke 3:779bb1e55f1a 1118 AD7124_CH_MAP_REG_SETUP(setup));
mahphalke 3:779bb1e55f1a 1119
mahphalke 3:779bb1e55f1a 1120 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1121 ad7124_register_map[AD7124_Channel_0 + current_channel])) < 0) {
mahphalke 3:779bb1e55f1a 1122 printf("\r\n\tError in setup assignment!!\r\n");
mahphalke 3:779bb1e55f1a 1123 } else {
mahphalke 3:779bb1e55f1a 1124 printf("\r\n\tSetup %d is assigned to channel %d successfully...\r\n", setup,
mahphalke 3:779bb1e55f1a 1125 current_channel);
mahphalke 3:779bb1e55f1a 1126 }
mahphalke 3:779bb1e55f1a 1127
mahphalke 3:779bb1e55f1a 1128 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1129 }
mahphalke 3:779bb1e55f1a 1130
mahphalke 3:779bb1e55f1a 1131
mahphalke 3:779bb1e55f1a 1132 /* @brief Select adc channel to be assigned to setup
mahphalke 3:779bb1e55f1a 1133 *
mahphalke 3:779bb1e55f1a 1134 * @param uint8_t current_setup- setup
mahphalke 3:779bb1e55f1a 1135 **/
mahphalke 3:779bb1e55f1a 1136 static void select_chn_assignment(uint8_t current_setup)
mahphalke 3:779bb1e55f1a 1137 {
mahphalke 3:779bb1e55f1a 1138 bool current_selection_done = false;
mahphalke 3:779bb1e55f1a 1139 char rx_char;
mahphalke 3:779bb1e55f1a 1140
mahphalke 3:779bb1e55f1a 1141 do {
mahphalke 3:779bb1e55f1a 1142 printf("\r\n\r\n\tDo you want to assign setup to a channel (y/n)?: ");
mahphalke 3:779bb1e55f1a 1143 rx_char = toupper(getchar());
mahphalke 3:779bb1e55f1a 1144
mahphalke 3:779bb1e55f1a 1145 if (rx_char == 'Y') {
mahphalke 3:779bb1e55f1a 1146 assign_setup_to_channel(current_setup);
mahphalke 3:779bb1e55f1a 1147 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1148 } else if (rx_char == 'N') {
mahphalke 3:779bb1e55f1a 1149 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1150 } else {
mahphalke 3:779bb1e55f1a 1151 printf("\r\n\tInvalid entry!!");
mahphalke 3:779bb1e55f1a 1152 }
mahphalke 3:779bb1e55f1a 1153 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1154 }
mahphalke 3:779bb1e55f1a 1155
mahphalke 3:779bb1e55f1a 1156
mahphalke 3:779bb1e55f1a 1157 /* @brief Configure the setup and check if want to assign to channel
mahphalke 3:779bb1e55f1a 1158 *
mahphalke 3:779bb1e55f1a 1159 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 1160 **/
mahphalke 3:779bb1e55f1a 1161 static int32_t menu_config_and_assign_setup(void)
mahphalke 3:779bb1e55f1a 1162 {
mahphalke 3:779bb1e55f1a 1163 uint8_t current_setup; // current setup to be configured
mahphalke 3:779bb1e55f1a 1164 int32_t error_code; // data read/write error
mahphalke 3:779bb1e55f1a 1165
mahphalke 3:779bb1e55f1a 1166 adi_clear_console();
mahphalke 3:779bb1e55f1a 1167
mahphalke 3:779bb1e55f1a 1168 /* Get the current setup selection */
mahphalke 3:779bb1e55f1a 1169 current_setup = get_setup_selection();
mahphalke 3:779bb1e55f1a 1170
mahphalke 3:779bb1e55f1a 1171 /* Select the filter parameters */
mahphalke 3:779bb1e55f1a 1172 config_filter_parameters(&setup[current_setup]);
mahphalke 3:779bb1e55f1a 1173
mahphalke 3:779bb1e55f1a 1174 /* Select the analog input parameters */
mahphalke 3:779bb1e55f1a 1175 config_analog_inputs(&setup[current_setup]);
mahphalke 3:779bb1e55f1a 1176
mahphalke 3:779bb1e55f1a 1177 /* Select device gain */
mahphalke 3:779bb1e55f1a 1178 ad7124_register_map[AD7124_Config_0 + current_setup].value =
mahphalke 3:779bb1e55f1a 1179 ((ad7124_register_map[AD7124_Config_0 + current_setup].value &
mahphalke 3:779bb1e55f1a 1180 ~AD7124_CFG_REG_PGA_MSK) |
mahphalke 3:779bb1e55f1a 1181 AD7124_CFG_REG_PGA(setup[current_setup].programmable_gain_bits));
mahphalke 3:779bb1e55f1a 1182
mahphalke 3:779bb1e55f1a 1183 /* Select the polarity (bit 11) */
mahphalke 3:779bb1e55f1a 1184 if (setup[current_setup].polarity) {
mahphalke 3:779bb1e55f1a 1185 // Bipolar (1)
mahphalke 3:779bb1e55f1a 1186 ad7124_register_map[AD7124_Config_0 + current_setup].value |=
mahphalke 3:779bb1e55f1a 1187 AD7124_CFG_REG_BIPOLAR;
mahphalke 3:779bb1e55f1a 1188 } else {
mahphalke 3:779bb1e55f1a 1189 // Unipolar (0)
mahphalke 3:779bb1e55f1a 1190 ad7124_register_map[AD7124_Config_0 + current_setup].value &=
mahphalke 3:779bb1e55f1a 1191 (~AD7124_CFG_REG_BIPOLAR);
mahphalke 3:779bb1e55f1a 1192 }
mahphalke 3:779bb1e55f1a 1193
mahphalke 3:779bb1e55f1a 1194 /* Enable/Disable analog inputs AINP & AINM buffers */
mahphalke 3:779bb1e55f1a 1195 if (setup[current_setup].input_buffers) {
mahphalke 3:779bb1e55f1a 1196 // Buffers enabled (1)
mahphalke 3:779bb1e55f1a 1197 ad7124_register_map[AD7124_Config_0 + current_setup].value |=
mahphalke 3:779bb1e55f1a 1198 (AD7124_CFG_REG_AIN_BUFP |
mahphalke 3:779bb1e55f1a 1199 AD7124_CFG_REG_AINN_BUFM);
mahphalke 3:779bb1e55f1a 1200 } else {
mahphalke 3:779bb1e55f1a 1201 // Buffers disabled (0)
mahphalke 3:779bb1e55f1a 1202 ad7124_register_map[AD7124_Config_0 + current_setup].value &=
mahphalke 3:779bb1e55f1a 1203 (~AD7124_CFG_REG_AIN_BUFP &
mahphalke 3:779bb1e55f1a 1204 ~AD7124_CFG_REG_AINN_BUFM);
mahphalke 3:779bb1e55f1a 1205 }
mahphalke 3:779bb1e55f1a 1206
mahphalke 3:779bb1e55f1a 1207 /* Enable/Disable reference buffer */
mahphalke 3:779bb1e55f1a 1208 if (setup[current_setup].reference_buffers) {
mahphalke 3:779bb1e55f1a 1209 // Buffers enabled (1)
mahphalke 3:779bb1e55f1a 1210 ad7124_register_map[AD7124_Config_0 + current_setup].value |=
mahphalke 3:779bb1e55f1a 1211 (AD7124_CFG_REG_REF_BUFP |
mahphalke 3:779bb1e55f1a 1212 AD7124_CFG_REG_REF_BUFM);
mahphalke 3:779bb1e55f1a 1213 } else {
mahphalke 3:779bb1e55f1a 1214 // Buffers disabled (0)
mahphalke 3:779bb1e55f1a 1215 ad7124_register_map[AD7124_Config_0 + current_setup].value &=
mahphalke 3:779bb1e55f1a 1216 (~AD7124_CFG_REG_REF_BUFP &
mahphalke 3:779bb1e55f1a 1217 ~AD7124_CFG_REG_REF_BUFM);
mahphalke 3:779bb1e55f1a 1218 }
mahphalke 3:779bb1e55f1a 1219
mahphalke 3:779bb1e55f1a 1220 /* Select the reference source */
mahphalke 3:779bb1e55f1a 1221 ad7124_register_map[AD7124_Config_0 + current_setup].value =
mahphalke 3:779bb1e55f1a 1222 ((ad7124_register_map[AD7124_Config_0 + current_setup].value &
mahphalke 3:779bb1e55f1a 1223 ~AD7124_CFG_REG_REF_SEL_MSK) |
mahphalke 3:779bb1e55f1a 1224 AD7124_CFG_REG_REF_SEL(setup[current_setup].reference));
mahphalke 3:779bb1e55f1a 1225
mahphalke 3:779bb1e55f1a 1226
mahphalke 3:779bb1e55f1a 1227 /* Write to ADC config register */
mahphalke 3:779bb1e55f1a 1228 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1229 ad7124_register_map[AD7124_Config_0 + current_setup])) < 0) {
mahphalke 3:779bb1e55f1a 1230 printf("\r\n\tError in configuring device setup!!\r\n");
mahphalke 3:779bb1e55f1a 1231 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1232 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1233 }
mahphalke 3:779bb1e55f1a 1234
mahphalke 3:779bb1e55f1a 1235 /* Select filter type */
mahphalke 3:779bb1e55f1a 1236 ad7124_register_map[AD7124_Filter_0 + current_setup].value =
mahphalke 3:779bb1e55f1a 1237 ((ad7124_register_map[AD7124_Filter_0 + current_setup].value &
mahphalke 3:779bb1e55f1a 1238 ~AD7124_FILT_REG_FILTER_MSK) |
mahphalke 3:779bb1e55f1a 1239 AD7124_FILT_REG_FILTER(setup[current_setup].filter));
mahphalke 3:779bb1e55f1a 1240
mahphalke 3:779bb1e55f1a 1241 /* Set the data rate FS value */
mahphalke 3:779bb1e55f1a 1242 ad7124_register_map[AD7124_Filter_0 + current_setup].value =
mahphalke 3:779bb1e55f1a 1243 ((ad7124_register_map[AD7124_Filter_0 + current_setup].value &
mahphalke 3:779bb1e55f1a 1244 ~AD7124_FILT_REG_FS_MSK) |
mahphalke 3:779bb1e55f1a 1245 AD7124_FILT_REG_FS(setup[current_setup].data_rate_fs_val));
mahphalke 3:779bb1e55f1a 1246
mahphalke 3:779bb1e55f1a 1247 /* Write to ADC filter register */
mahphalke 3:779bb1e55f1a 1248 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1249 ad7124_register_map[AD7124_Filter_0 + current_setup])) < 0) {
mahphalke 3:779bb1e55f1a 1250 printf("\r\n\tError in configuring device setup!!\r\n");
mahphalke 3:779bb1e55f1a 1251 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1252 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1253 }
mahphalke 3:779bb1e55f1a 1254
mahphalke 3:779bb1e55f1a 1255 /* Print selections */
mahphalke 3:779bb1e55f1a 1256 printf("\r\n\r\n\tSetup %d is configured successfully =>\r\n", current_setup);
mahphalke 3:779bb1e55f1a 1257 printf("\r\n\tFilter Type: %s", filter_name[setup[current_setup].filter]);
mahphalke 3:779bb1e55f1a 1258 printf("\r\n\tData Rate: %f", filter_data_rate_raw);
mahphalke 3:779bb1e55f1a 1259 printf("\r\n\tGain: %u", gain_raw);
mahphalke 3:779bb1e55f1a 1260 printf("\r\n\tReference: %s", reference_name[setup[current_setup].reference]);
mahphalke 3:779bb1e55f1a 1261 printf("\r\n");
mahphalke 3:779bb1e55f1a 1262
mahphalke 3:779bb1e55f1a 1263 select_chn_assignment(current_setup);
mahphalke 3:779bb1e55f1a 1264
mahphalke 3:779bb1e55f1a 1265 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1266 }
mahphalke 3:779bb1e55f1a 1267
mahphalke 3:779bb1e55f1a 1268
mahphalke 3:779bb1e55f1a 1269 /* @brief Connect analog inputs (AINP & AINM) to a channel
mahphalke 3:779bb1e55f1a 1270 *
mahphalke 3:779bb1e55f1a 1271 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 1272 **/
mahphalke 3:779bb1e55f1a 1273 static int32_t menu_connect_input_to_channel(void)
mahphalke 3:779bb1e55f1a 1274 {
mahphalke 3:779bb1e55f1a 1275 uint32_t pos_analog_input; // positive analog input
mahphalke 3:779bb1e55f1a 1276 uint32_t neg_analog_input; // negative analog input
mahphalke 3:779bb1e55f1a 1277 uint32_t analog_input; // user entered analog input
mahphalke 3:779bb1e55f1a 1278 uint8_t current_channel; // current channel
mahphalke 3:779bb1e55f1a 1279 bool current_selection_done =
mahphalke 3:779bb1e55f1a 1280 false; // flag checking if current menu selection is done
mahphalke 3:779bb1e55f1a 1281 int32_t error_code; // data read/write error
mahphalke 3:779bb1e55f1a 1282
mahphalke 3:779bb1e55f1a 1283 adi_clear_console();
mahphalke 3:779bb1e55f1a 1284
mahphalke 3:779bb1e55f1a 1285 /* Get the channel selection */
mahphalke 3:779bb1e55f1a 1286 current_channel = get_channel_selection();
mahphalke 3:779bb1e55f1a 1287
mahphalke 3:779bb1e55f1a 1288 /* Select analog inputs (positive and negative) */
mahphalke 3:779bb1e55f1a 1289 for (uint8_t index = 0; index < 2; index++) {
mahphalke 3:779bb1e55f1a 1290 current_selection_done = false;
mahphalke 3:779bb1e55f1a 1291
mahphalke 3:779bb1e55f1a 1292 do {
mahphalke 3:779bb1e55f1a 1293 if (index == 0) {
mahphalke 3:779bb1e55f1a 1294 printf("\r\n\tEnter positive analog input- AINP <0-31>: ");
mahphalke 3:779bb1e55f1a 1295 } else {
mahphalke 3:779bb1e55f1a 1296 printf("\r\n\tEnter negative analog input- AINM <0-31>: ");
mahphalke 3:779bb1e55f1a 1297 }
mahphalke 3:779bb1e55f1a 1298
mahphalke 3:779bb1e55f1a 1299 analog_input = adi_get_decimal_int(sizeof(analog_input));
mahphalke 3:779bb1e55f1a 1300
mahphalke 3:779bb1e55f1a 1301 /* Validate channel selection */
mahphalke 3:779bb1e55f1a 1302 if (analog_input < MAX_ANALOG_INPUTS) {
mahphalke 3:779bb1e55f1a 1303 /* Break the loop by setting below flag */
mahphalke 3:779bb1e55f1a 1304 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1305
mahphalke 3:779bb1e55f1a 1306 if (index == 0) {
mahphalke 3:779bb1e55f1a 1307 pos_analog_input = analog_input;
mahphalke 3:779bb1e55f1a 1308 } else {
mahphalke 3:779bb1e55f1a 1309 neg_analog_input = analog_input;
mahphalke 3:779bb1e55f1a 1310 }
mahphalke 3:779bb1e55f1a 1311 } else {
mahphalke 3:779bb1e55f1a 1312 printf("\r\n\tInvalid analog input!!\r\n");
mahphalke 3:779bb1e55f1a 1313 }
mahphalke 3:779bb1e55f1a 1314
mahphalke 3:779bb1e55f1a 1315 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1316 }
mahphalke 3:779bb1e55f1a 1317
mahphalke 3:779bb1e55f1a 1318 /* Select positive analog input */
mahphalke 3:779bb1e55f1a 1319 ad7124_register_map[AD7124_Channel_0 + current_channel].value =
mahphalke 3:779bb1e55f1a 1320 ((ad7124_register_map[AD7124_Channel_0 + current_channel].value &
mahphalke 3:779bb1e55f1a 1321 ~AD7124_CH_MAP_REG_AINP_MSK) |
mahphalke 3:779bb1e55f1a 1322 AD7124_CH_MAP_REG_AINP(pos_analog_input));
mahphalke 3:779bb1e55f1a 1323
mahphalke 3:779bb1e55f1a 1324 /* Select negative analog input */
mahphalke 3:779bb1e55f1a 1325 ad7124_register_map[AD7124_Channel_0 + current_channel].value =
mahphalke 3:779bb1e55f1a 1326 ((ad7124_register_map[AD7124_Channel_0 + current_channel].value &
mahphalke 3:779bb1e55f1a 1327 ~AD7124_CH_MAP_REG_AINM_MSK) |
mahphalke 3:779bb1e55f1a 1328 AD7124_CH_MAP_REG_AINM(neg_analog_input));
mahphalke 3:779bb1e55f1a 1329
mahphalke 3:779bb1e55f1a 1330 /* Write to ADC channel register */
mahphalke 3:779bb1e55f1a 1331 if ((error_code = ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1332 ad7124_register_map[AD7124_Channel_0 + current_channel])) < 0) {
mahphalke 3:779bb1e55f1a 1333 printf("\r\n\tError in analog input connection!!\r\n");
mahphalke 3:779bb1e55f1a 1334 } else {
mahphalke 3:779bb1e55f1a 1335 printf("\r\n\tAIN%d is connected to AINP and AIN%d is connectd to AINM for channel %d\r\n\r\n",
mahphalke 3:779bb1e55f1a 1336 pos_analog_input,
mahphalke 3:779bb1e55f1a 1337 neg_analog_input,
mahphalke 3:779bb1e55f1a 1338 current_channel);
mahphalke 3:779bb1e55f1a 1339 }
mahphalke 3:779bb1e55f1a 1340
mahphalke 3:779bb1e55f1a 1341 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1342
mahphalke 3:779bb1e55f1a 1343 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1344 }
mahphalke 3:779bb1e55f1a 1345
mahphalke 3:779bb1e55f1a 1346
mahphalke 3:779bb1e55f1a 1347 /* @brief display and handle console menu for calibrating adc
mahphalke 3:779bb1e55f1a 1348 *
mahphalke 3:779bb1e55f1a 1349 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 1350 **/
mahphalke 3:779bb1e55f1a 1351 static int32_t menu_calibrate_adc(void)
mahphalke 3:779bb1e55f1a 1352 {
mahphalke 3:779bb1e55f1a 1353 int32_t adc_control_reg_val;
mahphalke 3:779bb1e55f1a 1354 int32_t cal_error;
mahphalke 3:779bb1e55f1a 1355 int32_t error_code;
mahphalke 3:779bb1e55f1a 1356 uint8_t chn_cnt;
mahphalke 3:779bb1e55f1a 1357 uint8_t channel_enable_mask = 0;
mahphalke 3:779bb1e55f1a 1358
mahphalke 3:779bb1e55f1a 1359 adi_clear_console();
mahphalke 3:779bb1e55f1a 1360
mahphalke 3:779bb1e55f1a 1361 // Save the ADC control register
mahphalke 3:779bb1e55f1a 1362 ad7124_read_register(pAd7124_dev, &ad7124_register_map[AD7124_ADC_Control]);
mahphalke 3:779bb1e55f1a 1363 adc_control_reg_val = ad7124_register_map[AD7124_ADC_Control].value;
mahphalke 3:779bb1e55f1a 1364
mahphalke 3:779bb1e55f1a 1365 // Enable calibration error monitoring
mahphalke 3:779bb1e55f1a 1366 ad7124_register_map[AD7124_Error_En].value |= AD7124_ERREN_REG_ADC_CAL_ERR_EN;
mahphalke 3:779bb1e55f1a 1367 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_Error_En]);
mahphalke 3:779bb1e55f1a 1368
mahphalke 3:779bb1e55f1a 1369 // Need to store which channels are enabled in this config so it can be restored
mahphalke 3:779bb1e55f1a 1370 for(chn_cnt = 0 ; chn_cnt < AD7124_MAX_CHANNELS ; chn_cnt++) {
mahphalke 3:779bb1e55f1a 1371 ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1372 &ad7124_register_map[AD7124_Channel_0 + chn_cnt]);
mahphalke 3:779bb1e55f1a 1373 if (ad7124_register_map[AD7124_Channel_0 + chn_cnt].value &
mahphalke 3:779bb1e55f1a 1374 AD7124_CH_MAP_REG_CH_ENABLE) {
mahphalke 3:779bb1e55f1a 1375 channel_enable_mask |= (1 << chn_cnt);
mahphalke 3:779bb1e55f1a 1376
mahphalke 3:779bb1e55f1a 1377 /* Disable the curent channel */
mahphalke 3:779bb1e55f1a 1378 ad7124_register_map[AD7124_Channel_0 + chn_cnt].value &=
mahphalke 3:779bb1e55f1a 1379 (~AD7124_CH_MAP_REG_CH_ENABLE);
mahphalke 3:779bb1e55f1a 1380 ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1381 ad7124_register_map[AD7124_Channel_0 + chn_cnt]);
mahphalke 3:779bb1e55f1a 1382 }
mahphalke 3:779bb1e55f1a 1383 }
mahphalke 3:779bb1e55f1a 1384
mahphalke 3:779bb1e55f1a 1385 // Calibrate all the channels
mahphalke 3:779bb1e55f1a 1386 for (chn_cnt = 0 ; chn_cnt < AD7124_MAX_CHANNELS ; chn_cnt++) {
mahphalke 3:779bb1e55f1a 1387 printf("\r\n\tCalibrating Channel %d => \r\n", chn_cnt);
mahphalke 3:779bb1e55f1a 1388
mahphalke 3:779bb1e55f1a 1389 // Enable current channel
mahphalke 3:779bb1e55f1a 1390 ad7124_register_map[AD7124_Channel_0 + chn_cnt].value |=
mahphalke 3:779bb1e55f1a 1391 AD7124_CH_MAP_REG_CH_ENABLE;
mahphalke 3:779bb1e55f1a 1392 ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1393 ad7124_register_map[AD7124_Channel_0 + chn_cnt]);
mahphalke 3:779bb1e55f1a 1394
mahphalke 3:779bb1e55f1a 1395 // Write 0x800000 to offset register before full-scale calibration
mahphalke 3:779bb1e55f1a 1396 ad7124_register_map[AD7124_Offset_0 + chn_cnt].value = 0x800000;
mahphalke 3:779bb1e55f1a 1397 ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1398 ad7124_register_map[AD7124_Offset_0 + chn_cnt]);
mahphalke 3:779bb1e55f1a 1399
mahphalke 3:779bb1e55f1a 1400 // Start full scale internal calibration (mode: 6)
mahphalke 3:779bb1e55f1a 1401 printf("\tRunning full-scale internal calibration...\r\n");
mahphalke 3:779bb1e55f1a 1402 ad7124_register_map[AD7124_ADC_Control].value =
mahphalke 3:779bb1e55f1a 1403 ((ad7124_register_map[AD7124_ADC_Control].value & ~AD7124_ADC_CTRL_REG_MSK) | \
mahphalke 3:779bb1e55f1a 1404 AD7124_ADC_CTRL_REG_MODE(6));
mahphalke 3:779bb1e55f1a 1405 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_ADC_Control]);
mahphalke 3:779bb1e55f1a 1406
mahphalke 3:779bb1e55f1a 1407 /* Wait for calibration to over */
mahphalke 3:779bb1e55f1a 1408 if ((error_code = ad7124_wait_for_conv_ready(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1409 pAd7124_dev->spi_rdy_poll_cnt)) < 0) {
mahphalke 3:779bb1e55f1a 1410 printf("\tError in calibration...\r\n");
mahphalke 3:779bb1e55f1a 1411 } else {
mahphalke 3:779bb1e55f1a 1412 // Start zero scale internal calibration (mode: 5)
mahphalke 3:779bb1e55f1a 1413 printf("\tRunning zero-scale internal calibration...\r\n");
mahphalke 3:779bb1e55f1a 1414 ad7124_register_map[AD7124_ADC_Control].value =
mahphalke 3:779bb1e55f1a 1415 ((ad7124_register_map[AD7124_ADC_Control].value & ~AD7124_ADC_CTRL_REG_MSK) | \
mahphalke 3:779bb1e55f1a 1416 AD7124_ADC_CTRL_REG_MODE(5));
mahphalke 3:779bb1e55f1a 1417 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_ADC_Control]);
mahphalke 3:779bb1e55f1a 1418
mahphalke 3:779bb1e55f1a 1419 // Wait for calibration to over
mahphalke 3:779bb1e55f1a 1420 if((error_code = ad7124_wait_for_conv_ready(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1421 pAd7124_dev->spi_rdy_poll_cnt)) < 0) {
mahphalke 3:779bb1e55f1a 1422 printf("\tError in calibration...\r\n");
mahphalke 3:779bb1e55f1a 1423 } else {
mahphalke 3:779bb1e55f1a 1424 // Check for any calibration error (bit 18 of AD7124_ERROR register)
mahphalke 3:779bb1e55f1a 1425 ad7124_read_register(pAd7124_dev, &ad7124_register_map[AD7124_Error]);
mahphalke 3:779bb1e55f1a 1426 cal_error = ((ad7124_register_map[AD7124_Error].value >> 18) & 0x01);
mahphalke 3:779bb1e55f1a 1427
mahphalke 3:779bb1e55f1a 1428 if (!cal_error) {
mahphalke 3:779bb1e55f1a 1429 printf("\tCalibration Successful...\r\n");
mahphalke 3:779bb1e55f1a 1430 } else {
mahphalke 3:779bb1e55f1a 1431 printf("\tError in calibration...\r\n");
mahphalke 3:779bb1e55f1a 1432 }
mahphalke 3:779bb1e55f1a 1433 }
mahphalke 3:779bb1e55f1a 1434 }
mahphalke 3:779bb1e55f1a 1435
mahphalke 3:779bb1e55f1a 1436 // Disable current channel
mahphalke 3:779bb1e55f1a 1437 ad7124_register_map[AD7124_Channel_0 + chn_cnt].value &=
mahphalke 3:779bb1e55f1a 1438 (~AD7124_CH_MAP_REG_CH_ENABLE);
mahphalke 3:779bb1e55f1a 1439 ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1440 ad7124_register_map[AD7124_Channel_0 + chn_cnt]);
mahphalke 3:779bb1e55f1a 1441 }
mahphalke 3:779bb1e55f1a 1442
mahphalke 3:779bb1e55f1a 1443 // Need to restore the channels that were disabled during calibration
mahphalke 3:779bb1e55f1a 1444 for (chn_cnt = 0 ; chn_cnt < AD7124_MAX_CHANNELS ; chn_cnt++) {
mahphalke 3:779bb1e55f1a 1445 if (channel_enable_mask & (1 << chn_cnt)) {
mahphalke 3:779bb1e55f1a 1446 ad7124_register_map[AD7124_Channel_0 + chn_cnt].value |=
mahphalke 3:779bb1e55f1a 1447 AD7124_CH_MAP_REG_CH_ENABLE;
mahphalke 3:779bb1e55f1a 1448 ad7124_write_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1449 ad7124_register_map[AD7124_Channel_0 + chn_cnt]);
mahphalke 3:779bb1e55f1a 1450 }
mahphalke 3:779bb1e55f1a 1451 }
mahphalke 3:779bb1e55f1a 1452
mahphalke 3:779bb1e55f1a 1453 // Write back previous value of control register
mahphalke 3:779bb1e55f1a 1454 ad7124_register_map[AD7124_ADC_Control].value = adc_control_reg_val;
mahphalke 3:779bb1e55f1a 1455 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_ADC_Control]);
mahphalke 3:779bb1e55f1a 1456
mahphalke 3:779bb1e55f1a 1457 // Disable calibration error monitoring
mahphalke 3:779bb1e55f1a 1458 ad7124_register_map[AD7124_Error_En].value &=
mahphalke 3:779bb1e55f1a 1459 (~AD7124_ERREN_REG_ADC_CAL_ERR_EN);
mahphalke 3:779bb1e55f1a 1460 ad7124_write_register(pAd7124_dev, ad7124_register_map[AD7124_Error_En]);
mahphalke 3:779bb1e55f1a 1461
mahphalke 3:779bb1e55f1a 1462 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1463 adi_clear_console();
mahphalke 3:779bb1e55f1a 1464
mahphalke 3:779bb1e55f1a 1465 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1466 }
mahphalke 3:779bb1e55f1a 1467
mahphalke 3:779bb1e55f1a 1468
mahphalke 3:779bb1e55f1a 1469 /* @brief Display the setup
mahphalke 3:779bb1e55f1a 1470 *
mahphalke 3:779bb1e55f1a 1471 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 1472 **/
mahphalke 3:779bb1e55f1a 1473 static uint8_t menu_display_setup(void)
mahphalke 3:779bb1e55f1a 1474 {
mahphalke 3:779bb1e55f1a 1475 ad7124_setup_config device_setup; // setup to be displayed
mahphalke 3:779bb1e55f1a 1476 float filter_data_rate; // Filter data rate in SPS
mahphalke 3:779bb1e55f1a 1477 uint8_t setup_cnt; // setup counter
mahphalke 3:779bb1e55f1a 1478 uint8_t chn_cnt; // channel counter
mahphalke 3:779bb1e55f1a 1479 int32_t error_code; // data read/write error
mahphalke 3:779bb1e55f1a 1480 uint8_t power_mode_index; // Index pointing to power mode string
mahphalke 3:779bb1e55f1a 1481
mahphalke 3:779bb1e55f1a 1482 printf("\r\n\t---------------------------------------\r\n");
mahphalke 3:779bb1e55f1a 1483 printf("\r\n");
mahphalke 3:779bb1e55f1a 1484
mahphalke 3:779bb1e55f1a 1485 /* Extract and print the power mode */
mahphalke 3:779bb1e55f1a 1486 (void)ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1487 &ad7124_register_map[AD7124_ADC_Control]);
mahphalke 3:779bb1e55f1a 1488 power_mode_index =
mahphalke 3:779bb1e55f1a 1489 AD7124_ADC_CTRL_REG_POWER_MODE_RD(
mahphalke 3:779bb1e55f1a 1490 ad7124_register_map[AD7124_ADC_Control].value);
mahphalke 3:779bb1e55f1a 1491 printf("\tPower Mode: %s\r\n", power_modes_str[power_mode_index]);
mahphalke 3:779bb1e55f1a 1492
mahphalke 3:779bb1e55f1a 1493 printf("\r\n");
mahphalke 3:779bb1e55f1a 1494 printf("\t---------------------------------------\r\n");
mahphalke 3:779bb1e55f1a 1495 printf("\tChannel# | Status | Setup | AINP | AINM\r\n");
mahphalke 3:779bb1e55f1a 1496 printf("\t---------------------------------------\r\n");
mahphalke 3:779bb1e55f1a 1497
mahphalke 3:779bb1e55f1a 1498 for (chn_cnt = 0; chn_cnt < AD7124_MAX_CHANNELS; chn_cnt++) {
mahphalke 3:779bb1e55f1a 1499 /* Read the channel register */
mahphalke 3:779bb1e55f1a 1500 if ((error_code = ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1501 &ad7124_register_map[AD7124_Channel_0 + chn_cnt])) < 0) {
mahphalke 3:779bb1e55f1a 1502 printf("\r\nError reading setup!!\r\n");
mahphalke 3:779bb1e55f1a 1503 break;
mahphalke 3:779bb1e55f1a 1504 }
mahphalke 3:779bb1e55f1a 1505
mahphalke 3:779bb1e55f1a 1506 /* Extract the channel parameters from device register read value */
mahphalke 3:779bb1e55f1a 1507
mahphalke 3:779bb1e55f1a 1508 device_setup.channel_enabled =
mahphalke 3:779bb1e55f1a 1509 AD7124_CH_MAP_REG_CH_ENABLE_RD(ad7124_register_map[AD7124_Channel_0 +
mahphalke 3:779bb1e55f1a 1510 chn_cnt].value);
mahphalke 3:779bb1e55f1a 1511
mahphalke 3:779bb1e55f1a 1512 device_setup.setup_assigned =
mahphalke 3:779bb1e55f1a 1513 AD7124_CH_MAP_REG_SETUP_RD(ad7124_register_map[AD7124_Channel_0 +
mahphalke 3:779bb1e55f1a 1514 chn_cnt].value);
mahphalke 3:779bb1e55f1a 1515
mahphalke 3:779bb1e55f1a 1516 device_setup.pos_analog_input =
mahphalke 3:779bb1e55f1a 1517 AD7124_CH_MAP_REG_AINP_RD(ad7124_register_map[AD7124_Channel_0 +
mahphalke 3:779bb1e55f1a 1518 chn_cnt].value);
mahphalke 3:779bb1e55f1a 1519
mahphalke 3:779bb1e55f1a 1520 device_setup.neg_analog_input =
mahphalke 3:779bb1e55f1a 1521 AD7124_CH_MAP_REG_AINM_RD(ad7124_register_map[AD7124_Channel_0 +
mahphalke 3:779bb1e55f1a 1522 chn_cnt].value);
mahphalke 3:779bb1e55f1a 1523
mahphalke 3:779bb1e55f1a 1524 // Channel# | Status | Setup | AINP | AINM
mahphalke 3:779bb1e55f1a 1525 printf("\t%4d %13s %4d %7d %6d\r\n",
mahphalke 3:779bb1e55f1a 1526 chn_cnt,
mahphalke 3:779bb1e55f1a 1527 enable_disable_status[device_setup.channel_enabled],
mahphalke 3:779bb1e55f1a 1528 device_setup.setup_assigned,
mahphalke 3:779bb1e55f1a 1529 device_setup.pos_analog_input,
mahphalke 3:779bb1e55f1a 1530 device_setup.neg_analog_input);
mahphalke 3:779bb1e55f1a 1531 }
mahphalke 3:779bb1e55f1a 1532
mahphalke 3:779bb1e55f1a 1533 printf("\r\n");
mahphalke 3:779bb1e55f1a 1534 printf("\t-----------------------------------------------------------------------------------------------------------\r\n");
mahphalke 3:779bb1e55f1a 1535 printf("\tSetup# | Filter Type | Data Rate | AIN_BUFP | AIN_BUFM | REF_BUFP | REF_BUFM | Polarity | Gain | REF SOURCE\r\n");
mahphalke 3:779bb1e55f1a 1536 printf("\t-----------------------------------------------------------------------------------------------------------\r\n");
mahphalke 3:779bb1e55f1a 1537
mahphalke 3:779bb1e55f1a 1538 for (setup_cnt = 0; setup_cnt < AD7124_MAX_SETUPS; setup_cnt++) {
mahphalke 3:779bb1e55f1a 1539 /* Read the filter register */
mahphalke 3:779bb1e55f1a 1540 if ((error_code = ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1541 &ad7124_register_map[AD7124_Filter_0 + setup_cnt])) < 0) {
mahphalke 3:779bb1e55f1a 1542 printf("\r\nError reading setup!!\r\n");
mahphalke 3:779bb1e55f1a 1543 break;
mahphalke 3:779bb1e55f1a 1544 }
mahphalke 3:779bb1e55f1a 1545
mahphalke 3:779bb1e55f1a 1546 /* Read the config register */
mahphalke 3:779bb1e55f1a 1547 if ((error_code = ad7124_read_register(pAd7124_dev,
mahphalke 3:779bb1e55f1a 1548 &ad7124_register_map[AD7124_Config_0 + setup_cnt])) < 0) {
mahphalke 3:779bb1e55f1a 1549 printf("\r\nError reading setup!!\r\n");
mahphalke 3:779bb1e55f1a 1550 break;
mahphalke 3:779bb1e55f1a 1551 }
mahphalke 3:779bb1e55f1a 1552
mahphalke 3:779bb1e55f1a 1553 /* Extract the setup parameters from device register read value */
mahphalke 3:779bb1e55f1a 1554
mahphalke 3:779bb1e55f1a 1555 device_setup.filter =
mahphalke 3:779bb1e55f1a 1556 AD7124_FILT_REG_FILTER_RD(ad7124_register_map[AD7124_Filter_0 +
mahphalke 3:779bb1e55f1a 1557 setup_cnt].value);
mahphalke 3:779bb1e55f1a 1558
mahphalke 3:779bb1e55f1a 1559 device_setup.data_rate_fs_val =
mahphalke 3:779bb1e55f1a 1560 AD7124_FILT_REG_FS_RD(ad7124_register_map[AD7124_Filter_0 + setup_cnt].value);
mahphalke 3:779bb1e55f1a 1561
mahphalke 3:779bb1e55f1a 1562 device_setup.input_buffers =
mahphalke 3:779bb1e55f1a 1563 (AD7124_CFG_REG_AIN_BUFP_RD(ad7124_register_map[AD7124_Config_0 +
mahphalke 3:779bb1e55f1a 1564 setup_cnt].value) << 1) |
mahphalke 3:779bb1e55f1a 1565 (AD7124_CFG_REG_AINM_BUFP_RD(ad7124_register_map[AD7124_Config_0 +
mahphalke 3:779bb1e55f1a 1566 setup_cnt].value));
mahphalke 3:779bb1e55f1a 1567
mahphalke 3:779bb1e55f1a 1568 device_setup.reference_buffers =
mahphalke 3:779bb1e55f1a 1569 (AD7124_CFG_REG_REF_BUFP_RD(ad7124_register_map[AD7124_Config_0 +
mahphalke 3:779bb1e55f1a 1570 setup_cnt].value) << 1) |
mahphalke 3:779bb1e55f1a 1571 (AD7124_CFG_REG_REF_BUFM_RD(ad7124_register_map[AD7124_Config_0 +
mahphalke 3:779bb1e55f1a 1572 setup_cnt].value));
mahphalke 3:779bb1e55f1a 1573
mahphalke 3:779bb1e55f1a 1574 device_setup.polarity =
mahphalke 3:779bb1e55f1a 1575 AD7124_CFG_REG_BIPOLAR_RD(ad7124_register_map[AD7124_Config_0 +
mahphalke 3:779bb1e55f1a 1576 setup_cnt].value);
mahphalke 3:779bb1e55f1a 1577
mahphalke 3:779bb1e55f1a 1578 device_setup.programmable_gain_bits =
mahphalke 3:779bb1e55f1a 1579 AD7124_CFG_REG_PGA_RD(ad7124_register_map[AD7124_Config_0 + setup_cnt].value);
mahphalke 3:779bb1e55f1a 1580
mahphalke 3:779bb1e55f1a 1581 device_setup.reference =
mahphalke 3:779bb1e55f1a 1582 AD7124_CFG_REG_REF_SEL_RD(ad7124_register_map[AD7124_Config_0 +
mahphalke 3:779bb1e55f1a 1583 setup_cnt].value);
mahphalke 3:779bb1e55f1a 1584
mahphalke 3:779bb1e55f1a 1585 filter_data_rate = calculate_data_rate(device_setup.filter, power_mode,
mahphalke 3:779bb1e55f1a 1586 (float)device_setup.data_rate_fs_val);
mahphalke 3:779bb1e55f1a 1587
mahphalke 3:779bb1e55f1a 1588 // Setup# | Filter Type | Data Rate | AIN_BUFP | AIN_BUFM | REF_BUFP | REF_BUFM | Polarity | Gain | REF
mahphalke 3:779bb1e55f1a 1589 printf("\t%4d %15s %10.2f %12s %10s %10s %10s %10s %5d %12s\r\n",
mahphalke 3:779bb1e55f1a 1590 setup_cnt,
mahphalke 3:779bb1e55f1a 1591 filter_name[device_setup.filter],
mahphalke 3:779bb1e55f1a 1592 filter_data_rate,
mahphalke 3:779bb1e55f1a 1593 enable_disable_status[(device_setup.input_buffers >> 1) & 0x01],
mahphalke 3:779bb1e55f1a 1594 enable_disable_status[(device_setup.input_buffers & 0x01)],
mahphalke 3:779bb1e55f1a 1595 enable_disable_status[(device_setup.reference_buffers >> 1) & 0x01],
mahphalke 3:779bb1e55f1a 1596 enable_disable_status[device_setup.reference_buffers & 0x01],
mahphalke 3:779bb1e55f1a 1597 polarity_status[device_setup.polarity],
mahphalke 3:779bb1e55f1a 1598 p_gain[device_setup.programmable_gain_bits],
mahphalke 3:779bb1e55f1a 1599 reference_name[device_setup.reference]);
mahphalke 3:779bb1e55f1a 1600 }
mahphalke 3:779bb1e55f1a 1601
mahphalke 3:779bb1e55f1a 1602 adi_press_any_key_to_continue();
mahphalke 3:779bb1e55f1a 1603
mahphalke 3:779bb1e55f1a 1604 return MENU_CONTINUE;
mahphalke 3:779bb1e55f1a 1605 }
mahphalke 3:779bb1e55f1a 1606
mahphalke 3:779bb1e55f1a 1607
mahphalke 3:779bb1e55f1a 1608 /* @brief Configure the analog inputs for polarity and input buffers
mahphalke 3:779bb1e55f1a 1609 *
mahphalke 3:779bb1e55f1a 1610 * @param ad7124_setup_config *psetup- pointer to setup to be assigned
mahphalke 3:779bb1e55f1a 1611 **/
mahphalke 3:779bb1e55f1a 1612 static void config_analog_inputs(ad7124_setup_config *psetup)
mahphalke 3:779bb1e55f1a 1613 {
mahphalke 3:779bb1e55f1a 1614 bool current_selection_done =
mahphalke 3:779bb1e55f1a 1615 false; // flag checking if current menu selection is done
mahphalke 3:779bb1e55f1a 1616 uint32_t polarity; // Polarity of ADC channel
mahphalke 3:779bb1e55f1a 1617 uint32_t input_buffers; // Analog input buffers enable/disable status
mahphalke 3:779bb1e55f1a 1618 uint32_t ref_buffers; // Reference buffers enable/disable status
mahphalke 3:779bb1e55f1a 1619 uint32_t reference_sel; // Reference source selection
mahphalke 3:779bb1e55f1a 1620
mahphalke 3:779bb1e55f1a 1621 /* Polarity selection */
mahphalke 3:779bb1e55f1a 1622 do {
mahphalke 3:779bb1e55f1a 1623 printf("\r\n\tSelect the polarity <0: Unipolar, 1: Bipolar>: ");
mahphalke 3:779bb1e55f1a 1624 polarity = adi_get_decimal_int(sizeof(polarity));
mahphalke 3:779bb1e55f1a 1625
mahphalke 3:779bb1e55f1a 1626 /* Validate the range for polarity values */
mahphalke 3:779bb1e55f1a 1627 if (polarity <= 1) {
mahphalke 3:779bb1e55f1a 1628 psetup->polarity = (uint8_t)polarity;
mahphalke 3:779bb1e55f1a 1629 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1630 } else {
mahphalke 3:779bb1e55f1a 1631 printf("\r\n\tPolarity out of range!!\r\n");
mahphalke 3:779bb1e55f1a 1632 }
mahphalke 3:779bb1e55f1a 1633
mahphalke 3:779bb1e55f1a 1634 } while (current_selection_done == false) ;
mahphalke 3:779bb1e55f1a 1635
mahphalke 3:779bb1e55f1a 1636
mahphalke 3:779bb1e55f1a 1637 /* Input buffer enable/disable selection */
mahphalke 3:779bb1e55f1a 1638 current_selection_done = false;
mahphalke 3:779bb1e55f1a 1639
mahphalke 3:779bb1e55f1a 1640 do {
mahphalke 3:779bb1e55f1a 1641 printf("\r\n\tEnable the AINP/M Buffers <0: Disable, 1: Enable>: ");
mahphalke 3:779bb1e55f1a 1642 input_buffers = adi_get_decimal_int(sizeof(input_buffers));
mahphalke 3:779bb1e55f1a 1643
mahphalke 3:779bb1e55f1a 1644 /* Validate the range for input buffer values */
mahphalke 3:779bb1e55f1a 1645 if (input_buffers <= 1) {
mahphalke 3:779bb1e55f1a 1646 psetup->input_buffers = (uint8_t)input_buffers;
mahphalke 3:779bb1e55f1a 1647 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1648 } else {
mahphalke 3:779bb1e55f1a 1649 printf("\r\n\tInvalid selection !!\r\n");
mahphalke 3:779bb1e55f1a 1650 }
mahphalke 3:779bb1e55f1a 1651
mahphalke 3:779bb1e55f1a 1652 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1653
mahphalke 3:779bb1e55f1a 1654
mahphalke 3:779bb1e55f1a 1655 /* Reference buffer enable/disable selection */
mahphalke 3:779bb1e55f1a 1656 current_selection_done = false;
mahphalke 3:779bb1e55f1a 1657
mahphalke 3:779bb1e55f1a 1658 do {
mahphalke 3:779bb1e55f1a 1659 printf("\r\n\tEnable the Reference Buffers <0: Disable, 1: Enable>: ");
mahphalke 3:779bb1e55f1a 1660 ref_buffers = adi_get_decimal_int(sizeof(ref_buffers));
mahphalke 3:779bb1e55f1a 1661
mahphalke 3:779bb1e55f1a 1662 /* Validate the range for reference buffer values */
mahphalke 3:779bb1e55f1a 1663 if (ref_buffers <= 1) {
mahphalke 3:779bb1e55f1a 1664 psetup->reference_buffers = (uint8_t)ref_buffers;
mahphalke 3:779bb1e55f1a 1665 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1666 } else {
mahphalke 3:779bb1e55f1a 1667 printf("\r\n\tInvalid selection !!\r\n");
mahphalke 3:779bb1e55f1a 1668 }
mahphalke 3:779bb1e55f1a 1669
mahphalke 3:779bb1e55f1a 1670 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1671
mahphalke 3:779bb1e55f1a 1672
mahphalke 3:779bb1e55f1a 1673 /* Input buffer enable/disable selection */
mahphalke 3:779bb1e55f1a 1674 current_selection_done = false;
mahphalke 3:779bb1e55f1a 1675
mahphalke 3:779bb1e55f1a 1676 do {
mahphalke 3:779bb1e55f1a 1677 printf("\r\n\tEnter the reference source: ");
mahphalke 3:779bb1e55f1a 1678 printf("\r\n\t[0] %s \r\n\t[1] %s \r\n\t[2] %s \r\n\t[3] %s \r\n\t",
mahphalke 3:779bb1e55f1a 1679 reference_name[0],
mahphalke 3:779bb1e55f1a 1680 reference_name[1],
mahphalke 3:779bb1e55f1a 1681 reference_name[2],
mahphalke 3:779bb1e55f1a 1682 reference_name[3]);
mahphalke 3:779bb1e55f1a 1683
mahphalke 3:779bb1e55f1a 1684 reference_sel = adi_get_decimal_int(sizeof(reference_sel));
mahphalke 3:779bb1e55f1a 1685
mahphalke 3:779bb1e55f1a 1686 /* Validate the range for reference sources (0:3) */
mahphalke 3:779bb1e55f1a 1687 if (reference_sel <= 3) {
mahphalke 3:779bb1e55f1a 1688 psetup->reference = (uint8_t)reference_sel;
mahphalke 3:779bb1e55f1a 1689 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1690 } else {
mahphalke 3:779bb1e55f1a 1691 printf("\r\n\tInvalid selection !!\r\n");
mahphalke 3:779bb1e55f1a 1692 }
mahphalke 3:779bb1e55f1a 1693
mahphalke 3:779bb1e55f1a 1694 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1695 }
mahphalke 3:779bb1e55f1a 1696
mahphalke 3:779bb1e55f1a 1697
mahphalke 3:779bb1e55f1a 1698 /* @brief Calculate the data rate based on data rate FS value and vice a versa
mahphalke 3:779bb1e55f1a 1699 *
mahphalke 3:779bb1e55f1a 1700 * @param filter_type filter- Filter Type
mahphalke 3:779bb1e55f1a 1701 * @param uint8_t power_mode- Power mode of device
mahphalke 3:779bb1e55f1a 1702 * @param float data_rate- Actual Data Rate
mahphalke 3:779bb1e55f1a 1703 **/
mahphalke 3:779bb1e55f1a 1704 static float calculate_data_rate(filter_type filter, uint8_t power_mode,
mahphalke 3:779bb1e55f1a 1705 float data_rate)
mahphalke 3:779bb1e55f1a 1706 {
mahphalke 3:779bb1e55f1a 1707 float calc_data_rate = 120.0; // default data rate
mahphalke 3:779bb1e55f1a 1708
mahphalke 3:779bb1e55f1a 1709 // The data rate selection depends upon the power mode and device frequency.
mahphalke 3:779bb1e55f1a 1710 // fadc = Output data rate, fclk = master clock frequency
mahphalke 3:779bb1e55f1a 1711 // fclk = 614.4Khz (full power), 153.6Khz (mid power), 76.8Khz (low power)
mahphalke 3:779bb1e55f1a 1712 // fadc = 9.38SPS to 19200SPS (full power), 2.35SPS to 4800SPS (mid power),
mahphalke 3:779bb1e55f1a 1713 // 1.17SPS to 2400SPS for low power
mahphalke 3:779bb1e55f1a 1714
mahphalke 3:779bb1e55f1a 1715 // Calculate FS value for SINC4 and SINC3 filter
mahphalke 3:779bb1e55f1a 1716 // FS[10:0] = fclk / (32 * fadc) OR fadc = fclk / (32 * FS[10:0])
mahphalke 3:779bb1e55f1a 1717 if (filter == SINC4_FILTER || filter == SINC3_FILTER) {
mahphalke 3:779bb1e55f1a 1718 if (power_mode == FULL_POWER_MODE) {
mahphalke 3:779bb1e55f1a 1719 calc_data_rate = (FUL_POWER_MODE_FREQUENCY / (data_rate * 32));
mahphalke 3:779bb1e55f1a 1720 } else if (power_mode == MED_POWER_MODE) {
mahphalke 3:779bb1e55f1a 1721 calc_data_rate = (MED_POWER_MODE_FREQUENCY / (data_rate * 32));
mahphalke 3:779bb1e55f1a 1722 } else {
mahphalke 3:779bb1e55f1a 1723 // Low power mode (default)
mahphalke 3:779bb1e55f1a 1724 calc_data_rate = (LOW_POWER_MODE_FREQUENCY / (data_rate * 32));
mahphalke 3:779bb1e55f1a 1725 }
mahphalke 3:779bb1e55f1a 1726 }
mahphalke 3:779bb1e55f1a 1727
mahphalke 3:779bb1e55f1a 1728 // Calculate FS value for fast settling SINC4 filter
mahphalke 3:779bb1e55f1a 1729 // FS[10:0] = fclk / ((4+Avg-1) * 32 * fadc) Or fadc = fclk / ((4+Avg-1) * 32 * FS[10:0])
mahphalke 3:779bb1e55f1a 1730 // Avg = 16 for Full and Med power mode, 8 for low power mode
mahphalke 3:779bb1e55f1a 1731 if (filter == FAST_SETTLING_SINC4_FILTER) {
mahphalke 3:779bb1e55f1a 1732 if (power_mode == FULL_POWER_MODE) {
mahphalke 3:779bb1e55f1a 1733 calc_data_rate = (FUL_POWER_MODE_FREQUENCY / (19 * (data_rate * 32)));
mahphalke 3:779bb1e55f1a 1734 } else if (power_mode == MED_POWER_MODE) {
mahphalke 3:779bb1e55f1a 1735 calc_data_rate = (MED_POWER_MODE_FREQUENCY / (19 * (data_rate * 32)));
mahphalke 3:779bb1e55f1a 1736 } else {
mahphalke 3:779bb1e55f1a 1737 // Low power mode (default)
mahphalke 3:779bb1e55f1a 1738 calc_data_rate = (LOW_POWER_MODE_FREQUENCY / (11 * (data_rate * 32)));
mahphalke 3:779bb1e55f1a 1739 }
mahphalke 3:779bb1e55f1a 1740 }
mahphalke 3:779bb1e55f1a 1741
mahphalke 3:779bb1e55f1a 1742 // Calculate FS value for fast settling SINC3 filter
mahphalke 3:779bb1e55f1a 1743 // FS[10:0] = fclk / ((3+Avg-1) * 32 * fadc) Or fadc = fclk / ((3+Avg-1) * 32 * FS[10:0])
mahphalke 3:779bb1e55f1a 1744 // Avg = 16 for Full and Med power mode, 8 for low power mode
mahphalke 3:779bb1e55f1a 1745 if (filter == FAST_SETTLING_SINC3_FILTER) {
mahphalke 3:779bb1e55f1a 1746 if (power_mode == FULL_POWER_MODE) {
mahphalke 3:779bb1e55f1a 1747 calc_data_rate = (FUL_POWER_MODE_FREQUENCY / (18 * (data_rate * 32)));
mahphalke 3:779bb1e55f1a 1748 } else if (power_mode == MED_POWER_MODE) {
mahphalke 3:779bb1e55f1a 1749 calc_data_rate = (MED_POWER_MODE_FREQUENCY / (18 * (data_rate * 32)));
mahphalke 3:779bb1e55f1a 1750 } else {
mahphalke 3:779bb1e55f1a 1751 // Low power mode (default)
mahphalke 3:779bb1e55f1a 1752 calc_data_rate = (LOW_POWER_MODE_FREQUENCY / (10 * (data_rate * 32)));
mahphalke 3:779bb1e55f1a 1753 }
mahphalke 3:779bb1e55f1a 1754 }
mahphalke 3:779bb1e55f1a 1755
mahphalke 3:779bb1e55f1a 1756 return calc_data_rate;
mahphalke 3:779bb1e55f1a 1757 }
mahphalke 3:779bb1e55f1a 1758
mahphalke 3:779bb1e55f1a 1759
mahphalke 3:779bb1e55f1a 1760 /* @brief Configure the filter parameters
mahphalke 3:779bb1e55f1a 1761 *
mahphalke 3:779bb1e55f1a 1762 * @param ad7124_setup_config *psetup- pointer to setup to be assigned
mahphalke 3:779bb1e55f1a 1763 **/
mahphalke 3:779bb1e55f1a 1764 static void config_filter_parameters(ad7124_setup_config *psetup)
mahphalke 3:779bb1e55f1a 1765 {
mahphalke 3:779bb1e55f1a 1766 bool current_selection_done =
mahphalke 3:779bb1e55f1a 1767 false; // flag checking if current menu selection is done
mahphalke 3:779bb1e55f1a 1768 uint32_t filter_type = NUM_OF_FILTERS; // filter type selection
mahphalke 3:779bb1e55f1a 1769 uint16_t data_rate_fs = MAX_FILTER_DATA_RATE_FS +
mahphalke 3:779bb1e55f1a 1770 1; // filter data rate FS value
mahphalke 3:779bb1e55f1a 1771 uint8_t gain_bits_value = 0; // gain bits value
mahphalke 3:779bb1e55f1a 1772
mahphalke 3:779bb1e55f1a 1773 do {
mahphalke 3:779bb1e55f1a 1774 printf("\r\n\tEnter the filter type selection: ");
mahphalke 3:779bb1e55f1a 1775 printf("\r\n\t[0] %s \r\n\t[1] %s \r\n\t[2] %s \r\n\t[3] %s \r\n\t",
mahphalke 3:779bb1e55f1a 1776 filter_name[SINC4_FILTER],
mahphalke 3:779bb1e55f1a 1777 filter_name[SINC3_FILTER],
mahphalke 3:779bb1e55f1a 1778 filter_name[FAST_SETTLING_SINC4_FILTER],
mahphalke 3:779bb1e55f1a 1779 filter_name[FAST_SETTLING_SINC3_FILTER]);
mahphalke 3:779bb1e55f1a 1780
mahphalke 3:779bb1e55f1a 1781 filter_type = adi_get_decimal_int(sizeof(filter_type));
mahphalke 3:779bb1e55f1a 1782
mahphalke 3:779bb1e55f1a 1783 /* Check for valid menu item selection (menu keys 0:3) */
mahphalke 3:779bb1e55f1a 1784 if (filter_type <= 3) {
mahphalke 3:779bb1e55f1a 1785 switch (filter_type) {
mahphalke 3:779bb1e55f1a 1786 case 0:
mahphalke 3:779bb1e55f1a 1787 psetup->filter = SINC4_FILTER;
mahphalke 3:779bb1e55f1a 1788 break;
mahphalke 3:779bb1e55f1a 1789
mahphalke 3:779bb1e55f1a 1790 case 1:
mahphalke 3:779bb1e55f1a 1791 psetup->filter = SINC3_FILTER;
mahphalke 3:779bb1e55f1a 1792 break;
mahphalke 3:779bb1e55f1a 1793
mahphalke 3:779bb1e55f1a 1794 case 2:
mahphalke 3:779bb1e55f1a 1795 psetup->filter = FAST_SETTLING_SINC4_FILTER;
mahphalke 3:779bb1e55f1a 1796 break;
mahphalke 3:779bb1e55f1a 1797
mahphalke 3:779bb1e55f1a 1798 case 3:
mahphalke 3:779bb1e55f1a 1799 psetup->filter = FAST_SETTLING_SINC3_FILTER;
mahphalke 3:779bb1e55f1a 1800 break;
mahphalke 3:779bb1e55f1a 1801
mahphalke 3:779bb1e55f1a 1802 default:
mahphalke 3:779bb1e55f1a 1803 psetup->filter = SINC4_FILTER;
mahphalke 3:779bb1e55f1a 1804 break;
mahphalke 3:779bb1e55f1a 1805 }
mahphalke 3:779bb1e55f1a 1806
mahphalke 3:779bb1e55f1a 1807 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1808 } else {
mahphalke 3:779bb1e55f1a 1809 printf("\r\n\tInvalid filter type selection!!\r\n");
mahphalke 3:779bb1e55f1a 1810 }
mahphalke 3:779bb1e55f1a 1811
mahphalke 3:779bb1e55f1a 1812 } while (current_selection_done == false) ;
mahphalke 3:779bb1e55f1a 1813
mahphalke 3:779bb1e55f1a 1814
mahphalke 3:779bb1e55f1a 1815 /* Data rate selection */
mahphalke 3:779bb1e55f1a 1816 current_selection_done = false;
mahphalke 3:779bb1e55f1a 1817
mahphalke 3:779bb1e55f1a 1818 /* Get the data rate for the selected filter except SINC3 Post filter, which
mahphalke 3:779bb1e55f1a 1819 * has fixed data rates selectable from bits 19:17 of filter register */
mahphalke 3:779bb1e55f1a 1820 while (current_selection_done == false) {
mahphalke 3:779bb1e55f1a 1821 printf("\r\n\tEnter the filter Data Rate (in SPS): ");
mahphalke 3:779bb1e55f1a 1822 filter_data_rate_raw = adi_get_decimal_float(sizeof(filter_data_rate_raw) * 2);
mahphalke 3:779bb1e55f1a 1823
mahphalke 3:779bb1e55f1a 1824 // Get the value and round off to nearest integer
mahphalke 3:779bb1e55f1a 1825 data_rate_fs = (uint16_t)(calculate_data_rate(psetup->filter, power_mode,
mahphalke 3:779bb1e55f1a 1826 filter_data_rate_raw) + 0.5);
mahphalke 3:779bb1e55f1a 1827
mahphalke 3:779bb1e55f1a 1828 /* Validate entered filter data range */
mahphalke 3:779bb1e55f1a 1829 if (data_rate_fs >= MIN_FILTER_DATA_RATE
mahphalke 3:779bb1e55f1a 1830 && data_rate_fs <= MAX_FILTER_DATA_RATE_FS) {
mahphalke 3:779bb1e55f1a 1831 psetup->data_rate_fs_val = data_rate_fs;
mahphalke 3:779bb1e55f1a 1832 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1833 } else {
mahphalke 3:779bb1e55f1a 1834 printf("\r\n\tData rate out of range!!\r\n");
mahphalke 3:779bb1e55f1a 1835 }
mahphalke 3:779bb1e55f1a 1836 }
mahphalke 3:779bb1e55f1a 1837
mahphalke 3:779bb1e55f1a 1838
mahphalke 3:779bb1e55f1a 1839 /* Select the gain factor for the filter */
mahphalke 3:779bb1e55f1a 1840 current_selection_done = false;
mahphalke 3:779bb1e55f1a 1841
mahphalke 3:779bb1e55f1a 1842 do {
mahphalke 3:779bb1e55f1a 1843 printf("\r\n\tSelect the programmable gain <1-128>: ");
mahphalke 3:779bb1e55f1a 1844 gain_raw = adi_get_decimal_int(sizeof(gain_raw));
mahphalke 3:779bb1e55f1a 1845
mahphalke 3:779bb1e55f1a 1846 // Get the gain bits value
mahphalke 3:779bb1e55f1a 1847 for(gain_bits_value = 0 ; gain_bits_value <= MAX_GAIN_BITS_VALUE ;
mahphalke 3:779bb1e55f1a 1848 gain_bits_value++) {
mahphalke 3:779bb1e55f1a 1849 if (gain_raw == p_gain[gain_bits_value]) {
mahphalke 3:779bb1e55f1a 1850 psetup->programmable_gain_bits = gain_bits_value;
mahphalke 3:779bb1e55f1a 1851 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1852 break;
mahphalke 3:779bb1e55f1a 1853 }
mahphalke 3:779bb1e55f1a 1854 }
mahphalke 3:779bb1e55f1a 1855
mahphalke 3:779bb1e55f1a 1856 /* Validate the range for gain values */
mahphalke 3:779bb1e55f1a 1857 if (!current_selection_done) {
mahphalke 3:779bb1e55f1a 1858 printf("\r\n\tGain out of range!!\r\n");
mahphalke 3:779bb1e55f1a 1859 }
mahphalke 3:779bb1e55f1a 1860
mahphalke 3:779bb1e55f1a 1861 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1862 }
mahphalke 3:779bb1e55f1a 1863
mahphalke 3:779bb1e55f1a 1864
mahphalke 3:779bb1e55f1a 1865 /* @brief Get the channel selection
mahphalke 3:779bb1e55f1a 1866 *
mahphalke 3:779bb1e55f1a 1867 * @return uint8- Selected channel
mahphalke 3:779bb1e55f1a 1868 **/
mahphalke 3:779bb1e55f1a 1869 static uint8_t get_channel_selection(void)
mahphalke 3:779bb1e55f1a 1870 {
mahphalke 3:779bb1e55f1a 1871 uint32_t current_channel = AD7124_MAX_CHANNELS;
mahphalke 3:779bb1e55f1a 1872 bool current_selection_done = false;
mahphalke 3:779bb1e55f1a 1873
mahphalke 3:779bb1e55f1a 1874 do {
mahphalke 3:779bb1e55f1a 1875 printf("\r\n\tEnter Channel Value <0-15>: ");
mahphalke 3:779bb1e55f1a 1876 current_channel = adi_get_decimal_int(sizeof(current_channel));
mahphalke 3:779bb1e55f1a 1877
mahphalke 3:779bb1e55f1a 1878 /* Validate channel selection */
mahphalke 3:779bb1e55f1a 1879 if (current_channel < AD7124_MAX_CHANNELS) {
mahphalke 3:779bb1e55f1a 1880 /* Break the loop by setting below flag */
mahphalke 3:779bb1e55f1a 1881 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1882 } else {
mahphalke 3:779bb1e55f1a 1883 printf("\r\n\tInvalid channel selection!!\r\n");
mahphalke 3:779bb1e55f1a 1884 }
mahphalke 3:779bb1e55f1a 1885
mahphalke 3:779bb1e55f1a 1886 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1887
mahphalke 3:779bb1e55f1a 1888 return current_channel;
mahphalke 3:779bb1e55f1a 1889 }
mahphalke 3:779bb1e55f1a 1890
mahphalke 3:779bb1e55f1a 1891
mahphalke 3:779bb1e55f1a 1892 /* @brief Get the setup selection
mahphalke 3:779bb1e55f1a 1893 *
mahphalke 3:779bb1e55f1a 1894 * @return uint8- Selected setup
mahphalke 3:779bb1e55f1a 1895 **/
mahphalke 3:779bb1e55f1a 1896 static uint8_t get_setup_selection(void)
mahphalke 3:779bb1e55f1a 1897 {
mahphalke 3:779bb1e55f1a 1898 uint32_t current_setup = AD7124_MAX_SETUPS;
mahphalke 3:779bb1e55f1a 1899 bool current_selection_done = false;
mahphalke 3:779bb1e55f1a 1900
mahphalke 3:779bb1e55f1a 1901 /* Setup selection */
mahphalke 3:779bb1e55f1a 1902 do {
mahphalke 3:779bb1e55f1a 1903 printf("\r\n\tEnter Setup Selection <0-7>: ");
mahphalke 3:779bb1e55f1a 1904 current_setup = adi_get_decimal_int(sizeof(current_setup));
mahphalke 3:779bb1e55f1a 1905
mahphalke 3:779bb1e55f1a 1906 if (current_setup < AD7124_MAX_SETUPS) {
mahphalke 3:779bb1e55f1a 1907 current_selection_done = true;
mahphalke 3:779bb1e55f1a 1908 } else {
mahphalke 3:779bb1e55f1a 1909 printf("\r\n\tInvalid setup selection!!\r\n");
mahphalke 3:779bb1e55f1a 1910 }
mahphalke 3:779bb1e55f1a 1911
mahphalke 3:779bb1e55f1a 1912 } while (current_selection_done == false);
mahphalke 3:779bb1e55f1a 1913
mahphalke 3:779bb1e55f1a 1914 return current_setup;
mahphalke 3:779bb1e55f1a 1915 }
mahphalke 3:779bb1e55f1a 1916
mahphalke 3:779bb1e55f1a 1917
mahphalke 3:779bb1e55f1a 1918 /*
mahphalke 3:779bb1e55f1a 1919 * Definition of the power mode Menu Items and menu itself
mahphalke 3:779bb1e55f1a 1920 */
mahphalke 3:779bb1e55f1a 1921 static console_menu_item power_mode_menu_items[] = {
mahphalke 3:779bb1e55f1a 1922 { "Low Power Mode", 'L', menu_power_modes_selection, (uint32_t)LOW_POWER_MODE },
mahphalke 3:779bb1e55f1a 1923 { "Med Power Mode", 'M', menu_power_modes_selection, (uint32_t)MED_POWER_MODE },
mahphalke 3:779bb1e55f1a 1924 { "Full Power Mode", 'F', menu_power_modes_selection, (uint32_t)FULL_POWER_MODE },
mahphalke 3:779bb1e55f1a 1925 };
mahphalke 3:779bb1e55f1a 1926
mahphalke 3:779bb1e55f1a 1927 static console_menu power_mode_menu = {
mahphalke 3:779bb1e55f1a 1928 .title = "Power Mode Selection Menu",
mahphalke 3:779bb1e55f1a 1929 .items = power_mode_menu_items,
mahphalke 3:779bb1e55f1a 1930 .itemCount = ARRAY_SIZE(power_mode_menu_items),
mahphalke 3:779bb1e55f1a 1931 .headerItem = NULL,
mahphalke 3:779bb1e55f1a 1932 .footerItem = NULL,
mahphalke 3:779bb1e55f1a 1933 .enableEscapeKey = true
mahphalke 3:779bb1e55f1a 1934 };
mahphalke 3:779bb1e55f1a 1935
mahphalke 3:779bb1e55f1a 1936 /*
mahphalke 3:779bb1e55f1a 1937 * Definition of the Sampling Menu Items and menu itself
mahphalke 3:779bb1e55f1a 1938 */
mahphalke 3:779bb1e55f1a 1939 static console_menu_item acquisition_menu_items[] = {
mahphalke 3:779bb1e55f1a 1940 { "Single Conversion Mode", 'S', menu_single_conversion },
mahphalke 3:779bb1e55f1a 1941 { "Continuous Conversion Mode - Table View", 'T', menu_continuous_conversion_tabular },
mahphalke 3:779bb1e55f1a 1942 { "Continuous Conversion Mode - Stream Data", 'C', menu_continuous_conversion_stream },
mahphalke 3:779bb1e55f1a 1943 };
mahphalke 3:779bb1e55f1a 1944
mahphalke 3:779bb1e55f1a 1945 static console_menu acquisition_menu = {
mahphalke 3:779bb1e55f1a 1946 .title = "Data Acquisition Menu",
mahphalke 3:779bb1e55f1a 1947 .items = acquisition_menu_items,
mahphalke 3:779bb1e55f1a 1948 .itemCount = ARRAY_SIZE(acquisition_menu_items),
mahphalke 3:779bb1e55f1a 1949 .headerItem = NULL,
mahphalke 3:779bb1e55f1a 1950 .footerItem = NULL,
mahphalke 3:779bb1e55f1a 1951 .enableEscapeKey = true
mahphalke 3:779bb1e55f1a 1952 };
mahphalke 3:779bb1e55f1a 1953
mahphalke 3:779bb1e55f1a 1954 /*
mahphalke 3:779bb1e55f1a 1955 * Definition of the channel enable/disable Menu Items and menu itself
mahphalke 3:779bb1e55f1a 1956 */
mahphalke 3:779bb1e55f1a 1957 static console_menu_item chn_enable_disable_items[] = {
mahphalke 3:779bb1e55f1a 1958 { "Enable Channels", 'E', menu_channels_enable_disable, (uint32_t)CHN_ENABLE },
mahphalke 3:779bb1e55f1a 1959 { "Disable Channels", 'D', menu_channels_enable_disable, (uint32_t)CHN_DISABLE },
mahphalke 3:779bb1e55f1a 1960 };
mahphalke 3:779bb1e55f1a 1961
mahphalke 3:779bb1e55f1a 1962 static console_menu chn_enable_disable_menu = {
mahphalke 3:779bb1e55f1a 1963 .title = "Channel Enable/Disable Menu",
mahphalke 3:779bb1e55f1a 1964 .items = chn_enable_disable_items,
mahphalke 3:779bb1e55f1a 1965 .itemCount = ARRAY_SIZE(chn_enable_disable_items),
mahphalke 3:779bb1e55f1a 1966 .headerItem = NULL,
mahphalke 3:779bb1e55f1a 1967 .footerItem = NULL,
mahphalke 3:779bb1e55f1a 1968 .enableEscapeKey = true
mahphalke 3:779bb1e55f1a 1969 };
mahphalke 3:779bb1e55f1a 1970
mahphalke 3:779bb1e55f1a 1971 /*
mahphalke 3:779bb1e55f1a 1972 * Definition of the adc register read/write Menu Items and menu itself
mahphalke 3:779bb1e55f1a 1973 */
mahphalke 3:779bb1e55f1a 1974 static console_menu_item reg_read_write_items[] = {
mahphalke 3:779bb1e55f1a 1975 { "Read Device Register", 'R', menu_rw_ad7124_register, (uint32_t)DEVICE_REG_READ_ID },
mahphalke 3:779bb1e55f1a 1976 { "Write Device Register", 'W', menu_rw_ad7124_register, (uint32_t)DEVICE_REG_WRITE_ID },
mahphalke 3:779bb1e55f1a 1977 };
mahphalke 3:779bb1e55f1a 1978
mahphalke 3:779bb1e55f1a 1979 static console_menu reg_read_write_items_menu = {
mahphalke 3:779bb1e55f1a 1980 .title = "Register Read/Write Menu",
mahphalke 3:779bb1e55f1a 1981 .items = reg_read_write_items,
mahphalke 3:779bb1e55f1a 1982 .itemCount = ARRAY_SIZE(reg_read_write_items),
mahphalke 3:779bb1e55f1a 1983 .headerItem = NULL,
mahphalke 3:779bb1e55f1a 1984 .footerItem = NULL,
mahphalke 3:779bb1e55f1a 1985 .enableEscapeKey = true
mahphalke 3:779bb1e55f1a 1986 };
mahphalke 3:779bb1e55f1a 1987
mahphalke 3:779bb1e55f1a 1988
mahphalke 3:779bb1e55f1a 1989 /*!
mahphalke 3:779bb1e55f1a 1990 * @brief displays and handles the Sample Channel menu
mahphalke 3:779bb1e55f1a 1991 *
mahphalke 3:779bb1e55f1a 1992 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 1993 */
mahphalke 3:779bb1e55f1a 1994 static int32_t menu_sample_channels(void)
mahphalke 3:779bb1e55f1a 1995 {
mahphalke 3:779bb1e55f1a 1996 return (adi_do_console_menu(&acquisition_menu));
mahphalke 3:779bb1e55f1a 1997 }
mahphalke 3:779bb1e55f1a 1998
mahphalke 3:779bb1e55f1a 1999
mahphalke 3:779bb1e55f1a 2000 /* @brief display and handle console menu for enabling/disabling adc channels
mahphalke 3:779bb1e55f1a 2001 *
mahphalke 3:779bb1e55f1a 2002 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 2003 **/
mahphalke 3:779bb1e55f1a 2004 static int32_t menu_enable_disable_channels(void)
mahphalke 3:779bb1e55f1a 2005 {
mahphalke 3:779bb1e55f1a 2006 return (adi_do_console_menu(&chn_enable_disable_menu));
mahphalke 3:779bb1e55f1a 2007 }
mahphalke 3:779bb1e55f1a 2008
mahphalke 3:779bb1e55f1a 2009
mahphalke 3:779bb1e55f1a 2010 /* @brief display and handle console menu for reading/writing adc registers
mahphalke 3:779bb1e55f1a 2011 *
mahphalke 3:779bb1e55f1a 2012 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 2013 **/
mahphalke 3:779bb1e55f1a 2014 static int32_t menu_read_write_device_regs(void)
mahphalke 3:779bb1e55f1a 2015 {
mahphalke 3:779bb1e55f1a 2016 return (adi_do_console_menu(&reg_read_write_items_menu));
mahphalke 3:779bb1e55f1a 2017 }
mahphalke 3:779bb1e55f1a 2018
mahphalke 3:779bb1e55f1a 2019 /*!
mahphalke 3:779bb1e55f1a 2020 * @brief displays and handles the power mode select menu
mahphalke 3:779bb1e55f1a 2021 *
mahphalke 3:779bb1e55f1a 2022 * @return int32_t- menu status constant
mahphalke 3:779bb1e55f1a 2023 */
mahphalke 3:779bb1e55f1a 2024 static int32_t menu_select_power_mode(void)
mahphalke 3:779bb1e55f1a 2025 {
mahphalke 3:779bb1e55f1a 2026 return (adi_do_console_menu(&power_mode_menu));
mahphalke 3:779bb1e55f1a 2027 }
mahphalke 3:779bb1e55f1a 2028
mahphalke 3:779bb1e55f1a 2029
mahphalke 3:779bb1e55f1a 2030 /*
mahphalke 3:779bb1e55f1a 2031 * Definition of the Main Menu Items and menu itself
mahphalke 3:779bb1e55f1a 2032 */
mahphalke 3:779bb1e55f1a 2033 static console_menu_item main_menu_items[] = {
mahphalke 3:779bb1e55f1a 2034 { "Reset to Default Configuration", 'A', menu_reset },
mahphalke 3:779bb1e55f1a 2035 { "Reset to Configuration A", 'B', menu_reset_to_configuration, (uint32_t)AD7124_CONFIG_A },
mahphalke 3:779bb1e55f1a 2036 { "Reset to Configuration B", 'C', menu_reset_to_configuration, (uint32_t)AD7124_CONFIG_B },
mahphalke 3:779bb1e55f1a 2037 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2038 { "Read ID Register", 'D', menu_read_id },
mahphalke 3:779bb1e55f1a 2039 { "Read Status Register", 'E', menu_read_status },
mahphalke 3:779bb1e55f1a 2040 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2041 { "Sample Channels", 'F', menu_sample_channels },
mahphalke 3:779bb1e55f1a 2042 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2043 { "Select Power Mode", 'G', menu_select_power_mode },
mahphalke 3:779bb1e55f1a 2044 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2045 { "Enable/Disable Channels", 'H', menu_enable_disable_channels },
mahphalke 3:779bb1e55f1a 2046 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2047 { "Connect Analog Inputs to Channel", 'I', menu_connect_input_to_channel },
mahphalke 3:779bb1e55f1a 2048 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2049 { "Configure and Assign Setup", 'J', menu_config_and_assign_setup },
mahphalke 3:779bb1e55f1a 2050 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2051 { "Display setup", 'K', menu_display_setup },
mahphalke 3:779bb1e55f1a 2052 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2053 { "Read Temperature", 'L', menu_read_temperature },
mahphalke 3:779bb1e55f1a 2054 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2055 { "Calibrate ADC (Internal)", 'M', menu_calibrate_adc },
mahphalke 3:779bb1e55f1a 2056 { "", '\00', NULL },
mahphalke 3:779bb1e55f1a 2057 { "Read/Write Device Registers", 'N', menu_read_write_device_regs },
mahphalke 3:779bb1e55f1a 2058 };
mahphalke 3:779bb1e55f1a 2059
mahphalke 3:779bb1e55f1a 2060 console_menu ad7124_main_menu = {
mahphalke 3:779bb1e55f1a 2061 .title = "AD7124 Main Menu",
mahphalke 3:779bb1e55f1a 2062 .items = main_menu_items,
mahphalke 3:779bb1e55f1a 2063 .itemCount = ARRAY_SIZE(main_menu_items),
mahphalke 3:779bb1e55f1a 2064 .headerItem = NULL,
mahphalke 3:779bb1e55f1a 2065 .footerItem = NULL,
mahphalke 3:779bb1e55f1a 2066 .enableEscapeKey = false
mahphalke 3:779bb1e55f1a 2067 };