static void mcs_cb (GtkWidget *widget, gpointer data)
{
	unsigned step;
	struct iio_channel *tx_sample_master, *tx_sample_slave;
	long long tx_sample_master_freq, tx_sample_slave_freq;
	char temp[40], ensm_mode[40];
	unsigned tmp;
	static int fix_slave_tune = 1;

	tx_sample_master = iio_device_find_channel(dev, "voltage0", true);
	tx_sample_slave = iio_device_find_channel(dev_slave, "voltage0", true);

	iio_channel_attr_read_longlong(tx_sample_master, "sampling_frequency", &tx_sample_master_freq);
	iio_channel_attr_read_longlong(tx_sample_slave, "sampling_frequency", &tx_sample_slave_freq);

	if (tx_sample_master_freq != tx_sample_slave_freq) {
		printf("tx_sample_master_freq != tx_sample_slave_freq\nUpdating...\n");
		iio_channel_attr_write_longlong(tx_sample_slave, "sampling_frequency", tx_sample_master_freq);
	}

	if (fix_slave_tune) {
		iio_device_reg_read(dev, 0x6, &tmp);
		iio_device_reg_write(dev_slave, 0x6, tmp);
		iio_device_reg_read(dev, 0x7, &tmp);
		iio_device_reg_write(dev_slave, 0x7, tmp);
		fix_slave_tune = 0;
	}

	iio_device_attr_read(dev, "ensm_mode", ensm_mode, sizeof(ensm_mode));

	/* Move the parts int ALERT for MCS */
	iio_device_attr_write(dev, "ensm_mode", "alert");
	iio_device_attr_write(dev_slave, "ensm_mode", "alert");

	for (step = 1; step <= 5; step++) {
		sprintf(temp, "%d", step);
		/* Don't change the order here - the master controls the SYNC GPIO */
		iio_device_debug_attr_write(dev_slave, "multichip_sync", temp);
		iio_device_debug_attr_write(dev, "multichip_sync", temp);
		sleep(0.1);
	}

	iio_device_attr_write(dev, "ensm_mode", ensm_mode);
	iio_device_attr_write(dev_slave, "ensm_mode", ensm_mode);

#if 0
	iio_device_debug_attr_write(dev, "multichip_sync", "6");
	iio_device_debug_attr_write(dev_slave, "multichip_sync", "6");
#endif

}
static void *monitor_thread_fn(void *device_name)
{
	struct iio_context *ctx;
	struct iio_device *dev;
	uint32_t val;
	int ret;

	ctx = iio_create_default_context();
	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return (void *)-1;
	}

	dev = get_device(ctx, device_name);
	if (!dev) {
		fprintf(stderr, "Unable to find IIO device\n");
		iio_context_destroy(ctx);
		return (void *)-1;
	}

	/* Give the main thread a moment to start the DMA */
	sleep(1);

	/* Clear all status bits */
	iio_device_reg_write(dev, 0x80000088, 0x6);

	while (app_running) {
		ret = iio_device_reg_read(dev, 0x80000088, &val);
		if (ret) {
			fprintf(stderr, "Failed to read status register: %s\n",
					strerror(-ret));
			continue;
		}

		if (device_is_tx) {
			if (val & 1)
				fprintf(stderr, "Underflow detected\n");
		} else {
			if (val & 4)
				fprintf(stderr, "Overflow detected\n");
		}

		/* Clear bits */
		if (val)
			iio_device_reg_write(dev, 0x80000088, val);
		sleep(1);
	}

	iio_context_destroy(ctx);

	return (void *)0;
}
static void readReg(char* device, uint32_t address, uint32_t* data) {
	struct iio_device *dev;

	if (!device) {
		perror("readReg() - device is NULL");
	}

	dev = iio_context_find_device(ctx, device);

	if (!dev) {
		perror("readReg() - Unable to find device!");
		return;
	}
	/* register read */
	iio_device_reg_read(dev, address, data);
}
static void near_end_loopback_ctrl(unsigned channel, bool enable)
{

	unsigned tmp;
	struct iio_device *dev = (channel > 3) ?
		cf_ad9361_lpc : cf_ad9361_hpc;
	if (!dev)
		return;

	if (channel > 3)
		channel -= 4;

	if (iio_device_reg_read(dev, 0x80000400 + channel * 0x40, &tmp))
		return;

	if (enable)
		tmp |= 0x800;
	else
		tmp &= ~0x800;

	iio_device_reg_write(dev, 0x80000400 + channel * 0x40, tmp);
}