BMP280 driver: Forced mode. Soft reset.

This commit is contained in:
sheinz 2016-07-07 23:39:25 +03:00
parent 4a2679271e
commit 440ad67834
3 changed files with 93 additions and 5 deletions

View file

@ -8,12 +8,46 @@
#include "bmp280/bmp280.h" #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 scl_pin = 5;
const uint8_t sda_pin = 4; 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(&params);
params.mode = BMP280_MODE_FORCED;
while (1) {
while (!bmp280_init(&params, 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; bmp280_params_t params;
float pressure, temperature; float pressure, temperature;
@ -35,6 +69,7 @@ static void bmp280_task(void *pvParameters)
} }
} }
} }
#endif
void user_init(void) void user_init(void)
{ {
@ -45,5 +80,9 @@ void user_init(void)
printf("SDK version : %s\n", sdk_system_get_sdk_version()); printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV); 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
} }

View file

@ -163,6 +163,10 @@ bool bmp280_init(bmp280_params_t *params, uint8_t scl_pin, uint8_t sda_pin)
uint8_t oversampling_temp = uint8_t oversampling_temp =
(params->oversampling == BMP280_ULTRA_HIGH_RES) ? 2 : 1; (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) uint8_t ctrl = (oversampling_temp << 5) | (params->oversampling << 2)
| (params->mode); | (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() 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; return false;
} }
@ -255,3 +277,12 @@ bool bmp280_read(float *temperature, float *pressure)
return true; return true;
} }
bool bmp280_soft_reset()
{
if (!write_register8(BMP280_REG_RESET, BMP280_RESET_VALUE)) {
debug("Failed resetting sensor");
return false;
}
return true;
}

View file

@ -30,7 +30,7 @@
/** /**
* Uncomment to enable debug output. * Uncomment to enable debug output.
*/ */
#define BMP280_DEBUG // #define BMP280_DEBUG
/** /**
* BMP280 address is 0x77 if SDO pin is high, * BMP280 address is 0x77 if SDO pin is high,
@ -44,6 +44,7 @@
* Normal - Continues measurement. * Normal - Continues measurement.
*/ */
typedef enum { typedef enum {
BMP280_MODE_SLEEP = 0,
BMP280_MODE_FORCED = 1, BMP280_MODE_FORCED = 1,
BMP280_MODE_NORMAL = 3 BMP280_MODE_NORMAL = 3
} BMP280_Mode; } BMP280_Mode;
@ -95,7 +96,11 @@ typedef struct {
/** /**
* Initialize default parameters. * 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); 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(); 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. * Read compensated temperature and pressure data.
* Temperature in degrees Celsius.
* Pressure in Pascals.
*/ */
bool bmp280_read(float *temperature, float *pressure); bool bmp280_read(float *temperature, float *pressure);
/**
* Restart BMP280 module.
*/
bool bmp280_soft_reset();
#endif // __BMP280_H__ #endif // __BMP280_H__