From: Walter Fetter Lages Date: Tue, 5 Feb 2019 15:49:56 +0000 (-0200) Subject: Add I2C support. X-Git-Tag: f1~2^2~1 X-Git-Url: http://git.ece.ufrgs.br/?a=commitdiff_plain;h=37c90eaedf1b568fd708b147e6857efd11346fb3;p=bno055.git Add I2C support. --- diff --git a/include/bno055_i2c.h b/include/bno055_i2c.h new file mode 100644 index 0000000..ae8e162 --- /dev/null +++ b/include/bno055_i2c.h @@ -0,0 +1,75 @@ +/****************************************************************************** + BNO055 Library + I2C Interface + Copyright (C) 2019 Walter Fetter Lages + + 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 + . + +*******************************************************************************/ + +/** @file bno055_i2c.h + @brief BNO055 Library - I2C Interface +*/ + +#ifndef __BNO055_I2C_H__ +#define __BNO055_I2C_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +/** @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 diff --git a/lib/Makefile b/lib/Makefile index 558bc76..d3ef8fc 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,5 +1,5 @@ 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 diff --git a/lib/bno055_i2c.c b/lib/bno055_i2c.c new file mode 100644 index 0000000..0469daa --- /dev/null +++ b/lib/bno055_i2c.c @@ -0,0 +1,86 @@ +/****************************************************************************** + BNO055 Library + I2C Interface + Copyright (C) 2019 Walter Fetter Lages + + 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 + . + +*******************************************************************************/ + +#include +#include +#include +#include + +#include +#include + +#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; +} diff --git a/test/bno055_test.c b/test/bno055_test.c index ce1cc42..6a424e0 100644 --- a/test/bno055_test.c +++ b/test/bno055_test.c @@ -24,12 +24,14 @@ #include #include +#include #include - 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); } @@ -50,32 +52,41 @@ int main(int argc,char *argv[]) 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); @@ -176,7 +187,15 @@ int main(int argc,char *argv[]) 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; }