This is an forked repository to push changes related to no-OS repository adoption
Dependencies: adi_console_menu platform_drivers
Diff: app/ad717x_console_app.c
- Revision:
- 3:6c7324997606
- Parent:
- 1:48914f9593f1
- Child:
- 4:4592cc69bfa6
--- a/app/ad717x_console_app.c Tue Mar 31 11:25:12 2020 +0000 +++ b/app/ad717x_console_app.c Tue Apr 14 18:14:29 2020 +0000 @@ -43,9 +43,7 @@ // Include the device register address map headers and device register map based // on the user selected device (default is AD4111) -#if (defined(DEV_AD4111) || defined(DEV_AD4112) || \ - defined(DEV_AD4114) || defined(DEV_AD4115) || \ - defined(DEV_AD4116)) +#if defined(DEV_AD4111) || defined(DEV_AD4112) #include <ad411x_regs.h> static ad717x_st_reg *ad717x_device_map = ad4111_regs; static uint8_t ad717x_reg_count = sizeof(ad4111_regs) / sizeof(ad4111_regs[0]); @@ -92,6 +90,9 @@ #define DISPLAY_DATA_TABULAR 0 #define DISPLAY_DATA_STREAM 1 +// Open wire detect ADC count threshold (eqv of 300mv for bipolar mode) +#define OPEN_WIRE_DETECT_THRESHOLD 100000 + /******************************************************************************/ /********************** Variables and User Defined Data Types *****************/ /******************************************************************************/ @@ -111,6 +112,11 @@ // How many times a given channel is sampled in total for one sample run static uint32_t channel_samples_count[NUMBER_OF_CHANNELS] = { 0 }; +// Variables used for open wire detection functionality +static uint32_t analog_input_type; // Analog input type +static uint32_t channel_pair; // Channel pair for open wire detection +static int32_t open_wire_detect_sample_data[2]; // Sampled data for channel pair + /******************************************************************************/ /************************ Functions Declarations ******************************/ /******************************************************************************/ @@ -154,7 +160,7 @@ /*! * @brief determines if the Escape key was pressed - * @return bool- key press status + * @return key press status */ static bool was_escape_key_pressed(void) { @@ -173,7 +179,7 @@ /* @brief Perform the channel selection - * @return uint8- Selected channel + * @return Selected channel **/ static uint8_t get_channel_selection(void) { @@ -196,7 +202,7 @@ /* @brief Perform the setup selection - * @return uint8- Selected setup + * @return Selected setup **/ static uint8_t get_setup_selection(void) { @@ -219,7 +225,7 @@ /* @brief Assign setup to adc channel - * @param uint8_t setup- setup to be assigned + * @param setup to be assigned **/ static void assign_setup_to_channel(uint8_t setup) { @@ -290,7 +296,7 @@ /*! * @brief Handle the menu to read device ID - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_read_id(uint32_t menu_id) @@ -315,7 +321,7 @@ /*! * @brief Handle the menu to read device status register - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_read_status(uint32_t menu_id) @@ -586,10 +592,9 @@ } // All done, ADC put into standby mode - // 2 = sleep/standby mode device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(2)); + AD717X_ADCMODE_REG_MODE(STANDBY_MODE)); if ((error_code = AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG)) != SUCCESS) { @@ -675,16 +680,15 @@ // Clear the ADC CTRL MODE bits, selecting continuous mode device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(0)); + AD717X_ADCMODE_REG_MODE(CONTINUOUS_CONVERSION)); // read the channels, and store sample values for(uint8_t loopCount = 0 ; ((was_escape_key_pressed() != true) && (loopCount < channel_count)) ; loopCount++) { - // 1 = single conversion mode device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(1)); + AD717X_ADCMODE_REG_MODE(SINGLE_CONVERISION)); if ((error_code = AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG)) != SUCCESS) { @@ -739,10 +743,9 @@ } // All done, ADC put into standby mode - // 2 = sleep/standby mode device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(2)); + AD717X_ADCMODE_REG_MODE(STANDBY_MODE)); // Need to restore the channels that were disabled during acquisition for(uint8_t chn = 0 ; chn < NUMBER_OF_CHANNELS ; chn++) { @@ -772,7 +775,7 @@ /*! * @brief Handle the menu to sample the channels - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_sample_channels(uint32_t menu_id) @@ -782,7 +785,7 @@ /* @brief Enable or disable adc channels - * @param uint32_t action- channel ENABLE/DISABLE action + * @param channel ENABLE/DISABLE action * @return MENU_CONTINUE **/ int32_t menu_channels_enable_disable(uint32_t action) @@ -833,7 +836,7 @@ /*! * @brief Display the menu to enable/disable channel selection - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_chn_enable_disable_display(uint32_t menu_id) @@ -844,7 +847,7 @@ /*! * @brief Handle the menu to connect input to channel - * @param uint32_t user_analog_input- analog input to be connected + * @param analog input to be connected * @return MENU_CONTINUE */ int32_t menu_analog_input_connect(uint32_t user_analog_input) @@ -909,7 +912,7 @@ /*! * @brief Display the menu selections to connect analog input pins to a channel - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_input_chn_connect_display(uint32_t menu_id) @@ -931,7 +934,7 @@ /*! * @brief Handle the menu to select the filter type - * @param uint32_t filter_type- user selected filter type + * @param user selected filter type * @return MENU_DONE */ int32_t menu_filter_select(uint32_t user_input_filter_type) @@ -966,7 +969,7 @@ /*! * @brief Handle the menu to enable/disable the post filter - * @param uint32_t user_action- user selected action + * @param user selected action * @return MENU_DONE */ int32_t menu_postfiler_enable_disable(uint32_t user_action) @@ -997,7 +1000,7 @@ /*! * @brief Handle the menu to select the post filter - * @param uint32_t user_action- user selected post filter type + * @param user selected post filter type * @return MENU_DONE */ int32_t menu_postfiler_select(uint32_t user_input_post_filter_type) @@ -1026,7 +1029,7 @@ /*! * @brief Handle the menu to select the ODR value - * @param uint32_t user_input_odr_val- user selected ODR + * @param user selected ODR * @return MENU_DONE */ int32_t menu_odr_select(uint32_t user_input_odr_val) @@ -1055,7 +1058,7 @@ /*! * @brief Handle the menu to select the polarity - * @param uint32_t user_input_polarity- user selected polarity + * @param user selected polarity * @return MENU_DONE */ int32_t menu_polarity_select(uint32_t user_input_polarity) @@ -1086,7 +1089,7 @@ /*! * @brief Handle the menu to select the reference source - * @param uint32_t user_input_reference- user selected reference source + * @param user selected reference source * @return MENU_DONE */ int32_t menu_reference_source_select(uint32_t user_input_reference) @@ -1131,7 +1134,7 @@ /*! * @brief Handle the menu to enable/disable the reference buffers - * @param uint32_t user_action- user selected action + * @param user selected action * @return MENU_DONE */ int32_t menu_ref_buffer_enable_disable(uint32_t user_action) @@ -1168,7 +1171,7 @@ /*! * @brief Handle the menu to enable/disable the input buffers - * @param uint32_t user_action- user selected action + * @param user selected action * @return MENU_DONE */ int32_t menu_input_buffer_enable_disable(uint32_t user_action) @@ -1205,7 +1208,7 @@ /*! * @brief Handle the menu to configure and assign the device setup - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_config_and_assign_setup(uint32_t menu_id) @@ -1277,9 +1280,9 @@ /* @brief Get the data rate based on data rate FS value and vice a versa - * @param uint32_t filter- Filter Type - * @param uint32_t odr_reg_val- Filter data rate register value/bits - * @return float data_rate- Actual Data Rate + * @param Filter Type + * @param Filter data rate register value/bits + * @return Actual Data Rate **/ static float get_data_rate(uint32_t filter, uint32_t odr_reg_val) { @@ -1297,7 +1300,7 @@ /*! * @brief Handle the menu to display device setup - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_display_setup(uint32_t menu_id) @@ -1436,7 +1439,7 @@ /*! * @brief Handle the menu to read die temperature of device - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_read_temperature(uint32_t menu_id) @@ -1521,11 +1524,11 @@ } // Configure the device mode register - // Internal Ref: Enable, Mode: Single Conversion (1) + // Internal Ref: Enable, Mode: Single Conversion device_mode_reg->value |= AD717X_ADCMODE_REG_REF_EN; device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(1)); + AD717X_ADCMODE_REG_MODE(SINGLE_CONVERISION)); if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) { printf(EOL EOL "\tError Reading Temperature!!"); @@ -1591,7 +1594,7 @@ /*! * @brief Handle the menu to calibrate the device - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_calibrate_adc(uint32_t menu_id) @@ -1642,11 +1645,11 @@ device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG); - // Start full scale internal (gain) calibration (mode: 5) + // Start full scale internal (gain) calibration printf("\tRunning full-scale internal calibration..." EOL); device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(5)); + AD717X_ADCMODE_REG_MODE(INTERNAL_FULL_SCALE_CAL_MODE)); if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) { calibration_error = true; @@ -1658,11 +1661,11 @@ calibration_error = true; break; } else { - // Start zero scale internal (offset) calibration (mode: 4) + // Start zero scale internal (offset) calibration printf("\tRunning zero-scale internal calibration..." EOL); device_mode_reg->value = ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | - AD717X_ADCMODE_REG_MODE(4)); + AD717X_ADCMODE_REG_MODE(INTERNAL_OFFSET_CAL_MODE)); if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) { calibration_error = true; @@ -1705,9 +1708,257 @@ } +/*! + * @brief Handle the menu to select input type for open wire detection + * @param User selected analog input type + * @return MENU_DONE + */ +int32_t menu_input_type_selection(uint32_t user_input_type) +{ + // Save the analog input type, for channel and input pair selections + analog_input_type = user_input_type; + + return MENU_DONE; +} + + +/*! + * @brief Handle the menu to select channel pair for open wire detection + * @param User selected channel pair + * @return MENU_DONE + */ +int32_t menu_select_chn_pair(uint32_t user_channel_pair) +{ + // Save the channel pair for configuring channel map register + channel_pair = user_channel_pair; + + return MENU_DONE; +} + + +/*! + * @brief Handle the menu to select input pair for open wire detection + * @param User selected analog input + * @return MENU_DONE + */ +int32_t menu_select_input_pair(uint32_t user_analog_input) +{ + uint8_t current_channel; // current channel + ad717x_st_reg *device_chnmap_reg; // pointer to channel map register + ad717x_st_reg *device_mode_reg; // pointer to device mode register + bool input_pair_select_error = false; // Error in input pair selection + + if (input_pair_select_error == false) { + // Set the configurations for channel pair + for (uint8_t chn_cnt = 0; chn_cnt < 2; chn_cnt++) { + if (chn_cnt == 0) { + // Get the first channel from the pair + current_channel = (channel_pair >> CHN_PAIR_OFFSET); + } else { + // Get the second channel from the pair + current_channel = (channel_pair & CHN_PAIR_MASK); + } + + // Get the pointer to channel map register structure + device_chnmap_reg = AD717X_GetReg(pad717x_dev, + AD717X_CHMAP0_REG + current_channel); + + // Load the setup 0 value + device_chnmap_reg->value = + ((device_chnmap_reg->value & ~AD717X_CHMAP_REG_SETUP_SEL_MSK) | + AD717X_CHMAP_REG_SETUP_SEL(0)); + + // Select the analog input + device_chnmap_reg->value = + ((device_chnmap_reg->value & ~AD4111_CHMAP_REG_INPUT_MSK) | + AD4111_CHMAP_REG_INPUT(user_analog_input)); + + // Enable the channel + device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN; + + device_mode_reg = AD717X_GetReg(pad717x_dev, AD717X_ADCMODE_REG); + device_mode_reg->value = + ((device_mode_reg->value & ~AD717X_ADCMODE_REG_MODE_MSK) | + AD717X_ADCMODE_REG_MODE(SINGLE_CONVERISION)); + + if (AD717X_WriteRegister(pad717x_dev, AD717X_ADCMODE_REG) != SUCCESS) { + input_pair_select_error = true; + } + + if (AD717X_WriteRegister(pad717x_dev, + AD717X_CHMAP0_REG + current_channel) != SUCCESS) { + input_pair_select_error = true; + break; + } + + if (AD717X_WaitForReady(pad717x_dev, 10000) != SUCCESS) { + input_pair_select_error = true; + break; + } + + if (AD717X_ReadData(pad717x_dev, + &open_wire_detect_sample_data[chn_cnt]) != SUCCESS) { + input_pair_select_error = true; + break; + } + + // Disable the current channel + device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN); + + if (AD717X_WriteRegister(pad717x_dev, + AD717X_CHMAP0_REG + current_channel) != SUCCESS) { + input_pair_select_error = true; + break; + } + } + } + + if (input_pair_select_error == true) { + printf("\tError in analog input selection!!" EOL); + adi_press_any_key_to_continue(); + } + + return MENU_DONE; +} + + +/*! + * @brief Handle the menu to perform open wire detection + * @param menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +int32_t menu_open_wire_detection(uint32_t menu_id) +{ + ad717x_st_reg *device_chnmap_reg; // pointer to channel map register + ad717x_st_reg *device_gpiocon_reg; // pointer to gpio control register + ad717x_st_reg *device_setupcon_reg; // pointer to setup control register + uint8_t chn_cnt; // Current channel count + uint8_t chn_mask = 0; // Channel enable/disable mask + bool open_wire_detection_error = false; // Open wire detection error flag + + // Disable all the enabled channels before starting open wire detection + for (chn_cnt = 0; chn_cnt < NUMBER_OF_CHANNELS; chn_cnt++) { + device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt); + + if (AD717X_ReadRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) { + open_wire_detection_error = true; + break; + } + + if (device_chnmap_reg->value & AD717X_CHMAP_REG_CH_EN) { + // Save enabled channel + chn_mask |= (1 << chn_cnt); + + // Disable the curent channel + device_chnmap_reg->value &= (~AD717X_CHMAP_REG_CH_EN); + + // Write to ADC channel register + if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) { + open_wire_detection_error = true; + break; + } + } + } + + if (open_wire_detection_error == false) { + do { + // Enable the open wire detection on voltage channels + device_gpiocon_reg = AD717X_GetReg(pad717x_dev, AD717X_GPIOCON_REG); + + if (AD717X_ReadRegister(pad717x_dev, AD717X_GPIOCON_REG) == SUCCESS) { + device_gpiocon_reg->value |= (AD4111_GPIOCON_REG_OP_EN0_1 | + AD4111_GPIOCON_REG_OW_EN); + + if (AD717X_WriteRegister(pad717x_dev, AD717X_GPIOCON_REG) != SUCCESS) { + open_wire_detection_error = true; + break; + } + } else { + break; + } + + // Configure the setup control 0 register + device_setupcon_reg = AD717X_GetReg(pad717x_dev, AD717X_SETUPCON0_REG); + + if (AD717X_ReadRegister(pad717x_dev, AD717X_SETUPCON0_REG) == SUCCESS) { + device_setupcon_reg->value |= (AD717X_SETUP_CONF_REG_AINBUF_P | + AD717X_SETUP_CONF_REG_AINBUF_N | + AD717X_SETUP_CONF_REG_BI_UNIPOLAR); + + device_setupcon_reg->value = ((device_setupcon_reg->value & + ~AD717X_SETUP_CONF_REG_REF_SEL_MSK) | + AD717X_SETUP_CONF_REG_REF_SEL(EXTERNAL)); + + if (AD717X_WriteRegister(pad717x_dev, AD717X_SETUPCON0_REG) != SUCCESS) { + open_wire_detection_error = true; + break; + } + } else { + break; + } + } while (0); + + + if (open_wire_detection_error == false) { + // Select the analog input type + adi_do_console_menu(&open_wire_detect_input_type_menu); + + if (analog_input_type == SINGLE_ENDED_INPUT) { + // Select the single ended input channel pair + adi_do_console_menu(&open_wire_detect_se_channel_menu); + + // Select the single ended analog input pair + adi_do_console_menu(&open_wire_detect_se_analog_input_menu); + } else { + // Select the differential ended input channel pair + adi_do_console_menu(&open_wire_detect_de_channel_menu); + + // Select the differential ended analog input pair + adi_do_console_menu(&open_wire_detect_de_analog_input_menu); + } + + printf(EOL "\tChannel %d = %d" EOL, (channel_pair >> CHN_PAIR_OFFSET), + open_wire_detect_sample_data[0]); + + printf(EOL "\tChannel %d = %d" EOL, (channel_pair & CHN_PAIR_MASK), + open_wire_detect_sample_data[1]); + + // Check for open wire detection by measuring offset of channel pair sampled data + if (abs(open_wire_detect_sample_data[0] - open_wire_detect_sample_data[1]) > + OPEN_WIRE_DETECT_THRESHOLD) { + printf(EOL "\tOpen Wire Detected on Selected Input Pair!!" EOL); + } else { + printf(EOL "\tNo Open Wire Detected on Selected Input Pair..." EOL); + } + } else { + printf(EOL "\tError in Open Wire Detection!!" EOL); + } + } else { + printf(EOL "\tError in Open Wire Detection!!" EOL); + } + + // Need to restore the channels that were disabled during open wire detection + for (chn_cnt = 0 ; chn_cnt < NUMBER_OF_CHANNELS ; chn_cnt++) { + if (chn_mask & (1 << chn_cnt)) { + // Get the pointer to channel register + device_chnmap_reg = AD717X_GetReg(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt); + + device_chnmap_reg->value |= AD717X_CHMAP_REG_CH_EN; + + if (AD717X_WriteRegister(pad717x_dev, AD717X_CHMAP0_REG + chn_cnt) != SUCCESS) { + continue; + } + } + } + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + /* @brief Console menu to read/write the adc register - * @param rw_id- Read/Write ID/Operation - * @return int32_t- menu status constant + * @param Read/Write ID/Operation + * @return MENU_CONTINUE **/ int32_t menu_rw_ad717x_register(uint32_t rw_id) { @@ -1749,7 +2000,7 @@ /*! * @brief Handle the menu to read/write device registers - * @param uint32_t menu_id- (Optional parameter) + * @param menu_id- (Optional parameter) * @return MENU_CONTINUE */ int32_t menu_read_write_device_regs(uint32_t menu_id)