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