From d60cdb1d2e4b9194c78e52070868d4e61a1fb8a6 Mon Sep 17 00:00:00 2001 From: David Thornley Date: Thu, 9 Jul 2020 00:11:35 +1000 Subject: [PATCH] Added AM2320 support Ported raw from 8266 implementation, still using busy-wait for read function fix whitespace --- components/modules/Kconfig | 6 ++ components/modules/am2320.c | 139 ++++++++++++++++++++++++++++++++++++ docs/modules/am2320.md | 42 +++++++++++ mkdocs.yml | 1 + 4 files changed, 188 insertions(+) create mode 100644 components/modules/am2320.c create mode 100644 docs/modules/am2320.md diff --git a/components/modules/Kconfig b/components/modules/Kconfig index 574f0bfdd2..68a38c6203 100644 --- a/components/modules/Kconfig +++ b/components/modules/Kconfig @@ -54,6 +54,12 @@ config LUA_MODULE_ADC Includes the adc module. This module provides access to the adc1 hardware. +config LUA_MODULE_AM2320 + bool "AM2320 module" + default "n" + help + Support for AM2320 temp/humidity sensor + config LUA_MODULE_BIT bool "Bit module" default "n" diff --git a/components/modules/am2320.c b/components/modules/am2320.c new file mode 100644 index 0000000000..9b9f3f9064 --- /dev/null +++ b/components/modules/am2320.c @@ -0,0 +1,139 @@ +/* + * app/modules/am2320.c + * + * Copyright (c) 2016 Henk Vergonet + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "module.h" +#include "lauxlib.h" +#include "platform.h" +#include "lwip/udp.h" +#include + +static const uint32_t am2320_i2c_id = 0; +static const uint8_t am2320_i2c_addr = 0xb8 >> 1; + +static uint16_t crc16(uint8_t *ptr, unsigned int len) +{ + uint16_t crc =0xFFFF; + uint8_t i; + + while(len--) { + crc ^= *ptr++; + for(i=0;i<8;i++) { + if(crc & 0x01) { + crc >>= 1; + crc ^= 0xA001; + } else { + crc >>= 1; + } + } + } + return crc; +} + +/* make sure buf has lenght len+2 in order to accommodate for extra bytes */ +static int _read(uint32_t id, void *buf, uint8_t len, uint8_t off) +{ + int i; + uint8_t *b = (uint8_t *)buf; + uint16_t crc; + + // step 1: Wake sensor + platform_i2c_send_start(id); + platform_i2c_send_address(id, am2320_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER, 1); + ets_delay_us(800); + platform_i2c_send_stop(id); + + // step 2: Send read command + platform_i2c_send_start(id); + platform_i2c_send_address(id, am2320_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER, 1); + platform_i2c_send_byte(id, 0x03, 1); + platform_i2c_send_byte(id, off, 1); + platform_i2c_send_byte(id, len, 1); + platform_i2c_send_stop(id); + + // step 3: Read the data + ets_delay_us(1500); + platform_i2c_send_start(id); + platform_i2c_send_address(id, am2320_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER, 1); + ets_delay_us(30); + for(i=0; i