52 lines
1.4 KiB
C
52 lines
1.4 KiB
C
// common.c
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <sys/ioctl.h>
|
|
#include <linux/i2c-dev.h>
|
|
#include "common.h"
|
|
#include "i2c_linux.h"
|
|
|
|
/* Default Linux I2C bus and BME68x address (low = 0x76) :contentReference[oaicite:2]{index=2} */
|
|
#define DEFAULT_I2C_BUS "/dev/i2c-1"
|
|
#define DEFAULT_BME68X_ADDR BME68X_I2C_ADDR_LOW
|
|
|
|
int8_t bme68x_interface_init(struct bme68x_dev *bme, uint8_t intf)
|
|
{
|
|
if (intf != BME68X_I2C_INTF) {
|
|
return BME68X_E_INVALID_LENGTH;
|
|
}
|
|
// allocate space for our file-descriptor
|
|
int *fd = malloc(sizeof(int));
|
|
if (!fd) {
|
|
return BME68X_E_NULL_PTR;
|
|
}
|
|
*fd = i2c_open_bus(DEFAULT_I2C_BUS);
|
|
if (*fd < 0) {
|
|
free(fd);
|
|
return BME68X_E_COM_FAIL;
|
|
}
|
|
// tell the driver which slave address to use
|
|
if (ioctl(*fd, I2C_SLAVE, BME68X_I2C_ADDR_HIGH) < 0) {
|
|
perror("Failed to set I2C_SLAVE");
|
|
i2c_close_bus(*fd);
|
|
free(fd);
|
|
return BME68X_E_COM_FAIL;
|
|
}
|
|
bme->intf_ptr = fd;
|
|
bme->read = bme68x_i2c_read;
|
|
bme->write = bme68x_i2c_write;
|
|
bme->delay_us = bme68x_delay_us;
|
|
bme->intf = BME68X_I2C_INTF;
|
|
return BME68X_OK;
|
|
}
|
|
|
|
void bme68x_interface_deinit(struct bme68x_dev *bme)
|
|
{
|
|
if (bme->intf_ptr) {
|
|
int fd = *(int *)bme->intf_ptr;
|
|
i2c_close_bus(fd);
|
|
free(bme->intf_ptr);
|
|
bme->intf_ptr = NULL;
|
|
}
|
|
}
|