Commit 178835c1 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Add AM2320 driver

parent 8579b2a1
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -14,6 +14,11 @@ ifneq ($(findstring lm75,${drivers}), )
	COMMON_FLAGS += -DDRIVER_LM75
endif

ifneq ($(findstring am2320,${drivers}), )
	TARGETS += src/driver/am2320.cc
	COMMON_FLAGS += -DDRIVER_AM2320
endif

ifneq ($(findstring max44006,${drivers}), )
	TARGETS += src/driver/max44006.cc
	COMMON_FLAGS += -DDRIVER_MAX44006
+22 −0
Original line number Diff line number Diff line
#ifndef AM2320_H
#define AM2320_H

class AM2320 {
	private:
		AM2320(const AM2320 &copy);
		unsigned char const address;
		unsigned char txbuf[3];
		unsigned char rxbuf[8];

	public:
		AM2320(unsigned char const addr) : address(addr) {}

		void read();
		unsigned char getStatus();
		float getTemp();
		float getHumidity();
};

extern AM2320 am2320;

#endif
+14 −0
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@
#ifdef DRIVER_LM75
#include "driver/lm75.h"
#endif
#ifdef DRIVER_AM2320
#include "driver/am2320.h"
#endif
#ifdef DRIVER_MAX44009
#include "driver/max44009.h"
#endif
@@ -22,6 +25,17 @@ void loop(void)
	kout.printf_float(lm75.getTemp());
	kout << endl;
#endif
#ifdef DRIVER_AM2320
	am2320.read();
	if (am2320.getStatus() == 0) {
		kout.printf_float(am2320.getTemp());
		kout << " degC @ ";
		kout.printf_float(am2320.getHumidity());
		kout << " rel%" << endl;
	} else {
		kout << "AM2320 error " << dec << am2320.getStatus() << endl;
	}
#endif
#ifdef DRIVER_MAX44009
	kout.printf_float(max44009.getLux());
	kout << endl;

src/driver/am2320.cc

0 → 100644
+61 −0
Original line number Diff line number Diff line
#include "driver/am2320.h"
#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(DRIVER_SOFTI2C)
#include "driver/i2c.h"
#else
#include "driver/soft_i2c.h"
#endif
#include "arch.h"

void AM2320::read()
{
	txbuf[0] = 0;
	i2c.xmit(address, 1, txbuf, 0, rxbuf);
	arch.delay_ms(1);
	txbuf[0] = 3;
	txbuf[1] = 0;
	txbuf[2] = 4;
	rxbuf[3] = rxbuf[4] = rxbuf[5] = rxbuf[6] = 0;
	i2c.xmit(address, 3, txbuf, 0, rxbuf);
	arch.delay_ms(3);
	i2c.xmit(address, 0, txbuf, 8, rxbuf);
}

unsigned char AM2320::getStatus()
{
	if (rxbuf[0] != 3) {
		return 1;
	}
	if (rxbuf[1] != 4) {
		return 2;
	}
	unsigned short checksum = 0xffff;
	for (unsigned char i = 0; i < 6; i++) {
		checksum ^= rxbuf[i] & 0x00ff;
		for (unsigned char j = 0; j < 8; j++) {
			if (checksum & 0x0001) {
				checksum = (checksum >> 1) ^ 0xa001;
			} else {
				checksum >>= 1;
			}
		}
	}
	if ((rxbuf[6] != (checksum & 0x00ff)) || (rxbuf[7] != (checksum >> 8) & 0x00ff)) {
		return 3;
	}
	return 0;
}

float AM2320::getTemp()
{
	if (txbuf[5] & 0x80) {
		return (-256 * (rxbuf[4] & 0x7f) + rxbuf[5]) / 10.;
	}
	return (256 * rxbuf[4] + rxbuf[5]) / 10.;
}

float AM2320::getHumidity()
{
	return (rxbuf[2] * 256 + rxbuf[3]) / 10.;
}

AM2320 am2320(0x5c);