如何在Linux下调用IIO接口配置AD9361

目前配置AD9361的方法使用较多的是纯PL端配置和no_os裸机系统下PS端配置,但是基于Linux配置AD9361的文章不多,我自己在网上学习了一些IIO驱动知识(正点原子关于IIO驱动的视频)以及根据ADI官方提供的资料,做到可以读写AD9361中官方已经封装好的驱动,并且可以实现读写特定寄存器值。命令行的配置方式具体可以参考:AD9361 高性能、高度集成的RF捷变收发™器Linux器件驱动程序 [Analog Devices Wiki]

在开发板的linux系统中,进入sys/bus/iio/devices路径下,可以看到多个iio设备,进入iio:device0中,ls查看目录下的文件,非常多,都是ADI官方已经做好的驱动文件,我们只需要调用就可以实现配置AD9361的一些功能。

例如,使用cat命令,可以查看文件驱动文件的值,使用echo命令可以向驱动文件写入值。

但是上述的驱动文件是配置一些AD9361带宽,速率,模式等等一些功能,如果向对AD9361寄存器直接读写,需要到/sys/kernel/debug/iio中,通过debugfs进行低级寄存器访问direct_reg_access

但是要向从direct_reg_access中读到特定寄存器的值,需要先向其中写入寄存器地址,即先写再读。

如果想向特定寄存器写入一个值,参考下面截图中的命令:

上述均为用命令行去读写IIO驱动,那么在Linux系统下的Vitis中如何编程呢?

首先,要把刚刚我们想要读写的IIO驱动路径加入static char *file_path[]中

/* file_path corresponding to the ad9361 iio framework*/
static char *file_path[] = {
	"/sys/bus/iio/devices/iio:device0/calib_mode",
	"/sys/bus/iio/devices/iio:device0/calib_mode_available",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_coarse",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_coarse_available",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_fine",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_fine_available",
	"/sys/bus/iio/devices/iio:device0/ensm_mode",
	"/sys/bus/iio/devices/iio:device0/ensm_mode_available",
	"/sys/bus/iio/devices/iio:device0/filter_fir_config",
	"/sys/bus/iio/devices/iio:device0/gain_table_config",
	"/sys/bus/iio/devices/iio:device0/in_out_voltage_filter_fir_en",
	"/sys/bus/iio/devices/iio:device0/in_temp0_input",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_gain_control_mode",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_rssi",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_gain_control_mode",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_rssi",
	"/sys/bus/iio/devices/iio:device0/in_voltage2_offset",
	"/sys/bus/iio/devices/iio:device0/in_voltage2_raw",
	"/sys/bus/iio/devices/iio:device0/in_voltage2_scale",
	"/sys/bus/iio/devices/iio:device0/in_voltage_bb_dc_offset_tracking_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_filter_fir_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_gain_control_mode_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage_quadrature_tracking_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_bandwidth",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_bandwidth_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_dc_offset_tracking_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_port_select_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage_sampling_frequency",
	"/sys/bus/iio/devices/iio:device0/in_voltage_sampling_frequency_available",
	"/sys/bus/iio/devices/iio:device0/name",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_external",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_load",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_recall",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_save",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_store",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_frequency",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_frequency_available",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_powerdown",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_external",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_load",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_recall",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_save",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_store",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_frequency",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_frequency_available",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_powerdown",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_rssi",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_rssi",
	"/sys/bus/iio/devices/iio:device0/out_voltage2_raw",
	"/sys/bus/iio/devices/iio:device0/out_voltage2_scale",
	"/sys/bus/iio/devices/iio:device0/out_voltage3_raw",
	"/sys/bus/iio/devices/iio:device0/out_voltage3_scale",
	"/sys/bus/iio/devices/iio:device0/out_voltage_filter_fir_en",
	"/sys/bus/iio/devices/iio:device0/out_voltage_rf_bandwidth",
	"/sys/bus/iio/devices/iio:device0/out_voltage_rf_bandwidth_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage_rf_port_select_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage_sampling_frequency",
	"/sys/bus/iio/devices/iio:device0/out_voltage_sampling_frequency_available",
	"/sys/bus/iio/devices/iio:device0/rssi_gain_step_error",
	"/sys/bus/iio/devices/iio:device0/rx_path_rates",
	"/sys/bus/iio/devices/iio:device0/trx_rate_governor",
	"/sys/bus/iio/devices/iio:device0/trx_rate_governor_available",
	"/sys/bus/iio/devices/iio:device0/tx_path_rates",
	"/sys/bus/iio/devices/iio:device0/xo_correction",
	"/sys/bus/iio/devices/iio:device0/xo_correction_available",
	"/sys/kernel/debug/iio/iio:device0/direct_reg_access"

};

然后,创建相应的path_index,path_index内容需要和ile_path[]中的路径按顺序一一对应。

/* file_path index,the file_path must correspond to the order of files in the file_path */
enum path_index {
	CALIB_MODE,
	CALIB_MODE_AVAILABLE,
	DCXO_TUNE_COARSE,
	DCXO_TUNE_COARSE_AVAILABLE,
	DCXO_TUNE_FINE,
	DCXO_TUNE_FINE_AVAILABLE,
	ENSM_MODE,
	ENSM_MODE_AVAILABLE,
	FILTER_FIR_CONFIG,
	GAIN_TABLE_CONFIG,
	IN_OUT_VOLTAGE_FILTER_FIR_EN,
	IN_TEMP0_INPUT,
	IN_VOLTAGE0_GAIN_CONTROL_MODE,
	IN_VOLTAGE0_HARDWAREGAIN,
	IN_VOLTAGE0_HARDWAREGAIN_AVAILABLE,
	IN_VOLTAGE0_RF_PORT_SELECT,
	IN_VOLTAGE0_RSSI,
	IN_VOLTAGE1_GAIN_CONTROL_MODE,
	IN_VOLTAGE1_HARDWAREGAIN,
	IN_VOLTAGE1_HARDWAREGAIN_AVAILABLE,
	IN_VOLTAGE1_RF_PORT_SELECT,
	IN_VOLTAGE1_RSSI,
	IN_VOLTAGE2_OFFSET,
	IN_VOLTAGE2_RAW,
	IN_VOLTAGE2_SCALE,
	IN_VOLTAGE_BB_DC_OFFSET_TRACKING_EN,
	IN_VOLTAGE_FILTER_FIR_EN,
	IN_VOLTAGE_GAIN_CONTROL_MODE_AVAILABLE,
	IN_VOLTAGE_QUADRATURE_TRACKING_EN,
	IN_VOLTAGE_RF_BANDWIDTH,
	IN_VOLTAGE_RF_BANDWIDTH_AVAILABLE,
	IN_VOLTAGE_RF_DC_OFFSET_TRACKING_EN,
	IN_VOLTAGE_RF_PORT_SELECT_AVAILABLE,
	IN_VOLTAGE_SAMPLING_FREQUENCY,
	IN_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE,
	NAME,
	OUT_ALTVOLTAGE0_RX_LO_EXTERNAL,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_LOAD,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_RECALL,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_SAVE,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_STORE,
	OUT_ALTVOLTAGE0_RX_LO_FREQUENCY,
	OUT_ALTVOLTAGE0_RX_LO_FREQUENCY_AVAILABLE,
	OUT_ALTVOLTAGE0_RX_LO_POWERDOWN,
	OUT_ALTVOLTAGE1_TX_LO_EXTERNAL,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_LOAD,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_RECALL,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_SAVE,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_STORE,
	OUT_ALTVOLTAGE1_TX_LO_FREQUENCY,
	OUT_ALTVOLTAGE1_TX_LO_FREQUENCY_AVAILABLE,
	OUT_ALTVOLTAGE1_TX_LO_POWERDOWN,
	OUT_VOLTAGE0_HARDWAREGAIN,
	OUT_VOLTAGE0_HARDWAREGAIN_AVAILABLE,
	OUT_VOLTAGE0_RF_PORT_SELECT,
	OUT_VOLTAGE0_RSSI,
	OUT_VOLTAGE1_HARDWAREGAIN,
	OUT_VOLTAGE1_HARDWAREGAIN_AVAILABLE,
	OUT_VOLTAGE1_RF_PORT_SELECT,
	OUT_VOLTAGE1_RSSI,
	OUT_VOLTAGE2_RAW,
	OUT_VOLTAGE2_SCALE,
	OUT_VOLTAGE3_RAW,
	OUT_VOLTAGE3_SCALE,
	OUT_VOLTAGE_FILTER_FIR_EN,
	OUT_VOLTAGE_RF_BANDWIDTH,
	OUT_VOLTAGE_RF_BANDWIDTH_AVAILABLE,
	OUT_VOLTAGE_RF_PORT_SELECT_AVAILABLE,
	OUT_VOLTAGE_SAMPLING_FREQUENCY,
	OUT_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE,
	RSSI_GAIN_STEP_ERROR,
	RX_PATH_RATES,
	TRX_RATE_GOVERNOR,
	TRX_RATE_GOVERNOR_AVAILABLE,
	TX_PATH_RATES,
	XO_CORRECTION,
	XO_CORRECTION_AVAILABLE,
	DIRECT_REG_ACCESS,


};

然后向ad9361设备中添加成员

/* ad9361 data device struct member*/
struct ad9361_rf_phy {
	char *calib_mode;//Pointer to a string (dynamic memory allocation)
	char *calib_mode_available;
	int *dcxo_tune_coarse;//cat: dcxo_tune_coarse: No such device
	char *dcxo_tune_coarse_available;
	int *dcxo_tune_fine;//cat: dcxo_tune_fine: No such device
	char *dcxo_tune_fine_available;
	char *ensm_mode;
	char *ensm_mode_available;
	char *filter_fir_config;
	char *gain_table_config;
	int *in_out_voltage_filter_fir_en;
	int *in_temp0_input;
	char *in_voltage0_gain_control_mode;
	char *in_voltage0_hardwaregain;
	char *in_voltage0_hardwaregain_available;
	char *in_voltage0_rf_port_select;
	char *in_voltage0_rssi;
	char *in_voltage1_gain_control_mode;
	char *in_voltage1_hardwaregain;
	char *in_voltage1_hardwaregain_available;
	char *in_voltage1_rf_port_select;
	char *in_voltage1_rssi;
	int *in_voltage2_offset;
	int *in_voltage2_raw;
	double in_voltage2_scale;
	int *in_voltage_bb_dc_offset_tracking_en;
	int *in_voltage_filter_fir_en;
	char *in_voltage_gain_control_mode_available;
	int *in_voltage_quadrature_tracking_en;
	int *in_voltage_rf_bandwidth;
	char *in_voltage_rf_bandwidth_available;
	int *in_voltage_rf_dc_offset_tracking_en;
	char *in_voltage_rf_port_select_available;
	int *in_voltage_sampling_frequency;
	char *in_voltage_sampling_frequency_available;
	char *name;
	int *out_altvoltage0_RX_LO_external;
	int *out_altvoltage0_RX_LO_fastlock_load;
	int *out_altvoltage0_RX_LO_fastlock_recall;//cat: out_altvoltage0_RX_LO_fastlock_recall: Invalid argument
	char *out_altvoltage0_RX_LO_fastlock_save;
	int *out_altvoltage0_RX_LO_fastlock_store;
	char *out_altvoltage0_RX_LO_frequency;
	char *out_altvoltage0_RX_LO_frequency_available;
	int *out_altvoltage0_RX_LO_powerdown;
	int *out_altvoltage1_TX_LO_external;
	int *out_altvoltage1_TX_LO_fastlock_load;
	int *out_altvoltage1_TX_LO_fastlock_recall;
	char *out_altvoltage1_TX_LO_fastlock_save;
	int *out_altvoltage1_TX_LO_fastlock_store;
	char *out_altvoltage1_TX_LO_frequency;
	char *out_altvoltage1_TX_LO_frequency_available;
	int *out_altvoltage1_TX_LO_powerdown;
	char *out_voltage0_hardwaregain;
	char *out_voltage0_hardwaregain_available;
	char *out_voltage0_rf_port_select;
	char *out_voltage0_rssi;
	char *out_voltage1_hardwaregain;
	char *out_voltage1_hardwaregain_available;
	char *out_voltage1_rf_port_select;
	char *out_voltage1_rssi;
	int *out_voltage2_raw;
	double out_voltage2_scale;
	int *out_voltage3_raw;
	double out_voltage3_scale;
	int *out_voltage_filter_fir_en;
	int *out_voltage_rf_bandwidth;
	char *out_voltage_rf_bandwidth_available;
	char *out_voltage_rf_port_select_available;
	int *out_voltage_sampling_frequency;
	char *out_voltage_sampling_frequency_available;
	char *rssi_gain_step_error;
	char *rx_path_rates;
	char *trx_rate_governor;
	char *trx_rate_governor_available;
	char *tx_path_rates;
	int *xo_correction;
	char *xo_correction_available;
	char *direct_reg_access;
};
struct ad9361_rf_phy ad9361;

然后对数据流文件读写,因为从文件中读出来的都是字符串,所以有时候会涉及到字符串和整型数据的转化。

读函数有:

/*
 * @description			: read the specified file
 * @param - filename 	: the path of the file which will be read
 * @param - str 		: the read of file string
 * @return 				: 0:success; other: failure
 */
static int file_data_read(char *filename, char *str)//only read a char data
{
	int ret = 0;
	FILE *data_stream;

    data_stream = fopen(filename, "r"); /* read only open */
    if(data_stream == NULL) {
		printf("can't open file %s\r\n", filename);
		return -1;
	}

	ret = fscanf(data_stream, "%s", str);
    if(!ret) {
        printf("file read error!\r\n");
    } else if(ret == EOF) {
        /* Readjust the file pointer to the file header when you reach the end of the file */
        fseek(data_stream, 0, SEEK_SET);
    }
	fclose(data_stream);	/* close file */
	return 0;
}

//read a string data, the read size is max_size
static int file_data_read_string(char *filename, char *str, size_t max_size)
{
    FILE *data_stream;

    data_stream = fopen(filename, "r");
    if(data_stream == NULL) {
        printf("can't open file %s\r\n", filename);
        return -1;
    }

    // Reset str to an empty string
    str[0] = '\0';

    char line[256];  // Adjust the size as needed

    // Read lines until the end of the file
    while (fgets(line, sizeof(line), data_stream) != NULL) {
        // Concatenate the line to the existing content
        strncat(str, line, max_size - strlen(str) - 1);

        // Check if the entire line was read
        if (str[strlen(str) - 1] == '\n') {
            break;
        }
    }

    fclose(data_stream);  // Close the file

    return 0;
}


#define SENSOR_INT_DATA_SET_TWO(ret, index, reg_addr, data, member)\
	dev->member = data;\
	ret = file_data_write_two(file_path[index], reg_addr, data);\
/*
 * @description			: get ad9361 data
 * @param - dev 		: device struct
 * @return 				: 0:success; other: failure
 **/
static int sensor_read(struct ad9361_rf_phy *dev)
{
	int ret = 0;
	char str[MAX_BUFFER_SIZE];
	/* get iio sysfs data */
	file_data_read_string(file_path[CALIB_MODE], str,MAX_BUFFER_SIZE);
	dev->calib_mode = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[CALIB_MODE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->calib_mode_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, DCXO_TUNE_COARSE, str, dcxo_tune_coarse);

	file_data_read_string(file_path[DCXO_TUNE_COARSE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->dcxo_tune_coarse_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, DCXO_TUNE_FINE, str, dcxo_tune_fine);
	file_data_read_string(file_path[DCXO_TUNE_FINE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->dcxo_tune_fine_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[ENSM_MODE], str,MAX_BUFFER_SIZE);
	dev->ensm_mode = strdup(str);// Allocate memory for a copy of the string
	file_data_read_string(file_path[ENSM_MODE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->ensm_mode_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[FILTER_FIR_CONFIG], str,MAX_BUFFER_SIZE);
	dev->filter_fir_config = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[GAIN_TABLE_CONFIG], str,MAX_BUFFER_SIZE);
	dev->gain_table_config = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_OUT_VOLTAGE_FILTER_FIR_EN, str, in_out_voltage_filter_fir_en);
	SENSOR_INT_DATA_GET(ret, IN_TEMP0_INPUT, str, in_temp0_input);

	file_data_read_string(file_path[IN_VOLTAGE0_GAIN_CONTROL_MODE],str,MAX_BUFFER_SIZE);
	dev->in_voltage0_gain_control_mode = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->in_voltage0_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage0_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_RF_PORT_SELECT],str,MAX_BUFFER_SIZE);
	dev->in_voltage0_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_RSSI], str,MAX_BUFFER_SIZE);
	dev->in_voltage0_rssi = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_GAIN_CONTROL_MODE],str,MAX_BUFFER_SIZE);
	dev->in_voltage1_gain_control_mode = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_RF_PORT_SELECT], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_RSSI], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_rssi = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE2_OFFSET, str, in_voltage2_offset);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE2_RAW, str, in_voltage2_raw);

	SENSOR_FLOAT_DATA_GET(ret, IN_VOLTAGE2_SCALE, str, in_voltage2_scale);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_BB_DC_OFFSET_TRACKING_EN, str, in_voltage_bb_dc_offset_tracking_en);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_FILTER_FIR_EN, str, in_voltage_filter_fir_en);

	file_data_read_string(file_path[IN_VOLTAGE_GAIN_CONTROL_MODE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_gain_control_mode_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_QUADRATURE_TRACKING_EN, str, in_voltage_quadrature_tracking_en);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_RF_BANDWIDTH, str, in_voltage_rf_bandwidth);

	file_data_read_string(file_path[IN_VOLTAGE_RF_BANDWIDTH_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_rf_bandwidth_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_RF_DC_OFFSET_TRACKING_EN, str, in_voltage_rf_dc_offset_tracking_en);

	file_data_read_string(file_path[IN_VOLTAGE_RF_PORT_SELECT_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_rf_port_select_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_SAMPLING_FREQUENCY, str, in_voltage_sampling_frequency);

	file_data_read_string(file_path[IN_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_sampling_frequency_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[NAME], str,MAX_BUFFER_SIZE);
	dev->name = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_EXTERNAL, str, out_altvoltage0_RX_LO_external);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_LOAD, str, out_altvoltage0_RX_LO_fastlock_load);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_RECALL, str, out_altvoltage0_RX_LO_fastlock_recall);//cat: out_altvoltage0_RX_LO_fastlock_recall: Invalid argument

	file_data_read_string(file_path[OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_SAVE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage0_RX_LO_fastlock_save = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_STORE, str, out_altvoltage0_RX_LO_fastlock_store);

	file_data_read_string(file_path[OUT_ALTVOLTAGE0_RX_LO_FREQUENCY], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage0_RX_LO_frequency = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_ALTVOLTAGE0_RX_LO_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage0_RX_LO_frequency_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_POWERDOWN, str, out_altvoltage0_RX_LO_powerdown);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_EXTERNAL, str, out_altvoltage1_TX_LO_external);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_LOAD, str, out_altvoltage1_TX_LO_fastlock_load);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_RECALL, str, out_altvoltage1_TX_LO_fastlock_recall);//cat: out_altvoltage1_TX_LO_fastlock_recall: Invalid argument

	file_data_read_string(file_path[OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_SAVE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage1_TX_LO_fastlock_save = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_STORE, str, out_altvoltage1_TX_LO_fastlock_store);

	file_data_read_string(file_path[OUT_ALTVOLTAGE1_TX_LO_FREQUENCY], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage1_TX_LO_frequency = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_ALTVOLTAGE1_TX_LO_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage1_TX_LO_frequency_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_POWERDOWN, str, out_altvoltage1_TX_LO_powerdown);

	file_data_read_string(file_path[OUT_VOLTAGE0_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE0_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE0_RF_PORT_SELECT], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE0_RSSI], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_rssi = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_RF_PORT_SELECT], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_RSSI], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_rssi = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE2_RAW, str, out_voltage2_raw);

	SENSOR_FLOAT_DATA_GET(ret, OUT_VOLTAGE2_SCALE, str, out_voltage2_scale);

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE3_RAW, str, out_voltage3_raw);

	SENSOR_FLOAT_DATA_GET(ret, OUT_VOLTAGE3_SCALE, str, out_voltage3_scale);

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE_FILTER_FIR_EN, str, out_voltage_filter_fir_en);

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE_RF_BANDWIDTH, str, out_voltage_rf_bandwidth);

	file_data_read_string(file_path[OUT_VOLTAGE_RF_BANDWIDTH_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage_rf_bandwidth_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE_RF_PORT_SELECT_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage_rf_port_select_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE_SAMPLING_FREQUENCY, str, out_voltage_sampling_frequency);

	file_data_read_string(file_path[OUT_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage_sampling_frequency_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[RSSI_GAIN_STEP_ERROR], str,MAX_BUFFER_SIZE);
	dev->rssi_gain_step_error = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[RX_PATH_RATES], str,MAX_BUFFER_SIZE);
	dev->rx_path_rates = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[TRX_RATE_GOVERNOR], str,MAX_BUFFER_SIZE);
	dev->trx_rate_governor = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[TRX_RATE_GOVERNOR_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->trx_rate_governor_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[TX_PATH_RATES], str,MAX_BUFFER_SIZE);
	dev->tx_path_rates = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, XO_CORRECTION, str, xo_correction);

	file_data_read_string(file_path[XO_CORRECTION_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->xo_correction_available = strdup(str);// Allocate memory for a copy of the string

//	SENSOR_INT_DATA_GET(ret, DIRECT_REG_ACCESS, str, direct_reg_access);
	file_data_read_string(file_path[DIRECT_REG_ACCESS], str,MAX_BUFFER_SIZE);
	dev->direct_reg_access = strdup(str);// Allocate memory for a copy of the string
	return ret;
}



/*
 * @description			: read ad9361 reg
 * @param - dev 		: device struct
 * @return 				: 0:success; other: failure
 **/
static int sensor_read_reg(struct ad9361_rf_phy *dev, int reg_addr)
{
	int ret = 0;
	char str[MAX_BUFFER_SIZE];

	SENSOR_INT_DATA_SET(ret, DIRECT_REG_ACCESS, reg_addr, direct_reg_access);
	file_data_read_string(file_path[DIRECT_REG_ACCESS], str,MAX_BUFFER_SIZE);
	dev->direct_reg_access = strdup(str);// Allocate memory for a copy of the string
	printf("0x%x,%s\r\n",reg_addr,ad9361.direct_reg_access);
	return ret;
}

写函数有:

/*
 * @description			: write the specified file
 * @param - filename 	: the path of the file which will be read
 * @param - data 		: the int value you will write to the file
 * @return 				: 0:success; other: failure
 */
static int file_data_write(char *filename, int *data)
{
	int ret = 0;
	FILE *data_stream;
	char str[20];
	snprintf(str,sizeof(str), "%d", data);//turn integer to string
    data_stream = fopen(filename, "w"); /* write only open */
    if(data_stream == NULL) {
		printf("can't open file %s\r\n", filename);
		return -1;
	}
    fprintf(data_stream,"%s",str);
	fclose(data_stream);	/* close file */
	return 0;
}

/*
 * @description       : write the specified file
 * @param - filename  : the path of the file which will be read
 * @param - reg_addr  : the address you will write to the file
 * @param - data      : the data int value you will write to the file
 * @return            : 0: success; other: failure
 */
static int file_data_write_two(char *filename, int reg_addr, int data)
{
    int ret = 0;
    FILE *data_stream;
    char str[40]; // Increased buffer size to accommodate two integers
    snprintf(str, sizeof(str), "%d %d", reg_addr, data); // Concatenate two integers as a string

    data_stream = fopen(filename, "w"); /* write only open */
    if (data_stream == NULL) {
        printf("can't open file %s\r\n", filename);
        return -1;
    }
    fprintf(data_stream, "%s", str);
    fclose(data_stream); /* close file */
    return 0;
}


#define SENSOR_INT_DATA_SET(ret, index, data, member)\
	dev->member = data;\
	ret = file_data_write(file_path[index], data);\


#define SENSOR_INT_DATA_SET_TWO(ret, index, reg_addr, data, member)\
	dev->member = data;\
	ret = file_data_write_two(file_path[index], reg_addr, data);\

/*
 * @description			: write ad9361 reg
 * @param - dev 		: device struct
 * @return 				: 0:success; other: failure
 **/
static int sensor_write_reg(struct ad9361_rf_phy *dev, int reg_addr, int data)
{
	int ret = 0;
	char str[MAX_BUFFER_SIZE];

	SENSOR_INT_DATA_SET_TWO(ret, DIRECT_REG_ACCESS, reg_addr, data,direct_reg_access);
	file_data_read_string(file_path[DIRECT_REG_ACCESS], str,MAX_BUFFER_SIZE);
	dev->direct_reg_access = strdup(str);// Allocate memory for a copy of the string
	printf("0x%x,0x%x,%s\r\n",reg_addr,data,ad9361.direct_reg_access);
	return ret;
}

字符串转int和float

/* User add*/
double atof( const char* s ) {
    return strtod( s, ( char** )NULL );
}


int atoi( const char* s ) {
    int v = 0;
    int sign = 0;

    while ( ( *s == ' ' ) || ( (  int )( *s - 9 ) < 5u ) ) {
        ++s;
    }

    switch ( *s ) {
        case '-' : sign = -1;break;
        case '+' : ++s;break;
    }

    while ( ( unsigned int )( *s - '0' ) < 10u ) {
        v = v * 10 + *s - '0';
        ++s;
    }

    return sign ? -v : v;
}

完整main.c代码:

/*
assign WData1[31:0] =  slv_reg0[31:0];
assign WData2[31:0] =  slv_reg1[31:0];
assign WComm[7:0] =  slv_reg2[7:0];
assign CSW_n =  slv_reg2[8];
assign CSR_n =  slv_reg2[9];
 */


#include "xparameters.h"
#include <asm-generic/param.h>
#include "ps_pl.h"
#include <stdint.h>ad9361_bbdc_offs_enable_raw
#include <stdio.h>     /*\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u5b9a\u4e49*/
#include <stdlib.h>    /*\u6807\u51c6\u51fd\u6570\u5e93\u5b9a\u4e49*/
#include <unistd.h>    /*Unix\u6807\u51c6\u51fd\u6570\u5b9a\u4e49*/
#include <sys/types.h> /**/
#include <sys/stat.h>  /**/
#include <fcntl.h>     /*\u6587\u4ef6\u63a7\u5236\u5b9a\u4e49*/
#include <termios.h>   /*PPSIX\u7ec8\u7aef\u63a7\u5236\u5b9a\u4e49*/
#include <errno.h>     /*\u9519\u8bef\u53f7\u5b9a\u4e49*/
#include <sys/time.h>
#include <string.h>
#include <getopt.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <linux/input.h>
#include <linux/spi/spidev.h>
#include <asm-generic/ioctl.h>
#include <sys/mman.h>
#include <linux/module.h>

//#include <sys/ioctl.h>
#include <strings.h>
#include <signal.h>
#include <pthread.h>
#include <poll.h>
#include "board.h"


/* user add */
#include <sys/select.h>
#include <signal.h>
#include <linux/iio/iio.h>
#include <linux/iio/iio-debug.h>
#include <linux/iio/types.h>
#include <inttypes.h>




/* User add*/
double atof( const char* s ) {
    return strtod( s, ( char** )NULL );
}


int atoi( const char* s ) {
    int v = 0;
    int sign = 0;

    while ( ( *s == ' ' ) || ( (  int )( *s - 9 ) < 5u ) ) {
        ++s;
    }

    switch ( *s ) {
        case '-' : sign = -1;break;
        case '+' : ++s;break;
    }

    while ( ( unsigned int )( *s - '0' ) < 10u ) {
        v = v * 10 + *s - '0';
        ++s;
    }

    return sign ? -v : v;
}




/* string to number,converting an float string to an float value */
#define SENSOR_FLOAT_DATA_GET(ret, index, str, member)\
	ret = file_data_read(file_path[index], str);\
	dev->member = atof(str);\

/* string to number,converting an integer string to an integer value */
#define SENSOR_INT_DATA_GET(ret, index, str, member)\
	ret = file_data_read(file_path[index], str);\
	dev->member = atoi(str);\

/* file_path corresponding to the ad9361 iio framework*/
static char *file_path[] = {
	"/sys/bus/iio/devices/iio:device0/calib_mode",
	"/sys/bus/iio/devices/iio:device0/calib_mode_available",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_coarse",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_coarse_available",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_fine",
	"/sys/bus/iio/devices/iio:device0/dcxo_tune_fine_available",
	"/sys/bus/iio/devices/iio:device0/ensm_mode",
	"/sys/bus/iio/devices/iio:device0/ensm_mode_available",
	"/sys/bus/iio/devices/iio:device0/filter_fir_config",
	"/sys/bus/iio/devices/iio:device0/gain_table_config",
	"/sys/bus/iio/devices/iio:device0/in_out_voltage_filter_fir_en",
	"/sys/bus/iio/devices/iio:device0/in_temp0_input",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_gain_control_mode",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/in_voltage0_rssi",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_gain_control_mode",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/in_voltage1_rssi",
	"/sys/bus/iio/devices/iio:device0/in_voltage2_offset",
	"/sys/bus/iio/devices/iio:device0/in_voltage2_raw",
	"/sys/bus/iio/devices/iio:device0/in_voltage2_scale",
	"/sys/bus/iio/devices/iio:device0/in_voltage_bb_dc_offset_tracking_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_filter_fir_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_gain_control_mode_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage_quadrature_tracking_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_bandwidth",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_bandwidth_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_dc_offset_tracking_en",
	"/sys/bus/iio/devices/iio:device0/in_voltage_rf_port_select_available",
	"/sys/bus/iio/devices/iio:device0/in_voltage_sampling_frequency",
	"/sys/bus/iio/devices/iio:device0/in_voltage_sampling_frequency_available",
	"/sys/bus/iio/devices/iio:device0/name",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_external",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_load",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_recall",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_save",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_fastlock_store",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_frequency",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_frequency_available",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage0_RX_LO_powerdown",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_external",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_load",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_recall",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_save",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_fastlock_store",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_frequency",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_frequency_available",
	"/sys/bus/iio/devices/iio:device0/out_altvoltage1_TX_LO_powerdown",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/out_voltage0_rssi",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_hardwaregain",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_hardwaregain_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_rf_port_select",
	"/sys/bus/iio/devices/iio:device0/out_voltage1_rssi",
	"/sys/bus/iio/devices/iio:device0/out_voltage2_raw",
	"/sys/bus/iio/devices/iio:device0/out_voltage2_scale",
	"/sys/bus/iio/devices/iio:device0/out_voltage3_raw",
	"/sys/bus/iio/devices/iio:device0/out_voltage3_scale",
	"/sys/bus/iio/devices/iio:device0/out_voltage_filter_fir_en",
	"/sys/bus/iio/devices/iio:device0/out_voltage_rf_bandwidth",
	"/sys/bus/iio/devices/iio:device0/out_voltage_rf_bandwidth_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage_rf_port_select_available",
	"/sys/bus/iio/devices/iio:device0/out_voltage_sampling_frequency",
	"/sys/bus/iio/devices/iio:device0/out_voltage_sampling_frequency_available",
	"/sys/bus/iio/devices/iio:device0/rssi_gain_step_error",
	"/sys/bus/iio/devices/iio:device0/rx_path_rates",
	"/sys/bus/iio/devices/iio:device0/trx_rate_governor",
	"/sys/bus/iio/devices/iio:device0/trx_rate_governor_available",
	"/sys/bus/iio/devices/iio:device0/tx_path_rates",
	"/sys/bus/iio/devices/iio:device0/xo_correction",
	"/sys/bus/iio/devices/iio:device0/xo_correction_available",
	"/sys/kernel/debug/iio/iio:device0/direct_reg_access"

};

/* file_path index,the file_path must correspond to the order of files in the file_path */
enum path_index {
	CALIB_MODE,
	CALIB_MODE_AVAILABLE,
	DCXO_TUNE_COARSE,
	DCXO_TUNE_COARSE_AVAILABLE,
	DCXO_TUNE_FINE,
	DCXO_TUNE_FINE_AVAILABLE,
	ENSM_MODE,
	ENSM_MODE_AVAILABLE,
	FILTER_FIR_CONFIG,
	GAIN_TABLE_CONFIG,
	IN_OUT_VOLTAGE_FILTER_FIR_EN,
	IN_TEMP0_INPUT,
	IN_VOLTAGE0_GAIN_CONTROL_MODE,
	IN_VOLTAGE0_HARDWAREGAIN,
	IN_VOLTAGE0_HARDWAREGAIN_AVAILABLE,
	IN_VOLTAGE0_RF_PORT_SELECT,
	IN_VOLTAGE0_RSSI,
	IN_VOLTAGE1_GAIN_CONTROL_MODE,
	IN_VOLTAGE1_HARDWAREGAIN,
	IN_VOLTAGE1_HARDWAREGAIN_AVAILABLE,
	IN_VOLTAGE1_RF_PORT_SELECT,
	IN_VOLTAGE1_RSSI,
	IN_VOLTAGE2_OFFSET,
	IN_VOLTAGE2_RAW,
	IN_VOLTAGE2_SCALE,
	IN_VOLTAGE_BB_DC_OFFSET_TRACKING_EN,
	IN_VOLTAGE_FILTER_FIR_EN,
	IN_VOLTAGE_GAIN_CONTROL_MODE_AVAILABLE,
	IN_VOLTAGE_QUADRATURE_TRACKING_EN,
	IN_VOLTAGE_RF_BANDWIDTH,
	IN_VOLTAGE_RF_BANDWIDTH_AVAILABLE,
	IN_VOLTAGE_RF_DC_OFFSET_TRACKING_EN,
	IN_VOLTAGE_RF_PORT_SELECT_AVAILABLE,
	IN_VOLTAGE_SAMPLING_FREQUENCY,
	IN_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE,
	NAME,
	OUT_ALTVOLTAGE0_RX_LO_EXTERNAL,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_LOAD,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_RECALL,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_SAVE,
	OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_STORE,
	OUT_ALTVOLTAGE0_RX_LO_FREQUENCY,
	OUT_ALTVOLTAGE0_RX_LO_FREQUENCY_AVAILABLE,
	OUT_ALTVOLTAGE0_RX_LO_POWERDOWN,
	OUT_ALTVOLTAGE1_TX_LO_EXTERNAL,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_LOAD,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_RECALL,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_SAVE,
	OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_STORE,
	OUT_ALTVOLTAGE1_TX_LO_FREQUENCY,
	OUT_ALTVOLTAGE1_TX_LO_FREQUENCY_AVAILABLE,
	OUT_ALTVOLTAGE1_TX_LO_POWERDOWN,
	OUT_VOLTAGE0_HARDWAREGAIN,
	OUT_VOLTAGE0_HARDWAREGAIN_AVAILABLE,
	OUT_VOLTAGE0_RF_PORT_SELECT,
	OUT_VOLTAGE0_RSSI,
	OUT_VOLTAGE1_HARDWAREGAIN,
	OUT_VOLTAGE1_HARDWAREGAIN_AVAILABLE,
	OUT_VOLTAGE1_RF_PORT_SELECT,
	OUT_VOLTAGE1_RSSI,
	OUT_VOLTAGE2_RAW,
	OUT_VOLTAGE2_SCALE,
	OUT_VOLTAGE3_RAW,
	OUT_VOLTAGE3_SCALE,
	OUT_VOLTAGE_FILTER_FIR_EN,
	OUT_VOLTAGE_RF_BANDWIDTH,
	OUT_VOLTAGE_RF_BANDWIDTH_AVAILABLE,
	OUT_VOLTAGE_RF_PORT_SELECT_AVAILABLE,
	OUT_VOLTAGE_SAMPLING_FREQUENCY,
	OUT_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE,
	RSSI_GAIN_STEP_ERROR,
	RX_PATH_RATES,
	TRX_RATE_GOVERNOR,
	TRX_RATE_GOVERNOR_AVAILABLE,
	TX_PATH_RATES,
	XO_CORRECTION,
	XO_CORRECTION_AVAILABLE,
	DIRECT_REG_ACCESS,


};

/* ad9361 data device struct member*/
struct ad9361_rf_phy {
	char *calib_mode;//Pointer to a string (dynamic memory allocation)
	char *calib_mode_available;
	int *dcxo_tune_coarse;//cat: dcxo_tune_coarse: No such device
	char *dcxo_tune_coarse_available;
	int *dcxo_tune_fine;//cat: dcxo_tune_fine: No such device
	char *dcxo_tune_fine_available;
	char *ensm_mode;
	char *ensm_mode_available;
	char *filter_fir_config;
	char *gain_table_config;
	int *in_out_voltage_filter_fir_en;
	int *in_temp0_input;
	char *in_voltage0_gain_control_mode;
	char *in_voltage0_hardwaregain;
	char *in_voltage0_hardwaregain_available;
	char *in_voltage0_rf_port_select;
	char *in_voltage0_rssi;
	char *in_voltage1_gain_control_mode;
	char *in_voltage1_hardwaregain;
	char *in_voltage1_hardwaregain_available;
	char *in_voltage1_rf_port_select;
	char *in_voltage1_rssi;
	int *in_voltage2_offset;
	int *in_voltage2_raw;
	double in_voltage2_scale;
	int *in_voltage_bb_dc_offset_tracking_en;
	int *in_voltage_filter_fir_en;
	char *in_voltage_gain_control_mode_available;
	int *in_voltage_quadrature_tracking_en;
	int *in_voltage_rf_bandwidth;
	char *in_voltage_rf_bandwidth_available;
	int *in_voltage_rf_dc_offset_tracking_en;
	char *in_voltage_rf_port_select_available;
	int *in_voltage_sampling_frequency;
	char *in_voltage_sampling_frequency_available;
	char *name;
	int *out_altvoltage0_RX_LO_external;
	int *out_altvoltage0_RX_LO_fastlock_load;
	int *out_altvoltage0_RX_LO_fastlock_recall;//cat: out_altvoltage0_RX_LO_fastlock_recall: Invalid argument
	char *out_altvoltage0_RX_LO_fastlock_save;
	int *out_altvoltage0_RX_LO_fastlock_store;
	char *out_altvoltage0_RX_LO_frequency;
	char *out_altvoltage0_RX_LO_frequency_available;
	int *out_altvoltage0_RX_LO_powerdown;
	int *out_altvoltage1_TX_LO_external;
	int *out_altvoltage1_TX_LO_fastlock_load;
	int *out_altvoltage1_TX_LO_fastlock_recall;
	char *out_altvoltage1_TX_LO_fastlock_save;
	int *out_altvoltage1_TX_LO_fastlock_store;
	char *out_altvoltage1_TX_LO_frequency;
	char *out_altvoltage1_TX_LO_frequency_available;
	int *out_altvoltage1_TX_LO_powerdown;
	char *out_voltage0_hardwaregain;
	char *out_voltage0_hardwaregain_available;
	char *out_voltage0_rf_port_select;
	char *out_voltage0_rssi;
	char *out_voltage1_hardwaregain;
	char *out_voltage1_hardwaregain_available;
	char *out_voltage1_rf_port_select;
	char *out_voltage1_rssi;
	int *out_voltage2_raw;
	double out_voltage2_scale;
	int *out_voltage3_raw;
	double out_voltage3_scale;
	int *out_voltage_filter_fir_en;
	int *out_voltage_rf_bandwidth;
	char *out_voltage_rf_bandwidth_available;
	char *out_voltage_rf_port_select_available;
	int *out_voltage_sampling_frequency;
	char *out_voltage_sampling_frequency_available;
	char *rssi_gain_step_error;
	char *rx_path_rates;
	char *trx_rate_governor;
	char *trx_rate_governor_available;
	char *tx_path_rates;
	int *xo_correction;
	char *xo_correction_available;
	char *direct_reg_access;
};
struct ad9361_rf_phy ad9361;

/*
 * @description			: read the specified file
 * @param - filename 	: the path of the file which will be read
 * @param - str 		: the read of file string
 * @return 				: 0:success; other: failure
 */
static int file_data_read(char *filename, char *str)//only read a char data
{
	int ret = 0;
	FILE *data_stream;

    data_stream = fopen(filename, "r"); /* read only open */
    if(data_stream == NULL) {
		printf("can't open file %s\r\n", filename);
		return -1;
	}

	ret = fscanf(data_stream, "%s", str);
    if(!ret) {
        printf("file read error!\r\n");
    } else if(ret == EOF) {
        /* Readjust the file pointer to the file header when you reach the end of the file */
        fseek(data_stream, 0, SEEK_SET);
    }
	fclose(data_stream);	/* close file */
	return 0;
}

//read a string data, the read size is max_size
static int file_data_read_string(char *filename, char *str, size_t max_size)
{
    FILE *data_stream;

    data_stream = fopen(filename, "r");
    if(data_stream == NULL) {
        printf("can't open file %s\r\n", filename);
        return -1;
    }

    // Reset str to an empty string
    str[0] = '\0';

    char line[256];  // Adjust the size as needed

    // Read lines until the end of the file
    while (fgets(line, sizeof(line), data_stream) != NULL) {
        // Concatenate the line to the existing content
        strncat(str, line, max_size - strlen(str) - 1);

        // Check if the entire line was read
        if (str[strlen(str) - 1] == '\n') {
            break;
        }
    }

    fclose(data_stream);  // Close the file

    return 0;
}
/*
 * @description			: write the specified file
 * @param - filename 	: the path of the file which will be read
 * @param - data 		: the int value you will write to the file
 * @return 				: 0:success; other: failure
 */
static int file_data_write(char *filename, int *data)
{
	int ret = 0;
	FILE *data_stream;
	char str[20];
	snprintf(str,sizeof(str), "%d", data);//turn integer to string
    data_stream = fopen(filename, "w"); /* write only open */
    if(data_stream == NULL) {
		printf("can't open file %s\r\n", filename);
		return -1;
	}
    fprintf(data_stream,"%s",str);
	fclose(data_stream);	/* close file */
	return 0;
}
/*
 * @description       : write the specified file
 * @param - filename  : the path of the file which will be read
 * @param - data1     : the first int value you will write to the file
 * @param - data2     : the second int value you will write to the file
 * @return            : 0: success; other: failure
 */
static int file_data_write_two(char *filename, int reg_addr, int data)
{
    int ret = 0;
    FILE *data_stream;
    char str[40]; // Increased buffer size to accommodate two integers
    snprintf(str, sizeof(str), "%d %d", reg_addr, data); // Concatenate two integers as a string

    data_stream = fopen(filename, "w"); /* write only open */
    if (data_stream == NULL) {
        printf("can't open file %s\r\n", filename);
        return -1;
    }
    fprintf(data_stream, "%s", str);
    fclose(data_stream); /* close file */
    return 0;
}

#define SENSOR_INT_DATA_SET(ret, index, data, member)\
	dev->member = data;\
	ret = file_data_write(file_path[index], data);\


#define SENSOR_INT_DATA_SET_TWO(ret, index, reg_addr, data, member)\
	dev->member = data;\
	ret = file_data_write_two(file_path[index], reg_addr, data);\



/*
 * @description			: get ad9361 data
 * @param - dev 		: device struct
 * @return 				: 0:success; other: failure
 **/
static int sensor_read(struct ad9361_rf_phy *dev)
{
	int ret = 0;
	char str[MAX_BUFFER_SIZE];
	/* get iio sysfs data */
	file_data_read_string(file_path[CALIB_MODE], str,MAX_BUFFER_SIZE);
	dev->calib_mode = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[CALIB_MODE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->calib_mode_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, DCXO_TUNE_COARSE, str, dcxo_tune_coarse);

	file_data_read_string(file_path[DCXO_TUNE_COARSE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->dcxo_tune_coarse_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, DCXO_TUNE_FINE, str, dcxo_tune_fine);
	file_data_read_string(file_path[DCXO_TUNE_FINE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->dcxo_tune_fine_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[ENSM_MODE], str,MAX_BUFFER_SIZE);
	dev->ensm_mode = strdup(str);// Allocate memory for a copy of the string
	file_data_read_string(file_path[ENSM_MODE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->ensm_mode_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[FILTER_FIR_CONFIG], str,MAX_BUFFER_SIZE);
	dev->filter_fir_config = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[GAIN_TABLE_CONFIG], str,MAX_BUFFER_SIZE);
	dev->gain_table_config = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_OUT_VOLTAGE_FILTER_FIR_EN, str, in_out_voltage_filter_fir_en);
	SENSOR_INT_DATA_GET(ret, IN_TEMP0_INPUT, str, in_temp0_input);

	file_data_read_string(file_path[IN_VOLTAGE0_GAIN_CONTROL_MODE],str,MAX_BUFFER_SIZE);
	dev->in_voltage0_gain_control_mode = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->in_voltage0_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage0_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_RF_PORT_SELECT],str,MAX_BUFFER_SIZE);
	dev->in_voltage0_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE0_RSSI], str,MAX_BUFFER_SIZE);
	dev->in_voltage0_rssi = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_GAIN_CONTROL_MODE],str,MAX_BUFFER_SIZE);
	dev->in_voltage1_gain_control_mode = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_RF_PORT_SELECT], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[IN_VOLTAGE1_RSSI], str,MAX_BUFFER_SIZE);
	dev->in_voltage1_rssi = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE2_OFFSET, str, in_voltage2_offset);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE2_RAW, str, in_voltage2_raw);

	SENSOR_FLOAT_DATA_GET(ret, IN_VOLTAGE2_SCALE, str, in_voltage2_scale);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_BB_DC_OFFSET_TRACKING_EN, str, in_voltage_bb_dc_offset_tracking_en);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_FILTER_FIR_EN, str, in_voltage_filter_fir_en);

	file_data_read_string(file_path[IN_VOLTAGE_GAIN_CONTROL_MODE_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_gain_control_mode_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_QUADRATURE_TRACKING_EN, str, in_voltage_quadrature_tracking_en);

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_RF_BANDWIDTH, str, in_voltage_rf_bandwidth);

	file_data_read_string(file_path[IN_VOLTAGE_RF_BANDWIDTH_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_rf_bandwidth_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_RF_DC_OFFSET_TRACKING_EN, str, in_voltage_rf_dc_offset_tracking_en);

	file_data_read_string(file_path[IN_VOLTAGE_RF_PORT_SELECT_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_rf_port_select_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, IN_VOLTAGE_SAMPLING_FREQUENCY, str, in_voltage_sampling_frequency);

	file_data_read_string(file_path[IN_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->in_voltage_sampling_frequency_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[NAME], str,MAX_BUFFER_SIZE);
	dev->name = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_EXTERNAL, str, out_altvoltage0_RX_LO_external);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_LOAD, str, out_altvoltage0_RX_LO_fastlock_load);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_RECALL, str, out_altvoltage0_RX_LO_fastlock_recall);//cat: out_altvoltage0_RX_LO_fastlock_recall: Invalid argument

	file_data_read_string(file_path[OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_SAVE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage0_RX_LO_fastlock_save = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_FASTLOCK_STORE, str, out_altvoltage0_RX_LO_fastlock_store);

	file_data_read_string(file_path[OUT_ALTVOLTAGE0_RX_LO_FREQUENCY], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage0_RX_LO_frequency = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_ALTVOLTAGE0_RX_LO_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage0_RX_LO_frequency_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE0_RX_LO_POWERDOWN, str, out_altvoltage0_RX_LO_powerdown);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_EXTERNAL, str, out_altvoltage1_TX_LO_external);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_LOAD, str, out_altvoltage1_TX_LO_fastlock_load);

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_RECALL, str, out_altvoltage1_TX_LO_fastlock_recall);//cat: out_altvoltage1_TX_LO_fastlock_recall: Invalid argument

	file_data_read_string(file_path[OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_SAVE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage1_TX_LO_fastlock_save = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_FASTLOCK_STORE, str, out_altvoltage1_TX_LO_fastlock_store);

	file_data_read_string(file_path[OUT_ALTVOLTAGE1_TX_LO_FREQUENCY], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage1_TX_LO_frequency = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_ALTVOLTAGE1_TX_LO_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_altvoltage1_TX_LO_frequency_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_ALTVOLTAGE1_TX_LO_POWERDOWN, str, out_altvoltage1_TX_LO_powerdown);

	file_data_read_string(file_path[OUT_VOLTAGE0_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE0_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE0_RF_PORT_SELECT], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE0_RSSI], str,MAX_BUFFER_SIZE);
	dev->out_voltage0_rssi = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_HARDWAREGAIN], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_hardwaregain = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_HARDWAREGAIN_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_hardwaregain_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_RF_PORT_SELECT], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_rf_port_select = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE1_RSSI], str,MAX_BUFFER_SIZE);
	dev->out_voltage1_rssi = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE2_RAW, str, out_voltage2_raw);

	SENSOR_FLOAT_DATA_GET(ret, OUT_VOLTAGE2_SCALE, str, out_voltage2_scale);

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE3_RAW, str, out_voltage3_raw);

	SENSOR_FLOAT_DATA_GET(ret, OUT_VOLTAGE3_SCALE, str, out_voltage3_scale);

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE_FILTER_FIR_EN, str, out_voltage_filter_fir_en);

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE_RF_BANDWIDTH, str, out_voltage_rf_bandwidth);

	file_data_read_string(file_path[OUT_VOLTAGE_RF_BANDWIDTH_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage_rf_bandwidth_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[OUT_VOLTAGE_RF_PORT_SELECT_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage_rf_port_select_available = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, OUT_VOLTAGE_SAMPLING_FREQUENCY, str, out_voltage_sampling_frequency);

	file_data_read_string(file_path[OUT_VOLTAGE_SAMPLING_FREQUENCY_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->out_voltage_sampling_frequency_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[RSSI_GAIN_STEP_ERROR], str,MAX_BUFFER_SIZE);
	dev->rssi_gain_step_error = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[RX_PATH_RATES], str,MAX_BUFFER_SIZE);
	dev->rx_path_rates = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[TRX_RATE_GOVERNOR], str,MAX_BUFFER_SIZE);
	dev->trx_rate_governor = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[TRX_RATE_GOVERNOR_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->trx_rate_governor_available = strdup(str);// Allocate memory for a copy of the string

	file_data_read_string(file_path[TX_PATH_RATES], str,MAX_BUFFER_SIZE);
	dev->tx_path_rates = strdup(str);// Allocate memory for a copy of the string

	SENSOR_INT_DATA_GET(ret, XO_CORRECTION, str, xo_correction);

	file_data_read_string(file_path[XO_CORRECTION_AVAILABLE], str,MAX_BUFFER_SIZE);
	dev->xo_correction_available = strdup(str);// Allocate memory for a copy of the string

//	SENSOR_INT_DATA_GET(ret, DIRECT_REG_ACCESS, str, direct_reg_access);
	file_data_read_string(file_path[DIRECT_REG_ACCESS], str,MAX_BUFFER_SIZE);
	dev->direct_reg_access = strdup(str);// Allocate memory for a copy of the string
	return ret;
}



/*
 * @description			: read ad9361 reg
 * @param - dev 		: device struct
 * @return 				: 0:success; other: failure
 **/
static int sensor_read_reg(struct ad9361_rf_phy *dev, int reg_addr)
{
	int ret = 0;
	char str[MAX_BUFFER_SIZE];

	SENSOR_INT_DATA_SET(ret, DIRECT_REG_ACCESS, reg_addr, direct_reg_access);
	file_data_read_string(file_path[DIRECT_REG_ACCESS], str,MAX_BUFFER_SIZE);
	dev->direct_reg_access = strdup(str);// Allocate memory for a copy of the string
	printf("0x%x,%s\r\n",reg_addr,ad9361.direct_reg_access);
	return ret;
}

/*
 * @description			: write ad9361 reg
 * @param - dev 		: device struct
 * @return 				: 0:success; other: failure
 **/
static int sensor_write_reg(struct ad9361_rf_phy *dev, int reg_addr, int data)
{
	int ret = 0;
	char str[MAX_BUFFER_SIZE];

	SENSOR_INT_DATA_SET_TWO(ret, DIRECT_REG_ACCESS, reg_addr, data,direct_reg_access);
	file_data_read_string(file_path[DIRECT_REG_ACCESS], str,MAX_BUFFER_SIZE);
	dev->direct_reg_access = strdup(str);// Allocate memory for a copy of the string
	printf("0x%x,0x%x,%s\r\n",reg_addr,data,ad9361.direct_reg_access);
	return ret;
}
int main(void)
{	

	printf("enter mian!\r\n");
    SENSOR_INT_DATA_SET(ret, OUT_VOLTAGE_RF_BANDWIDTH, 18000000, out_voltage_rf_bandwidth);//write OUT VOLTAGE RF BANDWIDTH of the iio device

    ret = sensor_read(&ad9361);//read all ad9361 iio device drivers' value
	if(ret == 0){
		printf("out_voltage_rf_bandwidth=%s\r\n",ad9361.out_voltage_rf_bandwidth);



	while(1)
	{


	    sensor_write_reg(&ad9361,0x3F5,0x01);//write refgister

		sensor_read_reg(&ad9361,0x3F5);//read register

		sleep(5);


	}

	return(0);
}

编译Vitis工程,生成.elf文件,将debug文件夹下的.elf文件拷贝到开发板Linux的opt文件夹下。

进入开发板opt文件夹下,chmod加运行权限,然后直接执行.elf文件,即可完成读写操作。

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是基于Linux系统的IIO库和AD9361无线电芯片的读写文件配置应用程序代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <iio.h> #define IIO_DEVICE_NAME "ad9361-phy" #define RX_LO_FREQ 2400000000ULL #define RX_SAMPLING_FREQ 3072000ULL int main(int argc, char **argv) { struct iio_context *ctx; struct iio_device *dev; struct iio_channel *rx_lo, *rx_samp; struct iio_buffer *rx_buf; void *rx_data; int ret; ctx = iio_create_default_context(); if (!ctx) { perror("iio_create_default_context"); return EXIT_FAILURE; } dev = iio_context_find_device(ctx, IIO_DEVICE_NAME); if (!dev) { perror("iio_context_find_device"); ret = EXIT_FAILURE; goto err_ctx; } rx_lo = iio_device_find_channel(dev, "altvoltage0", true); if (!rx_lo) { perror("iio_device_find_channel"); ret = EXIT_FAILURE; goto err_ctx; } rx_samp = iio_device_find_channel(dev, "voltage0", true); if (!rx_samp) { perror("iio_device_find_channel"); ret = EXIT_FAILURE; goto err_ctx; } iio_channel_attr_write_longlong(rx_lo, "frequency", RX_LO_FREQ); iio_channel_attr_write_longlong(rx_samp, "sampling_frequency", RX_SAMPLING_FREQ); rx_buf = iio_device_create_buffer(dev, 4096, false); if (!rx_buf) { perror("iio_device_create_buffer"); ret = EXIT_FAILURE; goto err_ctx; } rx_data = iio_buffer_start(rx_buf); if (!rx_data) { perror("iio_buffer_start"); ret = EXIT_FAILURE; goto err_buf; } // 从rx_data缓冲区读取数据或将数据写入rx_data缓冲区 iio_buffer_destroy(rx_buf); iio_channel_attr_write_longlong(rx_samp, "sampling_frequency", 0); iio_channel_attr_write_longlong(rx_lo, "frequency", 0); iio_channel_release(rx_samp); iio_channel_release(rx_lo); iio_device_destroy(dev); iio_context_destroy(ctx); return EXIT_SUCCESS; err_buf: iio_buffer_destroy(rx_buf); err_ctx: iio_context_destroy(ctx); return ret; } ``` 在这个示例中,我们使用了IIO上下文库来连接AD9361无线电芯片。然后,我们通过IIO库的iio_channel_attr_write_longlong函数,将本地振荡器频率和采样频率设置为所需的值。接下来,我们创建一个IIO缓冲区,并使用iio_buffer_start函数获取缓冲区地址。最后,我们可以读取或写入数据到缓冲区,并在完成后清理IIO库资源。 需要注意的是,这个示例仅仅是在Linux系统上使用IIO库和AD9361无线电芯片进行读写文件配置的一个基本示例,实际应用中还需要根据具体场景进行适当的修改和扩展。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值