add I2C, BMP180, MLX90640

This commit is contained in:
Edward Emelianov
2022-09-27 20:48:29 +03:00
parent 7ad65ccc2f
commit 97eb3eea21
36 changed files with 2525 additions and 0 deletions

238
BMP180/BMP180.c Normal file
View File

@@ -0,0 +1,238 @@
/*
* This file is part of the bmp180 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <usefull_macros.h>
#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, &reg)) 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;
}

50
BMP180/BMP180.h Normal file
View File

@@ -0,0 +1,50 @@
/*
* This file is part of the BMP180 project.
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#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);

57
BMP180/Makefile Normal file
View File

@@ -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

1
BMP180/bmp180.cflags Normal file
View File

@@ -0,0 +1 @@
-std=c17

2
BMP180/bmp180.config Normal file
View File

@@ -0,0 +1,2 @@
#define _XOPEN_SOURCE 9999
#define _POSIX_C_SOURCE 333333L

1
BMP180/bmp180.creator Normal file
View File

@@ -0,0 +1 @@
[General]

174
BMP180/bmp180.creator.user Normal file
View File

@@ -0,0 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 6.0.0, 2022-09-25T19:34:55. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{7bd84e39-ca37-46d3-be9d-99ebea85bc0d}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
<valuemap type="QVariantMap">
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
<value type="QString" key="language">Cpp</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
</valuemap>
</valuemap>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
<value type="QString" key="language">QmlJS</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap>
</valuemap>
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="bool" key="EditorConfiguration.UseIndenter">false</value>
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">false</value>
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.CTest">false</value>
<value type="bool" key="AutoTest.Framework.Catch">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">2</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{65a14f9e-e008-4c1b-89df-4eaa4774b6e3}</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/tmp/1/home/eddy/BMP180</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
<value type="QString">all</value>
</valuelist>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Сборка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
<value type="QString">clean</value>
</valuelist>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Очистка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Очистка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Развёртывание</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Развёртывание</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.TargetCount</variable>
<value type="int">1</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
<value type="int">22</value>
</data>
<data>
<variable>Version</variable>
<value type="int">22</value>
</data>
</qtcreator>

1
BMP180/bmp180.cxxflags Normal file
View File

@@ -0,0 +1 @@
-std=c++17

5
BMP180/bmp180.files Normal file
View File

@@ -0,0 +1,5 @@
BMP180.c
BMP180.h
i2c.c
i2c.h
main.c

After

Width:  |  Height:  |  Size: 37 B

1
BMP180/bmp180.includes Normal file
View File

@@ -0,0 +1 @@
.

249
BMP180/i2c.c Normal file
View File

@@ -0,0 +1,249 @@
/*
* This file is part of the bmp180 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <usefull_macros.h>
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;
}

39
BMP180/i2c.h Normal file
View File

@@ -0,0 +1,39 @@
/*
* This file is part of the bmp180 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#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);

76
BMP180/main.c Normal file
View File

@@ -0,0 +1,76 @@
/*
* This file is part of the bmp180 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <usefull_macros.h>
#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;
}