25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

329 lines
9.0 KiB

  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://projects.drogon.net/raspberry-pi/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 <stdio.h>
  26. #include <stdarg.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #ifndef TRUE
  30. # define TRUE (1==1)
  31. # define FALSE (!TRUE)
  32. #endif
  33. #define PIN_NUM_CALC_SYSFD(x) (x>225 ? x - libwiring.pinBase : x)
  34. // GCC warning suppressor
  35. #define UNU __attribute__((unused))
  36. #define ENV_DEBUG "WIRINGPI_DEBUG"
  37. #define ENV_CODES "WIRINGPI_CODES"
  38. #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
  39. #define MODEL_UNKNOWN 0
  40. #define MODEL_KHADAS_VIM1 1
  41. #define MODEL_KHADAS_VIM2 2
  42. #define MODEL_KHADAS_VIM3 3
  43. #define MODEL_KHADAS_EDGE 4
  44. #define MAKER_UNKNOWN 0
  45. #define MAKER_AMLOGIC 1
  46. #define MAKER_ROCKCHIP 2
  47. // wiringPi modes
  48. #define MODE_PINS 0
  49. #define MODE_GPIO 1
  50. #define MODE_GPIO_SYS 2
  51. #define MODE_PHYS 3
  52. #define MODE_PIFACE 4
  53. #define MODE_UNINITIALISED -1
  54. // Pin modes
  55. #define INPUT 0
  56. #define OUTPUT 1
  57. #define PWM_OUTPUT 2
  58. #define GPIO_CLOCK 3
  59. #define SOFT_PWM_OUTPUT 4
  60. #define SOFT_TONE_OUTPUT 5
  61. #define PWM_TONE_OUTPUT 6
  62. #define LOW 0
  63. #define HIGH 1
  64. // Pull up/down/none
  65. #define PUD_OFF 0
  66. #define PUD_DOWN 1
  67. #define PUD_UP 2
  68. // PWM
  69. #define PWM_MODE_MS 0
  70. #define PWM_MODE_BAL 1
  71. // Interrupt levels
  72. #define INT_EDGE_SETUP 0
  73. #define INT_EDGE_FALLING 1
  74. #define INT_EDGE_RISING 2
  75. #define INT_EDGE_BOTH 3
  76. //Module names
  77. #define AML_MODULE_I2C "aml_i2c"
  78. extern const char *piModelNames [16] ;
  79. extern const char *piRevisionNames [16] ;
  80. extern const char *piMakerNames [16] ;
  81. extern const int piMemorySize [ 8] ;
  82. // Intended for the GPIO program Use at your own risk.
  83. // Threads
  84. #define PI_THREAD(X) void *X (UNU void *dummy)
  85. // Failure modes
  86. #define WPI_FATAL (1==1)
  87. #define WPI_ALMOST (1==2)
  88. #if !defined(ANDROID)
  89. #define PAGE_SIZE (4*1024)
  90. #endif
  91. #define BLOCK_SIZE (4*1024)
  92. #define MSG_ERR -1
  93. #define MSG_WARN -2
  94. struct libkhadas
  95. {
  96. /* H/W model info */
  97. int model, rev, mem, maker;
  98. /* wiringPi init mode */
  99. int mode;
  100. /* wiringPi core func */
  101. int (*getModeToGpio) (int mode, int pin);
  102. void (*setPadDrive) (int pin, int value);
  103. int (*getPadDrive) (int pin);
  104. void (*pinMode) (int pin, int mode);
  105. int (*getAlt) (int pin);
  106. int (*getPUPD) (int pin);
  107. void (*pullUpDnControl) (int pin, int pud);
  108. int (*digitalRead) (int pin);
  109. void (*digitalWrite) (int pin, int value);
  110. int (*analogRead) (int pin);
  111. void (*digitalWriteByte) (const int value);
  112. unsigned int (*digitalReadByte) (void);
  113. /* ISR Function pointer */
  114. void (*isrFunctions[256])(void);
  115. /* GPIO sysfs file discripter */
  116. int sysFds[256];
  117. /* GPIO pin base number */
  118. int pinBase;
  119. // Time for easy calculations
  120. uint64_t epochMilli, epochMicro ;
  121. };
  122. union reg_bitfield {
  123. unsigned int wvalue;
  124. struct {
  125. unsigned int bit0 : 1;
  126. unsigned int bit1 : 1;
  127. unsigned int bit2 : 1;
  128. unsigned int bit3 : 1;
  129. unsigned int bit4 : 1;
  130. unsigned int bit5 : 1;
  131. unsigned int bit6 : 1;
  132. unsigned int bit7 : 1;
  133. unsigned int bit8 : 1;
  134. unsigned int bit9 : 1;
  135. unsigned int bit10 : 1;
  136. unsigned int bit11 : 1;
  137. unsigned int bit12 : 1;
  138. unsigned int bit13 : 1;
  139. unsigned int bit14 : 1;
  140. unsigned int bit15 : 1;
  141. unsigned int bit16 : 1;
  142. unsigned int bit17 : 1;
  143. unsigned int bit18 : 1;
  144. unsigned int bit19 : 1;
  145. unsigned int bit20 : 1;
  146. unsigned int bit21 : 1;
  147. unsigned int bit22 : 1;
  148. unsigned int bit23 : 1;
  149. unsigned int bit24 : 1;
  150. unsigned int bit25 : 1;
  151. unsigned int bit26 : 1;
  152. unsigned int bit27 : 1;
  153. unsigned int bit28 : 1;
  154. unsigned int bit29 : 1;
  155. unsigned int bit30 : 1;
  156. unsigned int bit31 : 1;
  157. } bits;
  158. };
  159. // wiringPiNodeStruct:
  160. // This describes additional device nodes in the extended wiringPi
  161. // 2.0 scheme of things.
  162. // It's a simple linked list for now, but will hopefully migrate to
  163. // a binary tree for efficiency reasons - but then again, the chances
  164. // of more than 1 or 2 devices being added are fairly slim, so who
  165. // knows....
  166. struct wiringPiNodeStruct
  167. {
  168. int pinBase ;
  169. int pinMax ;
  170. int fd ; // Node specific
  171. unsigned int data0 ; // ditto
  172. unsigned int data1 ; // ditto
  173. unsigned int data2 ; // ditto
  174. unsigned int data3 ; // ditto
  175. void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
  176. void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
  177. int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
  178. //unsigned int (*digitalRead8) (struct wiringPiNodeStruct *node, int pin) ;
  179. void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
  180. // void (*digitalWrite8) (struct wiringPiNodeStruct *node, int pin, int value) ;
  181. void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
  182. int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
  183. void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
  184. struct wiringPiNodeStruct *next ;
  185. } ;
  186. extern struct wiringPiNodeStruct *wiringPiNodes ;
  187. // Export variables for the hardware pointers
  188. //extern volatile unsigned int *_wiringPiGpio ;
  189. //extern volatile unsigned int *_wiringPiPwm ;
  190. //extern volatile unsigned int *_wiringPiClk ;
  191. //extern volatile unsigned int *_wiringPiPads ;
  192. //extern volatile unsigned int *_wiringPiTimer ;
  193. //extern volatile unsigned int *_wiringPiTimerIrqRaw ;
  194. // Function prototypes
  195. // c++ wrappers thanks to a comment by Nick Lott
  196. // (and others on the Raspberry Pi forums)
  197. #ifdef __cplusplus
  198. extern "C" {
  199. #endif
  200. // Data
  201. // Internal WiringPi functions
  202. extern int wiringPiFailure (int fatal, const char *message, ...) ;
  203. extern int msg (int type, const char *message, ...);
  204. extern int moduleLoaded (char *);
  205. // Core wiringPi functions
  206. extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
  207. extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
  208. extern void wiringPiVersion (int *major, int *minor) ;
  209. extern int wiringPiSetup (void) ;
  210. extern int wiringPiSetupSys (void) ;
  211. extern int wiringPiSetupGpio (void) ;
  212. extern int wiringPiSetupPhys (void) ;
  213. extern int getModeToGpio (int wpiPin);
  214. extern void setPadDrive (int pin, int value);
  215. extern int getPadDrive (int pin);
  216. extern int getAlt (int pin);
  217. extern int getPUPD (int pin);
  218. extern void pinMode (int pin,int mode);
  219. extern void pullUpDnControl (int pin, int pud);
  220. extern int digitalRead (int pin);
  221. extern void digitalWrite (int pin, int value);
  222. extern unsigned int digitalReadByte (void);
  223. extern void digitalWriteByte (const int vaule);
  224. extern void pwmWrite (int pin, int value);
  225. extern int analogRead (int pin);
  226. //Hardware Specific stuffs
  227. extern int piGpioLayout (void);
  228. extern int piBoardRev (void);
  229. extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty);
  230. extern int wpiPinToGpio (int wpiPin);
  231. extern int physPinToGpio (int physPin);
  232. //Unsupported
  233. extern void pinModeAlt (int pin, int mode);
  234. extern void analogWrite (int pin, int value);
  235. extern void pwmToneWrite (int pin, int freq);
  236. extern void pwmSetMode (int mode);
  237. extern void pwmSetRange (unsigned int range);
  238. extern void pwmSetClock (int divisor);
  239. extern void gpioClockSet (int pin, int freq);
  240. extern unsigned int digitalReadByte (void);
  241. extern unsigned int digitalReadByte2 (void);
  242. extern void digitalWriteByte (int value);
  243. extern void digitalWriteByte2 (int value);
  244. //Interrupt
  245. extern int waitForInterrupt (int pin, int mS);
  246. extern int wiringPiISR (int pin, int mode, void (*function)(void));
  247. //Threads
  248. extern int piThreadCreate (void *(*fn)(void *));
  249. extern void piLock (int key);
  250. extern void piUnlock (int key);
  251. //Schedulling priority
  252. extern int piHiPri (const int pri);
  253. //From Arduino land
  254. extern void delay (unsigned int howLang);
  255. extern void delayMicroseconds (unsigned int howLang);
  256. extern unsigned int millis (void);
  257. extern unsigned int micros (void);
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #endif /* __WIRING_H__ */