--- /dev/null
+/******************************************************************************
+ BNO055 Library
+ I2C Interface
+ Copyright (C) 2019 Walter Fetter Lages <w.fetter@ieee.org>
+
+ 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 3 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/>.
+
+*******************************************************************************/
+
+/** @file bno055_i2c.h
+ @brief BNO055 Library - I2C Interface
+*/
+
+#ifndef __BNO055_I2C_H__
+#define __BNO055_I2C_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <bno055.h>
+
+/** @Brief: Bus read
+ * @Return : Status of the read
+ * @param dev_addr : Device address of the sensor
+ * @param reg_addr : Address of the first register to be read
+ * @param reg_data : Data read from the sensor,
+ * @param cnt : Number of bytes to be read
+ */
+extern s8 BNO055_i2c_read(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt);
+
+/** @Brief: Bus write
+ * @Return : Status of the write
+ * @param dev_addr : Device address of the sensor
+ * @param reg_addr : Address of the first register to be written
+ * @param reg_data : Value to be written into the register
+ * @param cnt : Number of bytes to be written
+ */
+extern s8 BNO055_i2c_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt);
+
+/**
+ * @Brief: I2C initilization routine
+ * @Return: 0 on success, -1 on failure and set errno to indicate the error
+ * @param dev_name: I2C bus master device name
+ * @param dev_addr : Device address of the sensor
+ * @param bno055: pointer to a bno055 structure
+*/
+extern int BNO055_i2c_init(const char *dev_name,u8 dev_addr,struct bno055_t *bno055);
+
+/**
+ * @Brief: I2C close routine
+ * @Return: 0 on success, -1 on failure and set errno to indicate the error
+ * @param bno055: pointer to a bno055 structure
+*/
+extern int BNO055_i2c_close(struct bno055_t *bno055);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
TARGET=libbno055ifc.a
-SRCS=bno055_tty.c bno055_delay.c
+SRCS=bno055_tty.c bno055_delay.c bno055_i2c.c
FLAGS=-O2 -Wall -MMD
INCLUDE=-I. -I../include -I../../BNO055_driver
--- /dev/null
+/******************************************************************************
+ BNO055 Library
+ I2C Interface
+ Copyright (C) 2019 Walter Fetter Lages <w.fetter@ieee.org>
+
+ 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 3 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 <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <linux/i2c-dev.h>
+
+#include <bno055_delay.h>
+#include <bno055_i2c.h>
+
+#define BUFFER_LEN 128
+
+/* This makes the code non-reentrant,
+but the Bosch BNO055_driver is already non-reentrant */
+static int fd;
+
+int BNO055_i2c_init(const char *dev_name,u8 dev_addr,struct bno055_t *bno055)
+{
+ int fd;
+
+ if((fd=open(dev_name,O_RDWR))==-1) return -1;
+
+ bno055->bus_write=BNO055_i2c_write;
+ bno055->bus_read=BNO055_i2c_read;
+ bno055->delay_msec=BNO055_delay;
+ bno055->dev_addr=dev_addr;
+
+ return 0;
+}
+
+int BNO055_i2c_close(struct bno055_t *bno055)
+{
+ bno055->bus_write=NULL;
+ bno055->bus_read=NULL;
+ bno055->delay_msec=NULL;
+ bno055->dev_addr=0;
+
+ if(close(fd)) return -1;
+
+ return 0;
+}
+
+s8 BNO055_i2c_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
+{
+ u8 array[BUFFER_LEN];
+ int i=0;
+
+ if(ioctl(fd,I2C_SLAVE,dev_addr)==-1) return BNO055_ERROR;
+
+ array[0]=reg_addr;
+ for (i=0;i < cnt;i++) array[i+1]=*(reg_data+i);
+
+ if(write(fd,array,cnt+1)==-1) return BNO055_ERROR;
+
+ return BNO055_SUCCESS;
+}
+
+s8 BNO055_i2c_read(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
+{
+ if(ioctl(fd,I2C_SLAVE,dev_addr)==-1) return BNO055_ERROR;
+
+ if(write(fd,®_addr,sizeof reg_addr)==-1) return BNO055_ERROR;
+
+ if(read(fd,reg_data,cnt)==-1) return BNO055_ERROR;
+
+ return BNO055_SUCCESS;
+}
#include <unistd.h>
#include <bno055_tty.h>
+#include <bno055_i2c.h>
#include <bno055_units.h>
-
void usage_help(const char *name)
{
- fprintf(stderr,"Usage: %s -i i2c_address | -t tty_device\n",name);
+ fprintf(stderr,"Usage: %s <-i i2c_address | -t> device\n",name);
+ fprintf(stderr,"For I2C, device is something like /dev/i2c-0.\n");
+ fprintf(stderr,"For TTY, device is something like /dev/ttyUSB0.\n");
exit(EXIT_FAILURE);
}
s8 temp;
u8 stat_clk;
u8 clk_src;
+ char interface;
+ u8 dev_addr;
if(argc < 2) usage_help(argv[0]);
- while((opt=getopt(argc,argv,"i:t:")) != -1)
+ while((opt=getopt(argc,argv,"i:t")) != -1)
{
switch(opt)
{
case 'i':
/* I2C interface */
-// BNO055_i2c_init(optarg);
+ interface='i';
+ dev_addr=strtol(optarg,NULL,0);
+ if(BNO055_i2c_init(argv[optind],dev_addr,&bno055))
+ {
+ perror("Error initializing I2C interface");
+ return -errno;
+ }
break;
case 't':
/* TTY interface */
- if(BNO055_tty_init(optarg,&bno055))
+ interface='t';
+ if(BNO055_tty_init(argv[optind],&bno055))
{
- perror("Error initializing tty interface");
+ perror("Error initializing TTY interface");
return -errno;
}
break;
-
+
default:
usage_help(argv[0]);
}
}
-
+
if(bno055_init(&bno055) == BNO055_SUCCESS)
{
printf("Chip id=0x%02x\n",bno055.chip_id);
printf("Gravity=%G, %G, %G m/s^2\n",gravity.x/BNO055_GRAVITY_DIV_MSQ,gravity.y/BNO055_GRAVITY_DIV_MSQ,gravity.z/BNO055_GRAVITY_DIV_MSQ);
else fprintf(stderr,"Error reading gravity.\n");
- BNO055_tty_close(&bno055);
+ switch(interface)
+ {
+ case 't':
+ BNO055_tty_close(&bno055);
+ break;
+ case 'i':
+ BNO055_i2c_close(&bno055);
+ break;
+ }
return 0;
}