<commit_before>/*
 * Copyright (C) 2015 Analog Devices, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

#include "ad9361.h"

#include <errno.h>
#include <iio.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_AD9361_SYNC_DEVS	4

int ad9361_multichip_sync(struct iio_device *master, struct iio_device **slaves,
		unsigned int num_slaves, unsigned int flags)
{
	char ensm_mode[MAX_AD9361_SYNC_DEVS][20];
	unsigned int i, step;
	bool mcs_is_debug_attr = !iio_device_find_attr(master, "multichip_sync");

	if (num_slaves >= MAX_AD9361_SYNC_DEVS || num_slaves < 1)
		return -EINVAL;

	if (flags & CHECK_SAMPLE_RATES) {
		struct iio_channel *tx_sample_master, *tx_sample_slave;
		long long tx_sample_master_freq, tx_sample_slave_freq;

		tx_sample_master = iio_device_find_channel(master, "voltage0", true);
		iio_channel_attr_read_longlong(tx_sample_master, "sampling_frequency", &tx_sample_master_freq);

		for (i = 0; i < num_slaves; i++) {
			tx_sample_slave = iio_device_find_channel(slaves[i], "voltage0", true);
			if (tx_sample_slave == NULL)
				return -ENODEV;

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

			if (tx_sample_master_freq != tx_sample_slave_freq) {
				fprintf(stderr, "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 (flags & FIXUP_INTERFACE_TIMING) {
		unsigned tmp, tmp2;
		iio_device_reg_read(master, 0x6, &tmp);
		iio_device_reg_read(master, 0x7, &tmp2);

		for (i = 0; i < num_slaves; i++) {
			iio_device_reg_write(slaves[i], 0x6, tmp);
			iio_device_reg_write(slaves[i], 0x7, tmp2);
		}
	}

	/* Move the parts int ALERT for MCS */
	iio_device_attr_read(master, "ensm_mode", ensm_mode[0], sizeof(ensm_mode));
	iio_device_attr_write(master, "ensm_mode", "alert");

	for (i = 0; i < num_slaves; i++) {
		iio_device_attr_read(slaves[i], "ensm_mode", ensm_mode[i + 1], sizeof(ensm_mode));
		iio_device_attr_write(slaves[i], "ensm_mode", "alert");
	}

	for (step = 0; step <= 5; step++) {
		for (i = 0; i < num_slaves; i++) {
			if (mcs_is_debug_attr)
				iio_device_debug_attr_write_longlong(slaves[i], "multichip_sync", step);
			else
				iio_device_attr_write_longlong(slaves[i], "multichip_sync", step);
		}

		/* The master controls the SYNC GPIO */
		if (mcs_is_debug_attr)
			iio_device_debug_attr_write_longlong(master, "multichip_sync", step);
		else
			iio_device_attr_write_longlong(master, "multichip_sync", step);

		usleep(1000);
	}

	iio_device_attr_write(master, "ensm_mode", ensm_mode[0]);

	for (i = 0; i < num_slaves; i++)
		iio_device_attr_write(slaves[i], "ensm_mode", ensm_mode[i + 1]);
<commit_msg>ad9361_multichip_sync.c: Build fix for Visual Studio<commit_after>/*
 * Copyright (C) 2015 Analog Devices, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

#include "ad9361.h"

#include <errno.h>
#include <iio.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

#define MAX_AD9361_SYNC_DEVS	4

int ad9361_multichip_sync(struct iio_device *master, struct iio_device **slaves,
		unsigned int num_slaves, unsigned int flags)
{
	char ensm_mode[MAX_AD9361_SYNC_DEVS][20];
	unsigned int i, step;
	bool mcs_is_debug_attr = !iio_device_find_attr(master, "multichip_sync");

	if (num_slaves >= MAX_AD9361_SYNC_DEVS || num_slaves < 1)
		return -EINVAL;

	if (flags & CHECK_SAMPLE_RATES) {
		struct iio_channel *tx_sample_master, *tx_sample_slave;
		long long tx_sample_master_freq, tx_sample_slave_freq;

		tx_sample_master = iio_device_find_channel(master, "voltage0", true);
		iio_channel_attr_read_longlong(tx_sample_master, "sampling_frequency", &tx_sample_master_freq);

		for (i = 0; i < num_slaves; i++) {
			tx_sample_slave = iio_device_find_channel(slaves[i], "voltage0", true);
			if (tx_sample_slave == NULL)
				return -ENODEV;

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

			if (tx_sample_master_freq != tx_sample_slave_freq) {
				fprintf(stderr, "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 (flags & FIXUP_INTERFACE_TIMING) {
		unsigned tmp, tmp2;
		iio_device_reg_read(master, 0x6, &tmp);
		iio_device_reg_read(master, 0x7, &tmp2);

		for (i = 0; i < num_slaves; i++) {
			iio_device_reg_write(slaves[i], 0x6, tmp);
			iio_device_reg_write(slaves[i], 0x7, tmp2);
		}
	}

	/* Move the parts int ALERT for MCS */
	iio_device_attr_read(master, "ensm_mode", ensm_mode[0], sizeof(ensm_mode));
	iio_device_attr_write(master, "ensm_mode", "alert");

	for (i = 0; i < num_slaves; i++) {
		iio_device_attr_read(slaves[i], "ensm_mode", ensm_mode[i + 1], sizeof(ensm_mode));
		iio_device_attr_write(slaves[i], "ensm_mode", "alert");
	}

	for (step = 0; step <= 5; step++) {
		for (i = 0; i < num_slaves; i++) {
			if (mcs_is_debug_attr)
				iio_device_debug_attr_write_longlong(slaves[i], "multichip_sync", step);
			else
				iio_device_attr_write_longlong(slaves[i], "multichip_sync", step);
		}

		/* The master controls the SYNC GPIO */
		if (mcs_is_debug_attr)
			iio_device_debug_attr_write_longlong(master, "multichip_sync", step);
		else
			iio_device_attr_write_longlong(master, "multichip_sync", step);

#ifdef _WIN32
		Sleep(1);
#else
		usleep(1000);
#endif
	}

	iio_device_attr_write(master, "ensm_mode", ensm_mode[0]);

	for (i = 0; i < num_slaves; i++)
		iio_device_attr_write(slaves[i], "ensm_mode", ensm_mode[i + 1]);

{'fraction_non_alphanumeric': 0.11587786259541985, 'fraction_numerical': 0.019083969465648856, 'mean_word_length': 5.341723136495644, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '201488', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2475, 'n_words': 572}