-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg/bme680 driver: add support for I2C/SPI BME680 driver #10502
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
PKG_NAME=bme680_driver | ||
PKG_URL=https://github.com/BoschSensortec/BME680_driver | ||
PKG_VERSION=63bb5336db4659519860832be2738c685133aa33 | ||
PKG_LICENSE=BSD-3-Clause | ||
|
||
.PHONY: all | ||
|
||
all: git-download | ||
@cp Makefile.$(PKG_NAME) $(PKG_BUILDDIR)/Makefile | ||
"$(MAKE)" -C $(PKG_BUILDDIR) | ||
|
||
include $(RIOTBASE)/pkg/pkg.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MODULE = bme680_driver | ||
|
||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
USEMODULE += bme680_driver_contrib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
INCLUDES += -I$(PKGDIRBASE)/bme680_driver | ||
INCLUDES += -I$(RIOTPKG)/bme680_driver/include | ||
|
||
DIRS += $(RIOTPKG)/bme680_driver/contrib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# bme680_driver | ||
|
||
## Introduction | ||
[bme680](https://github.com/BoschSensortec/BME680_driver) is an I2C/SPI API for BME680 sensor. | ||
|
||
The library is written and maintained by Bosch Sensortec, it is platform independent, as long as the right drivers are available for the given MCU. | ||
|
||
In addition, this driver can use floating point if available on your MCU. By default, this package do not use it. | ||
|
||
## Usage | ||
Add `USEPKG += bme680_driver` in your Makefile and `#include "bme680_hal.h"` to your code. Refer to the code documentation for more information on the API. | ||
|
||
## RIOT-OS interface | ||
This package uses the original source with an added interface for RIOT-OS. | ||
I2C or SPI can be select by setting xxxx define in your code application. | ||
|
||
In order to use floating point, you can set the ENABLE_FP variable: | ||
`ENABLE_FP=1 make BOARD=stm32f4discovery -C tests/pkg_bme680_driver` | ||
|
||
The following callbacks add support for the included drivers via I2C and SPI peripherals: | ||
|
||
* `i2c_read_hal` | ||
* `i2c_write_hal` | ||
* `spi_read_hal` | ||
* `spi_write_hal` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MODULE := bme680_driver_contrib | ||
|
||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "bme680_hal.h" | ||
|
||
#ifdef MODULE_PERIPH_I2C | ||
#include "periph/i2c.h" | ||
#endif | ||
#ifdef MODULE_PERIPH_SPI | ||
#include "periph/spi.h" | ||
#endif | ||
|
||
#include "xtimer.h" | ||
|
||
#ifndef BME680_SPI_SPEED | ||
#define BME680_SPI_SPEED (SPI_CLK_1MHZ) | ||
#endif /* BME680_SPI_SPEED */ | ||
|
||
#ifndef BME6800_SPI_MODE | ||
#define BME680_SPI_MODE (SPI_MODE_0) | ||
#endif /* BME680_SPI_MODE */ | ||
|
||
#ifndef BME680_NSS_PIN | ||
#define BME680_NSS_PIN GPIO_PIN(PA, 5) | ||
#endif /* BME680_NSS_PIN */ | ||
|
||
#ifndef BME680_I2C_ADDR | ||
#define BME680_I2C_ADDR (0x76) | ||
#endif /* BME_I2C_ADDR */ | ||
|
||
void ms_sleep(uint32_t msleep) | ||
{ | ||
xtimer_usleep(msleep * US_PER_MS); | ||
} | ||
|
||
#ifdef MODULE_PERIPH_I2C | ||
int8_t i2c_read_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) | ||
{ | ||
uint8_t ret; | ||
i2c_acquire(I2C_DEV(dev_id)); | ||
ret = i2c_read_regs(I2C_DEV(dev_id), BME680_I2C_ADDR, reg_addr, data, len, 0); | ||
i2c_release(I2C_DEV(dev_id)); | ||
return ret; | ||
} | ||
|
||
int8_t i2c_write_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) | ||
{ | ||
uint8_t ret; | ||
i2c_acquire(I2C_DEV(dev_id)); | ||
ret = i2c_write_regs(I2C_DEV(dev_id), BME680_I2C_ADDR, reg_addr,data,len,0); | ||
i2c_release(I2C_DEV(dev_id)); | ||
return ret; | ||
} | ||
#endif /* MODULE_PERIPH_I2C */ | ||
|
||
#ifdef MODULE_PERIPH_SPI | ||
int8_t spi_read_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) | ||
{ | ||
unsigned int cpsr; | ||
cpsr = irq_disable(); | ||
|
||
gpio_clear(BME680_NSS_PIN); | ||
spi_acquire(SPI_DEV(dev_id), SPI_CS_UNDEF, BME680_SPI_MODE, BME680_SPI_SPEED); | ||
spi_transfer_regs(SPI_DEV(dev_id), SPI_CS_UNDEF, reg_addr, NULL, data, len); | ||
gpio_set(BME680_NSS_PIN); | ||
|
||
irq_restore(cpsr); | ||
spi_release(SPI_DEV(dev_id)); | ||
return 0; | ||
} | ||
|
||
int8_t spi_write_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) | ||
{ | ||
unsigned int cpsr; | ||
cpsr = irq_disable(); | ||
|
||
gpio_clear(BME680_NSS_PIN); | ||
spi_acquire(SPI_DEV(dev_id), SPI_CS_UNDEF, BME680_SPI_MODE, BME680_SPI_SPEED); | ||
spi_transfer_regs(SPI_DEV(dev_id), SPI_CS_UNDEF, reg_addr, data, NULL, len); | ||
gpio_set(BME680_NSS_PIN); | ||
|
||
irq_restore(cpsr); | ||
spi_release(SPI_DEV(dev_id)); | ||
return 0; | ||
} | ||
#endif /* MODULE_PERIPH_SPI */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @defgroup bme680_driver package driver for I2C/SPI BME680 sensor | ||
* @ingroup pkg | ||
* @brief Provides the Bosch Sensortec's BME680 gas sensor API | ||
* @see https://github.com/BoschSensortec/BME680_driver | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2018 Mesotic SAS | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup pkg_bme680-driver | ||
* @{ | ||
* | ||
* @file | ||
* @brief Abstraction layer for RIOT adaption | ||
* | ||
* @author Dylan Laduranty <[email protected]> | ||
*/ | ||
|
||
#ifndef BME680_HAL_H | ||
#define BME680_HAL_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef SPI_NSS_PIN | ||
#define SPI_NSS_PIN GPIO_PIN(PA, 05) | ||
#endif /* SPI_NSS_PIN */ | ||
|
||
void ms_sleep(uint32_t msleep); | ||
|
||
#ifdef MODULE_PERIPH_I2C | ||
|
||
int8_t i2c_read_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len); | ||
int8_t i2c_write_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len); | ||
#endif | ||
|
||
#ifdef MODULE_PERIPH_SPI | ||
|
||
int8_t spi_read_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len); | ||
int8_t spi_write_hal(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len); | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* BME680_HAL_H */ | ||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
include ../Makefile.tests_common | ||
|
||
BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-uno \ | ||
chronos msb-430 msb-430h nucleo-f031k6 telosb \ | ||
wsn430-v1_3b wsn430-v1_4 z1 | ||
|
||
USEMODULE += xtimer | ||
FEATURES_REQUIRED = periph_spi | ||
USEPKG += bme680_driver | ||
|
||
ifeq ($(ENABLE_FP),1) | ||
CFLAGS += -DBME680_FLOAT_POINT_COMPENSATION | ||
endif | ||
|
||
include $(RIOTBASE)/Makefile.include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# U8g2 Package Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Readme copied from U8g2 without updating contents to bme680 |
||
|
||
## About | ||
This is a test application for the U8g2 package. This package is a graphical display library, including display drivers. | ||
|
||
## Usage | ||
This test application will initialize the U8g2 to output on one of the following: | ||
|
||
* output to terminal virtual screen. | ||
* output to SDL virtual screen. | ||
* output to I2C graphics screen. | ||
* output to SPI graphics screen. | ||
|
||
Note: you may have to run `make clean` between different output modes. | ||
|
||
### Output to terminal | ||
To output to this virtual screen, supply `TEST_OUTPUT=1` to the `make` command. | ||
|
||
### Output to SDL | ||
To output to this virtual screen, supply `TEST_OUTPUT=2` to the `make` command. | ||
|
||
This is a native-only option and requires SDL (32-bits) to be installed. | ||
|
||
### Output to SPI | ||
To output to screen, supply `TEST_OUTPUT=3` to the `make` command. | ||
|
||
* `TEST_SPI` — The SPI device. | ||
* `TEST_PIN_CS` — If applicable, the CS pin. | ||
* `TEST_PIN_DC` — If applicable, the Command/Data pin. | ||
* `TEST_PIN_RESET` — If applicable, the reset pin. | ||
* `TEST_DISPLAY` — The used display driver (see https://github.com/olikraus/u8g2/wiki/u8g2setupc). Make sure you select a SPI compatible display. | ||
|
||
### Output to I2C | ||
To output to screen, supply `TEST_OUTPUT=4` to the `make` command. | ||
|
||
Furthermore, you can configure the following: | ||
|
||
* `TEST_I2C` — The I2C device. | ||
* `TEST_ADDR` — The address to write commands to. | ||
* `TEST_PIN_RESET` — If applicable, the reset pin. | ||
* `TEST_DISPLAY` — The used display driver (see https://github.com/olikraus/u8g2/wiki/u8g2setupc). Make sure you select an I2C compatible display. | ||
|
||
## Expected result | ||
The output of this test depends on the output mode and used hardware. If it works, the application cycles through three screens with the text: 'This is RIOT-OS'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright (C) 2018 Mesotic SAS | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup tests | ||
* @{ | ||
* | ||
* @file | ||
* @brief Test application for the bme680_driver package. | ||
* | ||
* @author Dylan Laduranty <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "bme680.h" | ||
#include "bme680_defs.h" | ||
#include "bme680_hal.h" | ||
|
||
#ifdef MODULE_PERIPH_SPI | ||
#include "periph/spi.h" | ||
#endif | ||
|
||
#include "xtimer.h" | ||
|
||
|
||
int main(void) | ||
{ | ||
struct bme680_dev dev; | ||
|
||
/* You may assign a chip select identifier to be handled later */ | ||
dev.dev_id = 0; | ||
|
||
/* amb_temp can be set to 25 prior to configuring the gas sensor | ||
* or by performing a few temperature readings without operating the gas sensor. | ||
*/ | ||
dev.amb_temp = 25; | ||
dev.delay_ms = ms_sleep; | ||
|
||
#ifdef MODULE_PERIPH_I2C | ||
dev.intf = BME680_I2C_INTF; | ||
dev.read = i2c_read_hal; | ||
dev.write = i2c_write_hal; | ||
#else | ||
dev.intf = BME680_SPI_INTF; | ||
dev.read = spi_read_hal; | ||
dev.write = spi_write_hal; | ||
spi_init_cs(SPI_DEV(dev.dev_id), SPI_NSS_PIN); | ||
#endif | ||
|
||
printf("Intialize BME680 sensor..."); | ||
if(bme680_init(&dev) != BME680_OK) { | ||
puts("failed"); | ||
} | ||
else { | ||
puts("OK"); | ||
} | ||
|
||
|
||
uint8_t set_required_settings; | ||
uint8_t rslt; | ||
/* Set the temperature, pressure and humidity settings */ | ||
dev.tph_sett.os_hum = BME680_OS_2X; | ||
dev.tph_sett.os_pres = BME680_OS_4X; | ||
dev.tph_sett.os_temp = BME680_OS_8X; | ||
dev.tph_sett.filter = BME680_FILTER_SIZE_3; | ||
|
||
/* Set the remaining gas sensor settings and link the heating profile */ | ||
dev.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; | ||
/* Create a ramp heat waveform in 3 steps */ | ||
dev.gas_sett.heatr_temp = 320; /* degree Celsius */ | ||
dev.gas_sett.heatr_dur = 150; /* milliseconds */ | ||
|
||
/* Select the power mode */ | ||
/* Must be set before writing the sensor configuration */ | ||
dev.power_mode = BME680_FORCED_MODE; | ||
|
||
/* Set the required sensor settings needed */ | ||
set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | ||
| BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL; | ||
|
||
/* Set the desired sensor configuration */ | ||
rslt = bme680_set_sensor_settings(set_required_settings, &dev); | ||
|
||
/* Set the power mode */ | ||
rslt = bme680_set_sensor_mode(&dev); | ||
|
||
/* Get the total measurement duration so as to sleep or wait till the | ||
* measurement is complete */ | ||
uint16_t meas_period; | ||
bme680_get_profile_dur(&meas_period, &dev); | ||
|
||
struct bme680_field_data data; | ||
|
||
while(1) | ||
{ | ||
dev.delay_ms(meas_period); /* Delay till the measurement is ready */ | ||
|
||
rslt = bme680_get_sensor_data(&data, &dev); | ||
if(!rslt) | ||
#ifndef BME680_FLOAT_POINT_COMPENSATION | ||
printf("T: %d degC, P: %ld hPa, H %ld ", data.temperature, | ||
data.pressure, data.humidity); | ||
/* Avoid using measurements from an unstable heating setup */ | ||
if(data.status & BME680_GASM_VALID_MSK) | ||
printf(", G: %ld ohms", data.gas_resistance); | ||
#else | ||
printf("T: %.2f degC, P: %.2f hPa, H %2.f ", data.temperature, | ||
data.pressure, data.humidity); | ||
/* Avoid using measurements from an unstable heating setup */ | ||
if(data.status & BME680_GASM_VALID_MSK) | ||
printf(", G: %f ohms", data.gas_resistance); | ||
#endif | ||
printf("\r\n"); | ||
|
||
/* Trigger the next measurement if you would like to read data out | ||
continuously */ | ||
if (dev.power_mode == BME680_FORCED_MODE) { | ||
rslt = bme680_set_sensor_mode(&dev); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be: