From 97eb3eea21e7ee5026bc3588b6c8c40c0d18eaa1 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 27 Sep 2022 20:48:29 +0300 Subject: [PATCH] add I2C, BMP180, MLX90640 --- BMP180/BMP180.c | 238 +++++++++++++ BMP180/BMP180.h | 50 +++ BMP180/Makefile | 57 +++ BMP180/bmp180.cflags | 1 + BMP180/bmp180.config | 2 + BMP180/bmp180.creator | 1 + BMP180/bmp180.creator.user | 174 ++++++++++ BMP180/bmp180.cxxflags | 1 + BMP180/bmp180.files | 5 + BMP180/bmp180.includes | 1 + BMP180/i2c.c | 249 +++++++++++++ BMP180/i2c.h | 39 +++ BMP180/main.c | 76 ++++ I2C/I2C.c | 208 +++++++++++ I2C/Makefile | 57 +++ I2C/i2c.cflags | 1 + I2C/i2c.config | 2 + I2C/i2c.creator | 1 + I2C/i2c.creator.user | 174 ++++++++++ I2C/i2c.cxxflags | 1 + I2C/i2c.files | 1 + I2C/i2c.includes | 0 MLX90640/Makefile | 58 ++++ MLX90640/cmdlnopts.c | 91 +++++ MLX90640/cmdlnopts.h | 33 ++ MLX90640/main.c | 95 +++++ MLX90640/mlx90640.c | 557 ++++++++++++++++++++++++++++++ MLX90640/mlx90640.h | 73 ++++ MLX90640/mlx90640_regs.h | 90 +++++ MLX90640/mxl90640OPi.cflags | 1 + MLX90640/mxl90640OPi.config | 4 + MLX90640/mxl90640OPi.creator | 1 + MLX90640/mxl90640OPi.creator.user | 174 ++++++++++ MLX90640/mxl90640OPi.cxxflags | 1 + MLX90640/mxl90640OPi.files | 6 + MLX90640/mxl90640OPi.includes | 2 + 36 files changed, 2525 insertions(+) create mode 100644 BMP180/BMP180.c create mode 100644 BMP180/BMP180.h create mode 100644 BMP180/Makefile create mode 100644 BMP180/bmp180.cflags create mode 100644 BMP180/bmp180.config create mode 100644 BMP180/bmp180.creator create mode 100644 BMP180/bmp180.creator.user create mode 100644 BMP180/bmp180.cxxflags create mode 100644 BMP180/bmp180.files create mode 100644 BMP180/bmp180.includes create mode 100644 BMP180/i2c.c create mode 100644 BMP180/i2c.h create mode 100644 BMP180/main.c create mode 100644 I2C/I2C.c create mode 100644 I2C/Makefile create mode 100644 I2C/i2c.cflags create mode 100644 I2C/i2c.config create mode 100644 I2C/i2c.creator create mode 100644 I2C/i2c.creator.user create mode 100644 I2C/i2c.cxxflags create mode 100644 I2C/i2c.files create mode 100644 I2C/i2c.includes create mode 100644 MLX90640/Makefile create mode 100644 MLX90640/cmdlnopts.c create mode 100644 MLX90640/cmdlnopts.h create mode 100644 MLX90640/main.c create mode 100644 MLX90640/mlx90640.c create mode 100644 MLX90640/mlx90640.h create mode 100644 MLX90640/mlx90640_regs.h create mode 100644 MLX90640/mxl90640OPi.cflags create mode 100644 MLX90640/mxl90640OPi.config create mode 100644 MLX90640/mxl90640OPi.creator create mode 100644 MLX90640/mxl90640OPi.creator.user create mode 100644 MLX90640/mxl90640OPi.cxxflags create mode 100644 MLX90640/mxl90640OPi.files create mode 100644 MLX90640/mxl90640OPi.includes diff --git a/BMP180/BMP180.c b/BMP180/BMP180.c new file mode 100644 index 0000000..0ecbb79 --- /dev/null +++ b/BMP180/BMP180.c @@ -0,0 +1,238 @@ +/* + * This file is part of the bmp180 project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#include +#include + +#include "i2c.h" +#include "BMP180.h" + +#define BMP180_CHIP_ID 0x55 + +/** + * BMP180 registers + */ +#define BMP180_REG_OXLSB (0xF8) +#define BMP180_REG_OLSB (0xF7) +#define BMP180_REG_OMSB (0xF6) +#define BMP180_REG_OUT (BMP180_REG_OMSB) +#define BMP180_REG_CTRLMEAS (0xF4) +#define BMP180_REG_SOFTRESET (0xE0) +#define BMP180_REG_ID (0xD0) +#define BMP180_REG_CALIB (0xAA) + +// shift for oversampling +#define BMP180_CTRLM_OSS_SHIFT (6) +// start measurement +#define BMP180_CTRLM_SCO (1<<5) +// write it to BMP180_REG_SOFTRESET for soft reset +#define BMP180_SOFTRESET_VAL (0xB6) +// start measurement of T/P +#define BMP180_READ_T (0x0E) +#define BMP180_READ_P (0x14) + +// delays in milliseconds +//#define BMP180_T_DELAY (2) + +static BMP180_oversampling bmp180_os = BMP180_OVERSMAX; + +static struct { + int16_t AC1; + int16_t AC2; + int16_t AC3; + uint16_t AC4; + uint16_t AC5; + uint16_t AC6; + int16_t B1; + int16_t B2; + int16_t MB; + int16_t MC; + int16_t MD; + int32_t MCfix; + int32_t AC1_fix; +} __attribute__ ((packed)) CaliData = {0}; + +static BMP180_status bmpstatus = BMP180_NOTINIT; +static uint8_t calidata_rdy = 0; +//static uint32_t milliseconds_start = 0; // time of measurement start +//static uint32_t p_delay = 8; // delay for P measurement +static uint8_t uncomp_data[3]; // raw uncompensated data +static int32_t Tval; // uncompensated T value +// compensated values: +static uint32_t Pmeasured; // Pa +static float Tmeasured; // degC +static uint8_t devID = 0; + +BMP180_status BMP180_get_status(){ + return bmpstatus; +} + +void BMP180_setOS(BMP180_oversampling os){ + bmp180_os = os & 0x03; +} + +// get compensation data, return 1 if OK +static int readcompdata(){ + FNAME(); + if(!i2c_read_data8(BMP180_REG_CALIB, sizeof(CaliData), (uint8_t*)&CaliData)) return FALSE; + // convert big-endian into little-endian + uint8_t *arr = (uint8_t*)&CaliData; + for(int i = 0; i < (int)sizeof(CaliData); i+=2){ + register uint8_t val = arr[i]; + arr[i] = arr[i+1]; + arr[i+1] = val; + } + // prepare for further calculations + CaliData.MCfix = CaliData.MC << 11; + CaliData.AC1_fix = CaliData.AC1 << 2; + calidata_rdy = 1; + DBG("Calibration rdy"); + return TRUE; +} + +// do a soft-reset procedure +int BMP180_reset(){ + if(!i2c_write_reg8(BMP180_REG_SOFTRESET, BMP180_SOFTRESET_VAL)){ + DBG("Can't reset\n"); + return 0; + } + return 1; +} + +// read compensation data & write registers +int BMP180_init(){ + bmpstatus = BMP180_NOTINIT; + if(!i2c_read_reg8(BMP180_REG_ID, &devID)){ + DBG("Can't read BMP180_REG_ID"); + return FALSE; + } + DBG("Got device ID: 0x%02x", devID); + if(devID != BMP180_CHIP_ID){ + DBG("Not BMP180\n"); + return FALSE; + } + if(!readcompdata()){ + DBG("Can't read calibration data\n"); + return FALSE; + }else{ + DBG("AC1=%d, AC2=%d, AC3=%d, AC4=%u, AC5=%u, AC6=%u", CaliData.AC1, CaliData.AC2, CaliData.AC3, CaliData.AC4, CaliData.AC5, CaliData.AC6); + DBG("B1=%d, B2=%d", CaliData.B1, CaliData.B2); + DBG("MB=%d, MC=%d, MD=%d", CaliData.MB, CaliData.MC, CaliData.MD); + } + return TRUE; +} + +// @return 1 if OK, *devid -> BMP/BME +void BMP180_read_ID(uint8_t *devid){ + *devid = devID; +} + +// start measurement, @return 1 if all OK +int BMP180_start(){ + if(!calidata_rdy || bmpstatus == BMP180_BUSYT || bmpstatus == BMP180_BUSYP) return 0; + uint8_t reg = BMP180_READ_T | BMP180_CTRLM_SCO; + if(!i2c_write_reg8(BMP180_REG_CTRLMEAS, reg)){ + DBG("Can't write CTRL reg\n"); + return 0; + } + bmpstatus = BMP180_BUSYT; + return 1; +} + + +// calculate T degC and P in Pa +static inline void compens(uint32_t Pval){ + // T: + int32_t X1 = ((Tval - CaliData.AC6)*CaliData.AC5) >> 15; + int32_t X2 = CaliData.MCfix / (X1 + CaliData.MD); + int32_t B5 = X1 + X2; + Tmeasured = (B5 + 8.) / 160.; + // P: + int32_t B6 = B5 - 4000; + X1 = (CaliData.B2 * ((B6*B6) >> 12)) >> 11; + X2 = (CaliData.AC2 * B6) >> 11; + int32_t X3 = X1 + X2; + int32_t B3 = (((CaliData.AC1_fix + X3) << bmp180_os) + 2) >> 2; + X1 = (CaliData.AC3 * B6) >> 13; + X2 = (CaliData.B1 * ((B6 * B6) >> 12)) >> 16; + X3 = ((X1 + X2) + 2) >> 2; + uint32_t B4 = (CaliData.AC4 * (uint32_t) (X3 + 32768)) >> 15; + uint32_t B7 = (uint32_t)((int32_t)Pval - B3) * (50000 >> bmp180_os); + int32_t p = 0; + if(B7 < 0x80000000){ + p = (B7 << 1) / B4; + }else{ + p = (B7 / B4) << 1; + } + X1 = p >> 8; + X1 *= X1; + X1 = (X1 * 3038) >> 16; + X2 = (-7357 * p) / 65536; + Pmeasured = p + ((X1 + X2 + 3791) / 16); +} + +static int still_measuring(){ + uint8_t reg; + if(!i2c_read_reg8(BMP180_REG_CTRLMEAS, ®)) return TRUE; + if(reg & BMP180_CTRLM_SCO){ + return TRUE; + } + return FALSE; +} + +void BMP180_process(){ + uint8_t reg; + if(bmpstatus != BMP180_BUSYT && bmpstatus != BMP180_BUSYP) return; + if(bmpstatus == BMP180_BUSYT){ // wait for temperature + if(still_measuring()) return; + // get uncompensated data + DBG("Read uncompensated T\n"); + if(!i2c_read_data8(BMP180_REG_OUT, 2, uncomp_data)){ + bmpstatus = BMP180_ERR; + return; + } + Tval = uncomp_data[0] << 8 | uncomp_data[1]; + DBG("Start P measuring\n"); + reg = BMP180_READ_P | BMP180_CTRLM_SCO | (bmp180_os << BMP180_CTRLM_OSS_SHIFT); + if(!i2c_write_reg8(BMP180_REG_CTRLMEAS, reg)){ + bmpstatus = BMP180_ERR; + return; + } + bmpstatus = BMP180_BUSYP; + }else{ // wait for pressure + if(still_measuring()) return; + DBG("Read uncompensated P\n"); + if(!i2c_read_data8(BMP180_REG_OUT, 3, uncomp_data)){ + bmpstatus = BMP180_ERR; + return; + } + uint32_t Pval = uncomp_data[0] << 16 | uncomp_data[1] << 8 | uncomp_data[2]; + Pval >>= (8 - bmp180_os); + // calculate compensated values + compens(Pval); + DBG("All data ready\n"); + bmpstatus = BMP180_RDY; // data ready + } +} + +// read data & convert it +void BMP180_getdata(float *T, uint32_t *P){ + *T = Tmeasured; + *P = Pmeasured; + bmpstatus = BMP180_RELAX; +} diff --git a/BMP180/BMP180.h b/BMP180/BMP180.h new file mode 100644 index 0000000..6995e4f --- /dev/null +++ b/BMP180/BMP180.h @@ -0,0 +1,50 @@ +/* + * This file is part of the BMP180 project. + * Copyright 2021 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#pragma once +#include + +#define BMP180_I2C_ADDRESS (0x77) + +typedef enum{ + BMP180_NOTINIT, // wasn't inited + BMP180_BUSYT, // T measurement in progress + BMP180_BUSYP, // P measurement in progress + BMP180_ERR, // error in I2C + BMP180_RELAX, // relaxed state + BMP180_RDY, // data ready - can get it +} BMP180_status; + +typedef enum{ + BMP180_OVERS_1 = 0, // oversampling is off + BMP180_OVERS_2 = 1, + BMP180_OVERS_4 = 2, + BMP180_OVERS_8 = 3, + BMP180_OVERSMAX = 4 +} BMP180_oversampling; + + +int BMP180_reset(); +int BMP180_init(); +void BMP180_read_ID(uint8_t *devid); +void BMP180_setOS(BMP180_oversampling os); +BMP180_status BMP180_get_status(); +int BMP180_start(); +void BMP180_process(); +void BMP180_getdata(float *T, uint32_t *P); + diff --git a/BMP180/Makefile b/BMP180/Makefile new file mode 100644 index 0000000..da08ae1 --- /dev/null +++ b/BMP180/Makefile @@ -0,0 +1,57 @@ +# run `make DEF=...` to add extra defines +PROGRAM := bmp180 +LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all +LDFLAGS += -lusefull_macros +SRCS := $(wildcard *.c) +DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111 +OBJDIR := mk +CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99 +OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o)) +DEPS := $(OBJS:.o=.d) +TARGFILE := $(OBJDIR)/TARGET +CC = gcc +#TARGET := RELEASE + +ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes) + TARGET := $(file < $(TARGFILE)) +else + TARGET := RELEASE +endif + +ifeq ($(TARGET), DEBUG) + .DEFAULT_GOAL := debug +endif + +release: $(PROGRAM) + +debug: CFLAGS += -DEBUG -Werror +debug: TARGET := DEBUG +debug: $(PROGRAM) + +$(TARGFILE): $(OBJDIR) + @echo -e "\t\tTARGET: $(TARGET)" + @echo "$(TARGET)" > $(TARGFILE) + +$(PROGRAM) : $(TARGFILE) $(OBJS) + @echo -e "\t\tLD $(PROGRAM)" + $(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM) + +$(OBJDIR): + @mkdir $(OBJDIR) + +ifneq ($(MAKECMDGOALS),clean) +-include $(DEPS) +endif + +$(OBJDIR)/%.o: %.c + @echo -e "\t\tCC $<" + $(CC) $< -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ + +clean: + @echo -e "\t\tCLEAN" + @rm -rf $(OBJDIR) 2>/dev/null || true + +xclean: clean + @rm -f $(PROGRAM) + +.PHONY: clean xclean diff --git a/BMP180/bmp180.cflags b/BMP180/bmp180.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/BMP180/bmp180.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/BMP180/bmp180.config b/BMP180/bmp180.config new file mode 100644 index 0000000..5047553 --- /dev/null +++ b/BMP180/bmp180.config @@ -0,0 +1,2 @@ +#define _XOPEN_SOURCE 9999 +#define _POSIX_C_SOURCE 333333L diff --git a/BMP180/bmp180.creator b/BMP180/bmp180.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/BMP180/bmp180.creator @@ -0,0 +1 @@ +[General] diff --git a/BMP180/bmp180.creator.user b/BMP180/bmp180.creator.user new file mode 100644 index 0000000..a4ddb0b --- /dev/null +++ b/BMP180/bmp180.creator.user @@ -0,0 +1,174 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + false + true + *.md, *.MD, Makefile + false + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + Builtin.BuildSystem + + true + true + Builtin.DefaultTidyAndClazy + 2 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /tmp/1/home/eddy/BMP180 + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/BMP180/bmp180.cxxflags b/BMP180/bmp180.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/BMP180/bmp180.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/BMP180/bmp180.files b/BMP180/bmp180.files new file mode 100644 index 0000000..5ead7c2 --- /dev/null +++ b/BMP180/bmp180.files @@ -0,0 +1,5 @@ +BMP180.c +BMP180.h +i2c.c +i2c.h +main.c diff --git a/BMP180/bmp180.includes b/BMP180/bmp180.includes new file mode 100644 index 0000000..9c558e3 --- /dev/null +++ b/BMP180/bmp180.includes @@ -0,0 +1 @@ +. diff --git a/BMP180/i2c.c b/BMP180/i2c.c new file mode 100644 index 0000000..beab927 --- /dev/null +++ b/BMP180/i2c.c @@ -0,0 +1,249 @@ +/* + * This file is part of the bmp180 project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static uint8_t lastaddr = 0; +static int I2Cfd = -1; + +/** + * @brief i2c_read_reg8 - read 8-bit addressed register (8 bit) + * @param regaddr - register address + * @param data - data read + * @return state + */ +int i2c_read_reg8(uint8_t regaddr, uint8_t *data){ + if(I2Cfd < 1) return FALSE; + struct i2c_smbus_ioctl_data args; + union i2c_smbus_data sd; + args.read_write = I2C_SMBUS_READ; + args.command = regaddr; + args.size = I2C_SMBUS_BYTE_DATA; + args.data = &sd; + if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){ + WARN("i2c_read_reg8, ioctl()"); + return FALSE; + } + if(data) *data = sd.byte; + return TRUE; +} + +/** + * @brief i2c_write_reg8 - write to 8-bit addressed register + * @param regaddr - address + * @param data - data + * @return state + */ +int i2c_write_reg8(uint8_t regaddr, uint8_t data){ + if(I2Cfd < 1) return FALSE; + struct i2c_smbus_ioctl_data args; + union i2c_smbus_data sd; + sd.byte = data; + args.read_write = I2C_SMBUS_WRITE; + args.command = regaddr; + args.size = I2C_SMBUS_BYTE_DATA; + args.data = &sd; + if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){ + WARN("i2c_write_reg8, ioctl()"); + return FALSE; + } + return TRUE; +} + +/** + * @brief i2c_read_reg16 - read 16-bit addressed register (to 16-bit data) + * @param regaddr - address + * @param data - data + * @return state + */ +int i2c_read_reg16(uint16_t regaddr, uint16_t *data){ + if(I2Cfd < 1) return FALSE; + struct i2c_msg m[2]; + struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2}; + m[0].addr = lastaddr; m[1].addr = lastaddr; + m[0].flags = 0; + m[1].flags = I2C_M_RD; + m[0].len = 2; m[1].len = 2; + uint8_t a[2], d[2] = {0}; + a[0] = regaddr >> 8; + a[1] = regaddr & 0xff; + m[0].buf = a; m[1].buf = d; + if(ioctl(I2Cfd, I2C_RDWR, &x) < 0){ + WARN("i2c_read_reg16, ioctl()"); + return FALSE; + } + if(data) *data = (uint16_t)((d[0] << 8) | (d[1])); + return TRUE; +} + +/** + * @brief i2c_write_reg16 - write 16-bit data value to 16-bit addressed register + * @param regaddr - address + * @param data - data to write + * @return state + */ +int i2c_write_reg16(uint16_t regaddr, uint16_t data){ + if(I2Cfd < 1) return FALSE; + union i2c_smbus_data d; + d.block[0] = 3; + d.block[1] = regaddr & 0xff; + d.block[2] = data >> 8; + d.block[3] = data & 0xff; + struct i2c_smbus_ioctl_data args; + args.read_write = I2C_SMBUS_WRITE; + args.command = regaddr >> 8; + args.size = I2C_SMBUS_I2C_BLOCK_DATA; + args.data = &d; + if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){ + WARN("i2c_write_reg16, ioctl()"); + return FALSE; + } +/* printf("Block: "); + for(int i = 0; i < 4; ++i) printf("0x%02x ", d.block[i]); + printf("\n");*/ + return TRUE; +} + +/** + * @brief i2c_set_slave_address - set current slave address + * @param addr - address + * @return state + */ +int i2c_set_slave_address(uint8_t addr){ + if(I2Cfd < 1) return FALSE; + if(ioctl(I2Cfd, I2C_SLAVE, addr) < 0){ + WARN("i2c_set_slave_address, ioctl()"); + return FALSE; + } + lastaddr = addr; + return TRUE; +} + +/** + * @brief i2c_open - open I2C device + * @param path - full path to device + * @return state + */ +int i2c_open(const char *path){ + if(I2Cfd > 0) close(I2Cfd); + I2Cfd = open(path, O_RDWR); + if(I2Cfd < 1){ + WARN("i2c_open, open()"); + return FALSE; + } + return TRUE; +} + +void i2c_close(){ + if(I2Cfd > 0) close(I2Cfd); +} + +#if 0 +// Don't work :( +/** + * @brief read_regN8 - read up to I2C_SMBUS_BLOCK_MAX bytes from 8-bit addressed register + * @param regaddr - address + * @param data - data to read + * @param N - amount of bytes + * @return state + */ +static int read_regN8(uint8_t regaddr, uint8_t *data, uint16_t N){ + if(I2Cfd < 1 || N > I2C_SMBUS_BLOCK_MAX || N == 0 || !data) return FALSE; + struct i2c_smbus_ioctl_data args = {0}; + union i2c_smbus_data sd = {0}; + sd.block[0] = N; + DBG("block: %d, %d, %d", sd.block[0], sd.block[1], sd.block[2]); + DBG("Try to get %d bytes from 0x%02x", N, regaddr); + args.read_write = I2C_SMBUS_READ; + args.command = regaddr; + args.size = I2C_SMBUS_BLOCK_DATA; + args.data = &sd; + if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){ + WARN("read_regN8, ioctl()"); + return FALSE; + } + DBG("block: %d, %d, %d", sd.block[0], sd.block[1], sd.block[2]); + memcpy(data, sd.block+1, N); + return TRUE; +} +#endif + +/** + * @brief read_data16 - read data from 16-bit addressed register + * @param regaddr - address + * @param N - amount of bytes + * @param array - data read + * @return state + */ +int i2c_read_data16(uint16_t regaddr, uint16_t N, uint8_t *array){ + if(I2Cfd < 1 || N == 0 || !array) return FALSE; + struct i2c_msg m[2]; + struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2}; + m[0].addr = lastaddr; m[1].addr = lastaddr; + m[0].flags = 0; + m[1].flags = I2C_M_RD; + m[0].len = 2; m[1].len = N; + uint8_t a[2]; + a[0] = regaddr >> 8; + a[1] = regaddr & 0xff; + m[0].buf = a; m[1].buf = array; + if(ioctl(I2Cfd, I2C_RDWR, &x) < 0){ + WARN("i2c_read_data16, ioctl()"); + return FALSE; + } + return TRUE; +} + +/** + * @brief read_data8 - read data from 8-bit addressed register + * @param regaddr - address + * @param N - amount of bytes + * @param array - data read + * @return state + */ +int i2c_read_data8(uint8_t regaddr, uint16_t N, uint8_t *array){ + if(I2Cfd < 1 || N < 1 || N+regaddr > 0xff || !array) return FALSE; +#if 0 + uint16_t rest = N; + do{ + uint8_t l = (rest > I2C_SMBUS_BLOCK_MAX) ? I2C_SMBUS_BLOCK_MAX : (uint8_t)rest; + if(!read_regN8(regaddr, array, l)){ + DBG("can't read"); + return FALSE; + } + rest -= l; + regaddr += l; + array += l; + }while(rest); +#endif + for(uint16_t i = 0; i < N; ++i){ + if(!i2c_read_reg8((uint8_t)(regaddr+i), array++)){ + DBG("can't read @%dth byte", i); + return FALSE; + } + } + return TRUE; +} diff --git a/BMP180/i2c.h b/BMP180/i2c.h new file mode 100644 index 0000000..936cbd7 --- /dev/null +++ b/BMP180/i2c.h @@ -0,0 +1,39 @@ +/* + * This file is part of the bmp180 project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#pragma once + +#include + +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef TRUE +#define TRUE 1 +#endif + +int i2c_open(const char *path); +void i2c_close(); +int i2c_set_slave_address(uint8_t addr); +int i2c_read_reg8(uint8_t regaddr, uint8_t *data); +int i2c_write_reg8(uint8_t regaddr, uint8_t data); +int i2c_read_data8(uint8_t regaddr, uint16_t N, uint8_t *array); +int i2c_read_reg16(uint16_t regaddr, uint16_t *data); +int i2c_write_reg16(uint16_t regaddr, uint16_t data); +int i2c_read_data16(uint16_t regaddr, uint16_t N, uint8_t *array); + diff --git a/BMP180/main.c b/BMP180/main.c new file mode 100644 index 0000000..2edca52 --- /dev/null +++ b/BMP180/main.c @@ -0,0 +1,76 @@ +/* + * This file is part of the bmp180 project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#include +#include +#include + +#include "BMP180.h" +#include "i2c.h" + +typedef struct{ + char *device; + int slaveaddr; + int help; +} glob_pars; + +static glob_pars G = {.device = "/dev/i2c-3", .slaveaddr = BMP180_I2C_ADDRESS}; + +static myoption cmdlnopts[] = { + {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), _("show this help")}, + {"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("I2C device path")}, + {"slave", NEED_ARG, NULL, 'a', arg_int, APTR(&G.slaveaddr), _("I2C slave address")}, + end_option +}; + + +int main(int argc, char **argv){ + initial_setup(); + parseargs(&argc, &argv, cmdlnopts); + if(G.help) showhelp(-1, cmdlnopts); + if(G.slaveaddr < 0 || G.slaveaddr > 0x7f) ERRX("I2C address should be 7-bit"); + if(!i2c_open(G.device)) ERR("Can't open %s", G.device); + if(!i2c_set_slave_address((uint8_t)G.slaveaddr)){ + WARN("Can't set slave address 0x%02x", G.slaveaddr); + goto clo; + } + if(!i2c_read_reg8(0, NULL)) ERR("Can't connect!"); + while(!BMP180_init()) sleep(1); + while(!BMP180_start()) sleep(1); + while (1){ + BMP180_process(); + BMP180_status s = BMP180_get_status(); + if(s == BMP180_RDY){ // data ready - get it + float T; + uint32_t P; + BMP180_getdata(&T, &P); + double mm = P * 0.00750062; + printf("T=%.1f, P=%dPa (%.1fmmHg)\n", T, P, mm); + sleep(5); + while(!BMP180_start()) usleep(1000); + }else if(s == BMP180_ERR){ + printf("Error in measurement\n"); + BMP180_reset(); + BMP180_init(); + } + } + +clo: + i2c_close(); + return 0; +} diff --git a/I2C/I2C.c b/I2C/I2C.c new file mode 100644 index 0000000..40d5b90 --- /dev/null +++ b/I2C/I2C.c @@ -0,0 +1,208 @@ +/* + * This file is part of the i2c project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct{ + char *device; + int slaveaddr; + int help; + int reg16; + int reg8; + int data2write; + int datalen; +} glob_pars; +static glob_pars G = {.device = "/dev/i2c-3", .slaveaddr = 0x33}; +static myoption cmdlnopts[] = { + {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), _("show this help")}, + {"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("I2C device path")}, + {"slave", NEED_ARG, NULL, 'a', arg_int, APTR(&G.slaveaddr), _("I2C slave address")}, + {"reg16", NEED_ARG, NULL, 'r', arg_int, APTR(&G.reg16), _("16-bit register address to read/write")}, + {"reg8", NEED_ARG, NULL, 'R', arg_int, APTR(&G.reg8), _("8-bit register address to read/write")}, + {"data", NEED_ARG, NULL, 'D', arg_int, APTR(&G.data2write),_("data to write")}, + {"len", NEED_ARG, NULL, 'l', arg_int, APTR(&G.datalen), _("length of data to read")}, + end_option +}; + + +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef TRUE +#define TRUE 1 +#endif + +static uint16_t lastaddr = 0; + +static int i2c_read_reg8(int fd, uint8_t regaddr, uint8_t *data){ + struct i2c_smbus_ioctl_data args; + union i2c_smbus_data sd; + args.read_write = I2C_SMBUS_READ; + args.command = regaddr; + args.size = I2C_SMBUS_BYTE_DATA; + args.data = &sd; + if(ioctl(fd, I2C_SMBUS, &args) < 0) return FALSE; + *data = sd.byte; + return TRUE; +} +static int i2c_write_reg8(int fd, uint8_t regaddr, uint8_t data){ + struct i2c_smbus_ioctl_data args; + union i2c_smbus_data sd; + sd.byte = data; + args.read_write = I2C_SMBUS_WRITE; + args.command = regaddr; + args.size = I2C_SMBUS_BYTE_DATA; + args.data = &sd; + if(ioctl(fd, I2C_SMBUS, &args) < 0) return FALSE; + return TRUE; +} +static int i2c_read_reg(int fd, uint16_t regaddr, uint16_t *data){ + struct i2c_msg m[2]; + struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2}; + m[0].addr = lastaddr; m[1].addr = lastaddr; + m[0].flags = 0; + m[1].flags = I2C_M_RD; + m[0].len = 2; m[1].len = 2; + uint8_t a[2], d[2] = {0}; + a[0] = regaddr >> 8; + a[1] = regaddr & 0xff; + m[0].buf = a; m[1].buf = d; + if(ioctl(fd, I2C_RDWR, &x) < 0) return FALSE; + *data = (uint16_t)((d[0] << 8) | (d[1])); + return TRUE; +} +static int i2c_write_reg(int fd, uint16_t regaddr, uint16_t data){ + union i2c_smbus_data d; + d.block[0] = 3; + d.block[1] = regaddr & 0xff; + d.block[2] = data >> 8; + d.block[3] = data & 0xff; + struct i2c_smbus_ioctl_data args; + args.read_write = I2C_SMBUS_WRITE; + args.command = regaddr >> 8; + args.size = I2C_SMBUS_I2C_BLOCK_DATA; + args.data = &d; + if(ioctl(fd, I2C_SMBUS, &args) < 0) return FALSE; + printf("Block: "); + for(int i = 0; i < 4; ++i) printf("0x%02x ", d.block[i]); + printf("\n"); + return TRUE; +} + + +static inline int i2c_set_slave_address(int fd, uint8_t addr){ + if(ioctl (fd, I2C_SLAVE, addr) < 0) return FALSE; + lastaddr = addr; + return TRUE; +} + +static inline int i2c_open(const char *path){ + return open(path, O_RDWR); +} + +int main(int argc, char **argv){ + uint16_t d; + uint8_t d8; + initial_setup(); + parseargs(&argc, &argv, cmdlnopts); + if(G.help) showhelp(-1, cmdlnopts); + if(G.slaveaddr < 0 || G.slaveaddr > 0x7f) ERRX("I2C address should be 7-bit"); + if(G.reg16 && G.reg8) ERRX("Enter either 8-bit address or 16-bit"); + int fd = i2c_open(G.device); + if(fd < 0) ERR("Can't open %s", G.device); + if(G.datalen){ + if(G.datalen < 0) ERRX("data length is uint16_t"); + if(G.datalen + G.reg16 > 0xffff) ERRX("Data len + start reg should be uint16_t"); + } + if(!i2c_set_slave_address(fd, (uint8_t)G.slaveaddr)){ + WARN("Can't set slave address 0x%02x", G.slaveaddr); + goto clo; + } + if(!i2c_read_reg8(fd, (uint16_t)0, &d8)){ + WARN("Can't find slave 0x%02x", G.slaveaddr); + goto clo; + } + green("Connected to slave 0x%02x\n", G.slaveaddr); + if(!G.reg8 && !G.reg16) goto clo; // nothing to do + if(G.data2write){ // write data to register + if(G.reg8){ + if(G.data2write < 0 || G.data2write > 0xff){ + WARNX("Data to write should be uint8_t"); + goto clo; + } + printf("Try to write 0x%02x to 0x%02x ... ", G.data2write, G.reg8); + if(!i2c_write_reg8(fd, (uint8_t)G.reg16, (uint8_t)G.data2write)){ + WARN("Can't write"); goto clo; + } + else printf("OK\n"); + }else{ + if(G.data2write < 0 || G.data2write > 0xffff){ + WARNX("Data to write should be uint16_t"); + goto clo; + } + printf("Try to write 0x%04x to 0x%04x ... ", G.data2write, G.reg16); + if(!i2c_write_reg(fd, (uint16_t)G.reg16, (uint16_t)G.data2write)){ + WARN("Can't write"); goto clo; + } + else printf("OK\n"); + } + } + if(!G.datalen){ + if(G.reg8){ + if(!i2c_read_reg8(fd, (uint8_t)G.reg8, &d8)){ + WARN("Can't read"); goto clo; + } + printf("Read: 0x%02x\n", d8); + }else{ + if(!i2c_read_reg(fd, (uint16_t)G.reg16, &d)){ + WARN("Can't read"); goto clo; + } + printf("Read: 0x%04x\n", d); + } + }else{ + int reg = (G.reg8) ? G.reg8 : G.reg16; + int lastreg = G.datalen + reg; + for(int i = reg; i < lastreg; ++i){ + if(G.reg8){ + if(!i2c_read_reg8(fd, (uint8_t)i, &d8)){ + WARN("Can't read"); continue; + } + printf("%2d: 0x%02x -> 0x%02x\n", i-reg, i, d8); + }else{ + if(!i2c_read_reg(fd, (uint16_t)i, &d)){ + WARN("Can't read"); continue; + } + printf("%4d: 0x%04x -> 0x%04x\n", i-G.reg16, i, d); + } + } + } +clo: + close(fd); + return 0; +} + diff --git a/I2C/Makefile b/I2C/Makefile new file mode 100644 index 0000000..f78cc78 --- /dev/null +++ b/I2C/Makefile @@ -0,0 +1,57 @@ +# run `make DEF=...` to add extra defines +PROGRAM := i2c +LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all +LDFLAGS += -lusefull_macros +SRCS := $(wildcard *.c) +DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111 +OBJDIR := mk +CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99 +OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o)) +DEPS := $(OBJS:.o=.d) +TARGFILE := $(OBJDIR)/TARGET +CC = gcc +#TARGET := RELEASE + +ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes) + TARGET := $(file < $(TARGFILE)) +else + TARGET := RELEASE +endif + +ifeq ($(TARGET), DEBUG) + .DEFAULT_GOAL := debug +endif + +release: $(PROGRAM) + +debug: CFLAGS += -DEBUG -Werror +debug: TARGET := DEBUG +debug: $(PROGRAM) + +$(TARGFILE): $(OBJDIR) + @echo -e "\t\tTARGET: $(TARGET)" + @echo "$(TARGET)" > $(TARGFILE) + +$(PROGRAM) : $(TARGFILE) $(OBJS) + @echo -e "\t\tLD $(PROGRAM)" + $(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM) + +$(OBJDIR): + @mkdir $(OBJDIR) + +ifneq ($(MAKECMDGOALS),clean) +-include $(DEPS) +endif + +$(OBJDIR)/%.o: %.c + @echo -e "\t\tCC $<" + $(CC) $< -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ + +clean: + @echo -e "\t\tCLEAN" + @rm -rf $(OBJDIR) 2>/dev/null || true + +xclean: clean + @rm -f $(PROGRAM) + +.PHONY: clean xclean diff --git a/I2C/i2c.cflags b/I2C/i2c.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/I2C/i2c.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/I2C/i2c.config b/I2C/i2c.config new file mode 100644 index 0000000..e0284f4 --- /dev/null +++ b/I2C/i2c.config @@ -0,0 +1,2 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 diff --git a/I2C/i2c.creator b/I2C/i2c.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/I2C/i2c.creator @@ -0,0 +1 @@ +[General] diff --git a/I2C/i2c.creator.user b/I2C/i2c.creator.user new file mode 100644 index 0000000..ca95ca4 --- /dev/null +++ b/I2C/i2c.creator.user @@ -0,0 +1,174 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + false + true + *.md, *.MD, Makefile + false + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + Builtin.BuildSystem + + true + true + Builtin.DefaultTidyAndClazy + 2 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /tmp/1/home/eddy/I2C + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/I2C/i2c.cxxflags b/I2C/i2c.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/I2C/i2c.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/I2C/i2c.files b/I2C/i2c.files new file mode 100644 index 0000000..6234cb7 --- /dev/null +++ b/I2C/i2c.files @@ -0,0 +1 @@ +I2C.c diff --git a/I2C/i2c.includes b/I2C/i2c.includes new file mode 100644 index 0000000..e69de29 diff --git a/MLX90640/Makefile b/MLX90640/Makefile new file mode 100644 index 0000000..7c8d54e --- /dev/null +++ b/MLX90640/Makefile @@ -0,0 +1,58 @@ +# run `make DEF=...` to add extra defines +PROGRAM := mlx +LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all +LDFLAGS += -lwiringPi -lusefull_macros -L/usr/local/lib -lm -lcrypt +SRCS := $(wildcard *.c) +DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111 +OBJDIR := mk +CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99 +OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o)) +DEPS := $(OBJS:.o=.d) +TARGFILE := $(OBJDIR)/TARGET +CC = gcc +#TARGET := RELEASE + + +ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes) + TARGET := $(file < $(TARGFILE)) +else + TARGET := RELEASE +endif + +ifeq ($(TARGET), DEBUG) + .DEFAULT_GOAL := debug +endif + +release: $(PROGRAM) + +debug: CFLAGS += -DEBUG -Werror +debug: TARGET := DEBUG +debug: $(PROGRAM) + +$(TARGFILE): $(OBJDIR) + @echo -e "\t\tTARGET: $(TARGET)" + @echo "$(TARGET)" > $(TARGFILE) + +$(PROGRAM) : $(TARGFILE) $(OBJS) + @echo -e "\t\tLD $(PROGRAM)" + $(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM) + +$(OBJDIR): + @mkdir $(OBJDIR) + +ifneq ($(MAKECMDGOALS),clean) +-include $(DEPS) +endif + +$(OBJDIR)/%.o: %.c + @echo -e "\t\tCC $<" + $(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $< + +clean: + @echo -e "\t\tCLEAN" + @rm -rf $(OBJDIR) 2>/dev/null || true + +xclean: clean + @rm -f $(PROGRAM) + +.PHONY: clean xclean diff --git a/MLX90640/cmdlnopts.c b/MLX90640/cmdlnopts.c new file mode 100644 index 0000000..72a1fba --- /dev/null +++ b/MLX90640/cmdlnopts.c @@ -0,0 +1,91 @@ +/* geany_encoding=koi8-r + * cmdlnopts.c - the only function that parse cmdln args and returns glob parameters + * + * Copyright 2013 Edward V. Emelianoff + * + * 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., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#include +#include +#include +#include +#include +#include "cmdlnopts.h" +#include "usefull_macros.h" + +/* + * here are global parameters initialisation + */ +static int help; +static glob_pars G; + +// default PID filename: +#define DEFAULT_PIDFILE "/tmp/testcmdlnopts.pid" +#define DEFAULT_I2C "/dev/i2c-3" +#define DEFAULT_ADDR 0x33 + +#define STR(X) #X + +// DEFAULTS +// default global parameters +static glob_pars const Gdefault = { + .addr = DEFAULT_ADDR, + .device = DEFAULT_I2C, + .pidfile = DEFAULT_PIDFILE, + .logfile = NULL // don't save logs +}; + +/* + * Define command line options by filling structure: + * name has_arg flag val type argptr help +*/ +static myoption cmdlnopts[] = { +// common options + {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")}, + {"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), _("file to save logs")}, + {"pidfile", NEED_ARG, NULL, 'P', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")}, + {"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("I2C device path (default: " DEFAULT_I2C ")")}, + {"address", NEED_ARG, NULL, 'a', arg_int, APTR(&G.addr), _("slave address (default:" STR(DEFAULT_ADDR) ")")}, + end_option +}; + +/** + * Parse command line options and return dynamically allocated structure + * to global parameters + * @param argc - copy of argc from main + * @param argv - copy of argv from main + * @return allocated structure with global parameters + */ +glob_pars *parse_args(int argc, char **argv){ + int i; + void *ptr; + ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr); + size_t hlen = 1024; + char helpstring[1024], *hptr = helpstring; + snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n"); + // format of help: "Usage: progname [args]\n" + change_helpstring(helpstring); + // parse arguments + parseargs(&argc, &argv, cmdlnopts); + if(help) showhelp(-1, cmdlnopts); + if(argc > 0){ + WARNX("Ignoring arguments:"); + for (i = 0; i < argc; i++) + printf("\t%s\n", argv[i]); + } + return &G; +} + diff --git a/MLX90640/cmdlnopts.h b/MLX90640/cmdlnopts.h new file mode 100644 index 0000000..4abd390 --- /dev/null +++ b/MLX90640/cmdlnopts.h @@ -0,0 +1,33 @@ +/* + * This file is part of the mxl90640wPi project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#pragma once + +/* + * here are some typedef's for global data + */ +typedef struct{ + int addr; // slave address + char *device; // I2C device + char *pidfile; // name of PID file + char *logfile; // logging to this file +} glob_pars; + + +glob_pars *parse_args(int argc, char **argv); + diff --git a/MLX90640/main.c b/MLX90640/main.c new file mode 100644 index 0000000..68da319 --- /dev/null +++ b/MLX90640/main.c @@ -0,0 +1,95 @@ +/* + * This file is part of the mxl90640wPi project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#include +#include +#include +#include + +#include "cmdlnopts.h" +#include "mlx90640.h" + +static glob_pars *GP = NULL; + +void signals(int sig){ + if(sig){ + signal(sig, SIG_IGN); + DBG("Get signal %d, quit.\n", sig); + } + LOGERR("Exit with status %d", sig); + if(GP && GP->pidfile) // remove unnesessary PID file + unlink(GP->pidfile); + exit(sig); +} + +static double image[MLX_PIXNO]; +static double image2[MLX_PIXNO]; +static void pushima(const double *img){ + for(int i = 0; i < MLX_PIXNO; ++i){ + double val = *img++; + image[i] += val; + image2[i] += val*val; + } +} + +int main (int argc, char **argv){ + initial_setup(); + char *self = strdup(argv[0]); + GP = parse_args(argc, argv); + check4running(self, GP->pidfile); + FREE(self); + if(GP->addr < 0 || GP->addr > 0xff) ERRX("Wrong I2C address"); + if(GP->logfile) OPENLOG(GP->logfile, LOGLEVEL_ANY, 1); + + if(!mlx90640_init(GP->device, (uint8_t)GP->addr)) ERR("Can't open device"); + //mlx90640_dump_parameters(); +#define N 5 + double T0 = dtime(); + uint8_t simple = 2; + //for(uint8_t simple = 0; simple < 3; ++simple){ + memset(image, 0, sizeof(image)); + memset(image2, 0, sizeof(image)); + for(int i = 0; i < N; ++i){ + double *ima = NULL; + if(!mlx90640_take_image(simple, &ima) || !ima) ERRX("Can't take image"); + pushima(ima); + printf("Got image %d, T=%g\n", i, dtime() - T0); + } + double *im = image, *im2 = image2; + green("\nImage (simple=%d):\n", simple); + for(int row = 0; row < MLX_H; ++row){ + for(int col = 0; col < MLX_W; ++col){ + double v = *im++, v2 = *im2; + v /= N; v2 /= N; + printf("%5.1f ", v); + *im2++ = v2 - v*v; + } + printf("\n"); + } + + green("\nRMS:\n"); + im2 = image2; + for(int row = 0; row < MLX_H; ++row){ + for(int col = 0; col < MLX_W; ++col){ + printf("%5.1f ", *im2++); + } + printf("\n"); + } + //} + return 0; +} diff --git a/MLX90640/mlx90640.c b/MLX90640/mlx90640.c new file mode 100644 index 0000000..0b258af --- /dev/null +++ b/MLX90640/mlx90640.c @@ -0,0 +1,557 @@ +/* + * This file is part of the mxl90640wPi project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mlx90640.h" +#include "mlx90640_regs.h" + + +static int I2Cfd = -1; +static uint8_t lastaddr = 0; +static MLX90640_params params; + +static uint16_t dataarray[MLX_DMA_MAXLEN]; // array for raw data from sensor +static double mlx_image[MLX_PIXNO]; // ready image + +#define CREG_VAL(reg) dataarray[CREG_IDX(reg)] +#define IMD_VAL(reg) dataarray[IMD_IDX(reg)] + +// reg_control values for subpage #0 and #1 +static const uint16_t reg_control_val[2] = { + REG_CONTROL_CHESS | REG_CONTROL_RES18 | REG_CONTROL_REFR_8HZ | REG_CONTROL_SUBPSEL | REG_CONTROL_DATAHOLD | REG_CONTROL_SUBPEN, + REG_CONTROL_CHESS | REG_CONTROL_RES18 | REG_CONTROL_REFR_8HZ | REG_CONTROL_SUBP1 | REG_CONTROL_SUBPSEL | REG_CONTROL_DATAHOLD | REG_CONTROL_SUBPEN +}; + + +static int errctr = 0; +static double Tlast = 0.; +#define chstate() do{errctr = 0; Tlast = dtime(); DBG("chstate()");}while(0) +#define chkerr() do{DBG("chkerr(), T=%g", dtime()-Tlast); if(++errctr > MLX_MAXERR_COUNT){ DBG("-> M_ERROR"); return FALSE;}else continue;}while(0) +#define chktmout() do{DBG("chktmout, T=%g", dtime()-Tlast); if(dtime() - Tlast > MLX_TIMEOUT){ DBG("Timeout! -> M_ERROR"); return FALSE;}else continue;}while(0) + + +// read register value +static int read_reg(uint16_t regaddr, uint16_t *data){ + if(I2Cfd < 1) return FALSE; + struct i2c_msg m[2]; + struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2}; + m[0].addr = lastaddr; m[1].addr = lastaddr; + m[0].flags = 0; + m[1].flags = I2C_M_RD; + m[0].len = 2; m[1].len = 2; + uint8_t a[2], d[2] = {0}; + a[0] = regaddr >> 8; + a[1] = regaddr & 0xff; + m[0].buf = a; m[1].buf = d; + if(ioctl(I2Cfd, I2C_RDWR, &x) < 0) return FALSE; + if(data) *data = (uint16_t)((d[0] << 8) | (d[1])); + return TRUE; +} + + +//#if 0 +// read N values starting from regaddr +static int read_regN(uint16_t regaddr, uint16_t *data, uint16_t N){ + if(I2Cfd < 1 || N > 128 || N == 0) return FALSE; + struct i2c_msg m[2]; + struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2}; + m[0].addr = lastaddr; m[1].addr = lastaddr; + m[0].flags = 0; + m[1].flags = I2C_M_RD; + m[0].len = 2; m[1].len = N * 2; + uint8_t a[2], d[256] = {0}; + a[0] = regaddr >> 8; + a[1] = regaddr & 0xff; + m[0].buf = a; m[1].buf = d; + if(ioctl(I2Cfd, I2C_RDWR, &x) < 0) return FALSE; + if(data) for(int i = 0; i < N; ++i){ + *data++ = (uint16_t)((d[2*i] << 8) | (d[2*i + 1])); + } + return TRUE; +} + +// blocking read N uint16_t values starting from `reg` +// @param reg - register to read +// @param N (io) - amount of bytes to read / bytes read +// @return `dataarray` or NULL if failed +static uint16_t *read_data(uint16_t reg, uint16_t *N){ + if(I2Cfd < 1 || !N || *N < 1) return NULL; + uint16_t n = *N; + if(n < 1 || n > MLX_DMA_MAXLEN) return NULL; + uint16_t *data = dataarray; + uint16_t got = 0, rest = *N; + do{ + uint8_t l = (rest > 128) ? 128 : (uint8_t)rest; + if(!read_regN(reg, data, l)){ + DBG("can't read"); + break; + } + rest -= l; + reg += l; + data += l; + got += l; + }while(rest); + *N = got; + return dataarray; +} +//#endif +#if 0 +// blocking read N uint16_t values starting from `reg` +// @param reg - register to read +// @param N (io) - amount of bytes to read / bytes read +// @return `dataarray` or NULL if failed +static uint16_t *read_data(uint16_t reg, uint16_t *N){ + if(I2Cfd < 1 || !N || *N < 1) return NULL; + uint16_t n = *N; + if(n < 1 || n > MLX_DMA_MAXLEN) return NULL; + uint16_t i, *data = dataarray; + for(i = 0; i < n; ++i){ + if(!read_reg(reg++, data++)){ + DBG("can't read"); + break; + } + } + *N = i; + return dataarray; +} +#endif + + +// write register value +static int write_reg(uint16_t regaddr, uint16_t data){ + if(I2Cfd < 1) return FALSE; + union i2c_smbus_data d; + d.block[0] = 3; + d.block[1] = regaddr & 0xff; + d.block[2] = data >> 8; + d.block[3] = data & 0xff; + struct i2c_smbus_ioctl_data args; + args.read_write = I2C_SMBUS_WRITE; + args.command = regaddr >> 8; + args.size = I2C_SMBUS_I2C_BLOCK_DATA; + args.data = &d; + if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0) return FALSE; + return TRUE; +} + +int mlx90640_set_slave_address(uint8_t addr){ + if(I2Cfd < 1) return FALSE; + if(ioctl (I2Cfd, I2C_SLAVE, addr) < 0) return FALSE; + lastaddr = addr; + return TRUE; +} + +static void dumpIma(double *im){ + for(int row = 0; row < MLX_H; ++row){ + for(int col = 0; col < MLX_W; ++col){ + printf("%5.1f ", *im++); + } + printf("\n"); + } +} + +void mlx90640_dump_parameters(){ + printf("kVdd=%d\nvdd25=%d\nKvPTAT=%g\nKtPTAT=%g\nvPTAT25=%d\n", params.kVdd, params.vdd25, params.KvPTAT, params.KtPTAT, params.vPTAT25); + printf("alphaPTAT=%g\ngainEE=%d\ntgc=%g\ncpKv=%g\ncpKta=%g\n", params.alphaPTAT, params.gainEE, params.tgc, params.cpKta, params.cpKta); + printf("KsTa=%g\nCT[]={%g, %g, %g}\n", params.KsTa, params.CT[0], params.CT[1], params.CT[2]); + printf("ksTo[]={"); for(int i = 0; i < 4; ++i) printf("%s%g", (i) ? ", " : "", params.ksTo[i]); printf("}\n"); + printf("alphacorr[]={"); for(int i = 0; i < 4; ++i) printf("%s%g", (i) ? ", " : "", params.alphacorr[i]); printf("}\n"); + printf("alpha[]=\n"); dumpIma(params.alpha); + printf("offset[]=\n"); dumpIma(params.offset); + printf("kta[]=\n"); dumpIma(params.kta); + printf("kv[]={"); for(int i = 0; i < 4; ++i) printf("%s%g", (i) ? ", " : "", params.kv[i]); printf("}\n"); + printf("cpAlpha[]={%g, %g}\n", params.cpAlpha[0], params.cpAlpha[1]); + printf("cpOffset[]={%d, %d}\n", params.cpOffset[0], params.cpOffset[1]); + printf("outliers[]=\n"); + uint8_t *o = params.outliers; + for(int row = 0; row < MLX_H; ++row){ + for(int col = 0; col < MLX_W; ++col){ + printf("%d ", *o++); + } + printf("\n"); + } +} + +/***************************************************************************** + Calculate parameters & values + *****************************************************************************/ + +// fill OCC/ACC row/col arrays +static void occacc(int8_t *arr, int l, uint16_t *regstart){ + int n = l >> 2; // divide by 4 + int8_t *p = arr; + for(int i = 0; i < n; ++i){ + register uint16_t val = *regstart++; + *p++ = (val & 0x000F) >> 0; + *p++ = (val & 0x00F0) >> 4; + *p++ = (val & 0x0F00) >> 8; + *p++ = (val ) >> 12; + } + for(int i = 0; i < l; ++i, ++arr){ + if(*arr > 0x07) *arr -= 0x10; + } +} + +// get all parameters' values from `dataarray`, return FALSE if something failed +static int get_parameters(){ + int8_t i8; + int16_t i16; + uint16_t *pu16; + uint16_t val = CREG_VAL(REG_VDD); + i8 = (int8_t) (val >> 8); + params.kVdd = i8 * 32; // keep sign + if(params.kVdd == 0) return FALSE; + i16 = val & 0xFF; + params.vdd25 = ((i16 - 0x100) * 32) - (1<<13); + val = CREG_VAL(REG_KVTPTAT); + i16 = (val & 0xFC00) >> 10; + if(i16 > 0x1F) i16 -= 0x40; + params.KvPTAT = (double)i16 / (1<<12); + i16 = (val & 0x03FF); + if(i16 > 0x1FF) i16 -= 0x400; + params.KtPTAT = (double)i16 / 8.; + params.vPTAT25 = (int16_t) CREG_VAL(REG_PTAT); + val = CREG_VAL(REG_APTATOCCS) >> 12; + params.alphaPTAT = val / 4. + 8.; + params.gainEE = (int16_t)CREG_VAL(REG_GAIN); + if(params.gainEE == 0) return FALSE; + int8_t occRow[MLX_H]; + int8_t occColumn[MLX_W]; + occacc(occRow, MLX_H, &CREG_VAL(REG_OCCROW14)); + occacc(occColumn, MLX_W, &CREG_VAL(REG_OCCCOL14)); + int8_t accRow[MLX_H]; + int8_t accColumn[MLX_W]; + occacc(accRow, MLX_H, &CREG_VAL(REG_ACCROW14)); + occacc(accColumn, MLX_W, &CREG_VAL(REG_ACCCOL14)); + val = CREG_VAL(REG_APTATOCCS); + // need to do multiplication instead of bitshift, so: + double occRemScale = 1<<(val&0x0F), + occColumnScale = 1<<((val>>4)&0x0F), + occRowScale = 1<<((val>>8)&0x0F); + int16_t offavg = (int16_t) CREG_VAL(REG_OSAVG); + // even/odd column/row numbers are for starting from 1, so for starting from 0 we chould swap them: + // even - for 1,3,5,...; odd - for 0,2,4,... etc + int8_t ktaavg[4]; + // 0 - odd row, odd col; 1 - odd row even col; 2 - even row, odd col; 3 - even row, even col + val = CREG_VAL(REG_KTAAVGODDCOL); + ktaavg[2] = (int8_t)(val & 0xFF); // odd col (1,3,..), even row (2,4,..) -> col 0,2,..; row 1,3,.. + ktaavg[0] = (int8_t)(val >> 8); // odd col, odd row -> col 0,2,..; row 0,2,.. + val = CREG_VAL(REG_KTAAVGEVENCOL); + ktaavg[3] = (int8_t)(val & 0xFF); // even col, even row -> col 1,3,..; row 1,3,.. + ktaavg[1] = (int8_t)(val >> 8); // even col, odd row -> col 1,3,..; row 0,2,.. + // so index of ktaavg is 2*(row&1)+(col&1) + val = CREG_VAL(REG_KTAVSCALE); + uint8_t scale1 = ((val & 0xFF)>>4) + 8, scale2 = (val&0xF); + if(scale1 == 0 || scale2 == 0) return FALSE; + double mul = (double)(1<> 12); + diva *= (double)(1<<30); // alpha_scale + double accRowScale = 1<<((val & 0x0f00)>>8), + accColumnScale = 1<<((val & 0x00f0)>>4), + accRemScale = 1<<(val & 0x0f); + pu16 = &CREG_VAL(REG_OFFAK1); + double *kta = params.kta, *offset = params.offset; + uint8_t *ol = params.outliers; + for(int row = 0; row < MLX_H; ++row){ + int idx = (row&1)<<1; + for(int col = 0; col < MLX_W; ++col){ + // offset + register uint16_t rv = *pu16++; + i16 = (rv & 0xFC00) >> 10; + if(i16 > 0x1F) i16 -= 0x40; + *offset++ = (double)offavg + (double)occRow[row]*occRowScale + (double)occColumn[col]*occColumnScale + (double)i16*occRemScale; + // kta + i16 = (rv & 0xF) >> 1; + if(i16 > 0x03) i16 -= 0x08; + *kta++ = (ktaavg[idx|(col&1)] + i16*mul) / div; + // alpha + i16 = (rv & 0x3F0) >> 4; + if(i16 > 0x1F) i16 -= 0x40; + double oft = (double)a_r + accRow[row]*accRowScale + accColumn[col]*accColumnScale +i16*accRemScale; + *a++ = oft / diva; + *ol++ = (rv&1) ? 1 : 0; + } + } + scale1 = (CREG_VAL(REG_KTAVSCALE) >> 8) & 0xF; // kvscale + div = (double)(1<> 12; if(i16 > 0x07) i16 -= 0x10; + ktaavg[0] = (int8_t)i16; // odd col, odd row + i16 = (val & 0xF0) >> 4; if(i16 > 0x07) i16 -= 0x10; + ktaavg[1] = (int8_t)i16; // even col, odd row + i16 = (val & 0x0F00) >> 8; if(i16 > 0x07) i16 -= 0x10; + ktaavg[2] = (int8_t)i16; // odd col, even row + i16 = val & 0x0F; if(i16 > 0x07) i16 -= 0x10; + ktaavg[3] = (int8_t)i16; // even col, even row + for(int i = 0; i < 4; ++i) params.kv[i] = ktaavg[i] / div; + val = CREG_VAL(REG_CPOFF); + params.cpOffset[0] = (val & 0x03ff); + if(params.cpOffset[0] > 0x1ff) params.cpOffset[0] -= 0x400; + params.cpOffset[1] = val >> 10; + if(params.cpOffset[1] > 0x1f) params.cpOffset[1] -= 0x40; + params.cpOffset[1] += params.cpOffset[0]; + val = ((CREG_VAL(REG_KTAVSCALE) & 0xF0) >> 4) + 8; + i8 = (int8_t)(CREG_VAL(REG_KVTACP) & 0xFF); + params.cpKta = (double)i8 / (1<> 8; + i16 = CREG_VAL(REG_KVTACP) >> 8; + if(i16 > 0x7F) i16 -= 0x100; + params.cpKv = (double)i16 / (1< 0x7F) i16 -= 0x100; + params.tgc = (double)i16; + params.tgc /= 32.; + val = (CREG_VAL(REG_SCALEACC)>>12); // alpha_scale_CP + i16 = CREG_VAL(REG_ALPHA)>>10; // cp_P1_P0_ratio + if(i16 > 0x1F) i16 -= 0x40; + div = (double)(1<> 8); + params.KsTa = (double)i8/(1<<13); + div = 1<<((CREG_VAL(REG_CT34) & 0x0F) + 8); // kstoscale + DBG("kstoscale=%g (regct34=0x%04x)", div, CREG_VAL(REG_CT34)); + val = CREG_VAL(REG_KSTO12); + DBG("ksto12=0x%04x", val); + i8 = (int8_t)(val & 0xFF); + DBG("To1ee=%d", i8); + params.ksTo[0] = i8 / div; + i8 = (int8_t)(val >> 8); + DBG("To2ee=%d", i8); + params.ksTo[1] = i8 / div; + val = CREG_VAL(REG_KSTO34); + DBG("ksto34=0x%04x", val); + i8 = (int8_t)(val & 0xFF); + DBG("To3ee=%d", i8); + params.ksTo[2] = i8 / div; + i8 = (int8_t)(val >> 8); + DBG("To4ee=%d", i8); + params.ksTo[3] = i8 / div; + params.CT[0] = 0.; // 0degr - between ranges 1 and 2 + val = CREG_VAL(REG_CT34); + mul = ((val & 0x3000)>>12)*10.; // step + params.CT[1] = ((val & 0xF0)>>4)*mul; // CT3 - between ranges 2 and 3 + params.CT[2] = ((val & 0x0F00) >> 8)*mul + params.CT[1]; // CT4 - between ranges 3 and 4 + params.alphacorr[0] = 1./(1. + params.ksTo[0] * 40.); + params.alphacorr[1] = 1.; + params.alphacorr[2] = (1. + params.ksTo[1] * params.CT[1]); + params.alphacorr[3] = (1. + params.ksTo[2] * (params.CT[2] - params.CT[1])) * params.alphacorr[2]; + params.resolEE = (uint8_t)((CREG_VAL(REG_KTAVSCALE) & 0x3000) >> 12); + // Don't forget to check 'outlier' flags for wide purpose + return TRUE; +} + +/** + * @brief process_subpage - calculate all parameters from `dataarray` into `mlx_image` + * @param subpageno - number of subpage + * @param simpleimage == 0 - simplest, 1 - narrow range, 2 - extended range + */ +static void process_subpage(int subpageno, int simpleimage){ + DBG("\nprocess_subpage(%d)", subpageno); +#ifdef EBUG + chstate(); +#endif + double resol_corr = (double)(1<>10)); // calibrated resol/current resol + DBG("resolEE=%d, resolCur=%d", params.resolEE, ((reg_control_val[subpageno]&0x0C00)>>10)); + int16_t i16a = (int16_t)IMD_VAL(REG_IVDDPIX); + double dvdd = resol_corr*i16a - params.vdd25; + dvdd /= params.kVdd; + double dV = i16a - params.vdd25; + dV /= params.kVdd; + DBG("ram=%d, vdd25=%d, dvdd=%g, resol=%g", i16a, params.vdd25, dvdd, resol_corr); + DBG("Vd=%g", dvdd+3.3); + i16a = (int16_t)IMD_VAL(REG_ITAPTAT); + int16_t i16b = (int16_t)IMD_VAL(REG_ITAVBE); + double dTa = (double)i16a / (i16a * params.alphaPTAT + i16b); // vptatart + dTa *= (double)(1<<18); + dTa = (dTa / (1. + params.KvPTAT*dV) - params.vPTAT25); + dTa = dTa / params.KtPTAT; // without 25degr - Ta0 + DBG("Ta=%g", dTa+25.); + i16a = (int16_t)IMD_VAL(REG_IGAIN); + double Kgain = params.gainEE / (double)i16a; + DBG("Kgain=%g", Kgain); + double pixOS[2]; // pix_gain_CP_SPx + // 11.2.2.6.1 + pixOS[0] = ((int16_t)IMD_VAL(REG_ICPSP0))*Kgain; // pix_OS_CP_SPx + pixOS[1] = ((int16_t)IMD_VAL(REG_ICPSP1))*Kgain; + DBG("pixGain: %g/%g", pixOS[0], pixOS[1]); + for(int i = 0; i < 2; ++i){ // calc pixOS by gain + // 11.2.2.6.2 + pixOS[i] -= params.cpOffset[i]*(1. + params.cpKta*dTa)*(1. + params.cpKv*dvdd); + } + // now make first approximation to image + uint16_t pixno = 0; // current pixel number - for indexing in parameters etc + for(int row = 0; row < MLX_H; ++row){ + int idx = (row&1)<<1; // index for params.kv + for(int col = 0; col < MLX_W; ++col, ++pixno){ + uint8_t sp = (row&1)^(col&1); // subpage of current pixel + if(sp != subpageno) continue; + // 11.2.2.5.1 + double curval = (double)((int16_t)dataarray[pixno]) * Kgain; // gain compensation + // 11.2.2.5.3 + curval -= params.offset[pixno] * (1. + params.kta[pixno]*dTa) * + (1. + params.kv[idx|(col&1)]*dvdd); // add offset + // now `curval` is pix_OS == V_IR_emiss_comp + // 11.2.2.7 + double IRcompens = curval - params.tgc * pixOS[subpageno]; // IR_compensated + if(simpleimage == 0){ // ??? + curval = IRcompens; + /* + curval -= params.cpOffset[subpageno] * (1. - params.cpKta * dTa) * + (1. + params.cpKv * dvdd); // CP + curval = IRcompens - params.tgc * curval; // IR gradient compens + */ + }else{ + // 11.2.2.8 + double alphaComp = params.alpha[pixno] - params.tgc * params.cpAlpha[subpageno]; + alphaComp /= 1. + params.KsTa * dTa; + // 11.2.2.9: calculate To for basic range + double Tar = dTa + 273.15 + 25.; // Ta+273.15 + Tar = Tar*Tar*Tar*Tar; // T_aK4 (when \epsilon==1 this is T_{a-r} too) + double ac3 = alphaComp*alphaComp*alphaComp; + double Sx = ac3*IRcompens + alphaComp*ac3*Tar; + Sx = params.ksTo[1] * sqrt(sqrt(Sx)); + double To = IRcompens / (alphaComp * (1. - 273.15*params.ksTo[1]) + Sx) + Tar; + curval = sqrt(sqrt(To)) - 273.15; // To + // extended range + if(simpleimage == 2){ + int idx = 0; // range 1 by default + double ctx = -40.; + if(curval > params.CT[0] && curval < params.CT[1]){ // range 2 + idx = 1; ctx = params.CT[0]; + }else if(curval < params.CT[2]){ // range 3 + idx = 2; ctx = params.CT[1]; + }else{ // range 4 + idx = 3; ctx = params.CT[2]; + } + To = IRcompens / (alphaComp * params.alphacorr[idx] * (1. + params.ksTo[idx]*(curval - ctx))) + Tar; + curval = sqrt(sqrt(To)) - 273.15; + } + } + mlx_image[pixno] = curval; + } + } + DBG("Time: %g", dtime()-Tlast); +} + +static int process_readconf(){ + chstate(); + while(1){ + if(get_parameters()) return TRUE; + else chkerr(); + } +} +static int process_firstrun(){ + uint16_t reg, N; + write_reg(REG_CONTROL, REG_CONTROL_DEFAULT); + usleep(50); + write_reg(REG_CONTROL, REG_CONTROL_DEFAULT); + usleep(50); + chstate(); + while(1){ + if(write_reg(REG_CONTROL, reg_control_val[0]) + && read_reg(REG_CONTROL, ®)){ + DBG("REG_CTRL=0x%04x, T=%g", reg, dtime()-Tlast); + if(read_reg(REG_STATUS, ®)) DBG("REG_STATUS=0x%04x", reg); + N = REG_CALIDATA_LEN; + if(read_data(REG_CALIDATA, &N)){ + DBG("-> M_READCONF, T=%g", dtime()-Tlast); + return process_readconf(); + }else chkerr(); + }else chkerr(); + } + return FALSE; +} +// start image acquiring for next subpage +static int process_startima(int subpageno){ + chstate(); + DBG("startima(%d)", subpageno); + uint16_t reg, N; + while(1){ + // write `overwrite` flag twice + if(!write_reg(REG_CONTROL, reg_control_val[subpageno]) || + !write_reg(REG_STATUS, REG_STATUS_OVWEN) || + !write_reg(REG_STATUS, REG_STATUS_OVWEN)) chkerr(); + while(1){ + if(read_reg(REG_STATUS, ®)){ + if(reg & REG_STATUS_NEWDATA){ + DBG("got newdata: %g", dtime() - Tlast); + if(subpageno != (reg & REG_STATUS_SPNO)){ + DBG("wrong subpage number -> M_ERROR"); + return FALSE; + }else{ // all OK, run image reading + chstate(); + write_reg(REG_STATUS, 0); // clear rdy bit + N = MLX_PIXARRSZ; + if(read_data(REG_IMAGEDATA, &N) && N == MLX_PIXARRSZ){ + DBG("got readoutm N=%d: %g", N, dtime() - Tlast); + return TRUE; + }else chkerr(); + } + }else chktmout(); + }else chkerr(); + } + } + return FALSE; +} + +void mlx90640_restart(){ + memset(¶ms, 0, sizeof(params)); + process_firstrun(); +} + +// if state of MLX allows, make an image else return error +// @param simple ==1 for simplest image processing (without T calibration) +int mlx90640_take_image(uint8_t simple, double **image){ + if(I2Cfd < 1) return FALSE; + if(params.kVdd == 0){ // no parameters -> make first run + if(!process_firstrun()) return FALSE; + } + DBG("\n\n\n-> M_STARTIMA"); + for(int sp = 0; sp < 2; ++sp){ + if(!process_startima(sp)) return FALSE; // get first subpage + process_subpage(sp, simple); + } + if(image) *image = mlx_image; + return TRUE; +} + +int mlx90640_init(const char *dev, uint8_t ID){ + if(I2Cfd > 0) close(I2Cfd); + I2Cfd = open(dev, O_RDWR); + if(I2Cfd < 1) return FALSE; + if(!mlx90640_set_slave_address(ID)) return FALSE; + if(!read_reg(0, NULL)) return FALSE; + if(!process_firstrun()) return FALSE; + return TRUE; +} + diff --git a/MLX90640/mlx90640.h b/MLX90640/mlx90640.h new file mode 100644 index 0000000..4ac9790 --- /dev/null +++ b/MLX90640/mlx90640.h @@ -0,0 +1,73 @@ +/* + * This file is part of the mxl90640wPi project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#pragma once + +#include + +// timeout for reading operations, s +#define MLX_TIMEOUT 5. +// counter of errors, when > max -> M_ERROR +#define MLX_MAXERR_COUNT 10 +// wait after power off, s +#define MLX_POWOFF_WAIT 0.5 +// wait after power on, s +#define MLX_POWON_WAIT 2. + +// amount of pixels +#define MLX_W (32) +#define MLX_H (24) +#define MLX_PIXNO (MLX_W*MLX_H) +// pixels + service data +#define MLX_PIXARRSZ (MLX_PIXNO + 64) + +typedef struct{ + int16_t kVdd; + int16_t vdd25; + double KvPTAT; + double KtPTAT; + int16_t vPTAT25; + double alphaPTAT; + int16_t gainEE; + double tgc; + double cpKv; // K_V_CP + double cpKta; // K_Ta_CP + double KsTa; + double CT[3]; // range borders (0, 160, 320 degrC?) + double ksTo[4]; // K_S_To for each range * 273.15 + double alphacorr[4]; // Alpha_corr for each range + double alpha[MLX_PIXNO]; // full - with alpha_scale + double offset[MLX_PIXNO]; + double kta[MLX_PIXNO]; // full K_ta - with scale1&2 + double kv[4]; // full - with scale; 0 - odd row, odd col; 1 - odd row even col; 2 - even row, odd col; 3 - even row, even col + double cpAlpha[2]; // alpha_CP_subpage 0 and 1 + uint8_t resolEE; // resolution_EE + int16_t cpOffset[2]; + uint8_t outliers[MLX_PIXNO]; // outliers - bad pixels (if == 1) +} MLX90640_params; + +// default I2C address +#define MLX_DEFAULT_ADDR (0x33) +// max datalength by one read (in 16-bit values) +#define MLX_DMA_MAXLEN (832) + +void mlx90640_dump_parameters(); +int mlx90640_init(const char *dev, uint8_t ID); +int mlx90640_set_slave_address(uint8_t addr); +int mlx90640_take_image(uint8_t simple, double **image); +void mlx90640_restart(); diff --git a/MLX90640/mlx90640_regs.h b/MLX90640/mlx90640_regs.h new file mode 100644 index 0000000..4598e01 --- /dev/null +++ b/MLX90640/mlx90640_regs.h @@ -0,0 +1,90 @@ +/* + * This file is part of the mxl90640wPi project. + * Copyright 2022 Edward V. Emelianov . + * + * 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 3 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, see . + */ + +#pragma once + +#define REG_STATUS 0x8000 +#define REG_STATUS_OVWEN (1<<4) +#define REG_STATUS_NEWDATA (1<<3) +#define REG_STATUS_SPNO (1<<0) +#define REG_STATUS_SPMASK (3<<0) +#define REG_CONTROL 0x800D +#define REG_CONTROL_CHESS (1<<12) +#define REG_CONTROL_RES16 (0<<10) +#define REG_CONTROL_RES17 (1<<10) +#define REG_CONTROL_RES18 (2<<10) +#define REG_CONTROL_RES19 (3<<10) +#define REG_CONTROL_RESMASK (3<<10) +#define REG_CONTROL_REFR_05HZ (0<<7) +#define REG_CONTROL_REFR_1HZ (1<<7) +#define REG_CONTROL_REFR_2HZ (2<<7) +#define REG_CONTROL_REFR_4HZ (3<<7) +#define REG_CONTROL_REFR_8HZ (4<<7) +#define REG_CONTROL_REFR_16HZ (5<<7) +#define REG_CONTROL_REFR_32HZ (6<<7) +#define REG_CONTROL_REFR_64HZ (7<<7) +#define REG_CONTROL_SUBP1 (1<<4) +#define REG_CONTROL_SUBPMASK (3<<4) +#define REG_CONTROL_SUBPSEL (1<<3) +#define REG_CONTROL_DATAHOLD (1<<2) +#define REG_CONTROL_SUBPEN (1<<0) + +// default value +#define REG_CONTROL_DEFAULT (REG_CONTROL_CHESS|REG_CONTROL_RES18|REG_CONTROL_REFR_2HZ|REG_CONTROL_SUBPEN) + +// calibration data start & len +#define REG_CALIDATA 0x2410 +#define REG_CALIDATA_LEN 816 + +#define REG_APTATOCCS 0x2410 +#define REG_OSAVG 0x2411 +#define REG_OCCROW14 0x2412 +#define REG_OCCCOL14 0x2418 +#define REG_SCALEACC 0x2420 +#define REG_SENSIVITY 0x2421 +#define REG_ACCROW14 0x2422 +#define REG_ACCCOL14 0x2428 +#define REG_GAIN 0x2430 +#define REG_PTAT 0x2431 +#define REG_KVTPTAT 0x2432 +#define REG_VDD 0x2433 +#define REG_KVAVG 0x2434 +#define REG_ILCHESS 0x2435 +#define REG_KTAAVGODDCOL 0x2436 +#define REG_KTAAVGEVENCOL 0x2437 +#define REG_KTAVSCALE 0x2438 +#define REG_ALPHA 0x2439 +#define REG_CPOFF 0x243A +#define REG_KVTACP 0x243B +#define REG_KSTATGC 0x243C +#define REG_KSTO12 0x243D +#define REG_KSTO34 0x243E +#define REG_CT34 0x243F +#define REG_OFFAK1 0x2440 +// index of register in array (from REG_CALIDATA) +#define CREG_IDX(addr) ((addr)-REG_CALIDATA) + +#define REG_IMAGEDATA 0x0400 +#define REG_ITAVBE 0x0700 +#define REG_ICPSP0 0x0708 +#define REG_IGAIN 0x070A +#define REG_ITAPTAT 0x0720 +#define REG_ICPSP1 0x0728 +#define REG_IVDDPIX 0x072A +// indeg of register in array (from REG_IMAGEDATA) +#define IMD_IDX(addr) ((addr)-REG_IMAGEDATA) diff --git a/MLX90640/mxl90640OPi.cflags b/MLX90640/mxl90640OPi.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/MLX90640/mxl90640OPi.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/MLX90640/mxl90640OPi.config b/MLX90640/mxl90640OPi.config new file mode 100644 index 0000000..e93f8a9 --- /dev/null +++ b/MLX90640/mxl90640OPi.config @@ -0,0 +1,4 @@ +#define GNU_SOURCE 1 +#define _XOPEN_SOURCE 1111 +#define EBUG + diff --git a/MLX90640/mxl90640OPi.creator b/MLX90640/mxl90640OPi.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/MLX90640/mxl90640OPi.creator @@ -0,0 +1 @@ +[General] diff --git a/MLX90640/mxl90640OPi.creator.user b/MLX90640/mxl90640OPi.creator.user new file mode 100644 index 0000000..88d3f4b --- /dev/null +++ b/MLX90640/mxl90640OPi.creator.user @@ -0,0 +1,174 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + false + true + *.md, *.MD, Makefile + false + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + Builtin.BuildSystem + + true + true + Builtin.DefaultTidyAndClazy + 2 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /tmp/1/home/eddy/MLX90640_wiringPi + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/MLX90640/mxl90640OPi.cxxflags b/MLX90640/mxl90640OPi.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/MLX90640/mxl90640OPi.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/MLX90640/mxl90640OPi.files b/MLX90640/mxl90640OPi.files new file mode 100644 index 0000000..d70565e --- /dev/null +++ b/MLX90640/mxl90640OPi.files @@ -0,0 +1,6 @@ +cmdlnopts.c +cmdlnopts.h +main.c +mlx90640.c +mlx90640.h +mlx90640_regs.h diff --git a/MLX90640/mxl90640OPi.includes b/MLX90640/mxl90640OPi.includes new file mode 100644 index 0000000..774cf29 --- /dev/null +++ b/MLX90640/mxl90640OPi.includes @@ -0,0 +1,2 @@ +/usr/local/include +.