Example app for BME280 sensor driver
authorNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 15 Sep 2016 03:17:40 +0000 (05:17 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 15 Sep 2016 03:17:40 +0000 (05:17 +0200)
apps/base/i2c_bme280/Makefile [new file with mode: 0644]
apps/base/i2c_bme280/README [new file with mode: 0644]
apps/base/i2c_bme280/main.c [new file with mode: 0644]

diff --git a/apps/base/i2c_bme280/Makefile b/apps/base/i2c_bme280/Makefile
new file mode 100644 (file)
index 0000000..da44d1f
--- /dev/null
@@ -0,0 +1,12 @@
+# Makefile for "base" apps
+
+MODULE = $(shell basename $(shell cd .. && pwd && cd -))
+NAME = $(shell basename $(CURDIR))
+
+.PHONY: $(NAME).bin
+$(NAME).bin:
+       @make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@
+
+clean mrproper:
+       @make -C ../../.. --no-print-directory $@
+
diff --git a/apps/base/i2c_bme280/README b/apps/base/i2c_bme280/README
new file mode 100644 (file)
index 0000000..668eb65
--- /dev/null
@@ -0,0 +1,28 @@
+BME280 I2C Barometric, humidity and temperature sensor example
+
+Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
+
+
+/* ****************************************************************************
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ *************************************************************************** */
+
+This example shows the support of the BME280 I2C sensor.
+
+Periodic conversions (compensated values) are sent on UART0 using the
+following frame format :
+P: 986 hPa, T: 29,6 degC, H: 44,2 rH
+
+
diff --git a/apps/base/i2c_bme280/main.c b/apps/base/i2c_bme280/main.c
new file mode 100644 (file)
index 0000000..e061fe8
--- /dev/null
@@ -0,0 +1,163 @@
+/****************************************************************************
+ *   apps/base/i2c_bme280/main.c
+ *
+ * BME280 I2C Barometric, humidity and temperature sensor example
+ *
+ * Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ *************************************************************************** */
+
+
+#include "lib/stdint.h"
+#include "core/system.h"
+#include "core/systick.h"
+#include "core/watchdog.h"
+#include "core/pio.h"
+#include "lib/stdio.h"
+#include "lib/errno.h"
+#include "drivers/i2c.h"
+#include "drivers/serial.h"
+#include "drivers/gpio.h"
+
+#include "extdrv/bme280_humidity_sensor.h"
+
+
+#define MODULE_VERSION    0x01
+#define MODULE_NAME "E-Xanh Gardener"
+
+
+#define SELECTED_FREQ  FREQ_SEL_36MHz
+
+
+
+
+/***************************************************************************** */
+/* Pins configuration */
+/* Pins blocks are passed to set_pins() for pins configuration.
+ * Unused pin blocks can be removed safely with the corresponding set_pins() call
+ * All pins blocks may be safelly merged in a single block for single set_pins() call..
+ */
+const struct pio_config common_pins[] = {
+       /* UART 0 */
+       { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
+       { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
+       /* I2C 0 */
+       { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
+       { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
+       ARRAY_LAST_PIO,
+};
+
+
+
+
+/***************************************************************************** */
+/* Sensor */
+
+/* Note : 8bits address */
+#define BME280_ADDR     0xEC
+struct bme280_sensor_config bme280_sensor = {
+       .bus_num = I2C0,
+       .addr = BME280_ADDR,
+       .humidity_oversampling = BME280_OS_x16,
+       .temp_oversampling = BME280_OS_x16,
+       .pressure_oversampling = BME280_OS_x16,
+       .mode = BME280_NORMAL,
+       .standby_len = BME280_SB_62ms,
+       .filter_coeff = BME280_FILT_OFF,
+};
+
+void sensor_config(int uart_num)
+{
+       int ret = 0;
+
+       ret = bme280_configure(&bme280_sensor);
+       if (ret != 0) {
+               uprintf(uart_num, "Sensor config error: %d\n", ret);
+       }
+}
+
+void sensor_display(int uart_num)
+{
+       uint32_t pressure = 0, temp = 0;
+       uint16_t humidity = 0;
+       int ret = 0;
+
+       ret = bme280_sensor_read(&bme280_sensor, &pressure, &temp, &humidity);
+       if (ret != 0) {
+               uprintf(uart_num, "Sensor read error: %d\n", ret);
+       } else {
+               int comp_temp = 0;
+               uint32_t comp_pressure = 0;
+               uint32_t comp_humidity = 0;
+
+               comp_temp = bme280_compensate_temperature(&bme280_sensor, temp) / 10;
+               comp_pressure = bme280_compensate_pressure(&bme280_sensor, pressure) / 100;
+               comp_humidity = bme280_compensate_humidity(&bme280_sensor, humidity) / 10;
+               uprintf(uart_num, "P: %d hPa, T: %d,%02d degC, H: %d,%d rH\n",
+                               comp_pressure,
+                               comp_temp / 10,  (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
+                               comp_humidity / 10, comp_humidity % 10);
+       }
+}
+
+
+/***************************************************************************** */
+void system_init()
+{
+       system_set_default_power_state();
+       clock_config(SELECTED_FREQ);
+       set_pins(common_pins);
+       gpio_on();
+       /* System tick timer MUST be configured and running in order to use the sleeping
+        * functions */
+       systick_timer_on(1); /* 1ms */
+       systick_start();
+}
+
+/* Define our fault handler. This one is not mandatory, the dummy fault handler
+ * will be used when it's not overridden here.
+ * Note : The default one does a simple infinite loop. If the watchdog is deactivated
+ * the system will hang.
+ */
+void fault_info(const char* name, uint32_t len)
+{
+       uprintf(UART0, name);
+       while (1);
+}
+
+
+
+/***************************************************************************** */
+int main(void)
+{
+       system_init();
+       uart_on(UART0, 115200, NULL);
+
+
+       i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
+
+       /* Configure uv sensor */
+       sensor_config(UART0);
+
+       while (1) {
+               /* BME280 uv sensor */
+               msleep(200);
+               sensor_display(UART0);
+       }
+       return 0;
+}
+