I2C bus upgrade (#432)
This commit is contained in:
		
							parent
							
								
									d100f42b1f
								
							
						
					
					
						commit
						b83c2629b9
					
				
					 56 changed files with 909 additions and 804 deletions
				
			
		| 
						 | 
				
			
			@ -23,7 +23,6 @@
 | 
			
		|||
 */
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include "bmp280.h"
 | 
			
		||||
#include "i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
#ifdef BMP280_DEBUG
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -66,37 +65,36 @@ void bmp280_init_default_params(bmp280_params_t *params)
 | 
			
		|||
    params->standby = BMP280_STANDBY_250;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool read_register16(uint8_t i2c_addr, uint8_t addr, uint16_t *value)
 | 
			
		||||
static bool read_register16(i2c_dev_t* dev, uint8_t addr, uint16_t *value)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t d[] = {0, 0};
 | 
			
		||||
    if (!i2c_slave_read(i2c_addr, &addr, d, sizeof(d))) {
 | 
			
		||||
    if (!i2c_slave_read(dev->bus, dev->addr, &addr, d, sizeof(d))) {
 | 
			
		||||
        *value = d[0] | (d[1] << 8);
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline int read_data(uint8_t i2c_addr, uint8_t addr, uint8_t *value, uint8_t len)
 | 
			
		||||
static inline int read_data(i2c_dev_t* dev, uint8_t addr, uint8_t *value, uint8_t len)
 | 
			
		||||
{
 | 
			
		||||
    return i2c_slave_read(i2c_addr, &addr, value, len);
 | 
			
		||||
    return i2c_slave_read(dev->bus, dev->addr, &addr, value, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool read_calibration_data(bmp280_t *dev)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t i2c_addr = dev->i2c_addr;
 | 
			
		||||
 | 
			
		||||
    if (read_register16(i2c_addr, 0x88, &dev->dig_T1) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x8a, (uint16_t *)&dev->dig_T2) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x8c, (uint16_t *)&dev->dig_T3) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x8e, &dev->dig_P1) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x90, (uint16_t *)&dev->dig_P2) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x92, (uint16_t *)&dev->dig_P3) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x94, (uint16_t *)&dev->dig_P4) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x96, (uint16_t *)&dev->dig_P5) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x98, (uint16_t *)&dev->dig_P6) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x9a, (uint16_t *)&dev->dig_P7) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x9c, (uint16_t *)&dev->dig_P8) &&
 | 
			
		||||
        read_register16(i2c_addr, 0x9e, (uint16_t *)&dev->dig_P9)) {
 | 
			
		||||
    if (read_register16(&dev->i2c_dev, 0x88, &dev->dig_T1) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x8a, (uint16_t *)&dev->dig_T2) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x8c, (uint16_t *)&dev->dig_T3) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x8e, &dev->dig_P1) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x90, (uint16_t *)&dev->dig_P2) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x92, (uint16_t *)&dev->dig_P3) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x94, (uint16_t *)&dev->dig_P4) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x96, (uint16_t *)&dev->dig_P5) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x98, (uint16_t *)&dev->dig_P6) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x9a, (uint16_t *)&dev->dig_P7) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x9c, (uint16_t *)&dev->dig_P8) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0x9e, (uint16_t *)&dev->dig_P9)) {
 | 
			
		||||
 | 
			
		||||
        debug("Calibration data received:");
 | 
			
		||||
        debug("dig_T1=%d", dev->dig_T1);
 | 
			
		||||
| 
						 | 
				
			
			@ -120,15 +118,14 @@ static bool read_calibration_data(bmp280_t *dev)
 | 
			
		|||
 | 
			
		||||
static bool read_hum_calibration_data(bmp280_t *dev)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t i2c_addr = dev->i2c_addr;
 | 
			
		||||
    uint16_t h4, h5;
 | 
			
		||||
 | 
			
		||||
    if (!read_data(i2c_addr, 0xa1, &dev->dig_H1, 1) &&
 | 
			
		||||
        read_register16(i2c_addr, 0xe1, (uint16_t *)&dev->dig_H2) &&
 | 
			
		||||
        !read_data(i2c_addr, 0xe3, &dev->dig_H3, 1) &&
 | 
			
		||||
        read_register16(i2c_addr, 0xe4, &h4) &&
 | 
			
		||||
        read_register16(i2c_addr, 0xe5, &h5) &&
 | 
			
		||||
        !read_data(i2c_addr, 0xe7, (uint8_t *)&dev->dig_H6, 1)) {
 | 
			
		||||
    if (!read_data(&dev->i2c_dev, 0xa1, &dev->dig_H1, 1) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0xe1, (uint16_t *)&dev->dig_H2) &&
 | 
			
		||||
        !read_data(&dev->i2c_dev, 0xe3, &dev->dig_H3, 1) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0xe4, &h4) &&
 | 
			
		||||
        read_register16(&dev->i2c_dev, 0xe5, &h5) &&
 | 
			
		||||
        !read_data(&dev->i2c_dev, 0xe7, (uint8_t *)&dev->dig_H6, 1)) {
 | 
			
		||||
        dev->dig_H4 = (h4 & 0x00ff) << 4 | (h4 & 0x0f00) >> 8;
 | 
			
		||||
        dev->dig_H5 = h5 >> 4;
 | 
			
		||||
        debug("Calibration data received:");
 | 
			
		||||
| 
						 | 
				
			
			@ -144,21 +141,20 @@ static bool read_hum_calibration_data(bmp280_t *dev)
 | 
			
		|||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int write_register8(uint8_t i2c_addr, uint8_t addr, uint8_t value)
 | 
			
		||||
static int write_register8(i2c_dev_t* dev, uint8_t addr, uint8_t value)
 | 
			
		||||
{
 | 
			
		||||
    return i2c_slave_write(i2c_addr, &addr, &value, 1);
 | 
			
		||||
    return i2c_slave_write(dev->bus, dev->addr, &addr, &value, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t i2c_addr = dev->i2c_addr;
 | 
			
		||||
 | 
			
		||||
    if (i2c_addr != BMP280_I2C_ADDRESS_0 && i2c_addr != BMP280_I2C_ADDRESS_1) {
 | 
			
		||||
    if (dev->i2c_dev.addr != BMP280_I2C_ADDRESS_0 && dev->i2c_dev.addr != BMP280_I2C_ADDRESS_1) {
 | 
			
		||||
        debug("Invalid I2C address");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (read_data(i2c_addr, BMP280_REG_ID, &dev->id, 1)) {
 | 
			
		||||
    if (read_data(&dev->i2c_dev, BMP280_REG_ID, &dev->id, 1)) {
 | 
			
		||||
        debug("Sensor not found");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -169,7 +165,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    // Soft reset.
 | 
			
		||||
    if (write_register8(i2c_addr, BMP280_REG_RESET, BMP280_RESET_VALUE)) {
 | 
			
		||||
    if (write_register8(&dev->i2c_dev, BMP280_REG_RESET, BMP280_RESET_VALUE)) {
 | 
			
		||||
        debug("Failed resetting sensor");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -177,7 +173,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
 | 
			
		|||
    // Wait until finished copying over the NVP data.
 | 
			
		||||
    while (1) {
 | 
			
		||||
        uint8_t status;
 | 
			
		||||
        if (!read_data(i2c_addr, BMP280_REG_STATUS, &status, 1) && (status & 1) == 0)
 | 
			
		||||
        if (!read_data(&dev->i2c_dev, BMP280_REG_STATUS, &status, 1) && (status & 1) == 0)
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -193,7 +189,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
 | 
			
		|||
 | 
			
		||||
    uint8_t config = (params->standby << 5) | (params->filter << 2);
 | 
			
		||||
    debug("Writing config reg=%x", config);
 | 
			
		||||
    if (write_register8(i2c_addr, BMP280_REG_CONFIG, config)) {
 | 
			
		||||
    if (write_register8(&dev->i2c_dev, BMP280_REG_CONFIG, config)) {
 | 
			
		||||
        debug("Failed configuring sensor");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -210,14 +206,14 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
 | 
			
		|||
        // Write crtl hum reg first, only active after write to BMP280_REG_CTRL.
 | 
			
		||||
        uint8_t ctrl_hum = params->oversampling_humidity;
 | 
			
		||||
        debug("Writing ctrl hum reg=%x", ctrl_hum);
 | 
			
		||||
        if (write_register8(i2c_addr, BMP280_REG_CTRL_HUM, ctrl_hum)) {
 | 
			
		||||
        if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL_HUM, ctrl_hum)) {
 | 
			
		||||
            debug("Failed controlling sensor");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    debug("Writing ctrl reg=%x", ctrl);
 | 
			
		||||
    if (write_register8(i2c_addr, BMP280_REG_CTRL, ctrl)) {
 | 
			
		||||
    if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL, ctrl)) {
 | 
			
		||||
        debug("Failed controlling sensor");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -228,12 +224,12 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
 | 
			
		|||
bool bmp280_force_measurement(bmp280_t *dev)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t ctrl;
 | 
			
		||||
    if (read_data(dev->i2c_addr, BMP280_REG_CTRL, &ctrl, 1))
 | 
			
		||||
    if (read_data(&dev->i2c_dev, BMP280_REG_CTRL, &ctrl, 1))
 | 
			
		||||
        return false;
 | 
			
		||||
    ctrl &= ~0b11;  // clear two lower bits
 | 
			
		||||
    ctrl |= BMP280_MODE_FORCED;
 | 
			
		||||
    debug("Writing ctrl reg=%x", ctrl);
 | 
			
		||||
    if (write_register8(dev->i2c_addr, BMP280_REG_CTRL, ctrl)) {
 | 
			
		||||
    if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL, ctrl)) {
 | 
			
		||||
        debug("Failed starting forced mode");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -243,7 +239,7 @@ bool bmp280_force_measurement(bmp280_t *dev)
 | 
			
		|||
bool bmp280_is_measuring(bmp280_t *dev)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t status;
 | 
			
		||||
    if (read_data(dev->i2c_addr, BMP280_REG_STATUS, &status, 1))
 | 
			
		||||
    if (read_data(&dev->i2c_dev, BMP280_REG_STATUS, &status, 1))
 | 
			
		||||
        return false;
 | 
			
		||||
    if (status & (1 << 3)) {
 | 
			
		||||
        debug("Status: measuring");
 | 
			
		||||
| 
						 | 
				
			
			@ -345,7 +341,7 @@ bool bmp280_read_fixed(bmp280_t *dev, int32_t *temperature,
 | 
			
		|||
 | 
			
		||||
    // Need to read in one sequence to ensure they match.
 | 
			
		||||
    size_t size = humidity ? 8 : 6;
 | 
			
		||||
    if (read_data(dev->i2c_addr, 0xf7, data, size)) {
 | 
			
		||||
    if (read_data(&dev->i2c_dev, 0xf7, data, size)) {
 | 
			
		||||
        debug("Failed reading");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,7 @@
 | 
			
		|||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include "i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
| 
						 | 
				
			
			@ -127,7 +128,7 @@ typedef struct {
 | 
			
		|||
    int16_t  dig_H5;
 | 
			
		||||
    int8_t   dig_H6;
 | 
			
		||||
 | 
			
		||||
    uint8_t  i2c_addr;  /* I2C address. */
 | 
			
		||||
    i2c_dev_t  i2c_dev;  /* I2C dev setting. */
 | 
			
		||||
    uint8_t  id;        /* Chip ID */
 | 
			
		||||
} bmp280_t;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue