]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
Change i2c to use WiringPi I2C API so it will compile on Tinker Board
authorJohn Melton G0ORX <john.d.melton@googlemail.com>
Sun, 23 Feb 2020 11:50:18 +0000 (11:50 +0000)
committerJohn Melton G0ORX <john.d.melton@googlemail.com>
Sun, 23 Feb 2020 11:50:18 +0000 (11:50 +0000)
gpio.c
gpio.h
i2c.c

diff --git a/gpio.c b/gpio.c
index 4651f449f35d21f4bb0749bbf443aa11d331bd46..83c8b59fa1297700e367cd839dab9f306d1760f6 100644 (file)
--- a/gpio.c
+++ b/gpio.c
@@ -641,6 +641,7 @@ static unsigned long s6_debounce=0;
 
 static void s6Alert() {
     int t=millis();
+    g_print("s6Alert\n");
     if(millis()<s6_debounce) {
       return;
     }
@@ -1399,6 +1400,8 @@ void gpio_save_state() {
 
 static void setup_pin(int pin, int up_down, void(*pAlert)(void)) {
   int rc;
+g_print("setup_pin: pin=%d ip_down=%d pAlert=%p\n",pin,up_down,pAlert);
+  pinMode(pin,GPIO);
   pinMode(pin,INPUT);
   pullUpDnControl(pin,up_down);
   usleep(10000);
@@ -1580,14 +1583,17 @@ int gpio_init() {
       setup_pin(S3_BUTTON, PUD_UP, &s3Alert);
     }
   
+    g_print("ENABLE_S4_BUTTON: %d\n",ENABLE_S4_BUTTON);
     if(ENABLE_S4_BUTTON) {
       setup_pin(S4_BUTTON, PUD_UP, &s4Alert);
     }
   
+    g_print("ENABLE_S5_BUTTON: %d\n",ENABLE_S5_BUTTON);
     if(ENABLE_S5_BUTTON) {
       setup_pin(S5_BUTTON, PUD_UP, &s5Alert);
     }
   
+    g_print("ENABLE_S6_BUTTON: %d\n",ENABLE_S6_BUTTON);
     if(ENABLE_S6_BUTTON) {
       setup_pin(S6_BUTTON, PUD_UP, &s6Alert);
     }
diff --git a/gpio.h b/gpio.h
index b976e2b88b983d5859e6a382f121dc2816718aaa..f90f1887be25499397df35e93630fe327b452e93 100644 (file)
--- a/gpio.h
+++ b/gpio.h
@@ -179,7 +179,6 @@ extern int ENABLE_E2_ENCODER;
 extern int ENABLE_E2_PULLUP;
 extern int E2_ENCODER_A;
 extern int E2_ENCODER_B;
-extern int ENABLE_E2_TOP_ENCODER;
 extern int E2_TOP_ENCODER_A;
 extern int E2_TOP_ENCODER_B;
 extern int E2_FUNCTION;
@@ -187,7 +186,6 @@ extern int ENABLE_E3_ENCODER;
 extern int ENABLE_E3_PULLUP;
 extern int E3_ENCODER_A;
 extern int E3_ENCODER_B;
-extern int ENABLE_E3_TOP_ENCODER;
 extern int E3_TOP_ENCODER_A;
 extern int E3_TOP_ENCODER_B;
 extern int E3_FUNCTION;
@@ -195,7 +193,6 @@ extern int ENABLE_E4_ENCODER;
 extern int ENABLE_E4_PULLUP;
 extern int E4_ENCODER_A;
 extern int E4_ENCODER_B;
-extern int ENABLE_E4_TOP_ENCODER;
 extern int E4_TOP_ENCODER_A;
 extern int E4_TOP_ENCODER_B;
 extern int E4_FUNCTION;
@@ -203,7 +200,6 @@ extern int ENABLE_E5_ENCODER;
 extern int ENABLE_E5_PULLUP;
 extern int E5_ENCODER_A;
 extern int E5_ENCODER_B;
-extern int ENABLE_E5_TOP_ENCODER;
 extern int E5_TOP_ENCODER_A;
 extern int E5_TOP_ENCODER_B;
 extern int E5_FUNCTION;
diff --git a/i2c.c b/i2c.c
index dc2e691f22fbd51d0fcdf54029c4083dabf50077..cc1f032a5fd115cb256b186b641640c08e742db2 100644 (file)
--- a/i2c.c
+++ b/i2c.c
@@ -4,12 +4,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <linux/i2c-dev.h>
-#include <i2c/smbus.h>
+//#include <i2c/smbus.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 
 #include <gtk/gtk.h>
-#include "i2c.h"
+//#include "i2c.h"
+#include "wiringPiI2C.h"
 #include "gpio.h"
 #include "band.h"
 #include "band_menu.h"
@@ -23,6 +24,8 @@ char *i2c_device="/dev/i2c-1";
 unsigned int i2c_address_1=0X20;
 unsigned int i2c_address_2=0X23;
 
+static int fd;
+
 #define SW_2  0X8000
 #define SW_3  0X4000
 #define SW_4  0X2000
@@ -45,76 +48,25 @@ unsigned int i2c_sw[16]=
       SW_10,SW_11,SW_12,SW_13,SW_14,SW_15,SW_16,SW_17 };
 
 static int write_byte_data(unsigned char addr,unsigned char reg, unsigned char data) {
-  int fd;
   int rc;
 
-  if((fd=open(i2c_device, O_RDWR))<0) {
-    fprintf(stderr,"cannot open %s: %s\n",i2c_device,strerror(errno));
-    return(-1);
-  }
-
-  if(ioctl(fd,I2C_SLAVE,addr)<0) {
-    fprintf(stderr,"cannot aquire access to I2C device at 0x%02X\n",addr);
-    return(-1);
-  }
-
-  rc=i2c_smbus_write_byte_data(fd,reg,data);
-  if(rc<0) {
-    fprintf(stderr,"i2c_smbus_write_byte_data failed: device=%02X 0x%02X to 0x%02X: %s\n",addr,data,reg,strerror(errno));
-    return(-1);
-  }
-
-  close(fd);
+  rc=wiringPiI2CWriteReg8(fd,reg,data);
   
   return 0;
 }
 
 static unsigned char read_byte_data(unsigned char addr,unsigned char reg) {
-  int fd;
   int rc;
 
-  if((fd=open(i2c_device, O_RDWR))<0) {
-    fprintf(stderr,"cannot open %s: %s\n",i2c_device,strerror(errno));
-    exit(1);
-  }
-
-  if(ioctl(fd,I2C_SLAVE,addr)<0) {
-    fprintf(stderr,"cannot aquire access to I2C device at 0x%x\n",addr);
-    exit(1);
-  }
-
-  rc=i2c_smbus_read_byte_data(fd,reg);
-  if(rc<0) {
-    fprintf(stderr,"i2c_smbus_read_byte_data failed: 0x%2X: %s\n",reg,strerror(errno));
-    exit(1);
-  }
-
-  close(fd);
+  rc=wiringPiI2CReadReg8(fd,reg);
 
   return rc;
 }
 
 static unsigned int read_word_data(unsigned char addr,unsigned char reg) {
-  int fd;
   int rc;
 
-  if((fd=open(i2c_device, O_RDWR))<0) {
-    fprintf(stderr,"c$cannot open %s: %s\n",i2c_device,strerror(errno));
-    exit(1);
-  }
-
-  if(ioctl(fd,I2C_SLAVE,addr)<0) {
-    fprintf(stderr,"cannot aquire access to I2C device at 0x%x\n",addr);
-    exit(1);
-  }
-
-  rc=i2c_smbus_read_word_data(fd,reg);
-  if(rc<0) {
-    fprintf(stderr,"i2c_smbus_read_word_data failed: 0x%2X: %s\n",reg,strerror(errno));
-    exit(1);
-  }
-
-  close(fd);
+  rc=wiringPiI2CReadReg16(fd,reg);
 
   return rc;
 }
@@ -320,6 +272,13 @@ void i2c_init() {
   int flags, ints;
 
 fprintf(stderr,"i2c_init\n");
+
+  fd=wiringPiI2CSetup(i2c_address_1);
+  if(fd<0) {
+    g_print("i2c_init failed: fd=%d\n",fd);
+    return;
+  }
+
   // setup i2c
   if(write_byte_data(i2c_address_1,0x0A,0x44)<0) return;
   if(write_byte_data(i2c_address_1,0x0B,0x44)<0) return;
@@ -357,11 +316,16 @@ fprintf(stderr,"i2c_init\n");
   if(write_byte_data(i2c_address_1,0x05,0xFF)<0) return;
 
   // flush any interrupts
+  int count=0;
   do {
     flags=read_word_data(i2c_address_1,0x0E);
     if(flags) {
       ints=read_word_data(i2c_address_1,0x10);
       fprintf(stderr,"flush interrupt: flags=%04X ints=%04X\n",flags,ints);
+      count++;
+      if(count==10) {
+        return;
+      }
     }
   } while(flags!=0);