Пример #1
0
//-----------------------------------------------------------------------------
// Send a 1-Wire write bit. Provide 10us recovery time.
//
void OWWriteBit(int bit)
{
	bbb_mmio_set_output(pin_);
        if (bit)
        {
                // Write '1' bit
                bbb_mmio_set_low(pin_); // Drives DQ low
                tickDelay(A);
                bbb_mmio_set_high(pin_); // Releases the bus
                tickDelay(B); // Complete the time slot and 10us recovery
        }
        else
        {
                // Write '0' bit
                bbb_mmio_set_low(pin_); // Drives DQ low
                tickDelay(C);
                bbb_mmio_set_high(pin_); // Releases the bus
                tickDelay(D);
        }
}
Пример #2
0
//-----------------------------------------------------------------------------
// Read a bit from the 1-Wire bus and return it. Provide 10us recovery time.
//
int OWReadBit(void)
{
        int result;
	bbb_mmio_set_output(pin_);
        bbb_mmio_set_low(pin_); // Drives DQ low
        tickDelay(A);
        bbb_mmio_set_high(pin_); // Releases the bus
        tickDelay(E);
	bbb_mmio_set_input(pin_);
        result = bbb_mmio_input(pin_) & 0x01; // Sample the bit value from the slave
        tickDelay(F); // Complete the time slot and 10us recovery

        return result;
}
Пример #3
0
//-----------------------------------------------------------------------------
// Generate a 1-Wire reset, return 1 if no presence detect was found,
// return 0 otherwise.
// (NOTE: Does not handle alarm presence from DS2404/DS1994)
//
int OWTouchReset(void)
{
        int result;
	bbb_mmio_set_output(pin_);
        tickDelay(G);
        bbb_mmio_set_low(pin_); // Drives DQ low
        tickDelay(H);
        bbb_mmio_set_high(pin_); // Releases the bus
        tickDelay(I);
	bbb_mmio_set_input(pin_);
        result = bbb_mmio_input(pin_) ^ 0x01; // Sample for presence pulse from slave
        tickDelay(J); // Complete the reset sequence recovery
        return result; // Return sample presence pulse result
}
Пример #4
0
int bbb_dht_read(int type, int gpio_base, int gpio_number, float* humidity, float* temperature)
{
	int i = 0;
	// Validate humidity and temperature arguments and set them to zero.
	if (humidity == NULL || temperature == NULL) {
		return DHT_ERROR_ARGUMENT;
	}
	*temperature = 0.0f;
	*humidity = 0.0f;

	// Store the count that each DHT bit pulse is low and high.
	// Make sure array is initialized to start at zero.
	int pulseCounts[DHT_PULSES*2] = {0};

	// Get GPIO pin and set it as an output.
	gpio_t pin;
	if (bbb_mmio_get_gpio(gpio_base, gpio_number, &pin) < 0) {
		return DHT_ERROR_GPIO;
	}
	bbb_mmio_set_output(pin);

	// Bump up process priority and change scheduler to try to try to make process more 'real time'.
	set_max_priority();

	// Set pin high for ~500 milliseconds.
	bbb_mmio_set_high(pin);
	sleep_milliseconds(500);

	// The next calls are timing critical and care should be taken
	// to ensure no unnecssary work is done below.

	// Set pin low for ~20 milliseconds.
	bbb_mmio_set_low(pin);
	busy_wait_milliseconds(20);

	// Set pin as input.
	bbb_mmio_set_input(pin);

	// Wait for DHT to pull pin low.
	uint32_t count = 0;
	while (bbb_mmio_input(pin)) {
		if (++count >= DHT_MAXCOUNT) {
			// Timeout waiting for response.
			set_default_priority();
			return DHT_ERROR_TIMEOUT;
		}
	}

	// Record pulse widths for the expected result bits.
	for (i=0; i < DHT_PULSES*2; i+=2) {
		// Count how long pin is low and store in pulseCounts[i]
		while (!bbb_mmio_input(pin)) {
			if (++pulseCounts[i] >= DHT_MAXCOUNT) {
				// Timeout waiting for response.
				set_default_priority();
				return DHT_ERROR_TIMEOUT;
			}
		}
		// Count how long pin is high and store in pulseCounts[i+1]
		while (bbb_mmio_input(pin)) {
			if (++pulseCounts[i+1] >= DHT_MAXCOUNT) {
				// Timeout waiting for response.
				set_default_priority();
				return DHT_ERROR_TIMEOUT;
			}
		}
	}

	// Done with timing critical code, now interpret the results.

	// Drop back to normal priority.
	set_default_priority();

	// Compute the average low pulse width to use as a 50 microsecond reference threshold.
	// Ignore the first two readings because they are a constant 80 microsecond pulse.
	uint32_t threshold = 0;
	for (i=2; i < DHT_PULSES*2; i+=2) {
		threshold += pulseCounts[i];
	}
	threshold /= DHT_PULSES-1;

	// Interpret each high pulse as a 0 or 1 by comparing it to the 50us reference.
	// If the count is less than 50us it must be a ~28us 0 pulse, and if it's higher
	// then it must be a ~70us 1 pulse.
	uint8_t data[5] = {0};
	for (i=3; i < DHT_PULSES*2; i+=2) {
		int index = (i-3)/16;
		data[index] <<= 1;
		if (pulseCounts[i] >= threshold) {
			// One bit for long pulse.
			data[index] |= 1;
		}
		// Else zero bit for short pulse.
	}

	// Useful debug info:
	//printf("Data: 0x%x 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3], data[4]);

	// Verify checksum of received data.
	if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
		if (type == DHT11) {
			// Get humidity and temp for DHT11 sensor.
			*humidity = (float)data[0];
			*temperature = (float)data[2];
		}
		else if (type == DHT22) {
			// Calculate humidity and temp for DHT22 sensor.
			*humidity = (data[0] * 256 + data[1]) / 10.0f;
			*temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f;
			if (data[2] & 0x80) {
				*temperature *= -1.0f;
			}
		}
		return DHT_SUCCESS;
	}
	else {
		return DHT_ERROR_CHECKSUM;
	}
}