Example program for AD717x and AD411x family of products.

Dependencies:   adi_console_menu platform_drivers

Committer:
mahphalke
Date:
Tue Apr 14 18:14:29 2020 +0000
Revision:
3:6c7324997606
Parent:
1:48914f9593f1
Child:
4:4592cc69bfa6
Implemented the open wire detection functionality for AD4111

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 1:48914f9593f1 1 /*!
mahphalke 1:48914f9593f1 2 *****************************************************************************
mahphalke 1:48914f9593f1 3 @file: ad717x_console_app.c
mahphalke 1:48914f9593f1 4
mahphalke 1:48914f9593f1 5 @brief: Implementation of the menu functions which handles the
mahphalke 1:48914f9593f1 6 functionality of AD717x and AD411x family of devices.
mahphalke 1:48914f9593f1 7
mahphalke 1:48914f9593f1 8 @details: This file is specific to AD717x/AD411x console menu application handle.
mahphalke 1:48914f9593f1 9 The functions defined in this file performs the action
mahphalke 1:48914f9593f1 10 based on user selected console menu.
mahphalke 1:48914f9593f1 11 -----------------------------------------------------------------------------
mahphalke 1:48914f9593f1 12 Copyright (c) 2020 Analog Devices, Inc.
mahphalke 1:48914f9593f1 13 All rights reserved.
mahphalke 1:48914f9593f1 14
mahphalke 1:48914f9593f1 15 This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 1:48914f9593f1 16 By using this software you agree to the terms of the associated
mahphalke 1:48914f9593f1 17 Analog Devices Software License Agreement.
mahphalke 1:48914f9593f1 18 *****************************************************************************/
mahphalke 1:48914f9593f1 19
mahphalke 1:48914f9593f1 20 /******************************************************************************/
mahphalke 1:48914f9593f1 21 /***************************** Include Files **********************************/
mahphalke 1:48914f9593f1 22 /******************************************************************************/
mahphalke 1:48914f9593f1 23
mahphalke 1:48914f9593f1 24 #include <stdio.h>
mahphalke 1:48914f9593f1 25 #include <string.h>
mahphalke 1:48914f9593f1 26 #include <stdbool.h>
mahphalke 1:48914f9593f1 27 #include <ctype.h>
mahphalke 1:48914f9593f1 28
mahphalke 1:48914f9593f1 29 #include "app_config.h"
mahphalke 1:48914f9593f1 30
mahphalke 1:48914f9593f1 31 #include "ad717x.h"
mahphalke 1:48914f9593f1 32 #include "platform_support.h"
mahphalke 1:48914f9593f1 33 #include "platform_drivers.h"
mahphalke 1:48914f9593f1 34 #include "spi_extra.h"
mahphalke 1:48914f9593f1 35
mahphalke 1:48914f9593f1 36 #include "ad717x_console_app.h"
mahphalke 1:48914f9593f1 37 #include "ad717x_menu_defines.h"
mahphalke 1:48914f9593f1 38 #include "ad717x_support.h"
mahphalke 1:48914f9593f1 39
mahphalke 1:48914f9593f1 40 /******************************************************************************/
mahphalke 1:48914f9593f1 41 /********************** Macros and Constants Definitions **********************/
mahphalke 1:48914f9593f1 42 /******************************************************************************/
mahphalke 1:48914f9593f1 43
mahphalke 1:48914f9593f1 44 // Include the device register address map headers and device register map based
mahphalke 1:48914f9593f1 45 // on the user selected device (default is AD4111)
mahphalke 3:6c7324997606 46 #if defined(DEV_AD4111) || defined(DEV_AD4112)
mahphalke 1:48914f9593f1 47 #include <ad411x_regs.h>
mahphalke 1:48914f9593f1 48 static ad717x_st_reg *ad717x_device_map = ad4111_regs;
mahphalke 1:48914f9593f1 49 static uint8_t ad717x_reg_count = sizeof(ad4111_regs) / sizeof(ad4111_regs[0]);
mahphalke 1:48914f9593f1 50 #elif defined(DEV_AD7172_2)
mahphalke 1:48914f9593f1 51 #include <ad7172_2_regs.h>
mahphalke 1:48914f9593f1 52 static ad717x_st_reg *ad717x_device_map = ad7172_2_regs;
mahphalke 1:48914f9593f1 53 static uint8_t ad717x_reg_count = sizeof(ad7172_2_regs) / sizeof(
mahphalke 1:48914f9593f1 54 ad7172_2_regs[0]);
mahphalke 1:48914f9593f1 55 #elif defined(DEV_AD7172_4)
mahphalke 1:48914f9593f1 56 #include <ad7172_4_regs.h>
mahphalke 1:48914f9593f1 57 static ad717x_st_reg *ad717x_device_map = ad7172_4_regs;
mahphalke 1:48914f9593f1 58 static uint8_t ad717x_reg_count = sizeof(ad7172_4_regs) / sizeof(
mahphalke 1:48914f9593f1 59 ad7172_4_regs[0]);
mahphalke 1:48914f9593f1 60 #elif defined(DEV_AD7173_8)
mahphalke 1:48914f9593f1 61 #include <ad7173_8_regs.h>
mahphalke 1:48914f9593f1 62 static ad717x_st_reg *ad717x_device_map = ad7173_8_regs;
mahphalke 1:48914f9593f1 63 static uint8_t ad717x_reg_count = sizeof(ad7173_8_regs) / sizeof(
mahphalke 1:48914f9593f1 64 ad7173_8_regs[0]);
mahphalke 1:48914f9593f1 65 #elif defined(DEV_AD7175_2)
mahphalke 1:48914f9593f1 66 #include <ad7175_2_regs.h>
mahphalke 1:48914f9593f1 67 static ad717x_st_reg *ad717x_device_map = ad7175_2_regs;
mahphalke 1:48914f9593f1 68 static uint8_t ad717x_reg_count = sizeof(ad7175_2_regs) / sizeof(
mahphalke 1:48914f9593f1 69 ad7175_2_regs[0]);
mahphalke 1:48914f9593f1 70 #elif defined(DEV_AD7175_8)
mahphalke 1:48914f9593f1 71 #include <ad7175_8_regs.h>
mahphalke 1:48914f9593f1 72 static ad717x_st_reg *ad717x_device_map = ad7175_8_regs;
mahphalke 1:48914f9593f1 73 static uint8_t ad717x_reg_count = sizeof(ad7175_8_regs) / sizeof(
mahphalke 1:48914f9593f1 74 ad7175_8_regs[0]);
mahphalke 1:48914f9593f1 75 #elif defined(DEV_AD7176_2)
mahphalke 1:48914f9593f1 76 #include <ad7176_2_regs.h>
mahphalke 1:48914f9593f1 77 static ad717x_st_reg *ad717x_device_map = ad7176_2_regs;
mahphalke 1:48914f9593f1 78 static uint8_t ad717x_reg_count = sizeof(ad7176_2_regs) / sizeof(
mahphalke 1:48914f9593f1 79 ad7176_2_regs[0]);
mahphalke 1:48914f9593f1 80 #else
mahphalke 1:48914f9593f1 81 #include <ad411x_regs.h>
mahphalke 1:48914f9593f1 82 static ad717x_st_reg *ad717x_device_map = ad4111_regs;
mahphalke 1:48914f9593f1 83 static uint8_t ad717x_reg_count = sizeof(ad4111_regs) / sizeof(ad4111_regs[0]);
mahphalke 1:48914f9593f1 84 #endif
mahphalke 1:48914f9593f1 85
mahphalke 1:48914f9593f1 86
mahphalke 1:48914f9593f1 87 #define SHOW_ALL_CHANNELS false
mahphalke 1:48914f9593f1 88 #define SHOW_ENABLED_CHANNELS true
mahphalke 1:48914f9593f1 89
mahphalke 1:48914f9593f1 90 #define DISPLAY_DATA_TABULAR 0
mahphalke 1:48914f9593f1 91 #define DISPLAY_DATA_STREAM 1
mahphalke 1:48914f9593f1 92
mahphalke 3:6c7324997606 93 // Open wire detect ADC count threshold (eqv of 300mv for bipolar mode)
mahphalke 3:6c7324997606 94 #define OPEN_WIRE_DETECT_THRESHOLD 100000
mahphalke 3:6c7324997606 95
mahphalke 1:48914f9593f1 96 /******************************************************************************/
mahphalke 1:48914f9593f1 97 /********************** Variables and User Defined Data Types *****************/
mahphalke 1:48914f9593f1 98 /******************************************************************************/
mahphalke 1:48914f9593f1 99
mahphalke 1:48914f9593f1 100 // Pointer to the struct representing the AD717x device
mahphalke 1:48914f9593f1 101 static ad717x_dev *pad717x_dev = NULL;
mahphalke 1:48914f9593f1 102
mahphalke 1:48914f9593f1 103 // Device setup
mahphalke 1:48914f9593f1 104 static ad717x_setup_config device_setup;
mahphalke 1:48914f9593f1 105
mahphalke 1:48914f9593f1 106 // User selected input (pair/positive/negative)
mahphalke 1:48914f9593f1 107 static uint8_t input_to_select;
mahphalke 1:48914f9593f1 108
mahphalke 1:48914f9593f1 109 // Last Sampled values for All ADC channels
mahphalke 1:48914f9593f1 110 static uint32_t channel_samples[NUMBER_OF_CHANNELS] = { 0 };
mahphalke 1:48914f9593f1 111
mahphalke 1:48914f9593f1 112 // How many times a given channel is sampled in total for one sample run
mahphalke 1:48914f9593f1 113 static uint32_t channel_samples_count[NUMBER_OF_CHANNELS] = { 0 };
mahphalke 1:48914f9593f1 114
mahphalke 3:6c7324997606 115 // Variables used for open wire detection functionality
mahphalke 3:6c7324997606 116 static uint32_t analog_input_type; // Analog input type
mahphalke 3:6c7324997606 117 static uint32_t channel_pair; // Channel pair for open wire detection
mahphalke 3:6c7324997606 118 static int32_t open_wire_detect_sample_data[2]; // Sampled data for channel pair
mahphalke 3:6c7324997606 119
mahphalke 1:48914f9593f1 120 /******************************************************************************/
mahphalke 1:48914f9593f1 121 /************************ Functions Declarations ******************************/
mahphalke 1:48914f9593f1 122 /******************************************************************************/
mahphalke 1:48914f9593f1 123
mahphalke 1:48914f9593f1 124 static bool was_escape_key_pressed(void);
mahphalke 1:48914f9593f1 125
mahphalke 1:48914f9593f1 126 /******************************************************************************/
mahphalke 1:48914f9593f1 127 /************************ Functions Definitions *******************************/
mahphalke 1:48914f9593f1 128 /******************************************************************************/
mahphalke 1:48914f9593f1 129
mahphalke 1:48914f9593f1 130 /*!
mahphalke 1:48914f9593f1 131 * @brief Initialize the AD717x device and associated low level peripherals
mahphalke 1:48914f9593f1 132 * @return SUCCESS(0) Or FAILURE(negative)
mahphalke 1:48914f9593f1 133 */
mahphalke 1:48914f9593f1 134 int32_t ad717x_app_initialize(void)
mahphalke 1:48914f9593f1 135 {
mahphalke 1:48914f9593f1 136 // Init SPI extra parameters structure
mahphalke 1:48914f9593f1 137 mbed_spi_init_param spi_init_extra_params = {
mahphalke 1:48914f9593f1 138 .spi_clk_pin = SPI_SCK,
mahphalke 1:48914f9593f1 139 .spi_miso_pin = SPI_MISO,
mahphalke 1:48914f9593f1 140 .spi_mosi_pin = SPI_MOSI
mahphalke 1:48914f9593f1 141 };
mahphalke 1:48914f9593f1 142
mahphalke 1:48914f9593f1 143 // Used to create the ad717x device
mahphalke 1:48914f9593f1 144 ad717x_init_param ad717x_init = {
mahphalke 1:48914f9593f1 145 // spi_init_param type
mahphalke 1:48914f9593f1 146 {
mahphalke 1:48914f9593f1 147 2500000, // Max SPI Speed
mahphalke 1:48914f9593f1 148 SPI_SS_A, // Chip Select pin
mahphalke 1:48914f9593f1 149 SPI_MODE_3, // CPOL = 1, CPHA =1
mahphalke 1:48914f9593f1 150 &spi_init_extra_params, // SPI extra configurations
mahphalke 1:48914f9593f1 151 },
mahphalke 1:48914f9593f1 152 ad717x_device_map, // pointer to device register map
mahphalke 1:48914f9593f1 153 ad717x_reg_count, // number of device registers
mahphalke 1:48914f9593f1 154 };
mahphalke 1:48914f9593f1 155
mahphalke 1:48914f9593f1 156 // Initialze the device
mahphalke 1:48914f9593f1 157 return (AD717X_Init(&pad717x_dev, ad717x_init));
mahphalke 1:48914f9593f1 158 }
mahphalke 1:48914f9593f1 159
mahphalke 1:48914f9593f1 160
mahphalke 1:48914f9593f1 161 /*!
mahphalke 1:48914f9593f1 162 * @brief determines if the Escape key was pressed
mahphalke 3:6c7324997606 163 * @return key press status
mahphalke 1:48914f9593f1 164 */
mahphalke 1:48914f9593f1 165 static bool was_escape_key_pressed(void)
mahphalke 1:48914f9593f1 166 {
mahphalke 1:48914f9593f1 167 char rxChar;
mahphalke 1:48914f9593f1 168 bool wasPressed = false;
mahphalke 1:48914f9593f1 169
mahphalke 1:48914f9593f1 170 // Check for Escape key pressed
mahphalke 1:48914f9593f1 171 if ((rxChar = getchar_noblock()) > 0) {
mahphalke 1:48914f9593f1 172 if (rxChar == ESCAPE_KEY_CODE) {
mahphalke 1:48914f9593f1 173 wasPressed = true;
mahphalke 1:48914f9593f1 174 }
mahphalke 1:48914f9593f1 175 }
mahphalke 1:48914f9593f1 176
mahphalke 1:48914f9593f1 177 return (wasPressed);
mahphalke 1:48914f9593f1 178 }
mahphalke 1:48914f9593f1 179
mahphalke 1:48914f9593f1 180
mahphalke 1:48914f9593f1 181 /* @brief Perform the channel selection
mahphalke 3:6c7324997606 182 * @return Selected channel
mahphalke 1:48914f9593f1 183 **/
mahphalke 1:48914f9593f1 184 static uint8_t get_channel_selection(void)
mahphalke 1:48914f9593f1 185 {
mahphalke 1:48914f9593f1 186 uint32_t current_channel = 0;
mahphalke 1:48914f9593f1 187 bool current_selection_done = false;
mahphalke 1:48914f9593f1 188
mahphalke 1:48914f9593f1 189 do {
mahphalke 1:48914f9593f1 190 printf(EOL "\tEnter Channel Value <0-%d>: ", NUMBER_OF_CHANNELS-1);
mahphalke 1:48914f9593f1 191 current_channel = adi_get_decimal_int(sizeof(current_channel));
mahphalke 1:48914f9593f1 192
mahphalke 1:48914f9593f1 193 if (current_channel < NUMBER_OF_CHANNELS) {
mahphalke 1:48914f9593f1 194 current_selection_done = true;
mahphalke 1:48914f9593f1 195 } else {
mahphalke 1:48914f9593f1 196 printf(EOL "\tInvalid channel selection!!" EOL);
mahphalke 1:48914f9593f1 197 }
mahphalke 1:48914f9593f1 198 } while (current_selection_done == false);
mahphalke 1:48914f9593f1 199
mahphalke 1:48914f9593f1 200 return current_channel;
mahphalke 1:48914f9593f1 201 }
mahphalke 1:48914f9593f1 202
mahphalke 1:48914f9593f1 203
mahphalke 1:48914f9593f1 204 /* @brief Perform the setup selection
mahphalke 3:6c7324997606 205 * @return Selected setup
mahphalke 1:48914f9593f1 206 **/
mahphalke 1:48914f9593f1 207 static uint8_t get_setup_selection(void)
mahphalke 1:48914f9593f1 208 {
mahphalke 1:48914f9593f1 209 uint32_t current_setup = 0;
mahphalke 1:48914f9593f1 210 bool current_selection_done = false;
mahphalke 1:48914f9593f1 211
mahphalke 1:48914f9593f1 212 do {
mahphalke 1:48914f9593f1 213 printf(EOL "\tEnter Setup Selection <0-%d>: ", NUMBER_OF_SETUPS-1);
mahphalke 1:48914f9593f1 214 current_setup = adi_get_decimal_int(sizeof(current_setup));
mahphalke 1:48914f9593f1 215
mahphalke 1:48914f9593f1 216 if (current_setup < NUMBER_OF_SETUPS) {
mahphalke 1:48914f9593f1 217 current_selection_done = true;
mahphalke 1:48914f9593f1 218 } else {
mahphalke 1:48914f9593f1 219 printf(EOL "\tInvalid setup selection!!" EOL);
mahphalke 1:48914f9593f1 220 }
mahphalke 1:48914f9593f1 221 } while (current_selection_done == false);
mahphalke 1:48914f9593f1 222
mahphalke 1:48914f9593f1 223 return current_setup;
mahphalke 1:48914f9593f1 224 }
mahphalke 1:48914f9593f1 225
mahphalke 1:48914f9593f1 226
mahphalke 1:48914f9593f1 227 /* @brief Assign setup to adc channel
mahphalke 3:6c7324997606 228 * @param setup to be assigned
mahphalke 1:48914f9593f1 229 **/
mahphalke 1:48914f9593f1 230 static void assign_setup_to_channel(uint8_t setup)
mahphalke 1:48914f9593f1 231 {
mahphalke 1:48914f9593f1 232 uint8_t current_channel; // channel to be assigned with setup
mahphalke 1:48914f9593f1 233 ad717x_st_reg *device_chnmap_reg; // pointer to channel map register
mahphalke 1:48914f9593f1 234
mahphalke 1:48914f9593f1 235 adi_clear_console();
mahphalke 1:48914f9593f1 236
mahphalke 1:48914f9593f1 237 // Get the channel selection
mahphalke 1:48914f9593f1 238 current_channel = get_channel_selection();
mahphalke 1:48914f9593f1 239
mahphalke 1:48914f9593f1 240 // Get the pointer to channel map register structure
mahphalke 1:48914f9593f1 241 device_chnmap_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 242 AD717X_CHMAP0_REG + current_channel);
mahphalke 1:48914f9593f1 243
mahphalke 1:48914f9593f1 244 // Load the setup value
mahphalke 1:48914f9593f1 245 device_chnmap_reg->value =
mahphalke 1:48914f9593f1 246 ((device_chnmap_reg->value & ~AD717X_CHMAP_REG_SETUP_SEL_MSK) |
mahphalke 1:48914f9593f1 247 AD717X_CHMAP_REG_SETUP_SEL(setup));
mahphalke 1:48914f9593f1 248
mahphalke 1:48914f9593f1 249 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 250 AD717X_CHMAP0_REG + current_channel) != SUCCESS) {
mahphalke 1:48914f9593f1 251 printf(EOL "\tError in setup assignment!!" EOL);
mahphalke 1:48914f9593f1 252 } else {
mahphalke 1:48914f9593f1 253 printf(EOL "\tSetup %d is assigned to channel %d successfully..." EOL,
mahphalke 1:48914f9593f1 254 setup,
mahphalke 1:48914f9593f1 255 current_channel);
mahphalke 1:48914f9593f1 256 }
mahphalke 1:48914f9593f1 257
mahphalke 1:48914f9593f1 258 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 259 }
mahphalke 1:48914f9593f1 260
mahphalke 1:48914f9593f1 261
mahphalke 1:48914f9593f1 262 /* @brief Select adc channel to be assigned to setup
mahphalke 1:48914f9593f1 263 * @return none
mahphalke 1:48914f9593f1 264 **/
mahphalke 1:48914f9593f1 265 static void select_chn_assignment(void)
mahphalke 1:48914f9593f1 266 {
mahphalke 1:48914f9593f1 267 bool current_selection_done = false;
mahphalke 1:48914f9593f1 268 char rx_char;
mahphalke 1:48914f9593f1 269
mahphalke 1:48914f9593f1 270 do {
mahphalke 1:48914f9593f1 271 printf(EOL EOL "\tDo you want to assign setup to a channel (y/n)?: ");
mahphalke 1:48914f9593f1 272 rx_char = toupper(getchar());
mahphalke 1:48914f9593f1 273
mahphalke 1:48914f9593f1 274 if (rx_char == 'Y') {
mahphalke 1:48914f9593f1 275 assign_setup_to_channel(device_setup.setup);
mahphalke 1:48914f9593f1 276 current_selection_done = true;
mahphalke 1:48914f9593f1 277 } else if (rx_char == 'N') {
mahphalke 1:48914f9593f1 278 current_selection_done = true;
mahphalke 1:48914f9593f1 279 } else {
mahphalke 1:48914f9593f1 280 printf(EOL "\tInvalid entry!!");
mahphalke 1:48914f9593f1 281 }
mahphalke 1:48914f9593f1 282 } while (current_selection_done == false);
mahphalke 1:48914f9593f1 283 }
mahphalke 1:48914f9593f1 284
mahphalke 1:48914f9593f1 285
mahphalke 1:48914f9593f1 286 /*!
mahphalke 1:48914f9593f1 287 * @brief Display the header info for main menu
mahphalke 1:48914f9593f1 288 * @return None
mahphalke 1:48914f9593f1 289 */
mahphalke 1:48914f9593f1 290 void display_main_menu_header(void)
mahphalke 1:48914f9593f1 291 {
mahphalke 1:48914f9593f1 292 // Display the device name
mahphalke 1:48914f9593f1 293 printf(EOL "\tDevice: %s" EOL, ACTIVE_DEVICE_NAME);
mahphalke 1:48914f9593f1 294 }
mahphalke 1:48914f9593f1 295
mahphalke 1:48914f9593f1 296
mahphalke 1:48914f9593f1 297 /*!
mahphalke 1:48914f9593f1 298 * @brief Handle the menu to read device ID
mahphalke 3:6c7324997606 299 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 300 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 301 */
mahphalke 1:48914f9593f1 302 int32_t menu_read_id(uint32_t menu_id)
mahphalke 1:48914f9593f1 303 {
mahphalke 1:48914f9593f1 304 ad717x_st_reg *device_id_reg; // Pointer to register
mahphalke 1:48914f9593f1 305
mahphalke 1:48914f9593f1 306 device_id_reg = AD717X_GetReg(pad717x_dev, AD717X_ID_REG);
mahphalke 1:48914f9593f1 307 if (!device_id_reg) {
mahphalke 1:48914f9593f1 308 printf(EOL EOL "\tError reading device ID!!" EOL);
mahphalke 1:48914f9593f1 309 } else {
mahphalke 1:48914f9593f1 310 if (AD717X_ReadRegister(pad717x_dev, AD717X_ID_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 311 printf(EOL EOL "\tError reading device ID!!" EOL);
mahphalke 1:48914f9593f1 312 } else {
mahphalke 1:48914f9593f1 313 printf(EOL EOL "\tDevice ID: 0x%lx" EOL, device_id_reg->value);
mahphalke 1:48914f9593f1 314 }
mahphalke 1:48914f9593f1 315 }
mahphalke 1:48914f9593f1 316
mahphalke 1:48914f9593f1 317 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 318 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 319 }
mahphalke 1:48914f9593f1 320
mahphalke 1:48914f9593f1 321
mahphalke 1:48914f9593f1 322 /*!
mahphalke 1:48914f9593f1 323 * @brief Handle the menu to read device status register
mahphalke 3:6c7324997606 324 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 325 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 326 */
mahphalke 1:48914f9593f1 327 int32_t menu_read_status(uint32_t menu_id)
mahphalke 1:48914f9593f1 328 {
mahphalke 1:48914f9593f1 329 ad717x_st_reg *device_status_reg; // Pointer to register
mahphalke 1:48914f9593f1 330
mahphalke 1:48914f9593f1 331 device_status_reg = AD717X_GetReg(pad717x_dev, AD717X_STATUS_REG);
mahphalke 1:48914f9593f1 332 if (!device_status_reg) {
mahphalke 1:48914f9593f1 333 printf(EOL EOL "\tError reading status register!!" EOL);
mahphalke 1:48914f9593f1 334 } else {
mahphalke 1:48914f9593f1 335 if (AD717X_ReadRegister(pad717x_dev, AD717X_STATUS_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 336 printf(EOL EOL "\tError reading status register!!" EOL);
mahphalke 1:48914f9593f1 337 } else {
mahphalke 1:48914f9593f1 338 printf(EOL EOL "\tStatus Register: 0x%lx" EOL, device_status_reg->value);
mahphalke 1:48914f9593f1 339 }
mahphalke 1:48914f9593f1 340 }
mahphalke 1:48914f9593f1 341
mahphalke 1:48914f9593f1 342 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 343 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 344 }
mahphalke 1:48914f9593f1 345
mahphalke 1:48914f9593f1 346
mahphalke 1:48914f9593f1 347 /*
mahphalke 1:48914f9593f1 348 * @brief helper function get the bipolar setting for an ADC channel
mahphalke 1:48914f9593f1 349 *
mahphalke 1:48914f9593f1 350 * @param dev The device structure.
mahphalke 1:48914f9593f1 351 *
mahphalke 1:48914f9593f1 352 * @param channel ADC channel to get bipolar mode for.
mahphalke 1:48914f9593f1 353 *
mahphalke 1:48914f9593f1 354 * @return value of bipolar field in the setup for an ADC channel.
mahphalke 1:48914f9593f1 355 */
mahphalke 1:48914f9593f1 356 static bool ad717x_get_channel_bipolar(ad717x_dev *dev, uint8_t channel)
mahphalke 1:48914f9593f1 357 {
mahphalke 1:48914f9593f1 358 ad717x_st_reg *device_setup_reg; // Pointer to device setup register
mahphalke 1:48914f9593f1 359 ad717x_st_reg *device_chnmap_reg; // Pointer to device channelmap register
mahphalke 1:48914f9593f1 360 uint8_t polarity; // Polarity status flag
mahphalke 1:48914f9593f1 361 uint8_t setup; // Current setup
mahphalke 1:48914f9593f1 362
mahphalke 1:48914f9593f1 363 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + channel);
mahphalke 1:48914f9593f1 364 (void)AD717X_ReadRegister(pad717x_dev, AD717X_CHMAP0_REG + channel);
mahphalke 1:48914f9593f1 365
mahphalke 1:48914f9593f1 366 // Read the setup value for the current channel
mahphalke 1:48914f9593f1 367 setup = AD717X_CHMAP_REG_SETUP_SEL_RD(device_chnmap_reg->value);
mahphalke 1:48914f9593f1 368
mahphalke 1:48914f9593f1 369 device_setup_reg = AD717X_GetReg(pad717x_dev, AD717X_SETUPCON0_REG + setup);
mahphalke 1:48914f9593f1 370 (void)AD717X_ReadRegister(pad717x_dev, AD717X_SETUPCON0_REG + setup);
mahphalke 1:48914f9593f1 371
mahphalke 1:48914f9593f1 372 // Get the polarity bit for current setup
mahphalke 1:48914f9593f1 373 polarity = AD717X_SETUP_CONF_REG_BI_UNIPOLAR_RD(device_setup_reg->value);
mahphalke 1:48914f9593f1 374
mahphalke 1:48914f9593f1 375 if (polarity == BIPOLAR) {
mahphalke 1:48914f9593f1 376 return true;
mahphalke 1:48914f9593f1 377 } else {
mahphalke 1:48914f9593f1 378 return false;
mahphalke 1:48914f9593f1 379 }
mahphalke 1:48914f9593f1 380 }
mahphalke 1:48914f9593f1 381
mahphalke 1:48914f9593f1 382
mahphalke 1:48914f9593f1 383 /*
mahphalke 1:48914f9593f1 384 * @brief converts ADC sample value to voltage based on gain setting
mahphalke 1:48914f9593f1 385 *
mahphalke 1:48914f9593f1 386 * @param dev The device structure.
mahphalke 1:48914f9593f1 387 *
mahphalke 1:48914f9593f1 388 * @param channel ADC channel to get Setup for.
mahphalke 1:48914f9593f1 389 *
mahphalke 1:48914f9593f1 390 * @param sample Raw ADC sample
mahphalke 1:48914f9593f1 391 *
mahphalke 1:48914f9593f1 392 * @return Sample ADC value converted to voltage.
mahphalke 1:48914f9593f1 393 *
mahphalke 1:48914f9593f1 394 * @note The conversion equation is implemented for simplicity,
mahphalke 1:48914f9593f1 395 * not for accuracy or performance
mahphalke 1:48914f9593f1 396 *
mahphalke 1:48914f9593f1 397 */
mahphalke 1:48914f9593f1 398 static float ad717x_convert_sample_to_voltage(ad717x_dev *dev,
mahphalke 1:48914f9593f1 399 uint8_t channel,
mahphalke 1:48914f9593f1 400 uint32_t sample)
mahphalke 1:48914f9593f1 401 {
mahphalke 1:48914f9593f1 402 float converted_value;
mahphalke 1:48914f9593f1 403 bool is_bipolar = ad717x_get_channel_bipolar(dev, channel);
mahphalke 1:48914f9593f1 404
mahphalke 1:48914f9593f1 405 if (is_bipolar) {
mahphalke 1:48914f9593f1 406 converted_value = (((float)sample / (1 << (ADC_RESOLUTION - 1))) - 1) *
mahphalke 1:48914f9593f1 407 ADC_REF_VOLTAGE;
mahphalke 1:48914f9593f1 408 } else {
mahphalke 1:48914f9593f1 409 converted_value = (((float)sample * ADC_REF_VOLTAGE) / (1 << ADC_RESOLUTION));
mahphalke 1:48914f9593f1 410 }
mahphalke 1:48914f9593f1 411
mahphalke 1:48914f9593f1 412 return (converted_value);
mahphalke 1:48914f9593f1 413 }
mahphalke 1:48914f9593f1 414
mahphalke 1:48914f9593f1 415
mahphalke 1:48914f9593f1 416 /*!
mahphalke 1:48914f9593f1 417 * @brief displays the current sample value for a ADC channels
mahphalke 1:48914f9593f1 418 *
mahphalke 1:48914f9593f1 419 * @param showOnlyEnabledChannels only channels that are enabled are displayed
mahphalke 1:48914f9593f1 420 *
mahphalke 1:48914f9593f1 421 */
mahphalke 1:48914f9593f1 422 static void dislay_channel_samples(bool showOnlyEnabledChannels,
mahphalke 1:48914f9593f1 423 uint8_t console_mode)
mahphalke 1:48914f9593f1 424 {
mahphalke 1:48914f9593f1 425 ad717x_st_reg *device_chnmap_reg; // Pointer to channel map register
mahphalke 1:48914f9593f1 426 bool channel_printed = false; // Channel print status flag
mahphalke 1:48914f9593f1 427
mahphalke 1:48914f9593f1 428 switch (console_mode) {
mahphalke 1:48914f9593f1 429 case DISPLAY_DATA_TABULAR:
mahphalke 1:48914f9593f1 430 printf("\tCh\tValue\t\tCount\t\tVoltage" EOL);
mahphalke 1:48914f9593f1 431 for (uint8_t chn = 0; chn < NUMBER_OF_CHANNELS; chn++) {
mahphalke 1:48914f9593f1 432 // Get the pointer to channel register
mahphalke 1:48914f9593f1 433 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn);
mahphalke 1:48914f9593f1 434
mahphalke 1:48914f9593f1 435 // if showing all channels, or channel is enabled
mahphalke 1:48914f9593f1 436 if ((showOnlyEnabledChannels == false)
mahphalke 1:48914f9593f1 437 || (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN)) {
mahphalke 1:48914f9593f1 438 printf("\t%-2d\t%-10ld\t%ld\t\t% .6f" EOL,
mahphalke 1:48914f9593f1 439 chn,
mahphalke 1:48914f9593f1 440 channel_samples[chn],
mahphalke 1:48914f9593f1 441 channel_samples_count[chn],
mahphalke 1:48914f9593f1 442 ad717x_convert_sample_to_voltage(pad717x_dev, chn, channel_samples[chn]));
mahphalke 1:48914f9593f1 443 }
mahphalke 1:48914f9593f1 444 }
mahphalke 1:48914f9593f1 445 break;
mahphalke 1:48914f9593f1 446
mahphalke 1:48914f9593f1 447 case DISPLAY_DATA_STREAM:
mahphalke 1:48914f9593f1 448 // Output a CSV list of the sampled channels as voltages on a single line
mahphalke 1:48914f9593f1 449 for (uint8_t chn = 0 ; chn < NUMBER_OF_CHANNELS; chn++) {
mahphalke 1:48914f9593f1 450 // Get the pointer to channel register
mahphalke 1:48914f9593f1 451 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn);
mahphalke 1:48914f9593f1 452
mahphalke 1:48914f9593f1 453 // if showing all channels, or channel is enabled
mahphalke 1:48914f9593f1 454 if ((showOnlyEnabledChannels == false) ||
mahphalke 1:48914f9593f1 455 (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN)) {
mahphalke 1:48914f9593f1 456 /*
mahphalke 1:48914f9593f1 457 * add the comma before we output the next channel but
mahphalke 1:48914f9593f1 458 * only if at least one channel has been printed
mahphalke 1:48914f9593f1 459 */
mahphalke 1:48914f9593f1 460 if (channel_printed) {
mahphalke 1:48914f9593f1 461 printf(", ");
mahphalke 1:48914f9593f1 462 }
mahphalke 1:48914f9593f1 463
mahphalke 1:48914f9593f1 464 printf("%.6f", ad717x_convert_sample_to_voltage(pad717x_dev, chn,
mahphalke 1:48914f9593f1 465 channel_samples[chn]));
mahphalke 1:48914f9593f1 466
mahphalke 1:48914f9593f1 467 channel_printed = true;
mahphalke 1:48914f9593f1 468 }
mahphalke 1:48914f9593f1 469 }
mahphalke 1:48914f9593f1 470 printf(EOL);
mahphalke 1:48914f9593f1 471 break;
mahphalke 1:48914f9593f1 472
mahphalke 1:48914f9593f1 473 default:
mahphalke 1:48914f9593f1 474 break;
mahphalke 1:48914f9593f1 475 }
mahphalke 1:48914f9593f1 476 }
mahphalke 1:48914f9593f1 477
mahphalke 1:48914f9593f1 478
mahphalke 1:48914f9593f1 479 /*!
mahphalke 1:48914f9593f1 480 * @brief resets the channelSampleCounts to zero
mahphalke 1:48914f9593f1 481 *
mahphalke 1:48914f9593f1 482 * @details
mahphalke 1:48914f9593f1 483 */
mahphalke 1:48914f9593f1 484 static void clear_channel_samples(void)
mahphalke 1:48914f9593f1 485 {
mahphalke 1:48914f9593f1 486 for (uint8_t i = 0; i < NUMBER_OF_CHANNELS; i++) {
mahphalke 1:48914f9593f1 487 channel_samples[i] = 0;
mahphalke 1:48914f9593f1 488 channel_samples_count[i] = 0;
mahphalke 1:48914f9593f1 489 }
mahphalke 1:48914f9593f1 490 }
mahphalke 1:48914f9593f1 491
mahphalke 1:48914f9593f1 492
mahphalke 1:48914f9593f1 493 /*!
mahphalke 1:48914f9593f1 494 * @brief Continuously acquires samples in Continuous Conversion mode
mahphalke 1:48914f9593f1 495 *
mahphalke 1:48914f9593f1 496 * @details The ADC is run in continuous mode, and all samples are acquired
mahphalke 1:48914f9593f1 497 * and assigned to the channel they come from. Escape key an be used
mahphalke 1:48914f9593f1 498 * to exit the loop
mahphalke 1:48914f9593f1 499 */
mahphalke 1:48914f9593f1 500 static int32_t do_continuous_conversion(uint8_t display_mode)
mahphalke 1:48914f9593f1 501 {
mahphalke 1:48914f9593f1 502 int32_t error_code;
mahphalke 1:48914f9593f1 503 int32_t sample_data;
mahphalke 1:48914f9593f1 504 ad717x_st_reg *device_mode_reg;
mahphalke 1:48914f9593f1 505 ad717x_st_reg *device_chnmap_reg;
mahphalke 1:48914f9593f1 506 ad717x_st_reg *device_status_reg;
mahphalke 1:48914f9593f1 507
mahphalke 1:48914f9593f1 508 // Get the pointer to mode register
mahphalke 1:48914f9593f1 509 device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 1:48914f9593f1 510
mahphalke 1:48914f9593f1 511 // Clear the ADC CTRL MODE bits, has the effect of selecting continuous mode
mahphalke 1:48914f9593f1 512 device_mode_reg->value &= ~(AD717X_ADCMODE_REG_MODE(0xf));
mahphalke 1:48914f9593f1 513
mahphalke 1:48914f9593f1 514 if ((error_code = AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 515 AD717X_ADCMODE_REG)) != SUCCESS) {
mahphalke 1:48914f9593f1 516 printf("Error (%ld) setting AD717x Continuous conversion mode." EOL,
mahphalke 1:48914f9593f1 517 error_code);
mahphalke 1:48914f9593f1 518
mahphalke 1:48914f9593f1 519 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 520 return (MENU_CONTINUE);
mahphalke 1:48914f9593f1 521 }
mahphalke 1:48914f9593f1 522
mahphalke 1:48914f9593f1 523 clear_channel_samples();
mahphalke 1:48914f9593f1 524
mahphalke 1:48914f9593f1 525 /*
mahphalke 1:48914f9593f1 526 * If displaying data in stream form, want to output a channel header
mahphalke 1:48914f9593f1 527 */
mahphalke 1:48914f9593f1 528 if (display_mode == DISPLAY_DATA_STREAM) {
mahphalke 1:48914f9593f1 529 bool channel_printed = false;
mahphalke 1:48914f9593f1 530
mahphalke 1:48914f9593f1 531 for (uint8_t chn = 0; chn < NUMBER_OF_CHANNELS; chn++) {
mahphalke 1:48914f9593f1 532 // Get the pointer to channel register
mahphalke 1:48914f9593f1 533 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn);
mahphalke 1:48914f9593f1 534
mahphalke 1:48914f9593f1 535 // if showing all channels, or channel is enabled
mahphalke 1:48914f9593f1 536 if (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN) {
mahphalke 1:48914f9593f1 537 /*
mahphalke 1:48914f9593f1 538 * add the comma before we output the next channel but
mahphalke 1:48914f9593f1 539 * only if at least one channel has been printed
mahphalke 1:48914f9593f1 540 */
mahphalke 1:48914f9593f1 541 if (channel_printed) {
mahphalke 1:48914f9593f1 542 printf(", ");
mahphalke 1:48914f9593f1 543 }
mahphalke 1:48914f9593f1 544 printf("%d", chn);
mahphalke 1:48914f9593f1 545 }
mahphalke 1:48914f9593f1 546 channel_printed = true;
mahphalke 1:48914f9593f1 547 }
mahphalke 1:48914f9593f1 548 printf(EOL);
mahphalke 1:48914f9593f1 549 }
mahphalke 1:48914f9593f1 550
mahphalke 1:48914f9593f1 551 // Continuously read the channels, and store sample values
mahphalke 1:48914f9593f1 552 while (was_escape_key_pressed() != true) {
mahphalke 1:48914f9593f1 553 if (display_mode == DISPLAY_DATA_TABULAR) {
mahphalke 1:48914f9593f1 554 adi_clear_console();
mahphalke 1:48914f9593f1 555 printf("Running continuous conversion mode...\r\nPress Escape to stop" EOL EOL);
mahphalke 1:48914f9593f1 556 }
mahphalke 1:48914f9593f1 557
mahphalke 1:48914f9593f1 558 /*
mahphalke 1:48914f9593f1 559 * this polls the status register READY/ bit to determine when conversion is done
mahphalke 1:48914f9593f1 560 * this also ensures the STATUS register value is up to date and contains the
mahphalke 1:48914f9593f1 561 * channel that was sampled as well.
mahphalke 1:48914f9593f1 562 * Generally, no need to read STATUS separately, but for faster sampling
mahphalke 1:48914f9593f1 563 * enabling the DATA_STATUS bit means that status is appended to ADC data read
mahphalke 1:48914f9593f1 564 * so the channel being sampled is read back (and updated) as part of the same frame
mahphalke 1:48914f9593f1 565 */
mahphalke 1:48914f9593f1 566 if ((error_code = AD717X_WaitForReady(pad717x_dev, 10000)) != SUCCESS) {
mahphalke 1:48914f9593f1 567 printf("Error/Timeout waiting for conversion ready %ld" EOL EOL, error_code);
mahphalke 1:48914f9593f1 568 continue;
mahphalke 1:48914f9593f1 569 }
mahphalke 1:48914f9593f1 570
mahphalke 1:48914f9593f1 571 if ((error_code = AD717X_ReadData(pad717x_dev, &sample_data)) != SUCCESS) {
mahphalke 1:48914f9593f1 572 printf("Error reading ADC Data (%ld)." EOL, error_code);
mahphalke 1:48914f9593f1 573 continue;
mahphalke 1:48914f9593f1 574 }
mahphalke 1:48914f9593f1 575
mahphalke 1:48914f9593f1 576 /*
mahphalke 1:48914f9593f1 577 * No error, need to process the sample, what channel has been read? update that channelSample
mahphalke 1:48914f9593f1 578 */
mahphalke 1:48914f9593f1 579 device_status_reg = AD717X_GetReg(pad717x_dev, AD717X_STATUS_REG);
mahphalke 1:48914f9593f1 580 uint8_t channelRead = device_status_reg->value & 0x0000000F;
mahphalke 1:48914f9593f1 581
mahphalke 1:48914f9593f1 582 if (channelRead < NUMBER_OF_CHANNELS) {
mahphalke 1:48914f9593f1 583 channel_samples[channelRead] = sample_data;
mahphalke 1:48914f9593f1 584 channel_samples_count[channelRead]++;
mahphalke 1:48914f9593f1 585 } else {
mahphalke 1:48914f9593f1 586 printf("Channel Read was %d, which is not < %d" EOL,
mahphalke 1:48914f9593f1 587 channelRead,
mahphalke 1:48914f9593f1 588 NUMBER_OF_CHANNELS);
mahphalke 1:48914f9593f1 589 }
mahphalke 1:48914f9593f1 590
mahphalke 1:48914f9593f1 591 dislay_channel_samples(SHOW_ENABLED_CHANNELS, display_mode);
mahphalke 1:48914f9593f1 592 }
mahphalke 1:48914f9593f1 593
mahphalke 1:48914f9593f1 594 // All done, ADC put into standby mode
mahphalke 1:48914f9593f1 595 device_mode_reg->value =
mahphalke 1:48914f9593f1 596 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 597 AD717X_ADCMODE_REG_MODE(STANDBY_MODE));
mahphalke 1:48914f9593f1 598
mahphalke 1:48914f9593f1 599 if ((error_code = AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 600 AD717X_ADCMODE_REG)) != SUCCESS) {
mahphalke 1:48914f9593f1 601 printf("Error (%ld) setting ADC into standby mode." EOL, error_code);
mahphalke 1:48914f9593f1 602 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 603 }
mahphalke 1:48914f9593f1 604
mahphalke 1:48914f9593f1 605 return (MENU_CONTINUE);
mahphalke 1:48914f9593f1 606 }
mahphalke 1:48914f9593f1 607
mahphalke 1:48914f9593f1 608
mahphalke 1:48914f9593f1 609 /*!
mahphalke 1:48914f9593f1 610 * @brief Samples all enabled channels and displays in tabular form
mahphalke 1:48914f9593f1 611 *
mahphalke 1:48914f9593f1 612 * @details
mahphalke 1:48914f9593f1 613 */
mahphalke 1:48914f9593f1 614 int32_t menu_continuous_conversion_tabular(uint32_t channel_id)
mahphalke 1:48914f9593f1 615 {
mahphalke 1:48914f9593f1 616 do_continuous_conversion(DISPLAY_DATA_TABULAR);
mahphalke 1:48914f9593f1 617
mahphalke 1:48914f9593f1 618 adi_clear_console();
mahphalke 1:48914f9593f1 619 printf("Continuous Conversion completed..." EOL EOL);
mahphalke 1:48914f9593f1 620 dislay_channel_samples(SHOW_ALL_CHANNELS, DISPLAY_DATA_TABULAR);
mahphalke 1:48914f9593f1 621 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 622
mahphalke 1:48914f9593f1 623 return (MENU_CONTINUE);
mahphalke 1:48914f9593f1 624 }
mahphalke 1:48914f9593f1 625
mahphalke 1:48914f9593f1 626
mahphalke 1:48914f9593f1 627 /*!
mahphalke 1:48914f9593f1 628 * @brief Samples all enabled channels and displays on the console
mahphalke 1:48914f9593f1 629 *
mahphalke 1:48914f9593f1 630 * @details
mahphalke 1:48914f9593f1 631 */
mahphalke 1:48914f9593f1 632 int32_t menu_continuous_conversion_stream(uint32_t channel_id)
mahphalke 1:48914f9593f1 633 {
mahphalke 1:48914f9593f1 634 do_continuous_conversion(DISPLAY_DATA_STREAM);
mahphalke 1:48914f9593f1 635 printf("Continuous Conversion completed..." EOL EOL);
mahphalke 1:48914f9593f1 636
mahphalke 1:48914f9593f1 637 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 638 return (MENU_CONTINUE);
mahphalke 1:48914f9593f1 639 }
mahphalke 1:48914f9593f1 640
mahphalke 1:48914f9593f1 641
mahphalke 1:48914f9593f1 642 /*!
mahphalke 1:48914f9593f1 643 * @brief Samples all enabled channels once in Single Conversion mode
mahphalke 1:48914f9593f1 644 *
mahphalke 1:48914f9593f1 645 * @details This stores all channels that are enabled in a bitmask, and then
mahphalke 1:48914f9593f1 646 * runs the ADC in single conversion mode, which acquires one channel
mahphalke 1:48914f9593f1 647 * of data at a time. After capture, that channel is disabled, and
mahphalke 1:48914f9593f1 648 * single conversion run again, until no channels are enabled.
mahphalke 1:48914f9593f1 649 * The original enable state of each channel is then restored.
mahphalke 1:48914f9593f1 650 */
mahphalke 1:48914f9593f1 651 int32_t menu_single_conversion(uint32_t channel_id)
mahphalke 1:48914f9593f1 652 {
mahphalke 1:48914f9593f1 653 int32_t error_code;
mahphalke 1:48914f9593f1 654 uint16_t channel_enable_mask = 0;
mahphalke 1:48914f9593f1 655 uint8_t channel_count = 0;
mahphalke 1:48914f9593f1 656 int32_t sample_data;
mahphalke 1:48914f9593f1 657 ad717x_st_reg *device_chnmap_reg;
mahphalke 1:48914f9593f1 658 ad717x_st_reg *device_mode_reg;
mahphalke 1:48914f9593f1 659 ad717x_st_reg *device_status_reg;
mahphalke 1:48914f9593f1 660
mahphalke 1:48914f9593f1 661 // Need to store which channels are enabled in this config so it can be restored
mahphalke 1:48914f9593f1 662 for (uint8_t chn = 0 ; chn < NUMBER_OF_CHANNELS; chn++) {
mahphalke 1:48914f9593f1 663 // Get the pointer to channel register
mahphalke 1:48914f9593f1 664 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn);
mahphalke 1:48914f9593f1 665
mahphalke 1:48914f9593f1 666 if (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN) {
mahphalke 1:48914f9593f1 667 channel_enable_mask |= (1 << chn);
mahphalke 1:48914f9593f1 668 channel_count++;
mahphalke 1:48914f9593f1 669 }
mahphalke 1:48914f9593f1 670 }
mahphalke 1:48914f9593f1 671
mahphalke 1:48914f9593f1 672 clear_channel_samples();
mahphalke 1:48914f9593f1 673
mahphalke 1:48914f9593f1 674 adi_clear_console();
mahphalke 1:48914f9593f1 675 printf("Running Single conversion mode...\r\nPress Escape to stop" EOL EOL);
mahphalke 1:48914f9593f1 676
mahphalke 1:48914f9593f1 677 // Get the pointer to mode register
mahphalke 1:48914f9593f1 678 device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 1:48914f9593f1 679
mahphalke 1:48914f9593f1 680 // Clear the ADC CTRL MODE bits, selecting continuous mode
mahphalke 1:48914f9593f1 681 device_mode_reg->value =
mahphalke 1:48914f9593f1 682 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 683 AD717X_ADCMODE_REG_MODE(CONTINUOUS_CONVERSION));
mahphalke 1:48914f9593f1 684
mahphalke 1:48914f9593f1 685 // read the channels, and store sample values
mahphalke 1:48914f9593f1 686 for(uint8_t loopCount = 0 ; ((was_escape_key_pressed() != true)
mahphalke 1:48914f9593f1 687 && (loopCount < channel_count)) ; loopCount++) {
mahphalke 1:48914f9593f1 688
mahphalke 1:48914f9593f1 689 device_mode_reg->value =
mahphalke 1:48914f9593f1 690 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 691 AD717X_ADCMODE_REG_MODE(SINGLE_CONVERISION));
mahphalke 1:48914f9593f1 692
mahphalke 1:48914f9593f1 693 if ((error_code = AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 694 AD717X_ADCMODE_REG)) != SUCCESS) {
mahphalke 1:48914f9593f1 695 printf("Error (%ld) setting AD717x Single conversion mode." EOL, error_code);
mahphalke 1:48914f9593f1 696 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 697 continue;
mahphalke 1:48914f9593f1 698 }
mahphalke 1:48914f9593f1 699
mahphalke 1:48914f9593f1 700 /*
mahphalke 1:48914f9593f1 701 * this polls the status register READY/ bit to determine when conversion is done
mahphalke 1:48914f9593f1 702 * this also ensures the STATUS register value is up to date and contains the
mahphalke 1:48914f9593f1 703 * channel that was sampled as well. No need to read STATUS separately
mahphalke 1:48914f9593f1 704 */
mahphalke 1:48914f9593f1 705 if ((error_code = AD717X_WaitForReady(pad717x_dev, 10000)) != SUCCESS) {
mahphalke 1:48914f9593f1 706 printf("Error/Timeout waiting for conversion ready %ld" EOL, error_code);
mahphalke 1:48914f9593f1 707 continue;
mahphalke 1:48914f9593f1 708 }
mahphalke 1:48914f9593f1 709
mahphalke 1:48914f9593f1 710 if ((error_code = AD717X_ReadData(pad717x_dev, &sample_data)) != SUCCESS) {
mahphalke 1:48914f9593f1 711 printf("Error reading ADC Data (%ld)." EOL, error_code);
mahphalke 1:48914f9593f1 712 continue;
mahphalke 1:48914f9593f1 713 }
mahphalke 1:48914f9593f1 714
mahphalke 1:48914f9593f1 715 /*
mahphalke 1:48914f9593f1 716 * No error, need to process the sample, what channel has been read? update that channelSample
mahphalke 1:48914f9593f1 717 */
mahphalke 1:48914f9593f1 718 device_status_reg = AD717X_GetReg(pad717x_dev, AD717X_STATUS_REG);
mahphalke 1:48914f9593f1 719 uint8_t channelRead = device_status_reg->value & 0x0000000F;
mahphalke 1:48914f9593f1 720
mahphalke 1:48914f9593f1 721 if (channelRead < NUMBER_OF_CHANNELS) {
mahphalke 1:48914f9593f1 722 channel_samples[channelRead] = sample_data;
mahphalke 1:48914f9593f1 723 channel_samples_count[channelRead]++;
mahphalke 1:48914f9593f1 724
mahphalke 1:48914f9593f1 725 // Get the pointer to channel register
mahphalke 1:48914f9593f1 726 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + channelRead);
mahphalke 1:48914f9593f1 727
mahphalke 1:48914f9593f1 728 /* also need to clear the channel enable bit so the next single conversion cycle will sample the next channel */
mahphalke 1:48914f9593f1 729 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 1:48914f9593f1 730 if ((error_code = AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 731 AD717X_CHMAP0_REG + channelRead)) != SUCCESS) {
mahphalke 1:48914f9593f1 732 printf("Error (%ld) Clearing channel %d Enable bit." EOL,
mahphalke 1:48914f9593f1 733 error_code,
mahphalke 1:48914f9593f1 734 channelRead);
mahphalke 1:48914f9593f1 735
mahphalke 1:48914f9593f1 736 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 737 continue;
mahphalke 1:48914f9593f1 738 }
mahphalke 1:48914f9593f1 739 } else {
mahphalke 1:48914f9593f1 740 printf("Channel Read was %d, which is not < AD717x_CHANNEL_COUNT" EOL,
mahphalke 1:48914f9593f1 741 channelRead);
mahphalke 1:48914f9593f1 742 }
mahphalke 1:48914f9593f1 743 }
mahphalke 1:48914f9593f1 744
mahphalke 1:48914f9593f1 745 // All done, ADC put into standby mode
mahphalke 1:48914f9593f1 746 device_mode_reg->value =
mahphalke 1:48914f9593f1 747 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 748 AD717X_ADCMODE_REG_MODE(STANDBY_MODE));
mahphalke 1:48914f9593f1 749
mahphalke 1:48914f9593f1 750 // Need to restore the channels that were disabled during acquisition
mahphalke 1:48914f9593f1 751 for(uint8_t chn = 0 ; chn < NUMBER_OF_CHANNELS ; chn++) {
mahphalke 1:48914f9593f1 752 if (channel_enable_mask & (1 << chn)) {
mahphalke 1:48914f9593f1 753 // Get the pointer to channel register
mahphalke 1:48914f9593f1 754 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn);
mahphalke 1:48914f9593f1 755
mahphalke 1:48914f9593f1 756 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 1:48914f9593f1 757
mahphalke 1:48914f9593f1 758 if ((error_code = AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 759 AD717X_CHMAP0_REG + chn)) != SUCCESS) {
mahphalke 1:48914f9593f1 760 printf("Error (%ld) Setting channel %d Enable bit" EOL EOL, error_code, chn);
mahphalke 1:48914f9593f1 761
mahphalke 1:48914f9593f1 762 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 763 return (MENU_CONTINUE);
mahphalke 1:48914f9593f1 764 }
mahphalke 1:48914f9593f1 765 }
mahphalke 1:48914f9593f1 766 }
mahphalke 1:48914f9593f1 767
mahphalke 1:48914f9593f1 768 printf("Single Conversion completed..." EOL EOL);
mahphalke 1:48914f9593f1 769 dislay_channel_samples(SHOW_ENABLED_CHANNELS, DISPLAY_DATA_TABULAR);
mahphalke 1:48914f9593f1 770
mahphalke 1:48914f9593f1 771 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 772 return (MENU_CONTINUE);
mahphalke 1:48914f9593f1 773 }
mahphalke 1:48914f9593f1 774
mahphalke 1:48914f9593f1 775
mahphalke 1:48914f9593f1 776 /*!
mahphalke 1:48914f9593f1 777 * @brief Handle the menu to sample the channels
mahphalke 3:6c7324997606 778 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 779 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 780 */
mahphalke 1:48914f9593f1 781 int32_t menu_sample_channels(uint32_t menu_id)
mahphalke 1:48914f9593f1 782 {
mahphalke 1:48914f9593f1 783 return adi_do_console_menu(&acquisition_menu);
mahphalke 1:48914f9593f1 784 }
mahphalke 1:48914f9593f1 785
mahphalke 1:48914f9593f1 786
mahphalke 1:48914f9593f1 787 /* @brief Enable or disable adc channels
mahphalke 3:6c7324997606 788 * @param channel ENABLE/DISABLE action
mahphalke 1:48914f9593f1 789 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 790 **/
mahphalke 1:48914f9593f1 791 int32_t menu_channels_enable_disable(uint32_t action)
mahphalke 1:48914f9593f1 792 {
mahphalke 1:48914f9593f1 793 char rx_char; // received character from the serial port
mahphalke 1:48914f9593f1 794 uint8_t current_channel; // channel to be enabled
mahphalke 1:48914f9593f1 795 ad717x_st_reg *device_chnmap_reg; // Pointer to channel map register
mahphalke 1:48914f9593f1 796
mahphalke 1:48914f9593f1 797 do {
mahphalke 1:48914f9593f1 798 // Get the channel selection
mahphalke 1:48914f9593f1 799 current_channel = get_channel_selection();
mahphalke 1:48914f9593f1 800
mahphalke 1:48914f9593f1 801 // Get the pointer to channel register
mahphalke 1:48914f9593f1 802 device_chnmap_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 803 AD717X_CHMAP0_REG + current_channel);
mahphalke 1:48914f9593f1 804
mahphalke 1:48914f9593f1 805 if (action == SELECT_ENABLE) {
mahphalke 1:48914f9593f1 806 // Enable the selected channel
mahphalke 1:48914f9593f1 807 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 1:48914f9593f1 808 printf("\tChannel %d is Enabled ", current_channel);
mahphalke 1:48914f9593f1 809 } else {
mahphalke 1:48914f9593f1 810 // Disable the selected channel
mahphalke 1:48914f9593f1 811 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 1:48914f9593f1 812 printf("\tChannel %d is Disabled ", current_channel);
mahphalke 1:48914f9593f1 813 }
mahphalke 1:48914f9593f1 814
mahphalke 1:48914f9593f1 815 // Write to ADC channel register
mahphalke 1:48914f9593f1 816 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 817 AD717X_CHMAP0_REG + current_channel) != SUCCESS) {
mahphalke 1:48914f9593f1 818 printf("\tError in channel Enable/Disable!!" EOL);
mahphalke 1:48914f9593f1 819 break;
mahphalke 1:48914f9593f1 820 }
mahphalke 1:48914f9593f1 821
mahphalke 1:48914f9593f1 822 printf(EOL EOL "\tDo you want to continue (y/n)?: ");
mahphalke 1:48914f9593f1 823 rx_char = toupper(getchar());
mahphalke 1:48914f9593f1 824
mahphalke 1:48914f9593f1 825 if ((rx_char != 'N') && (rx_char != 'Y')) {
mahphalke 1:48914f9593f1 826 printf("Invalid entry!!" EOL);
mahphalke 1:48914f9593f1 827 } else {
mahphalke 1:48914f9593f1 828 // Print the entered character back on console window (serial port)
mahphalke 1:48914f9593f1 829 printf("%c" EOL, rx_char);
mahphalke 1:48914f9593f1 830 }
mahphalke 1:48914f9593f1 831 } while (rx_char != 'N');
mahphalke 1:48914f9593f1 832
mahphalke 1:48914f9593f1 833 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 834 }
mahphalke 1:48914f9593f1 835
mahphalke 1:48914f9593f1 836
mahphalke 1:48914f9593f1 837 /*!
mahphalke 1:48914f9593f1 838 * @brief Display the menu to enable/disable channel selection
mahphalke 3:6c7324997606 839 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 840 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 841 */
mahphalke 1:48914f9593f1 842 int32_t menu_chn_enable_disable_display(uint32_t menu_id)
mahphalke 1:48914f9593f1 843 {
mahphalke 1:48914f9593f1 844 return adi_do_console_menu(&chn_enable_disable_menu);
mahphalke 1:48914f9593f1 845 }
mahphalke 1:48914f9593f1 846
mahphalke 1:48914f9593f1 847
mahphalke 1:48914f9593f1 848 /*!
mahphalke 1:48914f9593f1 849 * @brief Handle the menu to connect input to channel
mahphalke 3:6c7324997606 850 * @param analog input to be connected
mahphalke 1:48914f9593f1 851 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 852 */
mahphalke 1:48914f9593f1 853 int32_t menu_analog_input_connect(uint32_t user_analog_input)
mahphalke 1:48914f9593f1 854 {
mahphalke 1:48914f9593f1 855 uint8_t current_channel; // current channel
mahphalke 1:48914f9593f1 856 ad717x_st_reg *device_chnmap_reg; // Pointer to channel map register
mahphalke 1:48914f9593f1 857
mahphalke 1:48914f9593f1 858 adi_clear_console();
mahphalke 1:48914f9593f1 859
mahphalke 1:48914f9593f1 860 // Get the channel selection
mahphalke 1:48914f9593f1 861 current_channel = get_channel_selection();
mahphalke 1:48914f9593f1 862
mahphalke 1:48914f9593f1 863 if (input_to_select == POS_ANALOG_INP_SELECT) {
mahphalke 1:48914f9593f1 864 printf(EOL "\tSelect Positive Analog Input" EOL);
mahphalke 1:48914f9593f1 865 device_setup.pos_analog_input = user_analog_input;
mahphalke 1:48914f9593f1 866 } else if (input_to_select == NEG_ANALOG_INP_SELECT) {
mahphalke 1:48914f9593f1 867 printf(EOL "\tSelect Negative Analog Input" EOL);
mahphalke 1:48914f9593f1 868 device_setup.neg_analog_input = user_analog_input;
mahphalke 1:48914f9593f1 869 } else {
mahphalke 1:48914f9593f1 870 device_setup.pos_analog_input = AD717X_CHMAP_REG_AINPOS_RD(user_analog_input);
mahphalke 1:48914f9593f1 871 device_setup.neg_analog_input = AD717X_CHMAP_REG_AINNEG_RD(user_analog_input);
mahphalke 1:48914f9593f1 872 }
mahphalke 1:48914f9593f1 873
mahphalke 1:48914f9593f1 874 // Get the pointer to channel map register structure
mahphalke 1:48914f9593f1 875 device_chnmap_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 876 AD717X_CHMAP0_REG + current_channel);
mahphalke 1:48914f9593f1 877
mahphalke 1:48914f9593f1 878 #if defined(DEV_AD4111) || defined(DEV_AD4112)
mahphalke 1:48914f9593f1 879 // Select analog input pair
mahphalke 1:48914f9593f1 880 device_chnmap_reg->value =
mahphalke 1:48914f9593f1 881 ((device_chnmap_reg->value & ~AD4111_CHMAP_REG_INPUT_MSK) |
mahphalke 1:48914f9593f1 882 AD4111_CHMAP_REG_INPUT(user_analog_input));
mahphalke 1:48914f9593f1 883 #elif
mahphalke 1:48914f9593f1 884 // Select positive analog input
mahphalke 1:48914f9593f1 885 device_chnmap_reg->value =
mahphalke 1:48914f9593f1 886 ((device_chnmap_reg->value & ~AD717X_CHMAP_REG_AINPOS_MSK) |
mahphalke 1:48914f9593f1 887 AD717X_CHMAP_REG_AINPOS(device_setup.pos_analog_input));
mahphalke 1:48914f9593f1 888
mahphalke 1:48914f9593f1 889 // Select negative analog input
mahphalke 1:48914f9593f1 890 device_chnmap_reg->value =
mahphalke 1:48914f9593f1 891 ((device_chnmap_reg->value & ~AD717X_CHMAP_REG_AINNEG_MSK) |
mahphalke 1:48914f9593f1 892 AD717X_CHMAP_REG_AINNEG(device_setup.neg_analog_input));
mahphalke 1:48914f9593f1 893 #endif
mahphalke 1:48914f9593f1 894
mahphalke 1:48914f9593f1 895 // Write to ADC channel register
mahphalke 1:48914f9593f1 896 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 897 AD717X_CHMAP0_REG + current_channel) != SUCCESS) {
mahphalke 1:48914f9593f1 898 printf(EOL "\tError in analog input connection!!" EOL);
mahphalke 1:48914f9593f1 899 } else {
mahphalke 1:48914f9593f1 900 printf(EOL "\t%s is connected to INP+ and %s is connectd to INP- for channel %d"
mahphalke 1:48914f9593f1 901 EOL
mahphalke 1:48914f9593f1 902 EOL,
mahphalke 1:48914f9593f1 903 input_pin_map[device_setup.pos_analog_input],
mahphalke 1:48914f9593f1 904 input_pin_map[device_setup.neg_analog_input],
mahphalke 1:48914f9593f1 905 current_channel);
mahphalke 1:48914f9593f1 906 }
mahphalke 1:48914f9593f1 907
mahphalke 1:48914f9593f1 908 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 909 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 910 }
mahphalke 1:48914f9593f1 911
mahphalke 1:48914f9593f1 912
mahphalke 1:48914f9593f1 913 /*!
mahphalke 1:48914f9593f1 914 * @brief Display the menu selections to connect analog input pins to a channel
mahphalke 3:6c7324997606 915 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 916 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 917 */
mahphalke 1:48914f9593f1 918 int32_t menu_input_chn_connect_display(uint32_t menu_id)
mahphalke 1:48914f9593f1 919 {
mahphalke 1:48914f9593f1 920 #if defined(DEV_AD4111) || defined(DEV_AD4112)
mahphalke 1:48914f9593f1 921 input_to_select = ANALOG_INP_PAIR_SELECT;
mahphalke 1:48914f9593f1 922 adi_do_console_menu(&analog_input_connect_menu);
mahphalke 1:48914f9593f1 923 #else
mahphalke 1:48914f9593f1 924 input_to_select = POS_ANALOG_INP_SELECT;
mahphalke 1:48914f9593f1 925 adi_do_console_menu(&analog_input_connect_menu);
mahphalke 1:48914f9593f1 926
mahphalke 1:48914f9593f1 927 input_to_select = NEG_ANALOG_INP_SELECT;
mahphalke 1:48914f9593f1 928 adi_do_console_menu(&analog_input_connect_menu);
mahphalke 1:48914f9593f1 929 #endif
mahphalke 1:48914f9593f1 930
mahphalke 1:48914f9593f1 931 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 932 }
mahphalke 1:48914f9593f1 933
mahphalke 1:48914f9593f1 934
mahphalke 1:48914f9593f1 935 /*!
mahphalke 1:48914f9593f1 936 * @brief Handle the menu to select the filter type
mahphalke 3:6c7324997606 937 * @param user selected filter type
mahphalke 1:48914f9593f1 938 * @return MENU_DONE
mahphalke 1:48914f9593f1 939 */
mahphalke 1:48914f9593f1 940 int32_t menu_filter_select(uint32_t user_input_filter_type)
mahphalke 1:48914f9593f1 941 {
mahphalke 1:48914f9593f1 942 ad717x_st_reg *device_filter_config_reg; // Pointer to filter config register
mahphalke 1:48914f9593f1 943
mahphalke 1:48914f9593f1 944 // Get the pointer to filter config register structure
mahphalke 1:48914f9593f1 945 device_filter_config_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 946 AD717X_FILTCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 947
mahphalke 1:48914f9593f1 948 device_setup.filter = user_input_filter_type;
mahphalke 1:48914f9593f1 949
mahphalke 1:48914f9593f1 950 device_filter_config_reg->value =
mahphalke 1:48914f9593f1 951 ((device_filter_config_reg->value & ~AD717X_FILT_CONF_REG_ORDER_MSK) |
mahphalke 1:48914f9593f1 952 AD717X_FILT_CONF_REG_ORDER(device_setup.filter));
mahphalke 1:48914f9593f1 953
mahphalke 1:48914f9593f1 954 if (device_setup.filter == SINC3_FILTER) {
mahphalke 1:48914f9593f1 955 device_filter_config_reg->value |= AD717X_FILT_CONF_REG_SINC3_MAP;
mahphalke 1:48914f9593f1 956 } else {
mahphalke 1:48914f9593f1 957 device_filter_config_reg->value &= (~AD717X_FILT_CONF_REG_SINC3_MAP);
mahphalke 1:48914f9593f1 958 }
mahphalke 1:48914f9593f1 959
mahphalke 1:48914f9593f1 960 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 961 AD717X_FILTCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 962 printf(EOL "\tError in Filter Selection!!" EOL);
mahphalke 1:48914f9593f1 963 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 964 }
mahphalke 1:48914f9593f1 965
mahphalke 1:48914f9593f1 966 return MENU_DONE;
mahphalke 1:48914f9593f1 967 }
mahphalke 1:48914f9593f1 968
mahphalke 1:48914f9593f1 969
mahphalke 1:48914f9593f1 970 /*!
mahphalke 1:48914f9593f1 971 * @brief Handle the menu to enable/disable the post filter
mahphalke 3:6c7324997606 972 * @param user selected action
mahphalke 1:48914f9593f1 973 * @return MENU_DONE
mahphalke 1:48914f9593f1 974 */
mahphalke 1:48914f9593f1 975 int32_t menu_postfiler_enable_disable(uint32_t user_action)
mahphalke 1:48914f9593f1 976 {
mahphalke 1:48914f9593f1 977 ad717x_st_reg *device_filter_config_reg; // Pointer to filter config register
mahphalke 1:48914f9593f1 978
mahphalke 1:48914f9593f1 979 // Get the pointer to filter config register structure
mahphalke 1:48914f9593f1 980 device_filter_config_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 981 AD717X_FILTCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 982
mahphalke 1:48914f9593f1 983 if (user_action == SELECT_ENABLE) {
mahphalke 1:48914f9593f1 984 device_setup.post_filter_enabled = SELECT_ENABLE;
mahphalke 1:48914f9593f1 985 device_filter_config_reg->value |= AD717X_FILT_CONF_REG_ENHFILTEN;
mahphalke 1:48914f9593f1 986 } else {
mahphalke 1:48914f9593f1 987 device_setup.post_filter_enabled = SELECT_DISBLE;
mahphalke 1:48914f9593f1 988 device_filter_config_reg->value &= (~AD717X_FILT_CONF_REG_ENHFILTEN);
mahphalke 1:48914f9593f1 989 }
mahphalke 1:48914f9593f1 990
mahphalke 1:48914f9593f1 991 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 992 AD717X_FILTCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 993 printf(EOL "\tError in Enabling/Disabling Postfilter!!" EOL);
mahphalke 1:48914f9593f1 994 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 995 }
mahphalke 1:48914f9593f1 996
mahphalke 1:48914f9593f1 997 return MENU_DONE;
mahphalke 1:48914f9593f1 998 }
mahphalke 1:48914f9593f1 999
mahphalke 1:48914f9593f1 1000
mahphalke 1:48914f9593f1 1001 /*!
mahphalke 1:48914f9593f1 1002 * @brief Handle the menu to select the post filter
mahphalke 3:6c7324997606 1003 * @param user selected post filter type
mahphalke 1:48914f9593f1 1004 * @return MENU_DONE
mahphalke 1:48914f9593f1 1005 */
mahphalke 1:48914f9593f1 1006 int32_t menu_postfiler_select(uint32_t user_input_post_filter_type)
mahphalke 1:48914f9593f1 1007 {
mahphalke 1:48914f9593f1 1008 ad717x_st_reg *device_filter_config_reg; // Pointer to filter config register
mahphalke 1:48914f9593f1 1009
mahphalke 1:48914f9593f1 1010 // Get the pointer to filter config register structure
mahphalke 1:48914f9593f1 1011 device_filter_config_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1012 AD717X_FILTCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 1013
mahphalke 1:48914f9593f1 1014 device_setup.postfilter = user_input_post_filter_type;
mahphalke 1:48914f9593f1 1015
mahphalke 1:48914f9593f1 1016 device_filter_config_reg->value =
mahphalke 1:48914f9593f1 1017 ((device_filter_config_reg->value & ~AD717X_FILT_CONF_REG_ENHFILT_MSK) |
mahphalke 1:48914f9593f1 1018 AD717X_FILT_CONF_REG_ENHFILT(device_setup.postfilter));
mahphalke 1:48914f9593f1 1019
mahphalke 1:48914f9593f1 1020 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1021 AD717X_FILTCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 1022 printf(EOL "\tError in Post-Filter Selection!!" EOL);
mahphalke 1:48914f9593f1 1023 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1024 }
mahphalke 1:48914f9593f1 1025
mahphalke 1:48914f9593f1 1026 return MENU_DONE;
mahphalke 1:48914f9593f1 1027 }
mahphalke 1:48914f9593f1 1028
mahphalke 1:48914f9593f1 1029
mahphalke 1:48914f9593f1 1030 /*!
mahphalke 1:48914f9593f1 1031 * @brief Handle the menu to select the ODR value
mahphalke 3:6c7324997606 1032 * @param user selected ODR
mahphalke 1:48914f9593f1 1033 * @return MENU_DONE
mahphalke 1:48914f9593f1 1034 */
mahphalke 1:48914f9593f1 1035 int32_t menu_odr_select(uint32_t user_input_odr_val)
mahphalke 1:48914f9593f1 1036 {
mahphalke 1:48914f9593f1 1037 ad717x_st_reg *device_filter_config_reg; // Pointer to filter config register
mahphalke 1:48914f9593f1 1038
mahphalke 1:48914f9593f1 1039 // Get the pointer to filter config register structure
mahphalke 1:48914f9593f1 1040 device_filter_config_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1041 AD717X_FILTCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 1042
mahphalke 1:48914f9593f1 1043 device_setup.odr_bits = user_input_odr_val;
mahphalke 1:48914f9593f1 1044
mahphalke 1:48914f9593f1 1045 device_filter_config_reg->value =
mahphalke 1:48914f9593f1 1046 ((device_filter_config_reg->value & ~AD717X_FILT_CONF_REG_ODR_MSK) |
mahphalke 1:48914f9593f1 1047 AD717X_FILT_CONF_REG_ODR(device_setup.odr_bits));
mahphalke 1:48914f9593f1 1048
mahphalke 1:48914f9593f1 1049 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1050 AD717X_FILTCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 1051 printf(EOL "\tError in ODR Selection!!" EOL);
mahphalke 1:48914f9593f1 1052 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1053 }
mahphalke 1:48914f9593f1 1054
mahphalke 1:48914f9593f1 1055 return MENU_DONE;
mahphalke 1:48914f9593f1 1056 }
mahphalke 1:48914f9593f1 1057
mahphalke 1:48914f9593f1 1058
mahphalke 1:48914f9593f1 1059 /*!
mahphalke 1:48914f9593f1 1060 * @brief Handle the menu to select the polarity
mahphalke 3:6c7324997606 1061 * @param user selected polarity
mahphalke 1:48914f9593f1 1062 * @return MENU_DONE
mahphalke 1:48914f9593f1 1063 */
mahphalke 1:48914f9593f1 1064 int32_t menu_polarity_select(uint32_t user_input_polarity)
mahphalke 1:48914f9593f1 1065 {
mahphalke 1:48914f9593f1 1066 ad717x_st_reg *device_setup_control_reg; // Pointer to setup control register
mahphalke 1:48914f9593f1 1067
mahphalke 1:48914f9593f1 1068 // Get the pointer to setup control register structure
mahphalke 1:48914f9593f1 1069 device_setup_control_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1070 AD717X_SETUPCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 1071
mahphalke 1:48914f9593f1 1072 if (user_input_polarity == BIPOLAR) {
mahphalke 1:48914f9593f1 1073 device_setup.polarity = BIPOLAR;
mahphalke 1:48914f9593f1 1074 device_setup_control_reg->value |= AD717X_SETUP_CONF_REG_BI_UNIPOLAR;
mahphalke 1:48914f9593f1 1075 } else {
mahphalke 1:48914f9593f1 1076 device_setup.polarity = UNIPOLAR;
mahphalke 1:48914f9593f1 1077 device_setup_control_reg->value &= (~AD717X_SETUP_CONF_REG_BI_UNIPOLAR);
mahphalke 1:48914f9593f1 1078 }
mahphalke 1:48914f9593f1 1079
mahphalke 1:48914f9593f1 1080 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1081 AD717X_SETUPCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 1082 printf(EOL "\tError in Polarity Selection!!" EOL);
mahphalke 1:48914f9593f1 1083 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1084 }
mahphalke 1:48914f9593f1 1085
mahphalke 1:48914f9593f1 1086 return MENU_DONE;
mahphalke 1:48914f9593f1 1087 }
mahphalke 1:48914f9593f1 1088
mahphalke 1:48914f9593f1 1089
mahphalke 1:48914f9593f1 1090 /*!
mahphalke 1:48914f9593f1 1091 * @brief Handle the menu to select the reference source
mahphalke 3:6c7324997606 1092 * @param user selected reference source
mahphalke 1:48914f9593f1 1093 * @return MENU_DONE
mahphalke 1:48914f9593f1 1094 */
mahphalke 1:48914f9593f1 1095 int32_t menu_reference_source_select(uint32_t user_input_reference)
mahphalke 1:48914f9593f1 1096 {
mahphalke 1:48914f9593f1 1097 ad717x_st_reg *device_setup_control_reg; // Pointer to setup control register
mahphalke 1:48914f9593f1 1098 ad717x_st_reg *device_mode_reg; // Pointer to adc mode register
mahphalke 1:48914f9593f1 1099
mahphalke 1:48914f9593f1 1100 device_setup.reference = user_input_reference;
mahphalke 1:48914f9593f1 1101
mahphalke 1:48914f9593f1 1102 // Get the pointer to device mode register structure
mahphalke 1:48914f9593f1 1103 device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 1:48914f9593f1 1104
mahphalke 1:48914f9593f1 1105 if (device_setup.reference == INTERNAL) {
mahphalke 1:48914f9593f1 1106 device_mode_reg->value |= AD717X_ADCMODE_REG_REF_EN;
mahphalke 1:48914f9593f1 1107 } else {
mahphalke 1:48914f9593f1 1108 device_mode_reg->value &= (~AD717X_ADCMODE_REG_REF_EN);
mahphalke 1:48914f9593f1 1109 }
mahphalke 1:48914f9593f1 1110
mahphalke 1:48914f9593f1 1111 if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 1112 printf(EOL "\tError in Polarity Selection!!" EOL);
mahphalke 1:48914f9593f1 1113 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1114 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1115 }
mahphalke 1:48914f9593f1 1116
mahphalke 1:48914f9593f1 1117 // Get the pointer to setup control register structure
mahphalke 1:48914f9593f1 1118 device_setup_control_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1119 AD717X_SETUPCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 1120
mahphalke 1:48914f9593f1 1121 device_setup_control_reg->value =
mahphalke 1:48914f9593f1 1122 ((device_setup_control_reg->value & ~AD717X_SETUP_CONF_REG_REF_SEL_MSK) |
mahphalke 1:48914f9593f1 1123 AD717X_SETUP_CONF_REG_REF_SEL(device_setup.reference));
mahphalke 1:48914f9593f1 1124
mahphalke 1:48914f9593f1 1125 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1126 AD717X_SETUPCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 1127 printf(EOL "\tError in Polarity Selection!!" EOL);
mahphalke 1:48914f9593f1 1128 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1129 }
mahphalke 1:48914f9593f1 1130
mahphalke 1:48914f9593f1 1131 return MENU_DONE;
mahphalke 1:48914f9593f1 1132 }
mahphalke 1:48914f9593f1 1133
mahphalke 1:48914f9593f1 1134
mahphalke 1:48914f9593f1 1135 /*!
mahphalke 1:48914f9593f1 1136 * @brief Handle the menu to enable/disable the reference buffers
mahphalke 3:6c7324997606 1137 * @param user selected action
mahphalke 1:48914f9593f1 1138 * @return MENU_DONE
mahphalke 1:48914f9593f1 1139 */
mahphalke 1:48914f9593f1 1140 int32_t menu_ref_buffer_enable_disable(uint32_t user_action)
mahphalke 1:48914f9593f1 1141 {
mahphalke 1:48914f9593f1 1142 ad717x_st_reg *device_setup_control_reg; // Pointer to setup control register
mahphalke 1:48914f9593f1 1143
mahphalke 1:48914f9593f1 1144 // Get the pointer to setup control register structure
mahphalke 1:48914f9593f1 1145 device_setup_control_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1146 AD717X_SETUPCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 1147
mahphalke 1:48914f9593f1 1148 device_setup.reference_buffers = user_action;
mahphalke 1:48914f9593f1 1149
mahphalke 1:48914f9593f1 1150 if (user_action == SELECT_ENABLE) {
mahphalke 1:48914f9593f1 1151 // Enable ref buffers (+ve/-ve)
mahphalke 1:48914f9593f1 1152 device_setup_control_reg->value |=
mahphalke 1:48914f9593f1 1153 (AD717X_SETUP_CONF_REG_REFBUF_P |
mahphalke 1:48914f9593f1 1154 AD717X_SETUP_CONF_REG_REFBUF_N);
mahphalke 1:48914f9593f1 1155 } else {
mahphalke 1:48914f9593f1 1156 // Disable ref buffers (+ve/-ve)
mahphalke 1:48914f9593f1 1157 device_setup_control_reg->value &=
mahphalke 1:48914f9593f1 1158 (~(AD717X_SETUP_CONF_REG_REFBUF_P |
mahphalke 1:48914f9593f1 1159 AD717X_SETUP_CONF_REG_REFBUF_N));
mahphalke 1:48914f9593f1 1160 }
mahphalke 1:48914f9593f1 1161
mahphalke 1:48914f9593f1 1162 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1163 AD717X_SETUPCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 1164 printf(EOL "\tError in Reference Buffer Selection!!" EOL);
mahphalke 1:48914f9593f1 1165 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1166 }
mahphalke 1:48914f9593f1 1167
mahphalke 1:48914f9593f1 1168 return MENU_DONE;
mahphalke 1:48914f9593f1 1169 }
mahphalke 1:48914f9593f1 1170
mahphalke 1:48914f9593f1 1171
mahphalke 1:48914f9593f1 1172 /*!
mahphalke 1:48914f9593f1 1173 * @brief Handle the menu to enable/disable the input buffers
mahphalke 3:6c7324997606 1174 * @param user selected action
mahphalke 1:48914f9593f1 1175 * @return MENU_DONE
mahphalke 1:48914f9593f1 1176 */
mahphalke 1:48914f9593f1 1177 int32_t menu_input_buffer_enable_disable(uint32_t user_action)
mahphalke 1:48914f9593f1 1178 {
mahphalke 1:48914f9593f1 1179 ad717x_st_reg *device_setup_control_reg; // Pointer to setup control register
mahphalke 1:48914f9593f1 1180
mahphalke 1:48914f9593f1 1181 // Get the pointer to setup control register structure
mahphalke 1:48914f9593f1 1182 device_setup_control_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1183 AD717X_SETUPCON0_REG + device_setup.setup);
mahphalke 1:48914f9593f1 1184
mahphalke 1:48914f9593f1 1185 device_setup.input_buffers = user_action;
mahphalke 1:48914f9593f1 1186
mahphalke 1:48914f9593f1 1187 if (user_action == SELECT_ENABLE) {
mahphalke 1:48914f9593f1 1188 // Enable ref buffers (+ve/-ve)
mahphalke 1:48914f9593f1 1189 device_setup_control_reg->value |=
mahphalke 1:48914f9593f1 1190 (AD717X_SETUP_CONF_REG_AINBUF_P |
mahphalke 1:48914f9593f1 1191 AD717X_SETUP_CONF_REG_AINBUF_N);
mahphalke 1:48914f9593f1 1192 } else {
mahphalke 1:48914f9593f1 1193 // Disable input buffers (+ve/-ve)
mahphalke 1:48914f9593f1 1194 device_setup_control_reg->value &=
mahphalke 1:48914f9593f1 1195 (~(AD717X_SETUP_CONF_REG_AINBUF_P |
mahphalke 1:48914f9593f1 1196 AD717X_SETUP_CONF_REG_AINBUF_N));
mahphalke 1:48914f9593f1 1197 }
mahphalke 1:48914f9593f1 1198
mahphalke 1:48914f9593f1 1199 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1200 AD717X_SETUPCON0_REG + device_setup.setup) != SUCCESS) {
mahphalke 1:48914f9593f1 1201 printf(EOL "\tError in Reference Buffer Selection!!" EOL);
mahphalke 1:48914f9593f1 1202 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1203 }
mahphalke 1:48914f9593f1 1204
mahphalke 1:48914f9593f1 1205 return MENU_DONE;
mahphalke 1:48914f9593f1 1206 }
mahphalke 1:48914f9593f1 1207
mahphalke 1:48914f9593f1 1208
mahphalke 1:48914f9593f1 1209 /*!
mahphalke 1:48914f9593f1 1210 * @brief Handle the menu to configure and assign the device setup
mahphalke 3:6c7324997606 1211 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 1212 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 1213 */
mahphalke 1:48914f9593f1 1214 int32_t menu_config_and_assign_setup(uint32_t menu_id)
mahphalke 1:48914f9593f1 1215 {
mahphalke 1:48914f9593f1 1216 float filter_odr; // filter ODR value
mahphalke 1:48914f9593f1 1217
mahphalke 1:48914f9593f1 1218 adi_clear_console();
mahphalke 1:48914f9593f1 1219
mahphalke 1:48914f9593f1 1220 // Get the current setup selection
mahphalke 1:48914f9593f1 1221 device_setup.setup = get_setup_selection();
mahphalke 1:48914f9593f1 1222
mahphalke 1:48914f9593f1 1223 // Select the filter type
mahphalke 1:48914f9593f1 1224 adi_do_console_menu(&filter_select_menu);
mahphalke 1:48914f9593f1 1225
mahphalke 1:48914f9593f1 1226 if (device_setup.filter == SINC5_SINC1_FILTER) {
mahphalke 1:48914f9593f1 1227 // Select the post filter parameters
mahphalke 1:48914f9593f1 1228 adi_do_console_menu(&postfilter_enable_disable_menu);
mahphalke 1:48914f9593f1 1229
mahphalke 1:48914f9593f1 1230 if (device_setup.post_filter_enabled == SELECT_ENABLE) {
mahphalke 1:48914f9593f1 1231 // Select the post filter type
mahphalke 1:48914f9593f1 1232 adi_do_console_menu(&postfilter_select_menu);
mahphalke 1:48914f9593f1 1233 }
mahphalke 1:48914f9593f1 1234
mahphalke 1:48914f9593f1 1235 // Select the SINC+SINC1 filter ODR
mahphalke 1:48914f9593f1 1236 adi_do_console_menu(&sinc5_1_data_rate_select_menu);
mahphalke 1:48914f9593f1 1237 filter_odr = sinc5_sinc1_odr_map[device_setup.odr_bits];
mahphalke 1:48914f9593f1 1238 } else {
mahphalke 1:48914f9593f1 1239 // Select the SINC3 filter ODR
mahphalke 1:48914f9593f1 1240 adi_do_console_menu(&sinc3_data_rate_select_menu);
mahphalke 1:48914f9593f1 1241 filter_odr = sinc3_odr_map[device_setup.odr_bits];
mahphalke 1:48914f9593f1 1242 }
mahphalke 1:48914f9593f1 1243
mahphalke 1:48914f9593f1 1244 // Select the polarity
mahphalke 1:48914f9593f1 1245 adi_do_console_menu(&polarity_select_menu);
mahphalke 1:48914f9593f1 1246
mahphalke 1:48914f9593f1 1247 // Select the reference source
mahphalke 1:48914f9593f1 1248 adi_do_console_menu(&reference_select_menu);
mahphalke 1:48914f9593f1 1249
mahphalke 1:48914f9593f1 1250 // Select the reference buffer
mahphalke 1:48914f9593f1 1251 adi_do_console_menu(&ref_buffer_enable_disable_menu);
mahphalke 1:48914f9593f1 1252
mahphalke 1:48914f9593f1 1253 // Select the input buffer
mahphalke 1:48914f9593f1 1254 adi_do_console_menu(&input_buffer_enable_disable_menu);
mahphalke 1:48914f9593f1 1255
mahphalke 1:48914f9593f1 1256 // Print selections
mahphalke 1:48914f9593f1 1257 printf(EOL EOL "\tSetup %ld is configured successfully =>" EOL,
mahphalke 1:48914f9593f1 1258 device_setup.setup);
mahphalke 1:48914f9593f1 1259 printf(EOL "\tFilter Type: %s", filter_name[device_setup.filter]);
mahphalke 1:48914f9593f1 1260
mahphalke 1:48914f9593f1 1261 if (device_setup.filter == SINC5_SINC1_FILTER
mahphalke 1:48914f9593f1 1262 && device_setup.post_filter_enabled) {
mahphalke 1:48914f9593f1 1263 printf("\r\n\tPost Filter Type: %s", postfilter_name[device_setup.postfilter]);
mahphalke 1:48914f9593f1 1264 }
mahphalke 1:48914f9593f1 1265
mahphalke 1:48914f9593f1 1266 printf(EOL "\tData Rate: %f", filter_odr);
mahphalke 1:48914f9593f1 1267 printf(EOL "\tPolarity: %s", polarity_status[device_setup.polarity]);
mahphalke 1:48914f9593f1 1268 printf(EOL "\tReference: %s", reference_name[device_setup.reference]);
mahphalke 1:48914f9593f1 1269 printf(EOL "\tReference Buffers: %s",
mahphalke 1:48914f9593f1 1270 enable_disable_status[device_setup.reference_buffers]);
mahphalke 1:48914f9593f1 1271 printf(EOL "\tInput Buffers: %s",
mahphalke 1:48914f9593f1 1272 enable_disable_status[device_setup.input_buffers]);
mahphalke 1:48914f9593f1 1273 printf(EOL);
mahphalke 1:48914f9593f1 1274
mahphalke 1:48914f9593f1 1275 // Select and assign the channel
mahphalke 1:48914f9593f1 1276 select_chn_assignment();
mahphalke 1:48914f9593f1 1277
mahphalke 1:48914f9593f1 1278 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1279 }
mahphalke 1:48914f9593f1 1280
mahphalke 1:48914f9593f1 1281
mahphalke 1:48914f9593f1 1282 /* @brief Get the data rate based on data rate FS value and vice a versa
mahphalke 3:6c7324997606 1283 * @param Filter Type
mahphalke 3:6c7324997606 1284 * @param Filter data rate register value/bits
mahphalke 3:6c7324997606 1285 * @return Actual Data Rate
mahphalke 1:48914f9593f1 1286 **/
mahphalke 1:48914f9593f1 1287 static float get_data_rate(uint32_t filter, uint32_t odr_reg_val)
mahphalke 1:48914f9593f1 1288 {
mahphalke 1:48914f9593f1 1289 float data_rate; // filter data rate
mahphalke 1:48914f9593f1 1290
mahphalke 1:48914f9593f1 1291 if (filter == SINC5_SINC1_FILTER) {
mahphalke 1:48914f9593f1 1292 data_rate = sinc5_sinc1_odr_map[odr_reg_val];
mahphalke 1:48914f9593f1 1293 } else {
mahphalke 1:48914f9593f1 1294 data_rate = sinc3_odr_map[odr_reg_val];
mahphalke 1:48914f9593f1 1295 }
mahphalke 1:48914f9593f1 1296
mahphalke 1:48914f9593f1 1297 return data_rate;
mahphalke 1:48914f9593f1 1298 }
mahphalke 1:48914f9593f1 1299
mahphalke 1:48914f9593f1 1300
mahphalke 1:48914f9593f1 1301 /*!
mahphalke 1:48914f9593f1 1302 * @brief Handle the menu to display device setup
mahphalke 3:6c7324997606 1303 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 1304 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 1305 */
mahphalke 1:48914f9593f1 1306 int32_t menu_display_setup(uint32_t menu_id)
mahphalke 1:48914f9593f1 1307 {
mahphalke 1:48914f9593f1 1308 float filter_data_rate; // Filter data rate in SPS
mahphalke 1:48914f9593f1 1309 uint8_t setup_cnt; // setup counter
mahphalke 1:48914f9593f1 1310 uint8_t chn_cnt; // channel counter
mahphalke 1:48914f9593f1 1311 ad717x_st_reg *device_chnmap_reg; // Pointer to channel map register
mahphalke 1:48914f9593f1 1312 ad717x_st_reg *device_setupcon_reg; // Pointer to setup control register
mahphalke 1:48914f9593f1 1313 ad717x_st_reg *device_filtercon_reg; // Pointer to filter control register
mahphalke 1:48914f9593f1 1314
mahphalke 1:48914f9593f1 1315 printf(EOL);
mahphalke 1:48914f9593f1 1316 printf("\t---------------------------------------" EOL);
mahphalke 1:48914f9593f1 1317 printf("\tChannel# | Status | Setup | INP0 | INP1" EOL);
mahphalke 1:48914f9593f1 1318 printf("\t---------------------------------------" EOL);
mahphalke 1:48914f9593f1 1319
mahphalke 1:48914f9593f1 1320 for (chn_cnt = 0; chn_cnt < NUMBER_OF_CHANNELS; chn_cnt++) {
mahphalke 1:48914f9593f1 1321 // Get the pointer to channel register
mahphalke 1:48914f9593f1 1322 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 1:48914f9593f1 1323 if (AD717X_ReadRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1324 printf(EOL "Error reading setup!!" EOL);
mahphalke 1:48914f9593f1 1325 break;
mahphalke 1:48914f9593f1 1326 }
mahphalke 1:48914f9593f1 1327
mahphalke 1:48914f9593f1 1328 // Extract the channel parameters from device register read value
mahphalke 1:48914f9593f1 1329 device_setup.channel_enabled = AD717X_CHMAP_REG_CH_EN_RD(
mahphalke 1:48914f9593f1 1330 device_chnmap_reg->value);
mahphalke 1:48914f9593f1 1331 device_setup.setup_assigned = AD717X_CHMAP_REG_SETUP_SEL_RD(
mahphalke 1:48914f9593f1 1332 device_chnmap_reg->value);
mahphalke 1:48914f9593f1 1333 device_setup.pos_analog_input = AD717X_CHMAP_REG_AINPOS_RD(
mahphalke 1:48914f9593f1 1334 device_chnmap_reg->value);
mahphalke 1:48914f9593f1 1335 device_setup.neg_analog_input = AD717X_CHMAP_REG_AINNEG_RD(
mahphalke 1:48914f9593f1 1336 device_chnmap_reg->value);
mahphalke 1:48914f9593f1 1337
mahphalke 1:48914f9593f1 1338 // Channel# | Status | Setup | INP0 | INP1
mahphalke 1:48914f9593f1 1339 printf("\t%4d %13s %4ld %8s %6s" EOL,
mahphalke 1:48914f9593f1 1340 chn_cnt,
mahphalke 1:48914f9593f1 1341 enable_disable_status[device_setup.channel_enabled],
mahphalke 1:48914f9593f1 1342 device_setup.setup_assigned,
mahphalke 1:48914f9593f1 1343 input_pin_map[device_setup.pos_analog_input],
mahphalke 1:48914f9593f1 1344 input_pin_map[device_setup.neg_analog_input]);
mahphalke 1:48914f9593f1 1345 }
mahphalke 1:48914f9593f1 1346
mahphalke 1:48914f9593f1 1347 printf(EOL);
mahphalke 1:48914f9593f1 1348 printf("\t-------------------------------------------------------------------------------------------------------------"
mahphalke 1:48914f9593f1 1349 EOL);
mahphalke 1:48914f9593f1 1350 printf("\tSetup# | Filter | Post Filter | Data Rate | INPBUF+ | INPBUF- | REFBUF+ | REFBUF- | Polarity | Ref Source"
mahphalke 1:48914f9593f1 1351 EOL);
mahphalke 1:48914f9593f1 1352 printf("\t-------------------------------------------------------------------------------------------------------------"
mahphalke 1:48914f9593f1 1353 EOL);
mahphalke 1:48914f9593f1 1354
mahphalke 1:48914f9593f1 1355 for (setup_cnt = 0; setup_cnt < NUMBER_OF_SETUPS; setup_cnt++) {
mahphalke 1:48914f9593f1 1356 // Get the pointer to filter control register
mahphalke 1:48914f9593f1 1357 device_filtercon_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1358 AD717X_FILTCON0_REG + setup_cnt);
mahphalke 1:48914f9593f1 1359 if (AD717X_ReadRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1360 AD717X_FILTCON0_REG + setup_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1361 printf("\r\nError reading setup!!\r\n");
mahphalke 1:48914f9593f1 1362 break;
mahphalke 1:48914f9593f1 1363 }
mahphalke 1:48914f9593f1 1364
mahphalke 1:48914f9593f1 1365 /* Extract the setup parameters from device register read value */
mahphalke 1:48914f9593f1 1366 device_setup.filter = AD717X_FILT_CONF_REG_ORDER_RD(
mahphalke 1:48914f9593f1 1367 device_filtercon_reg->value);
mahphalke 1:48914f9593f1 1368 device_setup.odr_bits = AD717X_FILT_CONF_REG_ODR_RD(
mahphalke 1:48914f9593f1 1369 device_filtercon_reg->value);
mahphalke 1:48914f9593f1 1370 device_setup.post_filter_enabled = AD717X_FILT_CONF_REG_ENHFILTEN_RD(
mahphalke 1:48914f9593f1 1371 device_filtercon_reg->value);
mahphalke 1:48914f9593f1 1372 device_setup.postfilter = AD717X_FILT_CONF_REG_ENHFILT_RD(
mahphalke 1:48914f9593f1 1373 device_filtercon_reg->value);
mahphalke 1:48914f9593f1 1374
mahphalke 1:48914f9593f1 1375 if (device_setup.filter == SINC3_FILTER) {
mahphalke 1:48914f9593f1 1376 // Post filter unavailable for SINC3 type filter
mahphalke 1:48914f9593f1 1377 device_setup.post_filter_enabled = SELECT_DISBLE;
mahphalke 1:48914f9593f1 1378 device_setup.postfilter = POST_FILTER_NA;
mahphalke 1:48914f9593f1 1379 }
mahphalke 1:48914f9593f1 1380
mahphalke 1:48914f9593f1 1381 // Get the pointer to setup control register
mahphalke 1:48914f9593f1 1382 device_setupcon_reg = AD717X_GetReg(pad717x_dev,
mahphalke 1:48914f9593f1 1383 AD717X_SETUPCON0_REG + setup_cnt);
mahphalke 1:48914f9593f1 1384 if (AD717X_ReadRegister(pad717x_dev,
mahphalke 1:48914f9593f1 1385 AD717X_SETUPCON0_REG + setup_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1386 printf("\r\nError reading setup!!\r\n");
mahphalke 1:48914f9593f1 1387 break;
mahphalke 1:48914f9593f1 1388 }
mahphalke 1:48914f9593f1 1389
mahphalke 1:48914f9593f1 1390 #if defined(DEV_AD4111) || defined(DEV_AD4112)
mahphalke 1:48914f9593f1 1391 device_setup.input_buffers =
mahphalke 1:48914f9593f1 1392 AD4111_SETUP_CONF_REG_AIN_BUF_RD(device_setupcon_reg->value);
mahphalke 1:48914f9593f1 1393 device_setup.reference_buffers =
mahphalke 1:48914f9593f1 1394 (AD4111_SETUP_CONF_REG_REFPOS_BUF_RD(device_setupcon_reg->value) << 1 |
mahphalke 1:48914f9593f1 1395 AD4111_SETUP_CONF_REG_REFNEG_BUF_RD(device_setupcon_reg->value));
mahphalke 1:48914f9593f1 1396 #elif defined(DEV_AD7172_2) || defined(DEV_AD7172_4) || defined(DEV_AD7175_8)
mahphalke 1:48914f9593f1 1397 device_setup.input_buffers =
mahphalke 1:48914f9593f1 1398 (AD717X_SETUP_CONF_REG_AINBUF_P_RD(device_setupcon_reg->value) << 1) |
mahphalke 1:48914f9593f1 1399 (AD717X_SETUP_CONF_REG_AINBUF_N_RD(device_setupcon_reg->value));
mahphalke 1:48914f9593f1 1400
mahphalke 1:48914f9593f1 1401 device_setup.reference_buffers =
mahphalke 1:48914f9593f1 1402 (AD717X_SETUP_CONF_REG_REFBUF_P_RD(device_setupcon_reg->value) << 1) |
mahphalke 1:48914f9593f1 1403 (AD717X_SETUP_CONF_REG_REFBUF_N_RD(device_setupcon_reg->value));
mahphalke 1:48914f9593f1 1404 #elif defined(DEV_AD7173_8)
mahphalke 1:48914f9593f1 1405 device_setup.input_buffers =
mahphalke 1:48914f9593f1 1406 AD717X_SETUP_CONF_REG_AIN_BUF_RD(device_setupcon_reg->value);
mahphalke 1:48914f9593f1 1407 device_setup.reference_buffers =
mahphalke 1:48914f9593f1 1408 AD717X_SETUP_CONF_REG_REF_BUF_RD(device_setupcon_reg->value);
mahphalke 1:48914f9593f1 1409 #endif
mahphalke 1:48914f9593f1 1410
mahphalke 1:48914f9593f1 1411 device_setup.polarity = AD717X_SETUP_CONF_REG_BI_UNIPOLAR_RD(
mahphalke 1:48914f9593f1 1412 device_setupcon_reg->value);
mahphalke 1:48914f9593f1 1413 device_setup.reference = AD717X_SETUP_CONF_REG_REF_SEL_RD(
mahphalke 1:48914f9593f1 1414 device_setupcon_reg->value);
mahphalke 1:48914f9593f1 1415
mahphalke 1:48914f9593f1 1416 // Get the actual data rate based on the filter selection
mahphalke 1:48914f9593f1 1417 filter_data_rate = get_data_rate(device_setup.filter,
mahphalke 1:48914f9593f1 1418 device_setup.odr_bits);
mahphalke 1:48914f9593f1 1419
mahphalke 1:48914f9593f1 1420 // Setup# | Filter | Post Filter | Data Rate | INPBUF+ | INPBUF- | REFBUF+ | REFBUF- | Polarity | Ref Source
mahphalke 1:48914f9593f1 1421 printf("\t%4d %11s %8s(%6s) %10.2f %9s %9s %9s %9s %10s %10s" EOL,
mahphalke 1:48914f9593f1 1422 setup_cnt,
mahphalke 1:48914f9593f1 1423 filter_name[device_setup.filter],
mahphalke 1:48914f9593f1 1424 postfilter_name[device_setup.postfilter],
mahphalke 1:48914f9593f1 1425 enable_disable_status[device_setup.post_filter_enabled],
mahphalke 1:48914f9593f1 1426 filter_data_rate,
mahphalke 1:48914f9593f1 1427 enable_disable_status[(device_setup.input_buffers >> 1) & 0x01],
mahphalke 1:48914f9593f1 1428 enable_disable_status[(device_setup.input_buffers & 0x01)],
mahphalke 1:48914f9593f1 1429 enable_disable_status[(device_setup.reference_buffers >> 1) & 0x01],
mahphalke 1:48914f9593f1 1430 enable_disable_status[device_setup.reference_buffers & 0x01],
mahphalke 1:48914f9593f1 1431 polarity_status[device_setup.polarity],
mahphalke 1:48914f9593f1 1432 reference_name[device_setup.reference]);
mahphalke 1:48914f9593f1 1433 }
mahphalke 1:48914f9593f1 1434
mahphalke 1:48914f9593f1 1435 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1436 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1437 }
mahphalke 1:48914f9593f1 1438
mahphalke 1:48914f9593f1 1439
mahphalke 1:48914f9593f1 1440 /*!
mahphalke 1:48914f9593f1 1441 * @brief Handle the menu to read die temperature of device
mahphalke 3:6c7324997606 1442 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 1443 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 1444 */
mahphalke 1:48914f9593f1 1445 int32_t menu_read_temperature(uint32_t menu_id)
mahphalke 1:48914f9593f1 1446 {
mahphalke 1:48914f9593f1 1447 uint8_t chn_cnt; // current channel
mahphalke 1:48914f9593f1 1448 uint16_t chn_mask = 0; // channels enable mask
mahphalke 1:48914f9593f1 1449 ad717x_st_reg *device_mode_reg; // Pointer to device mode register
mahphalke 1:48914f9593f1 1450 ad717x_st_reg *device_chnmap_reg; // Pointer to channel map register
mahphalke 1:48914f9593f1 1451 ad717x_st_reg *device_setup_control_reg; // Pointer to setup control register
mahphalke 1:48914f9593f1 1452 int32_t sample_data; // ADC sample result
mahphalke 1:48914f9593f1 1453 float temperature = 0.00; // Temperature in Celcius
mahphalke 1:48914f9593f1 1454 float conversion_result; // raw sample data to voltage conversion result
mahphalke 1:48914f9593f1 1455 int32_t prev_adc_reg_values[3]; // Holds the previous register values
mahphalke 1:48914f9593f1 1456 bool temperature_read_error; // Temperature read error status
mahphalke 1:48914f9593f1 1457
mahphalke 1:48914f9593f1 1458 temperature_read_error = false;
mahphalke 1:48914f9593f1 1459
mahphalke 1:48914f9593f1 1460 // Disable the other enabled channels to read temperature from only 0th channel
mahphalke 1:48914f9593f1 1461 for (chn_cnt = 1; chn_cnt < NUMBER_OF_CHANNELS; chn_cnt++) {
mahphalke 1:48914f9593f1 1462 // Get the pointer to channel register
mahphalke 1:48914f9593f1 1463 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 1:48914f9593f1 1464 if (AD717X_ReadRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1465 temperature_read_error = true;
mahphalke 1:48914f9593f1 1466 break;
mahphalke 1:48914f9593f1 1467 }
mahphalke 1:48914f9593f1 1468
mahphalke 1:48914f9593f1 1469 if (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN) {
mahphalke 1:48914f9593f1 1470 // Save enabled channel
mahphalke 1:48914f9593f1 1471 chn_mask |= (1 << chn_cnt);
mahphalke 1:48914f9593f1 1472
mahphalke 1:48914f9593f1 1473 // Disable the curent channel
mahphalke 1:48914f9593f1 1474 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 1:48914f9593f1 1475
mahphalke 1:48914f9593f1 1476 // Write to ADC channel register
mahphalke 1:48914f9593f1 1477 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1478 temperature_read_error = true;
mahphalke 1:48914f9593f1 1479 break;
mahphalke 1:48914f9593f1 1480 }
mahphalke 1:48914f9593f1 1481 }
mahphalke 1:48914f9593f1 1482 }
mahphalke 1:48914f9593f1 1483
mahphalke 1:48914f9593f1 1484 if (temperature_read_error == false) {
mahphalke 1:48914f9593f1 1485 // Get the pointer to device registers
mahphalke 1:48914f9593f1 1486 device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 1:48914f9593f1 1487 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG);
mahphalke 1:48914f9593f1 1488 device_setup_control_reg = AD717X_GetReg(pad717x_dev, AD717X_SETUPCON0_REG);
mahphalke 1:48914f9593f1 1489
mahphalke 1:48914f9593f1 1490 // Save the previous values of below registers in order to not disturb
mahphalke 1:48914f9593f1 1491 // the user configured setup.
mahphalke 1:48914f9593f1 1492 // *Note: This step is not required for someone who intended to just
mahphalke 1:48914f9593f1 1493 // read temperature. It is an application specific functionality.
mahphalke 1:48914f9593f1 1494 prev_adc_reg_values[0] = device_mode_reg->value;
mahphalke 1:48914f9593f1 1495 prev_adc_reg_values[1] = device_chnmap_reg->value;
mahphalke 1:48914f9593f1 1496 prev_adc_reg_values[2] = device_setup_control_reg->value;
mahphalke 1:48914f9593f1 1497
mahphalke 1:48914f9593f1 1498 // Configure the channel map 0 register
mahphalke 1:48914f9593f1 1499 // AINP = Temp + , AINM = Temp - , Setup = 0, CHN Enabled = True
mahphalke 1:48914f9593f1 1500 device_chnmap_reg->value =
mahphalke 1:48914f9593f1 1501 (AD717X_CHMAP_REG_AINPOS(TEMP_SENSOR_POS_INP_BITS) |
mahphalke 1:48914f9593f1 1502 AD717X_CHMAP_REG_AINNEG(TEMP_SENSOR_NEG_INP_BITS) |
mahphalke 1:48914f9593f1 1503 AD717X_CHMAP_REG_SETUP_SEL(0) |
mahphalke 1:48914f9593f1 1504 AD717X_CHMAP_REG_CH_EN);
mahphalke 1:48914f9593f1 1505
mahphalke 1:48914f9593f1 1506 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 1507 printf(EOL EOL "\tError Reading Temperature!!");
mahphalke 1:48914f9593f1 1508 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1509 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1510 }
mahphalke 1:48914f9593f1 1511
mahphalke 1:48914f9593f1 1512 // Configure the setup control 0 register
mahphalke 1:48914f9593f1 1513 // Polarity: Bipolar, Input Buffers: Enable, Ref: Internal
mahphalke 1:48914f9593f1 1514 device_setup_control_reg->value =
mahphalke 1:48914f9593f1 1515 (AD717X_SETUP_CONF_REG_BI_UNIPOLAR |
mahphalke 1:48914f9593f1 1516 AD717X_SETUP_CONF_REG_AINBUF_P |
mahphalke 1:48914f9593f1 1517 AD717X_SETUP_CONF_REG_AINBUF_N |
mahphalke 1:48914f9593f1 1518 AD717X_SETUP_CONF_REG_REF_SEL(INTERNAL));
mahphalke 1:48914f9593f1 1519
mahphalke 1:48914f9593f1 1520 if (AD717X_WriteRegister(pad717x_dev, AD717X_SETUPCON0_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 1521 printf(EOL EOL "\tError Reading Temperature!!");
mahphalke 1:48914f9593f1 1522 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1523 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1524 }
mahphalke 1:48914f9593f1 1525
mahphalke 1:48914f9593f1 1526 // Configure the device mode register
mahphalke 3:6c7324997606 1527 // Internal Ref: Enable, Mode: Single Conversion
mahphalke 1:48914f9593f1 1528 device_mode_reg->value |= AD717X_ADCMODE_REG_REF_EN;
mahphalke 1:48914f9593f1 1529 device_mode_reg->value =
mahphalke 1:48914f9593f1 1530 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 1531 AD717X_ADCMODE_REG_MODE(SINGLE_CONVERISION));
mahphalke 1:48914f9593f1 1532
mahphalke 1:48914f9593f1 1533 if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 1534 printf(EOL EOL "\tError Reading Temperature!!");
mahphalke 1:48914f9593f1 1535 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1536 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1537 }
mahphalke 1:48914f9593f1 1538
mahphalke 1:48914f9593f1 1539 for (uint8_t samples = 0; samples < 2; samples++) {
mahphalke 1:48914f9593f1 1540 // Wait for conversion to complete, then obtain sample
mahphalke 1:48914f9593f1 1541 AD717X_WaitForReady(pad717x_dev, 10000);
mahphalke 1:48914f9593f1 1542
mahphalke 1:48914f9593f1 1543 if (AD717X_ReadData(pad717x_dev, &sample_data) != SUCCESS) {
mahphalke 1:48914f9593f1 1544 temperature_read_error = true;
mahphalke 1:48914f9593f1 1545 break;
mahphalke 1:48914f9593f1 1546 }
mahphalke 1:48914f9593f1 1547 }
mahphalke 1:48914f9593f1 1548
mahphalke 1:48914f9593f1 1549 if (temperature_read_error == false) {
mahphalke 1:48914f9593f1 1550 conversion_result = (((float)sample_data / (1 << (ADC_RESOLUTION - 1))) - 1) *
mahphalke 1:48914f9593f1 1551 ADC_REF_VOLTAGE;
mahphalke 1:48914f9593f1 1552
mahphalke 1:48914f9593f1 1553 // Calculate the temparture in degree celcius (sensitivity: 477uV/K)
mahphalke 1:48914f9593f1 1554 // *Below equation is referred from the device datasheet
mahphalke 1:48914f9593f1 1555 temperature = ((float)conversion_result / 0.000477) - 273.15;
mahphalke 1:48914f9593f1 1556
mahphalke 1:48914f9593f1 1557 // All done, restore previous state of device registers
mahphalke 1:48914f9593f1 1558 // *Note: This step is not required for someone who intended to just
mahphalke 1:48914f9593f1 1559 // read temperature. It is an application specific functionality.
mahphalke 1:48914f9593f1 1560 device_mode_reg->value = prev_adc_reg_values[0];
mahphalke 1:48914f9593f1 1561 (void)AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 1:48914f9593f1 1562
mahphalke 1:48914f9593f1 1563 device_chnmap_reg->value = prev_adc_reg_values[1];
mahphalke 1:48914f9593f1 1564 (void)AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG);
mahphalke 1:48914f9593f1 1565
mahphalke 1:48914f9593f1 1566 device_setup_control_reg->value = prev_adc_reg_values[2];
mahphalke 1:48914f9593f1 1567 (void)AD717X_WriteRegister(pad717x_dev, AD717X_SETUPCON0_REG);
mahphalke 1:48914f9593f1 1568
mahphalke 1:48914f9593f1 1569 // Need to restore the channels those were disabled during temperaure read
mahphalke 1:48914f9593f1 1570 for (uint8_t i = 0 ; i < NUMBER_OF_CHANNELS ; i++) {
mahphalke 1:48914f9593f1 1571 if (chn_mask & (1 << i)) {
mahphalke 1:48914f9593f1 1572 // Get the pointer to channel register
mahphalke 1:48914f9593f1 1573 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + i);
mahphalke 1:48914f9593f1 1574 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 1:48914f9593f1 1575
mahphalke 1:48914f9593f1 1576 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + i) != SUCCESS) {
mahphalke 1:48914f9593f1 1577 // continue with other channels
mahphalke 1:48914f9593f1 1578 }
mahphalke 1:48914f9593f1 1579 }
mahphalke 1:48914f9593f1 1580 }
mahphalke 1:48914f9593f1 1581 }
mahphalke 1:48914f9593f1 1582 }
mahphalke 1:48914f9593f1 1583
mahphalke 1:48914f9593f1 1584 if (temperature_read_error == false) {
mahphalke 1:48914f9593f1 1585 printf(EOL EOL "\tTemperature: %.2f Celcius", temperature);
mahphalke 1:48914f9593f1 1586 } else {
mahphalke 1:48914f9593f1 1587 printf(EOL EOL "\tError Reading Temperature!!");
mahphalke 1:48914f9593f1 1588 }
mahphalke 1:48914f9593f1 1589
mahphalke 1:48914f9593f1 1590 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1591 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1592 }
mahphalke 1:48914f9593f1 1593
mahphalke 1:48914f9593f1 1594
mahphalke 1:48914f9593f1 1595 /*!
mahphalke 1:48914f9593f1 1596 * @brief Handle the menu to calibrate the device
mahphalke 3:6c7324997606 1597 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 1598 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 1599 */
mahphalke 1:48914f9593f1 1600 int32_t menu_calibrate_adc(uint32_t menu_id)
mahphalke 1:48914f9593f1 1601 {
mahphalke 1:48914f9593f1 1602 uint8_t chn_cnt; // Current channel
mahphalke 1:48914f9593f1 1603 uint16_t chn_mask = 0; // Channel enable mask
mahphalke 1:48914f9593f1 1604 ad717x_st_reg *device_chnmap_reg; // Pointer to channel map register
mahphalke 1:48914f9593f1 1605 ad717x_st_reg *device_mode_reg; // Pointer to device mode register
mahphalke 1:48914f9593f1 1606 bool calibration_error; // Calibration error flag
mahphalke 1:48914f9593f1 1607
mahphalke 1:48914f9593f1 1608 calibration_error = false;
mahphalke 1:48914f9593f1 1609
mahphalke 1:48914f9593f1 1610 for (chn_cnt = 0; chn_cnt < NUMBER_OF_CHANNELS; chn_cnt++) {
mahphalke 1:48914f9593f1 1611 // Get the pointer to channel register
mahphalke 1:48914f9593f1 1612 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 1:48914f9593f1 1613 if (AD717X_ReadRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1614 calibration_error = true;
mahphalke 1:48914f9593f1 1615 break;
mahphalke 1:48914f9593f1 1616 }
mahphalke 1:48914f9593f1 1617
mahphalke 1:48914f9593f1 1618 if (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN) {
mahphalke 1:48914f9593f1 1619 // Save enabled channel
mahphalke 1:48914f9593f1 1620 chn_mask |= (1 << chn_cnt);
mahphalke 1:48914f9593f1 1621
mahphalke 1:48914f9593f1 1622 // Disable the curent channel
mahphalke 1:48914f9593f1 1623 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 1:48914f9593f1 1624
mahphalke 1:48914f9593f1 1625 if(AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1626 calibration_error = true;
mahphalke 1:48914f9593f1 1627 break;
mahphalke 1:48914f9593f1 1628 }
mahphalke 1:48914f9593f1 1629 }
mahphalke 1:48914f9593f1 1630 }
mahphalke 1:48914f9593f1 1631
mahphalke 1:48914f9593f1 1632 if (calibration_error == false) {
mahphalke 1:48914f9593f1 1633 // Calibrate all the channels
mahphalke 1:48914f9593f1 1634 for (chn_cnt = 0 ; chn_cnt < NUMBER_OF_CHANNELS ; chn_cnt++) {
mahphalke 1:48914f9593f1 1635 printf(EOL "\tCalibrating Channel %d => " EOL, chn_cnt);
mahphalke 1:48914f9593f1 1636
mahphalke 1:48914f9593f1 1637 // Enable current channel
mahphalke 1:48914f9593f1 1638 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 1:48914f9593f1 1639 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 1:48914f9593f1 1640
mahphalke 1:48914f9593f1 1641 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1642 calibration_error = true;
mahphalke 1:48914f9593f1 1643 break;
mahphalke 1:48914f9593f1 1644 }
mahphalke 1:48914f9593f1 1645
mahphalke 1:48914f9593f1 1646 device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 1:48914f9593f1 1647
mahphalke 3:6c7324997606 1648 // Start full scale internal (gain) calibration
mahphalke 1:48914f9593f1 1649 printf("\tRunning full-scale internal calibration..." EOL);
mahphalke 1:48914f9593f1 1650 device_mode_reg->value =
mahphalke 1:48914f9593f1 1651 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 1652 AD717X_ADCMODE_REG_MODE(INTERNAL_FULL_SCALE_CAL_MODE));
mahphalke 1:48914f9593f1 1653
mahphalke 1:48914f9593f1 1654 if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 1655 calibration_error = true;
mahphalke 1:48914f9593f1 1656 break;
mahphalke 1:48914f9593f1 1657 }
mahphalke 1:48914f9593f1 1658
mahphalke 1:48914f9593f1 1659 // Wait for calibration to over
mahphalke 1:48914f9593f1 1660 if (AD717X_WaitForReady(pad717x_dev, 10000) != SUCCESS) {
mahphalke 1:48914f9593f1 1661 calibration_error = true;
mahphalke 1:48914f9593f1 1662 break;
mahphalke 1:48914f9593f1 1663 } else {
mahphalke 3:6c7324997606 1664 // Start zero scale internal (offset) calibration
mahphalke 1:48914f9593f1 1665 printf("\tRunning zero-scale internal calibration..." EOL);
mahphalke 1:48914f9593f1 1666 device_mode_reg->value =
mahphalke 1:48914f9593f1 1667 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 1668 AD717X_ADCMODE_REG_MODE(INTERNAL_OFFSET_CAL_MODE));
mahphalke 1:48914f9593f1 1669
mahphalke 1:48914f9593f1 1670 if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) {
mahphalke 1:48914f9593f1 1671 calibration_error = true;
mahphalke 1:48914f9593f1 1672 break;
mahphalke 1:48914f9593f1 1673 }
mahphalke 1:48914f9593f1 1674
mahphalke 1:48914f9593f1 1675 // Wait for calibration to over
mahphalke 1:48914f9593f1 1676 if (AD717X_WaitForReady(pad717x_dev, 10000) != SUCCESS) {
mahphalke 1:48914f9593f1 1677 printf("\tError in channel calibration..." EOL);
mahphalke 1:48914f9593f1 1678 } else {
mahphalke 1:48914f9593f1 1679 printf("\tCalibration Successful..." EOL);
mahphalke 1:48914f9593f1 1680 }
mahphalke 1:48914f9593f1 1681 }
mahphalke 1:48914f9593f1 1682
mahphalke 1:48914f9593f1 1683 // Disable current channel
mahphalke 1:48914f9593f1 1684 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 1:48914f9593f1 1685
mahphalke 1:48914f9593f1 1686 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1687 calibration_error = true;
mahphalke 1:48914f9593f1 1688 break;
mahphalke 1:48914f9593f1 1689 }
mahphalke 1:48914f9593f1 1690 }
mahphalke 1:48914f9593f1 1691 }
mahphalke 1:48914f9593f1 1692
mahphalke 1:48914f9593f1 1693 // Need to restore the channels that were disabled during calibration
mahphalke 1:48914f9593f1 1694 for (chn_cnt = 0 ; chn_cnt < NUMBER_OF_CHANNELS ; chn_cnt++) {
mahphalke 1:48914f9593f1 1695 if (chn_mask & (1 << chn_cnt)) {
mahphalke 1:48914f9593f1 1696 // Get the pointer to channel register
mahphalke 1:48914f9593f1 1697 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 1:48914f9593f1 1698 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 1:48914f9593f1 1699
mahphalke 1:48914f9593f1 1700 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 1:48914f9593f1 1701 // continue with other channels
mahphalke 1:48914f9593f1 1702 }
mahphalke 1:48914f9593f1 1703 }
mahphalke 1:48914f9593f1 1704 }
mahphalke 1:48914f9593f1 1705
mahphalke 1:48914f9593f1 1706 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1707 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1708 }
mahphalke 1:48914f9593f1 1709
mahphalke 1:48914f9593f1 1710
mahphalke 3:6c7324997606 1711 /*!
mahphalke 3:6c7324997606 1712 * @brief Handle the menu to select input type for open wire detection
mahphalke 3:6c7324997606 1713 * @param User selected analog input type
mahphalke 3:6c7324997606 1714 * @return MENU_DONE
mahphalke 3:6c7324997606 1715 */
mahphalke 3:6c7324997606 1716 int32_t menu_input_type_selection(uint32_t user_input_type)
mahphalke 3:6c7324997606 1717 {
mahphalke 3:6c7324997606 1718 // Save the analog input type, for channel and input pair selections
mahphalke 3:6c7324997606 1719 analog_input_type = user_input_type;
mahphalke 3:6c7324997606 1720
mahphalke 3:6c7324997606 1721 return MENU_DONE;
mahphalke 3:6c7324997606 1722 }
mahphalke 3:6c7324997606 1723
mahphalke 3:6c7324997606 1724
mahphalke 3:6c7324997606 1725 /*!
mahphalke 3:6c7324997606 1726 * @brief Handle the menu to select channel pair for open wire detection
mahphalke 3:6c7324997606 1727 * @param User selected channel pair
mahphalke 3:6c7324997606 1728 * @return MENU_DONE
mahphalke 3:6c7324997606 1729 */
mahphalke 3:6c7324997606 1730 int32_t menu_select_chn_pair(uint32_t user_channel_pair)
mahphalke 3:6c7324997606 1731 {
mahphalke 3:6c7324997606 1732 // Save the channel pair for configuring channel map register
mahphalke 3:6c7324997606 1733 channel_pair = user_channel_pair;
mahphalke 3:6c7324997606 1734
mahphalke 3:6c7324997606 1735 return MENU_DONE;
mahphalke 3:6c7324997606 1736 }
mahphalke 3:6c7324997606 1737
mahphalke 3:6c7324997606 1738
mahphalke 3:6c7324997606 1739 /*!
mahphalke 3:6c7324997606 1740 * @brief Handle the menu to select input pair for open wire detection
mahphalke 3:6c7324997606 1741 * @param User selected analog input
mahphalke 3:6c7324997606 1742 * @return MENU_DONE
mahphalke 3:6c7324997606 1743 */
mahphalke 3:6c7324997606 1744 int32_t menu_select_input_pair(uint32_t user_analog_input)
mahphalke 3:6c7324997606 1745 {
mahphalke 3:6c7324997606 1746 uint8_t current_channel; // current channel
mahphalke 3:6c7324997606 1747 ad717x_st_reg *device_chnmap_reg; // pointer to channel map register
mahphalke 3:6c7324997606 1748 ad717x_st_reg *device_mode_reg; // pointer to device mode register
mahphalke 3:6c7324997606 1749 bool input_pair_select_error = false; // Error in input pair selection
mahphalke 3:6c7324997606 1750
mahphalke 3:6c7324997606 1751 if (input_pair_select_error == false) {
mahphalke 3:6c7324997606 1752 // Set the configurations for channel pair
mahphalke 3:6c7324997606 1753 for (uint8_t chn_cnt = 0; chn_cnt < 2; chn_cnt++) {
mahphalke 3:6c7324997606 1754 if (chn_cnt == 0) {
mahphalke 3:6c7324997606 1755 // Get the first channel from the pair
mahphalke 3:6c7324997606 1756 current_channel = (channel_pair >> CHN_PAIR_OFFSET);
mahphalke 3:6c7324997606 1757 } else {
mahphalke 3:6c7324997606 1758 // Get the second channel from the pair
mahphalke 3:6c7324997606 1759 current_channel = (channel_pair & CHN_PAIR_MASK);
mahphalke 3:6c7324997606 1760 }
mahphalke 3:6c7324997606 1761
mahphalke 3:6c7324997606 1762 // Get the pointer to channel map register structure
mahphalke 3:6c7324997606 1763 device_chnmap_reg = AD717X_GetReg(pad717x_dev,
mahphalke 3:6c7324997606 1764 AD717X_CHMAP0_REG + current_channel);
mahphalke 3:6c7324997606 1765
mahphalke 3:6c7324997606 1766 // Load the setup 0 value
mahphalke 3:6c7324997606 1767 device_chnmap_reg->value =
mahphalke 3:6c7324997606 1768 ((device_chnmap_reg->value & ~AD717X_CHMAP_REG_SETUP_SEL_MSK) |
mahphalke 3:6c7324997606 1769 AD717X_CHMAP_REG_SETUP_SEL(0));
mahphalke 3:6c7324997606 1770
mahphalke 3:6c7324997606 1771 // Select the analog input
mahphalke 3:6c7324997606 1772 device_chnmap_reg->value =
mahphalke 3:6c7324997606 1773 ((device_chnmap_reg->value & ~AD4111_CHMAP_REG_INPUT_MSK) |
mahphalke 3:6c7324997606 1774 AD4111_CHMAP_REG_INPUT(user_analog_input));
mahphalke 3:6c7324997606 1775
mahphalke 3:6c7324997606 1776 // Enable the channel
mahphalke 3:6c7324997606 1777 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 3:6c7324997606 1778
mahphalke 3:6c7324997606 1779 device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG);
mahphalke 3:6c7324997606 1780 device_mode_reg->value =
mahphalke 3:6c7324997606 1781 ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) |
mahphalke 3:6c7324997606 1782 AD717X_ADCMODE_REG_MODE(SINGLE_CONVERISION));
mahphalke 3:6c7324997606 1783
mahphalke 3:6c7324997606 1784 if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) {
mahphalke 3:6c7324997606 1785 input_pair_select_error = true;
mahphalke 3:6c7324997606 1786 }
mahphalke 3:6c7324997606 1787
mahphalke 3:6c7324997606 1788 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 3:6c7324997606 1789 AD717X_CHMAP0_REG + current_channel) != SUCCESS) {
mahphalke 3:6c7324997606 1790 input_pair_select_error = true;
mahphalke 3:6c7324997606 1791 break;
mahphalke 3:6c7324997606 1792 }
mahphalke 3:6c7324997606 1793
mahphalke 3:6c7324997606 1794 if (AD717X_WaitForReady(pad717x_dev, 10000) != SUCCESS) {
mahphalke 3:6c7324997606 1795 input_pair_select_error = true;
mahphalke 3:6c7324997606 1796 break;
mahphalke 3:6c7324997606 1797 }
mahphalke 3:6c7324997606 1798
mahphalke 3:6c7324997606 1799 if (AD717X_ReadData(pad717x_dev,
mahphalke 3:6c7324997606 1800 &open_wire_detect_sample_data[chn_cnt]) != SUCCESS) {
mahphalke 3:6c7324997606 1801 input_pair_select_error = true;
mahphalke 3:6c7324997606 1802 break;
mahphalke 3:6c7324997606 1803 }
mahphalke 3:6c7324997606 1804
mahphalke 3:6c7324997606 1805 // Disable the current channel
mahphalke 3:6c7324997606 1806 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 3:6c7324997606 1807
mahphalke 3:6c7324997606 1808 if (AD717X_WriteRegister(pad717x_dev,
mahphalke 3:6c7324997606 1809 AD717X_CHMAP0_REG + current_channel) != SUCCESS) {
mahphalke 3:6c7324997606 1810 input_pair_select_error = true;
mahphalke 3:6c7324997606 1811 break;
mahphalke 3:6c7324997606 1812 }
mahphalke 3:6c7324997606 1813 }
mahphalke 3:6c7324997606 1814 }
mahphalke 3:6c7324997606 1815
mahphalke 3:6c7324997606 1816 if (input_pair_select_error == true) {
mahphalke 3:6c7324997606 1817 printf("\tError in analog input selection!!" EOL);
mahphalke 3:6c7324997606 1818 adi_press_any_key_to_continue();
mahphalke 3:6c7324997606 1819 }
mahphalke 3:6c7324997606 1820
mahphalke 3:6c7324997606 1821 return MENU_DONE;
mahphalke 3:6c7324997606 1822 }
mahphalke 3:6c7324997606 1823
mahphalke 3:6c7324997606 1824
mahphalke 3:6c7324997606 1825 /*!
mahphalke 3:6c7324997606 1826 * @brief Handle the menu to perform open wire detection
mahphalke 3:6c7324997606 1827 * @param menu_id- (Optional parameter)
mahphalke 3:6c7324997606 1828 * @return MENU_CONTINUE
mahphalke 3:6c7324997606 1829 */
mahphalke 3:6c7324997606 1830 int32_t menu_open_wire_detection(uint32_t menu_id)
mahphalke 3:6c7324997606 1831 {
mahphalke 3:6c7324997606 1832 ad717x_st_reg *device_chnmap_reg; // pointer to channel map register
mahphalke 3:6c7324997606 1833 ad717x_st_reg *device_gpiocon_reg; // pointer to gpio control register
mahphalke 3:6c7324997606 1834 ad717x_st_reg *device_setupcon_reg; // pointer to setup control register
mahphalke 3:6c7324997606 1835 uint8_t chn_cnt; // Current channel count
mahphalke 3:6c7324997606 1836 uint8_t chn_mask = 0; // Channel enable/disable mask
mahphalke 3:6c7324997606 1837 bool open_wire_detection_error = false; // Open wire detection error flag
mahphalke 3:6c7324997606 1838
mahphalke 3:6c7324997606 1839 // Disable all the enabled channels before starting open wire detection
mahphalke 3:6c7324997606 1840 for (chn_cnt = 0; chn_cnt < NUMBER_OF_CHANNELS; chn_cnt++) {
mahphalke 3:6c7324997606 1841 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 3:6c7324997606 1842
mahphalke 3:6c7324997606 1843 if (AD717X_ReadRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 3:6c7324997606 1844 open_wire_detection_error = true;
mahphalke 3:6c7324997606 1845 break;
mahphalke 3:6c7324997606 1846 }
mahphalke 3:6c7324997606 1847
mahphalke 3:6c7324997606 1848 if (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN) {
mahphalke 3:6c7324997606 1849 // Save enabled channel
mahphalke 3:6c7324997606 1850 chn_mask |= (1 << chn_cnt);
mahphalke 3:6c7324997606 1851
mahphalke 3:6c7324997606 1852 // Disable the curent channel
mahphalke 3:6c7324997606 1853 device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN);
mahphalke 3:6c7324997606 1854
mahphalke 3:6c7324997606 1855 // Write to ADC channel register
mahphalke 3:6c7324997606 1856 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 3:6c7324997606 1857 open_wire_detection_error = true;
mahphalke 3:6c7324997606 1858 break;
mahphalke 3:6c7324997606 1859 }
mahphalke 3:6c7324997606 1860 }
mahphalke 3:6c7324997606 1861 }
mahphalke 3:6c7324997606 1862
mahphalke 3:6c7324997606 1863 if (open_wire_detection_error == false) {
mahphalke 3:6c7324997606 1864 do {
mahphalke 3:6c7324997606 1865 // Enable the open wire detection on voltage channels
mahphalke 3:6c7324997606 1866 device_gpiocon_reg = AD717X_GetReg(pad717x_dev, AD717X_GPIOCON_REG);
mahphalke 3:6c7324997606 1867
mahphalke 3:6c7324997606 1868 if (AD717X_ReadRegister(pad717x_dev, AD717X_GPIOCON_REG) == SUCCESS) {
mahphalke 3:6c7324997606 1869 device_gpiocon_reg->value |= (AD4111_GPIOCON_REG_OP_EN0_1 |
mahphalke 3:6c7324997606 1870 AD4111_GPIOCON_REG_OW_EN);
mahphalke 3:6c7324997606 1871
mahphalke 3:6c7324997606 1872 if (AD717X_WriteRegister(pad717x_dev, AD717X_GPIOCON_REG) != SUCCESS) {
mahphalke 3:6c7324997606 1873 open_wire_detection_error = true;
mahphalke 3:6c7324997606 1874 break;
mahphalke 3:6c7324997606 1875 }
mahphalke 3:6c7324997606 1876 } else {
mahphalke 3:6c7324997606 1877 break;
mahphalke 3:6c7324997606 1878 }
mahphalke 3:6c7324997606 1879
mahphalke 3:6c7324997606 1880 // Configure the setup control 0 register
mahphalke 3:6c7324997606 1881 device_setupcon_reg = AD717X_GetReg(pad717x_dev, AD717X_SETUPCON0_REG);
mahphalke 3:6c7324997606 1882
mahphalke 3:6c7324997606 1883 if (AD717X_ReadRegister(pad717x_dev, AD717X_SETUPCON0_REG) == SUCCESS) {
mahphalke 3:6c7324997606 1884 device_setupcon_reg->value |= (AD717X_SETUP_CONF_REG_AINBUF_P |
mahphalke 3:6c7324997606 1885 AD717X_SETUP_CONF_REG_AINBUF_N |
mahphalke 3:6c7324997606 1886 AD717X_SETUP_CONF_REG_BI_UNIPOLAR);
mahphalke 3:6c7324997606 1887
mahphalke 3:6c7324997606 1888 device_setupcon_reg->value = ((device_setupcon_reg->value &
mahphalke 3:6c7324997606 1889 ~AD717X_SETUP_CONF_REG_REF_SEL_MSK) |
mahphalke 3:6c7324997606 1890 AD717X_SETUP_CONF_REG_REF_SEL(EXTERNAL));
mahphalke 3:6c7324997606 1891
mahphalke 3:6c7324997606 1892 if (AD717X_WriteRegister(pad717x_dev, AD717X_SETUPCON0_REG) != SUCCESS) {
mahphalke 3:6c7324997606 1893 open_wire_detection_error = true;
mahphalke 3:6c7324997606 1894 break;
mahphalke 3:6c7324997606 1895 }
mahphalke 3:6c7324997606 1896 } else {
mahphalke 3:6c7324997606 1897 break;
mahphalke 3:6c7324997606 1898 }
mahphalke 3:6c7324997606 1899 } while (0);
mahphalke 3:6c7324997606 1900
mahphalke 3:6c7324997606 1901
mahphalke 3:6c7324997606 1902 if (open_wire_detection_error == false) {
mahphalke 3:6c7324997606 1903 // Select the analog input type
mahphalke 3:6c7324997606 1904 adi_do_console_menu(&open_wire_detect_input_type_menu);
mahphalke 3:6c7324997606 1905
mahphalke 3:6c7324997606 1906 if (analog_input_type == SINGLE_ENDED_INPUT) {
mahphalke 3:6c7324997606 1907 // Select the single ended input channel pair
mahphalke 3:6c7324997606 1908 adi_do_console_menu(&open_wire_detect_se_channel_menu);
mahphalke 3:6c7324997606 1909
mahphalke 3:6c7324997606 1910 // Select the single ended analog input pair
mahphalke 3:6c7324997606 1911 adi_do_console_menu(&open_wire_detect_se_analog_input_menu);
mahphalke 3:6c7324997606 1912 } else {
mahphalke 3:6c7324997606 1913 // Select the differential ended input channel pair
mahphalke 3:6c7324997606 1914 adi_do_console_menu(&open_wire_detect_de_channel_menu);
mahphalke 3:6c7324997606 1915
mahphalke 3:6c7324997606 1916 // Select the differential ended analog input pair
mahphalke 3:6c7324997606 1917 adi_do_console_menu(&open_wire_detect_de_analog_input_menu);
mahphalke 3:6c7324997606 1918 }
mahphalke 3:6c7324997606 1919
mahphalke 3:6c7324997606 1920 printf(EOL "\tChannel %d = %d" EOL, (channel_pair >> CHN_PAIR_OFFSET),
mahphalke 3:6c7324997606 1921 open_wire_detect_sample_data[0]);
mahphalke 3:6c7324997606 1922
mahphalke 3:6c7324997606 1923 printf(EOL "\tChannel %d = %d" EOL, (channel_pair & CHN_PAIR_MASK),
mahphalke 3:6c7324997606 1924 open_wire_detect_sample_data[1]);
mahphalke 3:6c7324997606 1925
mahphalke 3:6c7324997606 1926 // Check for open wire detection by measuring offset of channel pair sampled data
mahphalke 3:6c7324997606 1927 if (abs(open_wire_detect_sample_data[0] - open_wire_detect_sample_data[1]) >
mahphalke 3:6c7324997606 1928 OPEN_WIRE_DETECT_THRESHOLD) {
mahphalke 3:6c7324997606 1929 printf(EOL "\tOpen Wire Detected on Selected Input Pair!!" EOL);
mahphalke 3:6c7324997606 1930 } else {
mahphalke 3:6c7324997606 1931 printf(EOL "\tNo Open Wire Detected on Selected Input Pair..." EOL);
mahphalke 3:6c7324997606 1932 }
mahphalke 3:6c7324997606 1933 } else {
mahphalke 3:6c7324997606 1934 printf(EOL "\tError in Open Wire Detection!!" EOL);
mahphalke 3:6c7324997606 1935 }
mahphalke 3:6c7324997606 1936 } else {
mahphalke 3:6c7324997606 1937 printf(EOL "\tError in Open Wire Detection!!" EOL);
mahphalke 3:6c7324997606 1938 }
mahphalke 3:6c7324997606 1939
mahphalke 3:6c7324997606 1940 // Need to restore the channels that were disabled during open wire detection
mahphalke 3:6c7324997606 1941 for (chn_cnt = 0 ; chn_cnt < NUMBER_OF_CHANNELS ; chn_cnt++) {
mahphalke 3:6c7324997606 1942 if (chn_mask & (1 << chn_cnt)) {
mahphalke 3:6c7324997606 1943 // Get the pointer to channel register
mahphalke 3:6c7324997606 1944 device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt);
mahphalke 3:6c7324997606 1945
mahphalke 3:6c7324997606 1946 device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN;
mahphalke 3:6c7324997606 1947
mahphalke 3:6c7324997606 1948 if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) {
mahphalke 3:6c7324997606 1949 continue;
mahphalke 3:6c7324997606 1950 }
mahphalke 3:6c7324997606 1951 }
mahphalke 3:6c7324997606 1952 }
mahphalke 3:6c7324997606 1953
mahphalke 3:6c7324997606 1954 adi_press_any_key_to_continue();
mahphalke 3:6c7324997606 1955 return MENU_CONTINUE;
mahphalke 3:6c7324997606 1956 }
mahphalke 3:6c7324997606 1957
mahphalke 3:6c7324997606 1958
mahphalke 1:48914f9593f1 1959 /* @brief Console menu to read/write the adc register
mahphalke 3:6c7324997606 1960 * @param Read/Write ID/Operation
mahphalke 3:6c7324997606 1961 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 1962 **/
mahphalke 1:48914f9593f1 1963 int32_t menu_rw_ad717x_register(uint32_t rw_id)
mahphalke 1:48914f9593f1 1964 {
mahphalke 1:48914f9593f1 1965 uint32_t reg_address;
mahphalke 1:48914f9593f1 1966 uint32_t reg_data;
mahphalke 1:48914f9593f1 1967 ad717x_st_reg *device_data_reg; // Pointer to data register
mahphalke 1:48914f9593f1 1968
mahphalke 1:48914f9593f1 1969 printf(EOL "\tEnter the register address (in hex): ");
mahphalke 1:48914f9593f1 1970 reg_address = adi_get_hex_integer(sizeof(reg_address));
mahphalke 1:48914f9593f1 1971
mahphalke 1:48914f9593f1 1972 // Get the pointer to data register
mahphalke 1:48914f9593f1 1973 device_data_reg = AD717X_GetReg(pad717x_dev, reg_address);
mahphalke 1:48914f9593f1 1974
mahphalke 1:48914f9593f1 1975 if ((uint32_t)DEVICE_REG_READ_ID == rw_id) {
mahphalke 1:48914f9593f1 1976 // Read from ADC channel register
mahphalke 1:48914f9593f1 1977 if (AD717X_ReadRegister(pad717x_dev, reg_address) != SUCCESS) {
mahphalke 1:48914f9593f1 1978 printf(EOL "Error reading setup!!" EOL);
mahphalke 1:48914f9593f1 1979 } else {
mahphalke 1:48914f9593f1 1980 reg_data = device_data_reg->value;
mahphalke 1:48914f9593f1 1981 printf(EOL "\tRead Value: 0x%lx", reg_data);
mahphalke 1:48914f9593f1 1982 }
mahphalke 1:48914f9593f1 1983 } else {
mahphalke 1:48914f9593f1 1984 printf(EOL "\tEnter the register data (in hex): ");
mahphalke 1:48914f9593f1 1985 reg_data = adi_get_hex_integer(sizeof(reg_data));
mahphalke 1:48914f9593f1 1986
mahphalke 1:48914f9593f1 1987 device_data_reg->value = reg_data;
mahphalke 1:48914f9593f1 1988
mahphalke 1:48914f9593f1 1989 if (AD717X_WriteRegister(pad717x_dev, reg_address) != SUCCESS) {
mahphalke 1:48914f9593f1 1990 printf(EOL "\tError in writing adc register!!" EOL);
mahphalke 1:48914f9593f1 1991 } else {
mahphalke 1:48914f9593f1 1992 printf(EOL "\tWrite Successful..." EOL);
mahphalke 1:48914f9593f1 1993 }
mahphalke 1:48914f9593f1 1994 }
mahphalke 1:48914f9593f1 1995
mahphalke 1:48914f9593f1 1996 adi_press_any_key_to_continue();
mahphalke 1:48914f9593f1 1997 return MENU_CONTINUE;
mahphalke 1:48914f9593f1 1998 }
mahphalke 1:48914f9593f1 1999
mahphalke 1:48914f9593f1 2000
mahphalke 1:48914f9593f1 2001 /*!
mahphalke 1:48914f9593f1 2002 * @brief Handle the menu to read/write device registers
mahphalke 3:6c7324997606 2003 * @param menu_id- (Optional parameter)
mahphalke 1:48914f9593f1 2004 * @return MENU_CONTINUE
mahphalke 1:48914f9593f1 2005 */
mahphalke 1:48914f9593f1 2006 int32_t menu_read_write_device_regs(uint32_t menu_id)
mahphalke 1:48914f9593f1 2007 {
mahphalke 1:48914f9593f1 2008 return (adi_do_console_menu(&reg_read_write_menu));
mahphalke 1:48914f9593f1 2009 }