From 440ad6783494fb072abbcbb96ff4ada5e21acf52 Mon Sep 17 00:00:00 2001 From: sheinz Date: Thu, 7 Jul 2016 23:39:25 +0300 Subject: [PATCH] BMP280 driver: Forced mode. Soft reset. --- examples/bmp280/bmp280_example.c | 43 ++++++++++++++++++++++++++++++-- extras/bmp280/bmp280.c | 33 +++++++++++++++++++++++- extras/bmp280/bmp280.h | 22 ++++++++++++++-- 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/examples/bmp280/bmp280_example.c b/examples/bmp280/bmp280_example.c index a9d6325..625a60e 100644 --- a/examples/bmp280/bmp280_example.c +++ b/examples/bmp280/bmp280_example.c @@ -8,12 +8,46 @@ #include "bmp280/bmp280.h" +// In forced mode user initiate measurement each time. +// In normal mode measurement is done continuously with specified standby time. +// #define MODE_FORCED const uint8_t scl_pin = 5; const uint8_t sda_pin = 4; +#ifdef MODE_FORCED +static void bmp280_task_forced(void *pvParameters) +{ + bmp280_params_t params; + float pressure, temperature; -static void bmp280_task(void *pvParameters) + bmp280_init_default_params(¶ms); + params.mode = BMP280_MODE_FORCED; + + while (1) { + while (!bmp280_init(¶ms, scl_pin, sda_pin)) { + printf("BMP280 initialization failed\n"); + vTaskDelay(1000 / portTICK_RATE_MS); + } + + while(1) { + vTaskDelay(1000 / portTICK_RATE_MS); + if (!bmp280_force_measurement()) { + printf("Failed initiating measurement\n"); + break; + } + while (bmp280_is_measuring()) {}; // wait for measurement to complete + + if (!bmp280_read(&temperature, &pressure)) { + printf("Temperature/pressure reading failed\n"); + break; + } + printf("Pressure: %.2f Pa, Temperature: %.2f C\n", pressure, temperature); + } + } +} +#else +static void bmp280_task_normal(void *pvParameters) { bmp280_params_t params; float pressure, temperature; @@ -35,6 +69,7 @@ static void bmp280_task(void *pvParameters) } } } +#endif void user_init(void) { @@ -45,5 +80,9 @@ void user_init(void) printf("SDK version : %s\n", sdk_system_get_sdk_version()); printf("GIT version : %s\n", GITSHORTREV); - xTaskCreate(bmp280_task, (signed char *)"bmp280_task", 256, NULL, 2, NULL); +#ifdef MODE_FORCED + xTaskCreate(bmp280_task_forced, (signed char *)"bmp280_task", 256, NULL, 2, NULL); +#else + xTaskCreate(bmp280_task_normal, (signed char *)"bmp280_task", 256, NULL, 2, NULL); +#endif } diff --git a/extras/bmp280/bmp280.c b/extras/bmp280/bmp280.c index 9d8939e..dc07bf5 100644 --- a/extras/bmp280/bmp280.c +++ b/extras/bmp280/bmp280.c @@ -163,6 +163,10 @@ bool bmp280_init(bmp280_params_t *params, uint8_t scl_pin, uint8_t sda_pin) uint8_t oversampling_temp = (params->oversampling == BMP280_ULTRA_HIGH_RES) ? 2 : 1; + if (params->mode == BMP280_MODE_FORCED) { + params->mode = BMP280_MODE_SLEEP; // initial mode for forced is sleep + } + uint8_t ctrl = (oversampling_temp << 5) | (params->oversampling << 2) | (params->mode); @@ -176,7 +180,25 @@ bool bmp280_init(bmp280_params_t *params, uint8_t scl_pin, uint8_t sda_pin) bool bmp280_force_measurement() { - // TODO: implement + uint8_t ctrl = read_register8(BMP280_REG_CTRL); + ctrl &= ~0b11; // clear two lower bits + ctrl |= BMP280_MODE_FORCED; + debug("Writing ctrl reg=%x", ctrl); + if (!write_register8(BMP280_REG_CTRL, ctrl)) { + debug("Failed starting forced mode"); + return false; + } + return true; +} + +bool bmp280_is_measuring() +{ + uint8_t status = read_register8(BMP280_REG_STATUS); + if (status & (1<<3)) { + debug("Status: measuring"); + return true; + } + debug("Status: idle"); return false; } @@ -255,3 +277,12 @@ bool bmp280_read(float *temperature, float *pressure) return true; } + +bool bmp280_soft_reset() +{ + if (!write_register8(BMP280_REG_RESET, BMP280_RESET_VALUE)) { + debug("Failed resetting sensor"); + return false; + } + return true; +} diff --git a/extras/bmp280/bmp280.h b/extras/bmp280/bmp280.h index fab5a8e..9307932 100644 --- a/extras/bmp280/bmp280.h +++ b/extras/bmp280/bmp280.h @@ -30,7 +30,7 @@ /** * Uncomment to enable debug output. */ -#define BMP280_DEBUG +// #define BMP280_DEBUG /** * BMP280 address is 0x77 if SDO pin is high, @@ -44,6 +44,7 @@ * Normal - Continues measurement. */ typedef enum { + BMP280_MODE_SLEEP = 0, BMP280_MODE_FORCED = 1, BMP280_MODE_NORMAL = 3 } BMP280_Mode; @@ -95,7 +96,11 @@ typedef struct { /** * Initialize default parameters. - * Default configuration is NORMAL mode. + * Default configuration: + * mode: NORAML + * filter: OFF + * oversampling: x4 + * standby time: 250ms */ void bmp280_init_default_params(bmp280_params_t *params); @@ -111,9 +116,22 @@ bool bmp280_init(bmp280_params_t *params, uint8_t scl_pin, uint8_t sda_pin); */ bool bmp280_force_measurement(); +/** + * Check if BMP280 is busy with measuring temperature/pressure. + * Return true if BMP280 is busy. + */ +bool bmp280_is_measuring(); + /** * Read compensated temperature and pressure data. + * Temperature in degrees Celsius. + * Pressure in Pascals. */ bool bmp280_read(float *temperature, float *pressure); +/** + * Restart BMP280 module. + */ +bool bmp280_soft_reset(); + #endif // __BMP280_H__