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