예제 #1
0
파일: hx711.c 프로젝트: AshishNamdev/linux
static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
{
	int ret;

	if (chan == 0) {
		if (hx711_data->gain_set == 32) {
			hx711_data->gain_set = hx711_data->gain_chan_a;

			ret = hx711_read(hx711_data);
			if (ret < 0)
				return ret;

			ret = hx711_wait_for_ready(hx711_data);
			if (ret)
				return ret;
		}
	} else {
		if (hx711_data->gain_set != 32) {
			hx711_data->gain_set = 32;

			ret = hx711_read(hx711_data);
			if (ret < 0)
				return ret;

			ret = hx711_wait_for_ready(hx711_data);
			if (ret)
				return ret;
		}
	}

	return 0;
}
예제 #2
0
파일: hx711.c 프로젝트: AshishNamdev/linux
static int hx711_write_raw(struct iio_dev *indio_dev,
				struct iio_chan_spec const *chan,
				int val,
				int val2,
				long mask)
{
	struct hx711_data *hx711_data = iio_priv(indio_dev);
	int ret;
	int gain;

	switch (mask) {
	case IIO_CHAN_INFO_SCALE:
		/*
		 * a scale greater than 1 mV per LSB is not possible
		 * with the HX711, therefore val must be 0
		 */
		if (val != 0)
			return -EINVAL;

		mutex_lock(&hx711_data->lock);

		gain = hx711_get_scale_to_gain(val2);
		if (gain < 0) {
			mutex_unlock(&hx711_data->lock);
			return gain;
		}

		if (gain != hx711_data->gain_set) {
			hx711_data->gain_set = gain;
			if (gain != 32)
				hx711_data->gain_chan_a = gain;

			ret = hx711_read(hx711_data);
			if (ret < 0) {
				mutex_unlock(&hx711_data->lock);
				return ret;
			}
		}

		mutex_unlock(&hx711_data->lock);
		return 0;
	default:
		return -EINVAL;
	}

	return 0;
}
예제 #3
0
파일: hx711.c 프로젝트: AshishNamdev/linux
static int hx711_read_raw(struct iio_dev *indio_dev,
				const struct iio_chan_spec *chan,
				int *val, int *val2, long mask)
{
	struct hx711_data *hx711_data = iio_priv(indio_dev);
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		mutex_lock(&hx711_data->lock);

		/*
		 * hx711_reset() must be called from here
		 * because it could be calling hx711_read() by itself
		 */
		if (hx711_reset(hx711_data)) {
			mutex_unlock(&hx711_data->lock);
			dev_err(hx711_data->dev, "reset failed!");
			return -EIO;
		}

		ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
		if (ret < 0) {
			mutex_unlock(&hx711_data->lock);
			return ret;
		}

		*val = hx711_read(hx711_data);

		mutex_unlock(&hx711_data->lock);

		if (*val < 0)
			return *val;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		*val = 0;
		mutex_lock(&hx711_data->lock);

		*val2 = hx711_get_gain_to_scale(hx711_data->gain_set);

		mutex_unlock(&hx711_data->lock);

		return IIO_VAL_INT_PLUS_NANO;
	default:
		return -EINVAL;
	}
}
예제 #4
0
파일: hx711.c 프로젝트: AshishNamdev/linux
static int hx711_reset(struct hx711_data *hx711_data)
{
	int ret;
	int val = gpiod_get_value(hx711_data->gpiod_dout);

	if (val) {
		/*
		 * an examination with the oszilloscope indicated
		 * that the first value read after the reset is not stable
		 * if we reset too short;
		 * the shorter the reset cycle
		 * the less reliable the first value after reset is;
		 * there were no problems encountered with a value
		 * of 10 ms or higher
		 */
		gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
		msleep(10);
		gpiod_set_value(hx711_data->gpiod_pd_sck, 0);

		ret = hx711_wait_for_ready(hx711_data);
		if (ret)
			return ret;
		/*
		 * after a reset the gain is 128 so we do a dummy read
		 * to set the gain for the next read
		 */
		ret = hx711_read(hx711_data);
		if (ret < 0)
			return ret;

		/*
		 * after a dummy read we need to wait vor readiness
		 * for not mixing gain pulses with the clock
		 */
		ret = hx711_wait_for_ready(hx711_data);
		if (ret)
			return ret;
	}

	return val;
}
예제 #5
0
파일: main.c 프로젝트: wuwx/simba
int main()
{
    struct hx711_driver_t hx711;
    float weight_a_128;
    float weight_a_64;
    float weight_b_32;
    int res;

    sys_start();

    /* Initialize the driver. */
    std_printf(OSTR("Initializing the HX711 driver... "));

    res = hx711_init(&hx711,
                     &pin_d2_dev,
                     &pin_d3_dev,
                     SCALE,
                     OFFSET);

    if (res == 0) {
        std_printf(OSTR("done.\r\n"));
    } else {
        std_printf(OSTR("failed with %d.\r\n"), res);
        return (res);
    }

    /* Start the driver. */
    std_printf(OSTR("Starting the HX711 driver... "));

    res = hx711_start(&hx711);

    if (res == 0) {
        std_printf(OSTR("done.\r\n"));
    } else {
        std_printf(OSTR("failed with %d.\r\n"), res);
        return (res);
    }

    /* Continiously read samples from the device and print them. */
    while (1) {
        res = hx711_read(&hx711, &weight_a_128, hx711_channel_gain_a_128_t);

        if (res != 0) {
            std_printf(OSTR("Read 128 failed with %d.\r\n"), res);
        }

        res = hx711_read(&hx711, &weight_a_64, hx711_channel_gain_a_64_t);

        if (res != 0) {
            std_printf(OSTR("Read 64 failed with %d.\r\n"), res);
        }

        res = hx711_read(&hx711, &weight_b_32, hx711_channel_gain_b_32_t);

        if (res != 0) {
            std_printf(OSTR("Read 32 failed with %d.\r\n"), res);
        }

        /* Print the samples. */
        std_printf(OSTR("Weight channel A gain 128: %f\r\n"
                        "Weight channel A gain 64:  %f\r\n"
                        "Weight channel B gain 32:  %f\r\n"
                        "\r\n"),
                   weight_a_128,
                   weight_a_64,
                   weight_b_32);
    }

    return (0);
}