Fix bug (extra fd) in BNO055_i2c_init().
authorWalter Fetter Lages <w.fetter@ieee.org>
Fri, 17 May 2019 04:56:22 +0000 (01:56 -0300)
committerWalter Fetter Lages <w.fetter@ieee.org>
Fri, 17 May 2019 04:56:22 +0000 (01:56 -0300)
.gitignore
README [new file with mode: 0644]
include/bno055_i2c.h
init.d/galileo-i2c [new file with mode: 0755]
lib/bno055_i2c.c
test/bno055_test.c
udev/rules.d/99-i2c-dev.rules [new file with mode: 0644]

index 0abb06e..226d90b 100644 (file)
@@ -2,6 +2,8 @@
 
 # Prerequisites
 *.d
+!init.d
+!rules.d
 
 # Object files
 *.o
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..e8e605d
--- /dev/null
+++ b/README
@@ -0,0 +1,18 @@
+For using the I2C interface, the user should have read and write access to
+/dev/i2c-0 (or the corresponding device if there is more than on I2C bus).
+
+The recomended way to grand the required premissions is to incude the user
+in the i2c group:
+
+groupadd -r i2c
+groupmems -g i2c -a <login>
+
+and set the /dev/i2c-0 read and write permissions to this group by
+installing the file 99-i2c-dev.rules in the /etc/udev/rules.d directory and
+reloading the udev rules and the i2c-dev module with the commands:
+
+udvadm control -R
+rmmod i2c-dev
+modprobe i2c-dev
+
+
index ae8e162..e9d5f2b 100644 (file)
@@ -16,7 +16,7 @@
         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
@@ -55,7 +55,7 @@ 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 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);
diff --git a/init.d/galileo-i2c b/init.d/galileo-i2c
new file mode 100755 (executable)
index 0000000..987d538
--- /dev/null
@@ -0,0 +1,63 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          galileo-i2c
+# Required-Start:
+# Should-Start:
+# Required-Stop:
+# Default-Start:     S
+# Default-Stop:
+# Short-Description: Configures the Galileo shield pins for I2C usage.
+# Description:       Configures the Galileo shield pins for I2C usage.
+### END INIT INFO
+
+case "$1" in
+    start|restart|force-reload)
+
+       # Pull-up on IO18
+       if [ ! -d /sys/class/gpio/gpio57 ] ; then
+               echo -n "57" > /sys/class/gpio/export
+       fi
+       echo "out" > /sys/class/gpio/gpio57/direction
+       echo "1" > /sys/class/gpio/gpio57/value
+
+       # Select I2C on IO18 and IO19
+       if [ ! -d /sys/class/gpio/gpio60 ] ; then
+               echo -n "60" > /sys/class/gpio/export
+       fi
+       echo "out" > /sys/class/gpio/gpio60/direction
+       echo "0" > /sys/class/gpio/gpio60/value
+
+       # Pull-up on IO19
+       if [ ! -d /sys/class/gpio/gpio59 ] ; then
+               echo -n "59" > /sys/class/gpio/export
+       fi
+       echo "out" > /sys/class/gpio/gpio59/direction
+       echo "1" > /sys/class/gpio/gpio59/value
+
+       chgrp i2c /dev/i2c-0
+       chmod g+rw /dev/i2c-0
+
+       ;;
+    stop)
+       # No pull-up on IO18
+       echo "in" > /sys/class/gpio/gpio57/direction
+       ecbo "57" > /sys/class/gpio/unexport
+
+       # GPIO on IO18 and IO19
+       echo "1" > /sys/class/gpio/gpio60/value
+       echo "60" > /sys/class/gpio/unexport
+
+       # No pull-up on  IO19
+       echo "in" > /sys/class/gpio/gpio59/direction
+       echo "59" > /sys/class/gpio/unexport
+       ;;
+    status)
+       ;;
+    *)
+       echo -n "Usage: $0 "
+       echo "{start|stop|restart|force-reload|status}"
+       exit 1
+       ;;
+esac
+
+exit 0
index 0469daa..4466bd9 100644 (file)
@@ -16,7 +16,7 @@
         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>
@@ -35,15 +35,13 @@ 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;
 }
 
@@ -53,7 +51,7 @@ int BNO055_i2c_close(struct bno055_t *bno055)
        bno055->bus_read=NULL;
        bno055->delay_msec=NULL;
        bno055->dev_addr=0;
-       
+
        if(close(fd)) return -1;
 
        return 0;
@@ -65,10 +63,10 @@ s8 BNO055_i2c_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
        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;
@@ -77,10 +75,10 @@ s8 BNO055_i2c_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
 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,&reg_addr,sizeof reg_addr)==-1) return BNO055_ERROR;
 
        if(read(fd,reg_data,cnt)==-1) return BNO055_ERROR;
-       
-       return BNO055_SUCCESS;  
+
+       return BNO055_SUCCESS;
 }
index 6a424e0..d1d6b3f 100644 (file)
@@ -54,7 +54,7 @@ int main(int argc,char *argv[])
        u8 clk_src;
        char interface;
        u8 dev_addr;
-       
+
        if(argc < 2) usage_help(argv[0]);
 
        while((opt=getopt(argc,argv,"i:t")) != -1)
@@ -81,12 +81,12 @@ int main(int argc,char *argv[])
                                        return -errno;
                                }
                                break;
-               
+
                        default:
                                usage_help(argv[0]);
                }
        }
-       
+
        if(bno055_init(&bno055) == BNO055_SUCCESS)
        {
                printf("Chip id=0x%02x\n",bno055.chip_id);
@@ -114,27 +114,27 @@ int main(int argc,char *argv[])
        if(bno055_set_accel_unit(BNO055_ACCEL_UNIT_MSQ) == BNO055_SUCCESS)
                printf("Acceleration unit set to m/s^2.\n");
        else fprintf(stderr,"Error setting acceleration unit to m/s^2.\n");
-               
+
        if(bno055_set_gyro_unit(BNO055_GYRO_UNIT_RPS) == BNO055_SUCCESS)
                printf("Gyroscope unit set to rad/s.\n");
        else fprintf(stderr,"Error setting gyroscope unit to rad/s.\n");
-       
+
        if(bno055_set_euler_unit(BNO055_EULER_UNIT_RAD) == BNO055_SUCCESS)
                printf("Euler angles unit set to rad.\n");
        else fprintf(stderr,"Error setting Euler angles unit to rad.\n");
-       
+
        if(bno055_set_tilt_unit(BNO055_TILT_UNIT_RAD) == BNO055_SUCCESS)
                printf("Tilt unit set to rad.\n");
        else fprintf(stderr,"Error setting tilt unit to rad.\n");
-       
+
        if(bno055_set_temp_unit(BNO055_TEMP_UNIT_CELSIUS) == BNO055_SUCCESS)
                printf("Temperature unit set to Celsius.\n");
        else fprintf(stderr,"Error setting temperature unit to Celsius.\n");
-       
+
        if(bno055_set_data_output_format(0x01) == BNO055_SUCCESS)
                printf("Data output format set to Android.\n");
        else fprintf(stderr,"Error setting data output format.\n");
-               
+
        if(bno055_set_operation_mode(BNO055_OPERATION_MODE_AMG) == BNO055_SUCCESS)
                printf("Operation mode set to AMG: raw (uncompesated) data.\n");
        else fprintf(stderr,"Error setting operation mode t0 AMG.\n");
@@ -142,7 +142,7 @@ int main(int argc,char *argv[])
        if(bno055_read_accel_xyz(&accel) == BNO055_SUCCESS)
                printf("Acceleration=%G, %G, %G m/s^2\n",accel.x/BNO055_ACCEL_DIV_MSQ,accel.y/BNO055_ACCEL_DIV_MSQ,accel.z/BNO055_ACCEL_DIV_MSQ);
        else fprintf(stderr,"Error reading acceleration.\n");
-               
+
        if(bno055_read_mag_xyz(&mag) == BNO055_SUCCESS)
                printf("Magnetometer=%G, %G, %G uT\n",mag.x/BNO055_MAG_DIV_UT,mag.y/BNO055_MAG_DIV_UT,mag.z/BNO055_MAG_DIV_UT);
        else fprintf(stderr,"Error reading magnetometer.\n");
@@ -158,11 +158,11 @@ int main(int argc,char *argv[])
        if(bno055_set_operation_mode(BNO055_OPERATION_MODE_NDOF) == BNO055_SUCCESS)
                printf("Opertation mode set to NDOF: fused (offset and tilt compensated data).\n");
        else fprintf(stderr,"Error setting operation mode to NDOF.\n");
-       
+
        if(bno055_read_accel_xyz(&accel) == BNO055_SUCCESS)
                printf("Acceleration=%G, %G, %G m/s^2\n",accel.x/BNO055_ACCEL_DIV_MSQ,accel.y/BNO055_ACCEL_DIV_MSQ,accel.z/BNO055_ACCEL_DIV_MSQ);
        else fprintf(stderr,"Error reading acceleration.\n");
-               
+
        if(bno055_read_mag_xyz(&mag) == BNO055_SUCCESS)
                printf("Magnetometer=%G, %G, %G uT\n",mag.x/BNO055_MAG_DIV_UT,mag.y/BNO055_MAG_DIV_UT,mag.z/BNO055_MAG_DIV_UT);
        else fprintf(stderr,"Error reading magnetometer.\n");
@@ -178,7 +178,7 @@ int main(int argc,char *argv[])
        if(bno055_read_quaternion_wxyz(&quaternion) == BNO055_SUCCESS)
                printf("Quaternion=%G, %G, %G, %G\n",quaternion.w/BNO055_QUATERNION_DIV,quaternion.x/BNO055_QUATERNION_DIV,quaternion.y/BNO055_QUATERNION_DIV,quaternion.z/BNO055_QUATERNION_DIV);
        else fprintf(stderr,"Error reading quaternion.\n");
-       
+
        if(bno055_read_linear_accel_xyz(&linear_accel) == BNO055_SUCCESS)
                printf("Linear acceleration=%G, %G, %G m/s^2\n",linear_accel.x/BNO055_LINEAR_ACCEL_DIV_MSQ,linear_accel.y/BNO055_LINEAR_ACCEL_DIV_MSQ,linear_accel.z/BNO055_LINEAR_ACCEL_DIV_MSQ);
        else fprintf(stderr,"Error reading linear acceleration.\n");
diff --git a/udev/rules.d/99-i2c-dev.rules b/udev/rules.d/99-i2c-dev.rules
new file mode 100644 (file)
index 0000000..b854019
--- /dev/null
@@ -0,0 +1,2 @@
+KERNEL=="i2c-[0-9]*", GROUP="i2c", MODE="0660"
+