From 4c0a2118db98b0c82fbb8f63a8941b1f6dd2e78f Mon Sep 17 00:00:00 2001 From: alanbjohnston Date: Fri, 11 Sep 2020 17:54:02 -0400 Subject: [PATCH] added test_i2c_bus function --- afsk/telem.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/afsk/telem.c b/afsk/telem.c index 97d6294c..f4836c3a 100644 --- a/afsk/telem.c +++ b/afsk/telem.c @@ -23,6 +23,7 @@ #define OFF -1 #define ON 1 +int test_i2c_bus(char *bus); const char pythonCmd[] = "python3 /home/pi/CubeSatSim/python/voltcurrent.py "; char pythonStr[100], pythonConfigStr[100], busStr[10]; int map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; @@ -95,6 +96,8 @@ int main(int argc, char *argv[]) { { printf("vB5 Present\n"); // Don't print normal board detection + print("Result: %d \n",test_i2c_bus(3)); + if (access("/dev/i2c-11", W_OK | R_OK) >= 0) { // Test if I2C Bus 11 is present printf("/dev/i2c-11 is present\n\n"); @@ -173,8 +176,8 @@ int main(int argc, char *argv[]) { printf("ERROR: %s bus has a problem \n Check software to see if enabled \n", "0"); strcpy(busStr,"1 -1"); } - } } + } } // Reading I2C voltage and current sensors @@ -240,3 +243,36 @@ int main(int argc, char *argv[]) { return 0; } + +int test_i2c_bus(char *bus) +{ + int result = 1; + char *busString = "/dev/i2c-3"; + + if (access(busString, W_OK | R_OK) >= 0) { // Test if I2C Bus 0 is present +// printf("bus is present\n\n"); + char result[128]; + char command[50] = "timeout 10 i2cdetect -y "; + strcat (command, itoa(bus)); +// printf("Command: %s \n", command); + FILE *i2cdetect = popen(command, "r"); + + while (fgets(result, 128, i2cdetect) != NULL) { + ; +// printf("result: %s", result); + } + int error = pclose(i2cdetect)/256; +// printf("%s error: %d \n", &command, error); + if (error != 0) + { + printf("ERROR: %d bus has a problem \n Check I2C wiring and pullup resistors \n", bus); + result = -1; + } + } else + { + printf("ERROR: %d bus has a problem \n Check software to see if enabled \n", bus); + result = -1 + } + } + return(result); +}