You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiringPiLegacy.c 5.4 KiB

8 kuukautta sitten
8 kuukautta sitten
8 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <wiringPi.h>
  2. #include "wiringPiLegacy.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. extern int wiringPiDebug ;
  8. const char* GetPiRevisionLegacy(char* line, int linelength, unsigned int* revision) {
  9. FILE *cpuFd ;
  10. char *c = NULL;
  11. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  12. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  13. while (fgets (line, linelength, cpuFd) != NULL)
  14. if (strncmp (line, "Revision", 8) == 0)
  15. break ;
  16. fclose (cpuFd) ;
  17. if (strncmp (line, "Revision", 8) != 0)
  18. piGpioLayoutOops ("No \"Revision\" line") ;
  19. // Chomp trailing CR/NL
  20. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  21. *c = 0 ;
  22. if (wiringPiDebug)
  23. printf ("GetPiRevisionLegacy: Revision string: %s\n", line) ;
  24. // Need to work out if it's using the new or old encoding scheme:
  25. // Scan to the first character of the revision number
  26. for (c = line ; *c ; ++c)
  27. if (*c == ':')
  28. break ;
  29. if (*c != ':')
  30. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  31. // Chomp spaces
  32. ++c ;
  33. while (isspace (*c))
  34. ++c ;
  35. if (!isxdigit (*c))
  36. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  37. *revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
  38. return c;
  39. }
  40. // useless
  41. int piGpioLayoutLegacy (void)
  42. {
  43. FILE *cpuFd ;
  44. char line [120] ;
  45. char *c ;
  46. static int gpioLayout = -1 ;
  47. if (gpioLayout != -1) // No point checking twice
  48. return gpioLayout ;
  49. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  50. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  51. #ifdef DONT_CARE_ANYMORE
  52. // Start by looking for the Architecture to make sure we're really running
  53. // on a Pi. I'm getting fed-up with people whinging at me because
  54. // they can't get it to work on weirdFruitPi boards...
  55. while (fgets (line, 120, cpuFd) != NULL)
  56. if (strncmp (line, "Hardware", 8) == 0)
  57. break ;
  58. if (strncmp (line, "Hardware", 8) != 0)
  59. piGpioLayoutOops ("No \"Hardware\" line") ;
  60. if (wiringPiDebug)
  61. printf ("piGpioLayout: Hardware: %s\n", line) ;
  62. // See if it's BCM2708 or BCM2709 or the new BCM2835.
  63. // OK. As of Kernel 4.8, we have BCM2835 only, regardless of model.
  64. // However I still want to check because it will trap the cheapskates and rip-
  65. // off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
  66. // I do not support so don't email me your bleating whinges about anything
  67. // other than a genuine Raspberry Pi.
  68. if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
  69. {
  70. fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
  71. fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
  72. fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
  73. fprintf (stderr, "at GitHub.com/wiringPi/wiringPi. If this is not a Raspberry Pi then you\n") ;
  74. fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
  75. fprintf (stderr, "Raspberry Pi ONLY.\n") ;
  76. exit (EXIT_FAILURE) ;
  77. }
  78. // Actually... That has caused me more than 10,000 emails so-far. Mosty by
  79. // people who think they know better by creating a statically linked
  80. // version that will not run with a new 4.9 kernel. I utterly hate and
  81. // despise those people.
  82. //
  83. // I also get bleats from people running other than Raspbian with another
  84. // distros compiled kernel rather than a foundation compiled kernel, so
  85. // this might actually help them. It might not - I only have the capacity
  86. // to support Raspbian.
  87. //
  88. // However, I've decided to leave this check out and rely purely on the
  89. // Revision: line for now. It will not work on a non-pi hardware or weird
  90. // kernels that don't give you a suitable revision line.
  91. // So - we're Probably on a Raspberry Pi. Check the revision field for the real
  92. // hardware type
  93. // In-future, I ought to use the device tree as there are now Pi entries in
  94. // /proc/device-tree/ ...
  95. // but I'll leave that for the next revision. Or the next.
  96. #endif
  97. // Isolate the Revision line
  98. rewind (cpuFd) ;
  99. while (fgets (line, 120, cpuFd) != NULL)
  100. if (strncmp (line, "Revision", 8) == 0)
  101. break ;
  102. fclose (cpuFd) ;
  103. if (strncmp (line, "Revision", 8) != 0)
  104. piGpioLayoutOops ("No \"Revision\" line") ;
  105. // Chomp trailing CR/NL
  106. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  107. *c = 0 ;
  108. if (wiringPiDebug)
  109. printf ("piGpioLayout: Revision string: %s\n", line) ;
  110. // Scan to the first character of the revision number
  111. for (c = line ; *c ; ++c)
  112. if (*c == ':')
  113. break ;
  114. if (*c != ':')
  115. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  116. // Chomp spaces
  117. ++c ;
  118. while (isspace (*c))
  119. ++c ;
  120. if (!isxdigit (*c))
  121. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  122. // Make sure its long enough
  123. if (strlen (c) < 4)
  124. piGpioLayoutOops ("Bogus revision line (too small)") ;
  125. // Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
  126. c = c + strlen (c) - 4 ;
  127. if (wiringPiDebug)
  128. printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;
  129. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  130. gpioLayout = 1 ;
  131. else
  132. gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.
  133. if (wiringPiDebug)
  134. printf ("piGpioLayout: Returning revision: %d\n", gpioLayout) ;
  135. return gpioLayout ;
  136. }