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.

wiringPi.h 11 KiB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * wiringPi.h:
  3. * Arduino like Wiring library for the Raspberry Pi.
  4. * Copyright (c) 2012-2017 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://github.com/nuncio-bitis/WiringPi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  21. ***********************************************************************
  22. */
  23. #ifndef __WIRING_PI_H__
  24. #define __WIRING_PI_H__
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #ifndef TRUE
  28. # define TRUE true
  29. # define FALSE false
  30. #endif
  31. // GCC warning suppressor - don't warn about unused parameters (-Wunused-parameter)
  32. #define UNU __attribute__((unused))
  33. #define UNUSED __attribute__((unused))
  34. // Mask for the bottom 64 pins which belong to the Raspberry Pi
  35. // The others are available for the other devices
  36. // @TODO Replace these checks with a check for (pin < 64)
  37. #define PI_GPIO_MASK (0xFFFFFFC0)
  38. #define MAX_ONBOARD_PINS 64
  39. // wiringPi modes
  40. #define WPI_MODE_PINS 0 // Virtual pin numbers 0 through 16
  41. #define WPI_MODE_GPIO 1 // Broadcom GPIO pin numbers
  42. #define WPI_MODE_GPIO_SYS 2 // Broadcom GPIO pin numbers, but uses /sys/class/gpio (slower)
  43. #define WPI_MODE_PHYS 3 // Raspberry Pi physical pins
  44. #define WPI_MODE_PIFACE 4 // UNUSED
  45. #define WPI_MODE_UNINITIALISED -1
  46. // Pin modes
  47. #define INPUT 0
  48. #define OUTPUT 1
  49. #define PWM_OUTPUT 2
  50. #define GPIO_CLOCK 3
  51. #define SOFT_PWM_OUTPUT 4
  52. #define SOFT_TONE_OUTPUT 5
  53. #define PWM_TONE_OUTPUT 6
  54. // Pin levels
  55. #define LOW 0
  56. #define HIGH 1
  57. // Pull up/down/none
  58. #define PUD_OFF 0
  59. #define PUD_DOWN 1
  60. #define PUD_UP 2
  61. // PWM
  62. #define PWM_MODE_MS 0
  63. #define PWM_MODE_BAL 1
  64. // Interrupt levels
  65. #define INT_EDGE_SETUP 0
  66. #define INT_EDGE_FALLING 1
  67. #define INT_EDGE_RISING 2
  68. #define INT_EDGE_BOTH 3
  69. // Pi model types and version numbers
  70. // Intended for the GPIO program
  71. // Use at your own risk
  72. // https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#new-style-revision-codes
  73. // Format (bits): FMMM'CCCC'PPPP'TTTTTTTT'RRRR
  74. // F bit (New flag)
  75. #define PI_FLAG_OLD 0 // old-style revision
  76. #define PI_FLAG_NEW 1 // new-style revision
  77. // MMM bits (Memory size)
  78. #define PI_MEM_256M 0
  79. #define PI_MEM_512M 1
  80. #define PI_MEM_1G 2
  81. #define PI_MEM_2G 3
  82. #define PI_MEM_4G 4
  83. #define PI_MEM_8G 5
  84. // CCCC bits (Manufacturer)
  85. #define PI_MAKER_SONY 0 // Sony UK
  86. #define PI_MAKER_EGOMAN 1
  87. #define PI_MAKER_EMBEST 2
  88. #define PI_MAKER_SONYJAPAN 3
  89. #define PI_MAKER_EMBEST4 4
  90. #define PI_MAKER_STADIUM 5
  91. // PPPP bits (Processor)
  92. #define PI_PROC_BCM2835 0
  93. #define PI_PROC_BCM2836 1
  94. #define PI_PROC_BCM2837 2
  95. #define PI_PROC_BCM2711 3
  96. // TTTTTTTT bits (Type)
  97. #define PI_MODEL_A 0x00
  98. #define PI_MODEL_B 0x01
  99. #define PI_MODEL_AP 0x02
  100. #define PI_MODEL_BP 0x03
  101. #define PI_MODEL_2B 0x04
  102. #define PI_ALPHA 0x05
  103. #define PI_MODEL_CM1 0x06
  104. // 0x07
  105. #define PI_MODEL_3B 0x08
  106. #define PI_MODEL_ZERO 0x09
  107. #define PI_MODEL_CM3 0x0A
  108. // 0x0B
  109. #define PI_MODEL_ZERO_W 0x0C
  110. #define PI_MODEL_3BP 0x0D
  111. #define PI_MODEL_3AP 0x0E
  112. // 0x0F
  113. #define PI_MODEL_CM3P 0x10
  114. #define PI_MODEL_4B 0x11
  115. #define PI_MODEL_ZERO_2W 0x12
  116. #define PI_MODEL_400 0x13
  117. #define PI_MODEL_CM4 0x14
  118. // RRRR bits (Revision)
  119. #define PI_VERSION_1 0
  120. #define PI_VERSION_1_1 1
  121. #define PI_VERSION_1_2 2
  122. #define PI_VERSION_2 3
  123. #define PI_VERSION_1_4 4
  124. extern const char *piModelNames [21];
  125. extern const char *piRevisionNames [ 5];
  126. extern const char *piMakerNames [16];
  127. extern const char *piProcessorNames[ 5];
  128. extern const char *piMemorySize [ 8];
  129. // Intended for the GPIO program
  130. // Use at your own risk.
  131. // Threads
  132. #define PI_THREAD(X) void *X (UNUSED void *dummy)
  133. // Failure modes
  134. #define WPI_FATAL true
  135. #define WPI_NON_FATAL false
  136. // wiringPiNodeStruct:
  137. // This describes additional device nodes in the extended wiringPi
  138. // 2.0 scheme of things. It's a simple linked list.
  139. struct wiringPiNodeStruct
  140. {
  141. int pinBase;
  142. int pinMax;
  143. int fd; // Node specific
  144. unsigned int data0; // ditto
  145. unsigned int data1; // ditto
  146. unsigned int data2; // ditto
  147. unsigned int data3; // ditto
  148. void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode);
  149. void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode);
  150. int (*digitalRead) (struct wiringPiNodeStruct *node, int pin);
  151. void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value);
  152. void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value);
  153. int (*pwmRead) (struct wiringPiNodeStruct *node, int pin);
  154. int (*analogRead) (struct wiringPiNodeStruct *node, int pin);
  155. void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value);
  156. struct wiringPiNodeStruct *next;
  157. };
  158. extern struct wiringPiNodeStruct *wiringPiNodes;
  159. // Export variables for the hardware pointers
  160. extern volatile unsigned int *_wiringPiGpio;
  161. extern volatile unsigned int *_wiringPiPwm;
  162. extern volatile unsigned int *_wiringPiClk;
  163. extern volatile unsigned int *_wiringPiPads;
  164. extern volatile unsigned int *_wiringPiTimer;
  165. extern volatile unsigned int *_wiringPiTimerIrqRaw;
  166. // ----------------------------------------------------------------------------
  167. // Function prototypes
  168. // c++ wrappers thanks to a comment by Nick Lott
  169. // (and others on the Raspberry Pi forums)
  170. #ifdef __cplusplus
  171. extern "C" {
  172. #endif
  173. // ----------------------------------------------------------------------------
  174. // Internal
  175. extern int wiringPiFailure (int fatal, const char *message, ...);
  176. // ----------------------------------------------------------------------------
  177. // Core wiringPi functions
  178. extern struct wiringPiNodeStruct *wiringPiFindNode (int pin);
  179. extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins);
  180. extern void wiringPiVersion (int *major, int *minor);
  181. extern int wiringPiSetup (void); // using WiringPi pin numbering
  182. extern int wiringPiSetupSys (void); // using /sys/class/gpio
  183. extern int wiringPiSetupGpio (void); // using BCM pin numbering
  184. extern int wiringPiSetupPhys (void); // using physical pin numbering
  185. // See "Pin modes" above (In, Out, SoftPWMOut, SoftToneOut, PWMToneOut, PWMOut, Clock)
  186. extern void pinMode (int pin, int mode);
  187. // Set Function Select on pin - GPIO, ALT0-ALT5
  188. extern void pinModeAlt (int pin, int mode);
  189. extern int getAlt (int pin); // Returns Function Select: 0-5)
  190. #define setAlt(pin, mode) pinModeAlt(pin, mode) // for symmetry
  191. // Set input pin as pull-up or pull-down
  192. extern void pullUpDnControl (int pin, int pud);
  193. // Read/Write pin (HIGH or LOW)
  194. extern int digitalRead (int pin);
  195. extern void digitalWrite (int pin, int value);
  196. // Analog read/write is only for an external hardware module,
  197. // therefore pin must be >= MAX_ONBOARD_PINS
  198. extern int analogRead (int pin);
  199. extern void analogWrite (int pin, int value);
  200. // On-Board Raspberry Pi hardware specific stuff
  201. extern int piGpioLayout (void);
  202. extern uint32_t piBoardId (int *model, int *proc, int *rev, int *mem, int *maker, int *overVolted);
  203. extern int wpiPinToGpio (int wpiPin);
  204. extern int physPinToGpio (int physPin);
  205. extern void setPadDrive (int group, int value);
  206. // PWM funcs
  207. extern void pwmWrite (int pin, int value);
  208. extern int pwmRead (int pin);
  209. extern void pwmToneWrite (int pin, int freq);
  210. extern void pwmSetMode (int mode); // balanced or mark/space mode
  211. extern void pwmSetRange (unsigned int range);
  212. extern void pwmSetClock (int divisor);
  213. // Set a GPIO as an output clock.
  214. // Only works on certain pins.
  215. extern void gpioClockSet (int pin, int freq);
  216. // Read all 32 bits in a bank
  217. // BCM GPIOs 0..31 (Bank 0)
  218. // BCM GPIOs 32..63 (Bank 1)
  219. extern uint32_t digitalReadBank(int bank);
  220. // Read/Write 8-bit data on 8 consecutive WiringPi pins:
  221. // WPi: 0, 1, 2, 3, 4, 5, 6, 7
  222. // BCM: 17, 18, 27, 22, 23, 24, 25, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  223. extern unsigned int digitalReadByte(void);
  224. extern unsigned int digitalReadByte2(void);
  225. extern void digitalWriteByte (int value);
  226. extern void digitalWriteByte2 (int value);
  227. // ----------------------------------------------------------------------------
  228. // Interrupts (Also Pi hardware specific)
  229. // Set up for a callback to be called when the specified pin changes according to mode.
  230. // mode = INT_EDGE_SETUP, INT_EDGE_FALLING, INT_EDGE_RISING, INT_EDGE_BOTH
  231. // Uses SYS mode, and also waitForInterrupt
  232. // @NOTE: This leaves gpios exported in /sys/class/gpio when the program ends.
  233. extern int wiringPiISR (int pin, int mode, void (*function)(int));
  234. // Same as wiringPiISR above, but takes a list of pins to wait for.
  235. // Note that the mode applies to all the specified pins.
  236. extern int wiringPiISRmulti (int pins[], int n_pins, int mode, void (*function)(int));
  237. // Internal-only (unless you know how to set up /sys/class/gpio)
  238. // Wait for "interrupt" on a GPIO pin.
  239. // Must be in SYS mode (wiringPiSetupSys was called for setup)
  240. // @NOTE: This leaves gpios exported in /sys/class/gpio when the program ends.
  241. extern int waitForInterrupt (int pin, int mS);
  242. // ----------------------------------------------------------------------------
  243. // Threads
  244. extern int piThreadCreate (void *(*fn)(void *));
  245. extern void piLock (int key);
  246. extern void piUnlock (int key);
  247. // Scheduling priority
  248. extern int piHiPri (const int pri);
  249. // ----------------------------------------------------------------------------
  250. extern void delay (unsigned int milliseconds);
  251. extern void delayMicroseconds (unsigned int microseconds);
  252. // Please use these for code that's easier to comprehend:
  253. #define delayMs(ms) delay(ms)
  254. #define delayUs(us) delayMicroseconds(us)
  255. // These report the amount of time passed since wiringPiSetup* was called.
  256. extern unsigned int millis(void);
  257. extern unsigned int micros(void);
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #endif // __WIRING_PI_H__