Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

730 wiersze
19 KiB

  1. /*
  2. * wiringPi:
  3. * Arduino compatable (ish) Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012 Gordon Henderson
  5. *
  6. * Thanks to code samples from Gert Jan van Loo and the
  7. * BCM2835 ARM Peripherals manual, however it's missing
  8. * the clock section /grr/mutter/
  9. ***********************************************************************
  10. * This file is part of wiringPi:
  11. * https://projects.drogon.net/raspberry-pi/wiringpi/
  12. *
  13. * wiringPi is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * wiringPi is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  25. ***********************************************************************
  26. */
  27. // Revisions:
  28. // 2 Jul 2012:
  29. // Fixed a few more bugs to do with range-checking when in GPIO mode.
  30. // 11 Jun 2012:
  31. // Fixed some typos.
  32. // Added c++ support for the .h file
  33. // Added a new function to allow for using my "pin" numbers, or native
  34. // GPIO pin numbers.
  35. // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
  36. //
  37. // 02 May 2012:
  38. // Added in the 2 UART pins
  39. // Change maxPins to numPins to more accurately reflect purpose
  40. // Pad drive current fiddling
  41. #undef DEBUG_PADS
  42. #include <stdio.h>
  43. #include <stdint.h>
  44. #include <unistd.h>
  45. #include <errno.h>
  46. #include <string.h>
  47. #include <time.h>
  48. #include <fcntl.h>
  49. #include <sys/time.h>
  50. #include <sys/mman.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include "wiringPi.h"
  54. #ifndef TRUE
  55. #define TRUE (1==1)
  56. #define FALSE (1==2)
  57. #endif
  58. // Port function select bits
  59. #define FSEL_INPT 0b000
  60. #define FSEL_OUTP 0b001
  61. #define FSEL_ALT0 0b100
  62. #define FSEL_ALT0 0b100
  63. #define FSEL_ALT1 0b101
  64. #define FSEL_ALT2 0b110
  65. #define FSEL_ALT3 0b111
  66. #define FSEL_ALT4 0b011
  67. #define FSEL_ALT5 0b010
  68. // Access from ARM Running Linux
  69. // Take from Gerts code. Some of this is not in the manual
  70. // that I can find )-:
  71. #define BCM2708_PERI_BASE 0x20000000
  72. #define GPIO_PADS (BCM2708_PERI_BASE + 0x100000)
  73. #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000)
  74. #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
  75. #define GPIO_PWM (BCM2708_PERI_BASE + 0x20C000)
  76. #define PAGE_SIZE (4*1024)
  77. #define BLOCK_SIZE (4*1024)
  78. // PWM
  79. #define PWM_CONTROL 0
  80. #define PWM_STATUS 1
  81. #define PWM0_RANGE 4
  82. #define PWM0_DATA 5
  83. #define PWM1_RANGE 8
  84. #define PWM1_DATA 9
  85. #define PWMCLK_CNTL 40
  86. #define PWMCLK_DIV 41
  87. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  88. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  89. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  90. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  91. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  92. #define PWM1_SERIAL 0x0200 // Run in serial mode
  93. #define PWM1_ENABLE 0x0100 // Channel Enable
  94. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  95. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  96. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  97. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  98. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  99. #define PWM0_SERIAL 0x0002 // Run in serial mode
  100. #define PWM0_ENABLE 0x0001 // Channel Enable
  101. // Locals to hold pointers to the hardware
  102. static volatile uint32_t *gpio ;
  103. static volatile uint32_t *pwm ;
  104. static volatile uint32_t *clk ;
  105. // The BCM2835 has 54 GPIO pins.
  106. // BCM2835 data sheet, Page 90 onwards.
  107. // There are 6 control registers, each control the functions of a block
  108. // of 10 pins.
  109. // Each control register has 10 sets of 3 bits per GPIO pin:
  110. //
  111. // 000 = GPIO Pin X is an input
  112. // 001 = GPIO Pin X is an output
  113. // 100 = GPIO Pin X takes alternate function 0
  114. // 101 = GPIO Pin X takes alternate function 1
  115. // 110 = GPIO Pin X takes alternate function 2
  116. // 111 = GPIO Pin X takes alternate function 3
  117. // 011 = GPIO Pin X takes alternate function 4
  118. // 010 = GPIO Pin X takes alternate function 5
  119. //
  120. // So the 3 bits for port X are:
  121. // X / 10 + ((X % 10) * 3)
  122. // sysFds:
  123. // Map a file descriptor from the /sys/class/gpio/gpioX/value file
  124. static int sysFds [64] ;
  125. // Mode
  126. static int gpioPinMode ;
  127. // Doing it the Arduino way with lookup tables...
  128. // Yes, it's probably more innefficient than all the bit-twidling, but it
  129. // does tend to make it all a bit clearer. At least to me!
  130. // pinToGpio:
  131. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  132. static int pinToGpio [] =
  133. {
  134. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
  135. 0, 1, // I2C - SDA0, SCL0
  136. 8, 7, // SPI - CE1, CE0
  137. 10, 9, 11, // SPI - MOSI, MISO, SCLK
  138. 14, 15, // UART - Tx, Rx
  139. } ;
  140. // gpioToGPFSEL:
  141. // Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5)
  142. static uint8_t gpioToGPFSEL [] =
  143. {
  144. 0,0,0,0,0,0,0,0,0,0,
  145. 1,1,1,1,1,1,1,1,1,1,
  146. 2,2,2,2,2,2,2,2,2,2,
  147. 3,3,3,3,3,3,3,3,3,3,
  148. 4,4,4,4,4,4,4,4,4,4,
  149. 5,5,5,5,5,5,5,5,5,5,
  150. } ;
  151. // gpioToShift
  152. // Define the shift up for the 3 bits per pin in each GPFSEL port
  153. static uint8_t gpioToShift [] =
  154. {
  155. 0,3,6,9,12,15,18,21,24,27,
  156. 0,3,6,9,12,15,18,21,24,27,
  157. 0,3,6,9,12,15,18,21,24,27,
  158. 0,3,6,9,12,15,18,21,24,27,
  159. 0,3,6,9,12,15,18,21,24,27,
  160. } ;
  161. // gpioToGPSET:
  162. // (Word) offset to the GPIO Set registers for each GPIO pin
  163. static uint8_t gpioToGPSET [] =
  164. {
  165. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  166. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  167. } ;
  168. // gpioToGPCLR:
  169. // (Word) offset to the GPIO Clear registers for each GPIO pin
  170. static uint8_t gpioToGPCLR [] =
  171. {
  172. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
  173. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  174. } ;
  175. // gpioToGPLEV:
  176. // (Word) offset to the GPIO Input level registers for each GPIO pin
  177. static uint8_t gpioToGPLEV [] =
  178. {
  179. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
  180. 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
  181. } ;
  182. // gpioToPUDCLK
  183. // (Word) offset to the Pull Up Down Clock regsiter
  184. static uint8_t gpioToPUDCLK [] =
  185. {
  186. 38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,
  187. 39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,
  188. } ;
  189. // gpioToPwmALT
  190. // the ALT value to put a GPIO pin into PWM mode
  191. static uint8_t gpioToPwmALT [] =
  192. {
  193. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  194. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  195. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  196. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  197. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  198. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  199. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  200. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  201. } ;
  202. static uint8_t gpioToPwmPort [] =
  203. {
  204. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  205. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  206. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  207. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  208. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  209. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  210. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  211. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  212. } ;
  213. // Time for easy calculations
  214. static unsigned long long epoch ;
  215. //////////////////////////////////////////////////////////////////////////////////
  216. /*
  217. * wiringPiGpioMode:
  218. * Set the mode - use Pin numbers (0-16) or GPIO number (seemingly random)
  219. *********************************************************************************
  220. */
  221. void wiringPiGpioMode (int mode)
  222. {
  223. gpioPinMode = mode ;
  224. }
  225. /*
  226. * wiringPiSetup:
  227. * Must be called once at the start of your program execution.
  228. *
  229. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  230. * memory mapped hardware directly.
  231. *********************************************************************************
  232. */
  233. int wiringPiSetup (void)
  234. {
  235. int fd ;
  236. uint8_t *gpioMem, *pwmMem, *clkMem ;
  237. struct timeval tv ;
  238. #ifdef DEBUG_PADS
  239. uint8_t *gpioMem, *padsMem, *pwmMem, *clkMem ;
  240. uint32_t *pads ;
  241. #endif
  242. // Set Pin mode by default
  243. wiringPiGpioMode (WPI_MODE_PINS) ;
  244. // Open the master /dev/memory device
  245. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  246. {
  247. fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  248. return -1 ;
  249. }
  250. // GPIO:
  251. // Allocate 2 pages - 1 ...
  252. if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  253. {
  254. fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
  255. return -1 ;
  256. }
  257. // ... presumably to make sure we can round it up to a whole page size
  258. if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
  259. gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;
  260. gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;
  261. if ((int32_t)gpio < 0)
  262. {
  263. fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  264. return -1 ;
  265. }
  266. // PWM
  267. if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  268. {
  269. fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
  270. return -1 ;
  271. }
  272. if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
  273. pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;
  274. pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;
  275. if ((int32_t)pwm < 0)
  276. {
  277. fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
  278. return -1 ;
  279. }
  280. // Clock control (needed for PWM)
  281. if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  282. {
  283. fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
  284. return -1 ;
  285. }
  286. if (((uint32_t)clkMem % PAGE_SIZE) != 0)
  287. clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;
  288. clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;
  289. if ((int32_t)clk < 0)
  290. {
  291. fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
  292. return -1 ;
  293. }
  294. #ifdef DEBUG_PADS
  295. if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  296. {
  297. fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
  298. return -1 ;
  299. }
  300. if (((uint32_t)padsMem % PAGE_SIZE) != 0)
  301. padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;
  302. pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;
  303. if ((int32_t)pads < 0)
  304. {
  305. fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
  306. return -1 ;
  307. }
  308. printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
  309. printf ("%08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
  310. // *(pads + 11) = 0x1F ;
  311. printf ("%08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
  312. #endif
  313. gettimeofday (&tv, NULL) ;
  314. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  315. return 0 ;
  316. }
  317. /*
  318. * wiringPiSetupGpio:
  319. * Must be called once at the start of your program execution.
  320. *
  321. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  322. * memory mapped hardware directly.
  323. *********************************************************************************
  324. */
  325. int wiringPiSetupGpio (void)
  326. {
  327. int x = wiringPiSetup () ;
  328. if (x != 0)
  329. return x ;
  330. wiringPiGpioMode (WPI_MODE_GPIO) ;
  331. return 0 ;
  332. }
  333. /*
  334. * wiringPiSetupSys:
  335. * Must be called once at the start of your program execution.
  336. *
  337. * Initialisation (again), however this time we are using the /sys/class/gpio
  338. * interface to the GPIO systems - slightly slower, but always usable as
  339. * a non-root user, assuming the devices are already exported and setup correctly.
  340. */
  341. int wiringPiSetupSys (void)
  342. {
  343. int fd, pin ;
  344. struct timeval tv ;
  345. char fName [128] ;
  346. // Set GPIO_SYS mode by default
  347. wiringPiGpioMode (WPI_MODE_GPIO_SYS) ;
  348. // Open and scan the directory, looking for exported GPIOs, and pre-open
  349. // the 'value' part to speed things up for later
  350. for (pin = 0 ; pin < 64 ; ++pin)
  351. {
  352. sysFds [pin] = -1 ;
  353. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  354. if ((fd = open (fName, O_RDWR)) == -1)
  355. continue ;
  356. sysFds [pin] = fd ;
  357. }
  358. gettimeofday (&tv, NULL) ;
  359. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  360. return 0 ;
  361. }
  362. /*
  363. * pinMode:
  364. * Sets the mode of a pin to be input, output or PWM output
  365. *********************************************************************************
  366. */
  367. void pinMode (int pin, int mode)
  368. {
  369. static int pwmRunning = FALSE ;
  370. int gpioPin, fSel, shift ;
  371. int alt ;
  372. // We can't change the mode in GPIO_SYS mode
  373. if (gpioPinMode == WPI_MODE_GPIO_SYS)
  374. return ;
  375. if (gpioPinMode == WPI_MODE_PINS)
  376. {
  377. if ((pin < 0) || (pin >= NUM_PINS))
  378. return ;
  379. gpioPin = pinToGpio [pin] ;
  380. }
  381. else
  382. gpioPin = pin ;
  383. fSel = gpioToGPFSEL [gpioPin] ;
  384. shift = gpioToShift [gpioPin] ;
  385. /**/ if (mode == INPUT)
  386. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  387. else if (mode == OUTPUT)
  388. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  389. else if (mode == PWM_OUTPUT)
  390. {
  391. if ((alt = gpioToPwmALT [gpioPin]) == 0) // Not a PWM pin
  392. return ;
  393. // Set pin to PWM mode
  394. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  395. // We didn't initialise the PWM hardware at setup time - because it's possible that
  396. // something else is using the PWM - e.g. the Audio systems! So if we use PWM
  397. // here, then we're assuming that nothing else is, otherwise things are going
  398. // to sound a bit funny...
  399. if (!pwmRunning)
  400. {
  401. // Gert/Doms Values
  402. *(clk + PWMCLK_DIV) = 0x5A000000 | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz)
  403. *(clk + PWMCLK_CNTL) = 0x5A000011 ; // Source=osc and enable
  404. digitalWrite (pin, LOW) ;
  405. *(pwm + PWM_CONTROL) = 0 ; // Disable PWM
  406. delayMicroseconds (10) ;
  407. *(pwm + PWM0_RANGE) = 0x400 ;
  408. delayMicroseconds (10) ;
  409. *(pwm + PWM1_RANGE) = 0x400 ;
  410. delayMicroseconds (10) ;
  411. // Enable PWMs
  412. *(pwm + PWM0_DATA) = 512 ;
  413. *(pwm + PWM1_DATA) = 512 ;
  414. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  415. }
  416. }
  417. // When we change mode of any pin, we remove the pull up/downs
  418. pullUpDnControl (pin, PUD_OFF) ;
  419. }
  420. /*
  421. * digitalWrite:
  422. * Set an output bit
  423. *********************************************************************************
  424. */
  425. void digitalWrite (int pin, int value)
  426. {
  427. int gpioPin ;
  428. if (gpioPinMode == WPI_MODE_PINS)
  429. {
  430. if ((pin < 0) || (pin >= NUM_PINS))
  431. return ;
  432. gpioPin = pinToGpio [pin] ;
  433. }
  434. else
  435. gpioPin = pin ;
  436. if (gpioPinMode == WPI_MODE_GPIO_SYS)
  437. {
  438. if (sysFds [gpioPin] != -1)
  439. {
  440. if (value == LOW)
  441. write (sysFds [gpioPin], "0\n", 2) ;
  442. else
  443. write (sysFds [gpioPin], "1\n", 2) ;
  444. }
  445. }
  446. else
  447. {
  448. if (value == LOW)
  449. *(gpio + gpioToGPCLR [gpioPin]) = 1 << gpioPin ;
  450. else
  451. *(gpio + gpioToGPSET [gpioPin]) = 1 << gpioPin ;
  452. }
  453. }
  454. /*
  455. * pwnWrite:
  456. * Set an output PWM value
  457. *********************************************************************************
  458. */
  459. void pwmWrite (int pin, int value)
  460. {
  461. int port, gpioPin ;
  462. // We can't do this in GPIO_SYS mode
  463. if (gpioPinMode == WPI_MODE_GPIO_SYS)
  464. return ;
  465. if (gpioPinMode == WPI_MODE_PINS)
  466. {
  467. if ((pin < 0) || (pin >= NUM_PINS))
  468. return ;
  469. gpioPin = pinToGpio [pin] ;
  470. }
  471. else
  472. gpioPin = pin ;
  473. port = gpioToPwmPort [gpioPin] ;
  474. *(pwm + port) = value & ~0x400 ;
  475. }
  476. /*
  477. * digitalRead:
  478. * Read the value of a given Pin, returning HIGH or LOW
  479. *********************************************************************************
  480. */
  481. int digitalRead (int pin)
  482. {
  483. int gpioPin ;
  484. char c ;
  485. if (gpioPinMode == WPI_MODE_PINS)
  486. {
  487. if ((pin < 0) || (pin >= NUM_PINS))
  488. return 0 ;
  489. gpioPin = pinToGpio [pin] ;
  490. }
  491. else
  492. gpioPin = pin ;
  493. if (gpioPinMode == WPI_MODE_GPIO_SYS)
  494. {
  495. if (sysFds [gpioPin] == -1)
  496. return 0 ;
  497. else
  498. {
  499. lseek (sysFds [gpioPin], 0L, SEEK_SET) ;
  500. read (sysFds [gpioPin], &c, 1) ;
  501. return (c == '0') ? 0 : 1 ;
  502. }
  503. }
  504. else
  505. {
  506. if ((*(gpio + gpioToGPLEV [gpioPin]) & (1 << gpioPin)) != 0)
  507. return HIGH ;
  508. else
  509. return LOW ;
  510. }
  511. }
  512. /*
  513. * pullUpDownCtrl:
  514. * Control the internal pull-up/down resistors on a GPIO pin
  515. * The Arduino only has pull-ups and these are enabled by writing 1
  516. * to a port when in input mode - this paradigm doesn't quite apply
  517. * here though.
  518. *********************************************************************************
  519. */
  520. void pullUpDnControl (int pin, int pud)
  521. {
  522. int gpioPin ;
  523. // We can't do this in GPIO_SYS mode
  524. if (gpioPinMode == WPI_MODE_GPIO_SYS)
  525. return ;
  526. if (gpioPinMode == WPI_MODE_PINS)
  527. {
  528. if ((pin < 0) || (pin >= NUM_PINS))
  529. return ;
  530. gpioPin = pinToGpio [pin] ;
  531. }
  532. else
  533. gpioPin = pin ;
  534. *(gpio + 37) = pud ;
  535. delayMicroseconds (10) ;
  536. *(gpio + gpioToPUDCLK [gpioPin]) = 1 << gpioPin ;
  537. delayMicroseconds (10) ;
  538. *(gpio + 37) = 0 ;
  539. *(gpio + gpioToPUDCLK [gpioPin]) = 0 ;
  540. }
  541. /*
  542. * delay: delayMicroseconds
  543. * Wait for some number of milli/micro seconds
  544. *********************************************************************************
  545. */
  546. void delay (unsigned int howLong)
  547. {
  548. struct timespec sleeper, dummy ;
  549. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  550. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  551. nanosleep (&sleeper, &dummy) ;
  552. }
  553. void delayMicroseconds (unsigned int howLong)
  554. {
  555. struct timespec sleeper, dummy ;
  556. sleeper.tv_sec = 0 ;
  557. sleeper.tv_nsec = (long)(howLong * 1000) ;
  558. nanosleep (&sleeper, &dummy) ;
  559. }
  560. /*
  561. * millis:
  562. * Return a number of milliseconds as an unsigned int.
  563. *********************************************************************************
  564. */
  565. unsigned int millis (void)
  566. {
  567. struct timeval tv ;
  568. unsigned long long t1 ;
  569. gettimeofday (&tv, NULL) ;
  570. t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  571. return (uint32_t)(t1 - epoch) ;
  572. }