EVAL-AD7124 Mbed Example Program.

Dependencies:   adi_console_menu platform_drivers

Committer:
Kjansen
Date:
Tue Aug 03 12:05:42 2021 +0100
Revision:
7:3e1005bd4d41
Parent:
5:ca3854efb1e9
No-OS Adoption Changes:

* Updated the .lib files for adoption of no-OS repository as-is.
* Replaced platform_drivers.h with required header files.
* Added designated initializers for nanodac_init_params as the latest
spi_init_param in no-OS repository include a new field member platform_ops.
* Added Support Macros.
* Updated the copyright year of the nanodac_console_app.c file

Mbed OS update changes:
1) Added the mbed_app.json file with custom parameters
2) Updated the mbed-os version to 6.8.0

Updated the readme file.

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