AD7768-1 MBED IIO Application Example

Dependencies:   platform_drivers

Committer:
epena
Date:
Fri Sep 24 19:07:29 2021 +0800
Revision:
2:eb624ef78449
Parent:
1:c0429edee15b
Deleting the redefinition of data_capture_ops

Who changed what in which revision?

UserRevisionLine numberNew contents of line
epena 1:c0429edee15b 1 /***************************************************************************//**
epena 1:c0429edee15b 2 * @file ad77681_data_capture.c
epena 1:c0429edee15b 3 * @brief Data capture interface for AD7768-1 IIO application
epena 1:c0429edee15b 4 * @details This module handles the AD7768-1 data capturing
epena 1:c0429edee15b 5 ********************************************************************************
epena 1:c0429edee15b 6 * Copyright (c) 2021 Analog Devices, Inc.
epena 1:c0429edee15b 7 *
epena 1:c0429edee15b 8 * All rights reserved.
epena 1:c0429edee15b 9 *
epena 1:c0429edee15b 10 * This software is proprietary to Analog Devices, Inc. and its licensors.
epena 1:c0429edee15b 11 * By using this software you agree to the terms of the associated
epena 1:c0429edee15b 12 * Analog Devices Software License Agreement.
epena 1:c0429edee15b 13 *******************************************************************************/
epena 1:c0429edee15b 14
epena 1:c0429edee15b 15 /******************************************************************************/
epena 1:c0429edee15b 16 /***************************** Include Files **********************************/
epena 1:c0429edee15b 17 /******************************************************************************/
epena 1:c0429edee15b 18
epena 1:c0429edee15b 19 #include <string.h>
epena 1:c0429edee15b 20 #include <stdlib.h>
epena 1:c0429edee15b 21
epena 1:c0429edee15b 22 #include "app_config.h"
epena 1:c0429edee15b 23 #include "ad77681_iio.h"
epena 1:c0429edee15b 24 #include "ad77681_data_capture.h"
epena 1:c0429edee15b 25 #include "adc_data_capture.h"
epena 1:c0429edee15b 26 #include "error.h"
epena 1:c0429edee15b 27
epena 1:c0429edee15b 28 /******************************************************************************/
epena 1:c0429edee15b 29 /********************** Macros and Constants Definition ***********************/
epena 1:c0429edee15b 30 /******************************************************************************/
epena 1:c0429edee15b 31
epena 1:c0429edee15b 32 /* AD77681 ADC Resolution */
epena 1:c0429edee15b 33 #define AD77681_ADC_RESOLUTION 24
epena 1:c0429edee15b 34 /* AD77681 Buffer Length */
epena 1:c0429edee15b 35 #define AD77681_SAMPLE_DATA_BUFF_LEN 6
epena 1:c0429edee15b 36 /* AD77681 24 Bits Sign Extension Value */
epena 1:c0429edee15b 37 #define AD77681_24_BITS_SIGN_EXTENSION 0xFFFFFF
epena 1:c0429edee15b 38 /* AD77681 2 Bytes Shift Value */
epena 1:c0429edee15b 39 #define AD77681_2_BYTES_SHIFT 16
epena 1:c0429edee15b 40 /* AD77681 1 Byte Shift Value */
epena 1:c0429edee15b 41 #define AD77681_1_BYTE_SHIFT 8
epena 1:c0429edee15b 42
epena 1:c0429edee15b 43 /******************************************************************************/
epena 1:c0429edee15b 44 /********************** Variables and User Defined Data Types *****************/
epena 1:c0429edee15b 45 /******************************************************************************/
epena 1:c0429edee15b 46
epena 1:c0429edee15b 47 /******************************************************************************/
epena 1:c0429edee15b 48 /************************ Functions Declarations ******************************/
epena 1:c0429edee15b 49 /******************************************************************************/
epena 1:c0429edee15b 50
epena 1:c0429edee15b 51 /* Perform single sample read pre-operations */
epena 1:c0429edee15b 52 static int32_t ad77681_single_sample_read_start_ops(uint8_t input_chn);
epena 1:c0429edee15b 53 /* Perform continuous sample read pre-operations */
epena 1:c0429edee15b 54 static int32_t ad77681_continuous_sample_read_start_ops(uint32_t chn_mask);
epena 1:c0429edee15b 55 /* Perform conversion on input channel and read single conversion sample */
epena 1:c0429edee15b 56 static int32_t ad77681_perform_conv_and_read_sample(uint32_t *read_adc_data,
epena 1:c0429edee15b 57 uint8_t chn);
epena 1:c0429edee15b 58 /* Read ADC sampled/conversion data */
epena 1:c0429edee15b 59 static int32_t ad77681_read_converted_sample(uint32_t *adc_raw, uint8_t chn_indx);
epena 1:c0429edee15b 60
epena 1:c0429edee15b 61 /* Define the variable for data_capture_ops structure */
epena 1:c0429edee15b 62 struct data_capture_ops data_capture_ops = {
epena 1:c0429edee15b 63 /* Point AD77681 data capture functions to generic ADC data capture functions */
epena 1:c0429edee15b 64 .single_sample_read_start_ops = ad77681_single_sample_read_start_ops,
epena 1:c0429edee15b 65 .perform_conv_and_read_sample = ad77681_perform_conv_and_read_sample,
epena 1:c0429edee15b 66 .single_sample_read_stop_ops = NULL,
epena 1:c0429edee15b 67 .continuous_sample_read_start_ops = ad77681_continuous_sample_read_start_ops,
epena 1:c0429edee15b 68 .read_converted_sample = ad77681_read_converted_sample,
epena 1:c0429edee15b 69 .continuous_sample_read_stop_ops = NULL,
epena 1:c0429edee15b 70 .trigger_next_conversion = NULL,
epena 1:c0429edee15b 71 };
epena 1:c0429edee15b 72
epena 1:c0429edee15b 73 /******************************************************************************/
epena 1:c0429edee15b 74 /************************ Functions Definitions *******************************/
epena 1:c0429edee15b 75 /******************************************************************************/
epena 1:c0429edee15b 76
epena 1:c0429edee15b 77 /*!
epena 1:c0429edee15b 78 * @brief Perform the operations required before starting continuous sample read
epena 1:c0429edee15b 79 * @param input_chn[out] - ADC input channel
epena 1:c0429edee15b 80 * @return SUCCESS in case of success, FAILURE otherwise
epena 1:c0429edee15b 81 */
epena 1:c0429edee15b 82 int32_t ad77681_single_sample_read_start_ops(uint8_t input_chn)
epena 1:c0429edee15b 83 {
epena 1:c0429edee15b 84
epena 1:c0429edee15b 85 int32_t single_read_sts = FAILURE;
epena 1:c0429edee15b 86
epena 1:c0429edee15b 87 single_read_sts = ad77681_set_conv_mode
epena 1:c0429edee15b 88 (
epena 1:c0429edee15b 89 p_ad77681_dev_inst,
epena 1:c0429edee15b 90 AD77681_CONV_SINGLE,
epena 1:c0429edee15b 91 AD77681_AIN_SHORT,
epena 1:c0429edee15b 92 false
epena 1:c0429edee15b 93 );
epena 1:c0429edee15b 94
epena 1:c0429edee15b 95 return single_read_sts;
epena 1:c0429edee15b 96 }
epena 1:c0429edee15b 97
epena 1:c0429edee15b 98 /*!
epena 1:c0429edee15b 99 * @brief Perform the operations required before starting continuous sample read
epena 1:c0429edee15b 100 * @param chn_mask[in] - holds the list of all active channels set from IIO client for data capturing
epena 1:c0429edee15b 101 * @return SUCCESS in case of success, FAILURE otherwise
epena 1:c0429edee15b 102 */
epena 1:c0429edee15b 103 int32_t ad77681_continuous_sample_read_start_ops(uint32_t chn_mask)
epena 1:c0429edee15b 104 {
epena 1:c0429edee15b 105 int32_t cont_read_sts = FAILURE;
epena 1:c0429edee15b 106
epena 1:c0429edee15b 107 cont_read_sts = ad77681_set_conv_mode
epena 1:c0429edee15b 108 (
epena 1:c0429edee15b 109 p_ad77681_dev_inst,
epena 1:c0429edee15b 110 AD77681_CONV_CONTINUOUS,
epena 1:c0429edee15b 111 AD77681_AIN_SHORT,
epena 1:c0429edee15b 112 false
epena 1:c0429edee15b 113 );
epena 1:c0429edee15b 114
epena 1:c0429edee15b 115 return cont_read_sts;
epena 1:c0429edee15b 116 }
epena 1:c0429edee15b 117
epena 1:c0429edee15b 118 /*!
epena 1:c0429edee15b 119 * @brief Read ADC raw data for recently sampled channel
epena 1:c0429edee15b 120 * @param adc_raw[out] - Pointer to adc data read variable
epena 1:c0429edee15b 121 * @param chn_indx[in] - Channel for which data is to read
epena 1:c0429edee15b 122 * @return SUCCESS in case of success, FAILURE otherwise
epena 1:c0429edee15b 123 * @note This function is intended to call from the conversion end trigger
epena 1:c0429edee15b 124 * event. Therefore, this function should just read raw ADC data
epena 1:c0429edee15b 125 * without further monitoring conversion end event
epena 1:c0429edee15b 126 */
epena 1:c0429edee15b 127 int32_t ad77681_read_converted_sample(uint32_t *adc_raw, uint8_t chn_indx)
epena 1:c0429edee15b 128 {
epena 1:c0429edee15b 129 uint32_t ad77681_sample_data = 0;
epena 1:c0429edee15b 130 uint8_t ad77681_sample_data_buff[AD77681_SAMPLE_DATA_BUFF_LEN] = { 0, 0, 0, 0, 0, 0 };
epena 1:c0429edee15b 131
epena 1:c0429edee15b 132 if (!adc_raw) {
epena 1:c0429edee15b 133 return FAILURE;
epena 1:c0429edee15b 134 }
epena 1:c0429edee15b 135
epena 1:c0429edee15b 136 if (ad77681_spi_read_adc_data(p_ad77681_dev_inst, ad77681_sample_data_buff,
epena 1:c0429edee15b 137 AD77681_REGISTER_DATA_READ) != SUCCESS) {
epena 1:c0429edee15b 138 return FAILURE;
epena 1:c0429edee15b 139 }
epena 1:c0429edee15b 140
epena 1:c0429edee15b 141 ad77681_sample_data = (uint32_t)
epena 1:c0429edee15b 142 (
epena 1:c0429edee15b 143 (ad77681_sample_data_buff[1] << AD77681_2_BYTES_SHIFT) |
epena 1:c0429edee15b 144 (ad77681_sample_data_buff[2] << AD77681_1_BYTE_SHIFT ) |
epena 1:c0429edee15b 145 (ad77681_sample_data_buff[3])
epena 1:c0429edee15b 146 );
epena 1:c0429edee15b 147 *adc_raw = ad77681_sample_data & AD77681_24_BITS_SIGN_EXTENSION;
epena 1:c0429edee15b 148
epena 1:c0429edee15b 149 return SUCCESS;
epena 1:c0429edee15b 150 }
epena 1:c0429edee15b 151
epena 1:c0429edee15b 152 /*!
epena 1:c0429edee15b 153 * @brief Read ADC single sample data
epena 1:c0429edee15b 154 * @param read_adc_data[out] - Pointer to adc data read variable
epena 1:c0429edee15b 155 * @param chn[in] - Channel for which data is to read
epena 1:c0429edee15b 156 * @return SUCCESS in case of success, FAILURE otherwise
epena 1:c0429edee15b 157 * @details This function performs the sampling on previously active channel
epena 1:c0429edee15b 158 * and then read conversion result
epena 1:c0429edee15b 159 */
epena 1:c0429edee15b 160 int32_t ad77681_perform_conv_and_read_sample(uint32_t *read_adc_data,
epena 1:c0429edee15b 161 uint8_t chn)
epena 1:c0429edee15b 162 {
epena 1:c0429edee15b 163 uint32_t ad77681_sample_data = 0;
epena 1:c0429edee15b 164
epena 1:c0429edee15b 165 if (ad77681_read_converted_sample(&ad77681_sample_data, 0) != SUCCESS) {
epena 1:c0429edee15b 166 return FAILURE;
epena 1:c0429edee15b 167 }
epena 1:c0429edee15b 168 *read_adc_data = ad77681_sample_data;
epena 1:c0429edee15b 169
epena 1:c0429edee15b 170 return SUCCESS;
epena 1:c0429edee15b 171 }