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.
 
 
 
 
 

252 regels
6.8 KiB

  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. }
  137. /*
  138. * wiringPiSetupSys:
  139. * Must be called once at the start of your program execution.
  140. *
  141. * Initialisation (again), however this time we are using the /sys/class/gpio
  142. * interface to the GPIO systems - slightly slower, but always usable as
  143. * a non-root user, assuming the devices are already exported and setup correctly.
  144. */
  145. /*
  146. int wiringPiSetupSys (void)
  147. {
  148. char fName [128] ;
  149. if (wiringPiSetuped)
  150. return 0 ;
  151. wiringPiSetuped = TRUE ;
  152. if (getenv (ENV_DEBUG) != NULL)
  153. wiringPiDebug = TRUE ;
  154. if (getenv (ENV_CODES) != NULL)
  155. wiringPiReturnCodes = TRUE ;
  156. if (wiringPiDebug)
  157. printf ("wiringPi: wiringPiSetupSys called\n") ;
  158. int model, rev, mem, maker, overVolted ;
  159. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  160. if (piGpioLayout () == GPIO_LAYOUT_PI1_REV1)
  161. {
  162. pinToGpio = pinToGpioR1 ;
  163. physToGpio = physToGpioR1 ;
  164. }
  165. else
  166. {
  167. pinToGpio = pinToGpioR2 ;
  168. physToGpio = physToGpioR2 ;
  169. }
  170. // Open and scan the directory, looking for exported GPIOs, and pre-open
  171. // the 'value' interface to speed things up for later
  172. for (int pin = 0, maxpin=GetMaxPin() ; pin <= maxpin ; ++pin)
  173. {
  174. int pinFS = GPIOToSysFS(pin);
  175. if (pinFS>=0) {
  176. sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
  177. sysFds [pin] = open (fName, O_RDWR) ;
  178. }
  179. }
  180. initialiseEpoch () ;
  181. wiringPiMode = WPI_MODE_GPIO_SYS ;
  182. return 0 ;
  183. }
  184. */