From 39663ba396094657cee64df5eec6f4bc2be64f11 Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Sat, 9 Mar 2024 14:11:06 +0100 Subject: [PATCH] #194 --- wiringPi/Makefile | 3 +- wiringPi/wiringPi.c | 92 +++++++++++++++++++++++------------------------ wiringPi/wiringPi.h | 2 +- wiringPi/wiringPiLegacy.c | 57 +++++++++++++++++++++++++++++ wiringPi/wiringPiLegacy.h | 2 ++ 5 files changed, 107 insertions(+), 49 deletions(-) create mode 100644 wiringPi/wiringPiLegacy.c create mode 100644 wiringPi/wiringPiLegacy.h diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 28501ec..88fc48e 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -61,7 +61,8 @@ SRC = wiringPi.c \ bmp180.c htu21d.c ds18b20.c rht03.c \ drcSerial.c drcNet.c \ pseudoPins.c \ - wpiExtensions.c + wpiExtensions.c \ + wiringPiLegacy.c HEADERS = $(shell ls *.h) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index bbf1713..4581adb 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -70,12 +70,14 @@ #include #include #include +#include #include "softPwm.h" #include "softTone.h" #include "wiringPi.h" #include "../version.h" +#include "wiringPiLegacy.h" // Environment Variables @@ -874,7 +876,7 @@ static void usingGpioMemCheck (const char *what) ********************************************************************************* */ -static void piGpioLayoutOops (const char *why) +void piGpioLayoutOops (const char *why) { fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ; fprintf (stderr, " -> %s\n", why) ; @@ -1006,7 +1008,7 @@ int piGpioLayout (void) gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's. if (wiringPiDebug) - printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ; + printf ("piGpioLayout: Returning revision: %d\n", gpioLayout) ; return gpioLayout ; } @@ -1023,6 +1025,36 @@ int piBoardRev (void) } +const char* revfile = "/proc/device-tree/system/linux,revision"; + + +const char* GetPiRevision(char* line, int linelength, unsigned int* revision) { + + const char* c = NULL; + uint32_t Revision = 0; + _Static_assert(sizeof(Revision)==4, "should be unsigend integer with 4 byte size"); + + FILE* fp = fopen(revfile,"rb"); + if (!fp) { + if (wiringPiDebug) + perror(revfile); + return NULL; // revision file not found or no access + } + int result = fread(&Revision, sizeof(Revision), 1, fp); + fclose(fp); + if (result<1) { + if (wiringPiDebug) + perror(revfile); + return NULL; // read error + } + Revision = bswap_32(Revision); + snprintf(line, linelength, "Revision\t: %x", Revision); + c = &line[11]; + *revision = Revision; + if (wiringPiDebug) + printf("GetPiRevision: Revision string: \"%s\" (%s) - 0x%x\n", line, c, *revision); + return c; +} /* * piBoardId: @@ -1093,10 +1125,10 @@ int piBoardRev (void) void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty) { - FILE *cpuFd ; - char line [120] ; - char *c ; - unsigned int revision ; + const int maxlength = 120; + char line [maxlength+1] ; + const char *c ; + unsigned int revision = 0x00 ; int bRev, bType, bProc, bMfg, bMem, bWarranty ; // Will deal with the properly later on - for now, lets just get it going... @@ -1104,47 +1136,13 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty) (void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result. - if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) - piGpioLayoutOops ("Unable to open /proc/cpuinfo") ; - - while (fgets (line, 120, cpuFd) != NULL) - if (strncmp (line, "Revision", 8) == 0) - break ; - - fclose (cpuFd) ; - - if (strncmp (line, "Revision", 8) != 0) - piGpioLayoutOops ("No \"Revision\" line") ; - -// Chomp trailing CR/NL - - for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) - *c = 0 ; - - if (wiringPiDebug) - printf ("piBoardId: Revision string: %s\n", line) ; - -// Need to work out if it's using the new or old encoding scheme: - -// Scan to the first character of the revision number - - for (c = line ; *c ; ++c) - if (*c == ':') - break ; - - if (*c != ':') - piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ; - -// Chomp spaces - - ++c ; - while (isspace (*c)) - ++c ; - - if (!isxdigit (*c)) - piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ; - - revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x + c = GetPiRevision(line, maxlength, &revision); // device tree + if (NULL==c) { + c = GetPiRevisionLegacy(line, maxlength, &revision); // proc/cpuinfo + } + if (NULL==c) { + piGpioLayoutOops ("GetPiRevision failed!") ; + } // Check for new way: diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 94875b2..1c8c34d 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -195,7 +195,7 @@ extern "C" { // Data // Internal - +extern void piGpioLayoutOops (const char *why); extern int wiringPiFailure (int fatal, const char *message, ...) ; // Core wiringPi functions diff --git a/wiringPi/wiringPiLegacy.c b/wiringPi/wiringPiLegacy.c new file mode 100644 index 0000000..e83816b --- /dev/null +++ b/wiringPi/wiringPiLegacy.c @@ -0,0 +1,57 @@ + +#include +#include "wiringPiLegacy.h" +#include +#include +#include +#include + +extern int wiringPiDebug ; + +const char* GetPiRevisionLegacy(char* line, int linelength, unsigned int* revision) { + FILE *cpuFd ; + char *c = NULL; + + if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) + piGpioLayoutOops ("Unable to open /proc/cpuinfo") ; + + while (fgets (line, linelength, cpuFd) != NULL) + if (strncmp (line, "Revision", 8) == 0) + break ; + + fclose (cpuFd) ; + + if (strncmp (line, "Revision", 8) != 0) + piGpioLayoutOops ("No \"Revision\" line") ; + +// Chomp trailing CR/NL + + for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) + *c = 0 ; + + if (wiringPiDebug) + printf ("GetPiRevisionLegacy: Revision string: %s\n", line) ; + +// Need to work out if it's using the new or old encoding scheme: + +// Scan to the first character of the revision number + + for (c = line ; *c ; ++c) + if (*c == ':') + break ; + + if (*c != ':') + piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ; + +// Chomp spaces + + ++c ; + while (isspace (*c)) + ++c ; + + if (!isxdigit (*c)) + piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ; + + *revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x + return c; +} \ No newline at end of file diff --git a/wiringPi/wiringPiLegacy.h b/wiringPi/wiringPiLegacy.h new file mode 100644 index 0000000..e6edc8b --- /dev/null +++ b/wiringPi/wiringPiLegacy.h @@ -0,0 +1,2 @@ + +const char* GetPiRevisionLegacy(char* line, int linelength, unsigned int* revision); \ No newline at end of file