Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
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.

1809 lines
53 KiB

  1. /* bcm2835.c
  2. // C and C++ support for Broadcom BCM 2835 as used in Raspberry Pi
  3. // http://elinux.org/RPi_Low-level_peripherals
  4. // http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
  5. //
  6. // Author: Mike McCauley
  7. // Copyright (C) 2011-2013 Mike McCauley
  8. // $Id: bcm2835.c,v 1.25 2018/01/16 21:55:07 mikem Exp mikem $
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <sys/mman.h>
  15. #include <string.h>
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #define BCK2835_LIBRARY_BUILD
  21. #include "bcm2835.h"
  22. /* This define enables a little test program (by default a blinking output on pin RPI_GPIO_PIN_11)
  23. // You can do some safe, non-destructive testing on any platform with:
  24. // gcc bcm2835.c -D BCM2835_TEST
  25. // ./a.out
  26. */
  27. /*#define BCM2835_TEST*/
  28. /* Uncommenting this define compiles alternative I2C code for the version 1 RPi
  29. // The P1 header I2C pins are connected to SDA0 and SCL0 on V1.
  30. // By default I2C code is generated for the V2 RPi which has SDA1 and SCL1 connected.
  31. */
  32. /* #define I2C_V1*/
  33. /* Physical address and size of the peripherals block
  34. // May be overridden on RPi2
  35. */
  36. uint32_t *bcm2835_peripherals_base = (uint32_t *)BCM2835_PERI_BASE;
  37. uint32_t bcm2835_peripherals_size = BCM2835_PERI_SIZE;
  38. /* Virtual memory address of the mapped peripherals block
  39. */
  40. uint32_t *bcm2835_peripherals = (uint32_t *)MAP_FAILED;
  41. /* And the register bases within the peripherals block
  42. */
  43. volatile uint32_t *bcm2835_gpio = (uint32_t *)MAP_FAILED;
  44. volatile uint32_t *bcm2835_pwm = (uint32_t *)MAP_FAILED;
  45. volatile uint32_t *bcm2835_clk = (uint32_t *)MAP_FAILED;
  46. volatile uint32_t *bcm2835_pads = (uint32_t *)MAP_FAILED;
  47. volatile uint32_t *bcm2835_spi0 = (uint32_t *)MAP_FAILED;
  48. volatile uint32_t *bcm2835_bsc0 = (uint32_t *)MAP_FAILED;
  49. volatile uint32_t *bcm2835_bsc1 = (uint32_t *)MAP_FAILED;
  50. volatile uint32_t *bcm2835_st = (uint32_t *)MAP_FAILED;
  51. volatile uint32_t *bcm2835_aux = (uint32_t *)MAP_FAILED;
  52. volatile uint32_t *bcm2835_spi1 = (uint32_t *)MAP_FAILED;
  53. /* This variable allows us to test on hardware other than RPi.
  54. // It prevents access to the kernel memory, and does not do any peripheral access
  55. // Instead it prints out what it _would_ do if debug were 0
  56. */
  57. static uint8_t debug = 0;
  58. /* I2C The time needed to transmit one byte. In microseconds.
  59. */
  60. static int i2c_byte_wait_us = 0;
  61. // Time for millis()
  62. static unsigned long long epoch ;
  63. /*
  64. // Low level register access functions
  65. */
  66. /* Function to return the pointers to the hardware register bases */
  67. uint32_t* bcm2835_regbase(uint8_t regbase)
  68. {
  69. switch (regbase)
  70. {
  71. case BCM2835_REGBASE_ST:
  72. return (uint32_t *)bcm2835_st;
  73. case BCM2835_REGBASE_GPIO:
  74. return (uint32_t *)bcm2835_gpio;
  75. case BCM2835_REGBASE_PWM:
  76. return (uint32_t *)bcm2835_pwm;
  77. case BCM2835_REGBASE_CLK:
  78. return (uint32_t *)bcm2835_clk;
  79. case BCM2835_REGBASE_PADS:
  80. return (uint32_t *)bcm2835_pads;
  81. case BCM2835_REGBASE_SPI0:
  82. return (uint32_t *)bcm2835_spi0;
  83. case BCM2835_REGBASE_BSC0:
  84. return (uint32_t *)bcm2835_bsc0;
  85. case BCM2835_REGBASE_BSC1:
  86. return (uint32_t *)bcm2835_st;
  87. case BCM2835_REGBASE_AUX:
  88. return (uint32_t *)bcm2835_aux;
  89. case BCM2835_REGBASE_SPI1:
  90. return (uint32_t *)bcm2835_spi1;
  91. }
  92. return (uint32_t *)MAP_FAILED;
  93. }
  94. void bcm2835_set_debug(uint8_t d)
  95. {
  96. debug = d;
  97. }
  98. unsigned int bcm2835_version(void)
  99. {
  100. return BCM2835_VERSION;
  101. }
  102. /* Read with memory barriers from peripheral
  103. *
  104. */
  105. uint32_t bcm2835_peri_read(volatile uint32_t* paddr)
  106. {
  107. uint32_t ret;
  108. if (debug)
  109. {
  110. printf("bcm2835_peri_read paddr %p\n", (void *) paddr);
  111. return 0;
  112. }
  113. else
  114. {
  115. __sync_synchronize();
  116. ret = *paddr;
  117. __sync_synchronize();
  118. return ret;
  119. }
  120. }
  121. /* read from peripheral without the read barrier
  122. * This can only be used if more reads to THE SAME peripheral
  123. * will follow. The sequence must terminate with memory barrier
  124. * before any read or write to another peripheral can occur.
  125. * The MB can be explicit, or one of the barrier read/write calls.
  126. */
  127. uint32_t bcm2835_peri_read_nb(volatile uint32_t* paddr)
  128. {
  129. if (debug)
  130. {
  131. printf("bcm2835_peri_read_nb paddr %p\n", paddr);
  132. return 0;
  133. }
  134. else
  135. {
  136. return *paddr;
  137. }
  138. }
  139. /* Write with memory barriers to peripheral
  140. */
  141. void bcm2835_peri_write(volatile uint32_t* paddr, uint32_t value)
  142. {
  143. if (debug)
  144. {
  145. printf("bcm2835_peri_write paddr %p, value %08X\n", paddr, value);
  146. }
  147. else
  148. {
  149. __sync_synchronize();
  150. *paddr = value;
  151. __sync_synchronize();
  152. }
  153. }
  154. /* write to peripheral without the write barrier */
  155. void bcm2835_peri_write_nb(volatile uint32_t* paddr, uint32_t value)
  156. {
  157. if (debug)
  158. {
  159. printf("bcm2835_peri_write_nb paddr %p, value %08X\n",
  160. paddr, value);
  161. }
  162. else
  163. {
  164. *paddr = value;
  165. }
  166. }
  167. /* Set/clear only the bits in value covered by the mask
  168. * This is not atomic - can be interrupted.
  169. */
  170. void bcm2835_peri_set_bits(volatile uint32_t* paddr, uint32_t value, uint32_t mask)
  171. {
  172. uint32_t v = bcm2835_peri_read(paddr);
  173. v = (v & ~mask) | (value & mask);
  174. bcm2835_peri_write(paddr, v);
  175. }
  176. /*
  177. // Low level convenience functions
  178. */
  179. /* Function select
  180. // pin is a BCM2835 GPIO pin number NOT RPi pin number
  181. // There are 6 control registers, each control the functions of a block
  182. // of 10 pins.
  183. // Each control register has 10 sets of 3 bits per GPIO pin:
  184. //
  185. // 000 = GPIO Pin X is an input
  186. // 001 = GPIO Pin X is an output
  187. // 100 = GPIO Pin X takes alternate function 0
  188. // 101 = GPIO Pin X takes alternate function 1
  189. // 110 = GPIO Pin X takes alternate function 2
  190. // 111 = GPIO Pin X takes alternate function 3
  191. // 011 = GPIO Pin X takes alternate function 4
  192. // 010 = GPIO Pin X takes alternate function 5
  193. //
  194. // So the 3 bits for port X are:
  195. // X / 10 + ((X % 10) * 3)
  196. */
  197. void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode)
  198. {
  199. /* Function selects are 10 pins per 32 bit word, 3 bits per pin */
  200. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPFSEL0/4 + (pin/10);
  201. uint8_t shift = (pin % 10) * 3;
  202. uint32_t mask = BCM2835_GPIO_FSEL_MASK << shift;
  203. uint32_t value = mode << shift;
  204. bcm2835_peri_set_bits(paddr, value, mask);
  205. }
  206. /* Set output pin */
  207. void bcm2835_gpio_set(uint8_t pin)
  208. {
  209. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPSET0/4 + pin/32;
  210. uint8_t shift = pin % 32;
  211. bcm2835_peri_write(paddr, 1 << shift);
  212. }
  213. /* Clear output pin */
  214. void bcm2835_gpio_clr(uint8_t pin)
  215. {
  216. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPCLR0/4 + pin/32;
  217. uint8_t shift = pin % 32;
  218. bcm2835_peri_write(paddr, 1 << shift);
  219. }
  220. /* Set all output pins in the mask */
  221. void bcm2835_gpio_set_multi(uint32_t mask)
  222. {
  223. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPSET0/4;
  224. bcm2835_peri_write(paddr, mask);
  225. }
  226. /* Clear all output pins in the mask */
  227. void bcm2835_gpio_clr_multi(uint32_t mask)
  228. {
  229. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPCLR0/4;
  230. bcm2835_peri_write(paddr, mask);
  231. }
  232. /* Read input pin */
  233. uint8_t bcm2835_gpio_lev(uint8_t pin)
  234. {
  235. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPLEV0/4 + pin/32;
  236. uint8_t shift = pin % 32;
  237. uint32_t value = bcm2835_peri_read(paddr);
  238. return (value & (1 << shift)) ? HIGH : LOW;
  239. }
  240. /* See if an event detection bit is set
  241. // Sigh cant support interrupts yet
  242. */
  243. uint8_t bcm2835_gpio_eds(uint8_t pin)
  244. {
  245. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPEDS0/4 + pin/32;
  246. uint8_t shift = pin % 32;
  247. uint32_t value = bcm2835_peri_read(paddr);
  248. return (value & (1 << shift)) ? HIGH : LOW;
  249. }
  250. uint32_t bcm2835_gpio_eds_multi(uint32_t mask)
  251. {
  252. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPEDS0/4;
  253. uint32_t value = bcm2835_peri_read(paddr);
  254. return (value & mask);
  255. }
  256. /* Write a 1 to clear the bit in EDS */
  257. void bcm2835_gpio_set_eds(uint8_t pin)
  258. {
  259. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPEDS0/4 + pin/32;
  260. uint8_t shift = pin % 32;
  261. uint32_t value = 1 << shift;
  262. bcm2835_peri_write(paddr, value);
  263. }
  264. void bcm2835_gpio_set_eds_multi(uint32_t mask)
  265. {
  266. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPEDS0/4;
  267. bcm2835_peri_write(paddr, mask);
  268. }
  269. /* Rising edge detect enable */
  270. void bcm2835_gpio_ren(uint8_t pin)
  271. {
  272. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPREN0/4 + pin/32;
  273. uint8_t shift = pin % 32;
  274. uint32_t value = 1 << shift;
  275. bcm2835_peri_set_bits(paddr, value, value);
  276. }
  277. void bcm2835_gpio_clr_ren(uint8_t pin)
  278. {
  279. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPREN0/4 + pin/32;
  280. uint8_t shift = pin % 32;
  281. uint32_t value = 1 << shift;
  282. bcm2835_peri_set_bits(paddr, 0, value);
  283. }
  284. /* Falling edge detect enable */
  285. void bcm2835_gpio_fen(uint8_t pin)
  286. {
  287. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPFEN0/4 + pin/32;
  288. uint8_t shift = pin % 32;
  289. uint32_t value = 1 << shift;
  290. bcm2835_peri_set_bits(paddr, value, value);
  291. }
  292. void bcm2835_gpio_clr_fen(uint8_t pin)
  293. {
  294. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPFEN0/4 + pin/32;
  295. uint8_t shift = pin % 32;
  296. uint32_t value = 1 << shift;
  297. bcm2835_peri_set_bits(paddr, 0, value);
  298. }
  299. /* High detect enable */
  300. void bcm2835_gpio_hen(uint8_t pin)
  301. {
  302. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPHEN0/4 + pin/32;
  303. uint8_t shift = pin % 32;
  304. uint32_t value = 1 << shift;
  305. bcm2835_peri_set_bits(paddr, value, value);
  306. }
  307. void bcm2835_gpio_clr_hen(uint8_t pin)
  308. {
  309. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPHEN0/4 + pin/32;
  310. uint8_t shift = pin % 32;
  311. uint32_t value = 1 << shift;
  312. bcm2835_peri_set_bits(paddr, 0, value);
  313. }
  314. /* Low detect enable */
  315. void bcm2835_gpio_len(uint8_t pin)
  316. {
  317. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPLEN0/4 + pin/32;
  318. uint8_t shift = pin % 32;
  319. uint32_t value = 1 << shift;
  320. bcm2835_peri_set_bits(paddr, value, value);
  321. }
  322. void bcm2835_gpio_clr_len(uint8_t pin)
  323. {
  324. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPLEN0/4 + pin/32;
  325. uint8_t shift = pin % 32;
  326. uint32_t value = 1 << shift;
  327. bcm2835_peri_set_bits(paddr, 0, value);
  328. }
  329. /* Async rising edge detect enable */
  330. void bcm2835_gpio_aren(uint8_t pin)
  331. {
  332. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPAREN0/4 + pin/32;
  333. uint8_t shift = pin % 32;
  334. uint32_t value = 1 << shift;
  335. bcm2835_peri_set_bits(paddr, value, value);
  336. }
  337. void bcm2835_gpio_clr_aren(uint8_t pin)
  338. {
  339. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPAREN0/4 + pin/32;
  340. uint8_t shift = pin % 32;
  341. uint32_t value = 1 << shift;
  342. bcm2835_peri_set_bits(paddr, 0, value);
  343. }
  344. /* Async falling edge detect enable */
  345. void bcm2835_gpio_afen(uint8_t pin)
  346. {
  347. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPAFEN0/4 + pin/32;
  348. uint8_t shift = pin % 32;
  349. uint32_t value = 1 << shift;
  350. bcm2835_peri_set_bits(paddr, value, value);
  351. }
  352. void bcm2835_gpio_clr_afen(uint8_t pin)
  353. {
  354. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPAFEN0/4 + pin/32;
  355. uint8_t shift = pin % 32;
  356. uint32_t value = 1 << shift;
  357. bcm2835_peri_set_bits(paddr, 0, value);
  358. }
  359. /* Set pullup/down */
  360. void bcm2835_gpio_pud(uint8_t pud)
  361. {
  362. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPPUD/4;
  363. bcm2835_peri_write(paddr, pud);
  364. }
  365. /* Pullup/down clock
  366. // Clocks the value of pud into the GPIO pin
  367. */
  368. void bcm2835_gpio_pudclk(uint8_t pin, uint8_t on)
  369. {
  370. volatile uint32_t* paddr = bcm2835_gpio + BCM2835_GPPUDCLK0/4 + pin/32;
  371. uint8_t shift = pin % 32;
  372. bcm2835_peri_write(paddr, (on ? 1 : 0) << shift);
  373. }
  374. /* Read GPIO pad behaviour for groups of GPIOs */
  375. uint32_t bcm2835_gpio_pad(uint8_t group)
  376. {
  377. if (bcm2835_pads == MAP_FAILED)
  378. return 0;
  379. volatile uint32_t* paddr = bcm2835_pads + BCM2835_PADS_GPIO_0_27/4 + group;
  380. return bcm2835_peri_read(paddr);
  381. }
  382. /* Set GPIO pad behaviour for groups of GPIOs
  383. // powerup value for all pads is
  384. // BCM2835_PAD_SLEW_RATE_UNLIMITED | BCM2835_PAD_HYSTERESIS_ENABLED | BCM2835_PAD_DRIVE_8mA
  385. */
  386. void bcm2835_gpio_set_pad(uint8_t group, uint32_t control)
  387. {
  388. if (bcm2835_pads == MAP_FAILED)
  389. return;
  390. volatile uint32_t* paddr = bcm2835_pads + BCM2835_PADS_GPIO_0_27/4 + group;
  391. bcm2835_peri_write(paddr, control | BCM2835_PAD_PASSWRD);
  392. }
  393. /* Some convenient arduino-like functions
  394. // milliseconds
  395. */
  396. void bcm2835_delay(unsigned int millis)
  397. {
  398. struct timespec sleeper;
  399. sleeper.tv_sec = (time_t)(millis / 1000);
  400. sleeper.tv_nsec = (long)(millis % 1000) * 1000000;
  401. nanosleep(&sleeper, NULL);
  402. }
  403. /* microseconds */
  404. void bcm2835_delayMicroseconds(uint64_t micros)
  405. {
  406. struct timespec t1;
  407. uint64_t start;
  408. if (debug)
  409. {
  410. /* Cant access sytem timers in debug mode */
  411. printf("bcm2835_delayMicroseconds %lld\n", (long long int) micros);
  412. return;
  413. }
  414. /* Calling nanosleep() takes at least 100-200 us, so use it for
  415. // long waits and use a busy wait on the System Timer for the rest.
  416. */
  417. start = bcm2835_st_read();
  418. /* Not allowed to access timer registers (result is not as precise)*/
  419. if (start==0)
  420. {
  421. t1.tv_sec = 0;
  422. t1.tv_nsec = 1000 * (long)(micros);
  423. nanosleep(&t1, NULL);
  424. return;
  425. }
  426. if (micros > 450)
  427. {
  428. t1.tv_sec = 0;
  429. t1.tv_nsec = 1000 * (long)(micros - 200);
  430. nanosleep(&t1, NULL);
  431. }
  432. bcm2835_st_delay(start, micros);
  433. }
  434. // This function is added in order to simulate arduino millis() function
  435. unsigned int bcm2835_millis(void)
  436. {
  437. struct timeval now;
  438. unsigned long long ms;
  439. gettimeofday(&now, NULL);
  440. ms = (now.tv_sec * 1000000 + now.tv_usec) / 1000 ;
  441. return ((uint32_t) (ms - epoch ));
  442. }
  443. /*
  444. // Higher level convenience functions
  445. */
  446. /* Set the state of an output */
  447. void bcm2835_gpio_write(uint8_t pin, uint8_t on)
  448. {
  449. if (on)
  450. bcm2835_gpio_set(pin);
  451. else
  452. bcm2835_gpio_clr(pin);
  453. }
  454. /* Set the state of a all 32 outputs in the mask to on or off */
  455. void bcm2835_gpio_write_multi(uint32_t mask, uint8_t on)
  456. {
  457. if (on)
  458. bcm2835_gpio_set_multi(mask);
  459. else
  460. bcm2835_gpio_clr_multi(mask);
  461. }
  462. /* Set the state of a all 32 outputs in the mask to the values in value */
  463. void bcm2835_gpio_write_mask(uint32_t value, uint32_t mask)
  464. {
  465. bcm2835_gpio_set_multi(value & mask);
  466. bcm2835_gpio_clr_multi((~value) & mask);
  467. }
  468. /* Set the pullup/down resistor for a pin
  469. //
  470. // The GPIO Pull-up/down Clock Registers control the actuation of internal pull-downs on
  471. // the respective GPIO pins. These registers must be used in conjunction with the GPPUD
  472. // register to effect GPIO Pull-up/down changes. The following sequence of events is
  473. // required:
  474. // 1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
  475. // to remove the current Pull-up/down)
  476. // 2. Wait 150 cycles ? this provides the required set-up time for the control signal
  477. // 3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
  478. // modify ? NOTE only the pads which receive a clock will be modified, all others will
  479. // retain their previous state.
  480. // 4. Wait 150 cycles ? this provides the required hold time for the control signal
  481. // 5. Write to GPPUD to remove the control signal
  482. // 6. Write to GPPUDCLK0/1 to remove the clock
  483. //
  484. // RPi has P1-03 and P1-05 with 1k8 pullup resistor
  485. */
  486. void bcm2835_gpio_set_pud(uint8_t pin, uint8_t pud)
  487. {
  488. bcm2835_gpio_pud(pud);
  489. delayMicroseconds(10);
  490. bcm2835_gpio_pudclk(pin, 1);
  491. delayMicroseconds(10);
  492. bcm2835_gpio_pud(BCM2835_GPIO_PUD_OFF);
  493. bcm2835_gpio_pudclk(pin, 0);
  494. }
  495. int bcm2835_spi_begin(void)
  496. {
  497. volatile uint32_t* paddr;
  498. if (bcm2835_spi0 == MAP_FAILED)
  499. return 0; /* bcm2835_init() failed, or not root */
  500. /* Set the SPI0 pins to the Alt 0 function to enable SPI0 access on them */
  501. bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_ALT0); /* CE1 */
  502. bcm2835_gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_ALT0); /* CE0 */
  503. bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_ALT0); /* MISO */
  504. bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0); /* MOSI */
  505. bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_ALT0); /* CLK */
  506. /* Set the SPI CS register to the some sensible defaults */
  507. paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  508. bcm2835_peri_write(paddr, 0); /* All 0s */
  509. /* Clear TX and RX fifos */
  510. bcm2835_peri_write_nb(paddr, BCM2835_SPI0_CS_CLEAR);
  511. return 1; // OK
  512. }
  513. void bcm2835_spi_end(void)
  514. {
  515. /* Set all the SPI0 pins back to input */
  516. bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_INPT); /* CE1 */
  517. bcm2835_gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_INPT); /* CE0 */
  518. bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_INPT); /* MISO */
  519. bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_INPT); /* MOSI */
  520. bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_INPT); /* CLK */
  521. }
  522. void bcm2835_spi_setBitOrder(uint8_t __attribute__((unused)) order)
  523. {
  524. /* BCM2835_SPI_BIT_ORDER_MSBFIRST is the only one supported by SPI0 */
  525. }
  526. /* defaults to 0, which means a divider of 65536.
  527. // The divisor must be a power of 2. Odd numbers
  528. // rounded down. The maximum SPI clock rate is
  529. // of the APB clock
  530. */
  531. void bcm2835_spi_setClockDivider(uint16_t divider)
  532. {
  533. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CLK/4;
  534. bcm2835_peri_write(paddr, divider);
  535. }
  536. void bcm2835_spi_setDataMode(uint8_t mode)
  537. {
  538. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  539. /* Mask in the CPO and CPHA bits of CS */
  540. bcm2835_peri_set_bits(paddr, mode << 2, BCM2835_SPI0_CS_CPOL | BCM2835_SPI0_CS_CPHA);
  541. }
  542. /* Writes (and reads) a single byte to SPI */
  543. uint8_t bcm2835_spi_transfer(uint8_t value)
  544. {
  545. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  546. volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4;
  547. uint32_t ret;
  548. /* This is Polled transfer as per section 10.6.1
  549. // BUG ALERT: what happens if we get interupted in this section, and someone else
  550. // accesses a different peripheral?
  551. // Clear TX and RX fifos
  552. */
  553. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
  554. /* Set TA = 1 */
  555. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
  556. /* Maybe wait for TXD */
  557. while (!(bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD))
  558. ;
  559. /* Write to FIFO, no barrier */
  560. bcm2835_peri_write_nb(fifo, value);
  561. /* Wait for DONE to be set */
  562. while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE))
  563. ;
  564. /* Read any byte that was sent back by the slave while we sere sending to it */
  565. ret = bcm2835_peri_read_nb(fifo);
  566. /* Set TA = 0, and also set the barrier */
  567. bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
  568. return ret;
  569. }
  570. /* Writes (and reads) an number of bytes to SPI */
  571. void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len)
  572. {
  573. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  574. volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4;
  575. uint32_t TXCnt=0;
  576. uint32_t RXCnt=0;
  577. /* This is Polled transfer as per section 10.6.1
  578. // BUG ALERT: what happens if we get interupted in this section, and someone else
  579. // accesses a different peripheral?
  580. */
  581. /* Clear TX and RX fifos */
  582. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
  583. /* Set TA = 1 */
  584. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
  585. /* Use the FIFO's to reduce the interbyte times */
  586. while((TXCnt < len)||(RXCnt < len))
  587. {
  588. /* TX fifo not full, so add some more bytes */
  589. while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD))&&(TXCnt < len ))
  590. {
  591. bcm2835_peri_write_nb(fifo, tbuf[TXCnt]);
  592. TXCnt++;
  593. }
  594. /* Rx fifo not empty, so get the next received bytes */
  595. while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD))&&( RXCnt < len ))
  596. {
  597. rbuf[RXCnt] = bcm2835_peri_read_nb(fifo);
  598. RXCnt++;
  599. }
  600. }
  601. /* Wait for DONE to be set */
  602. while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE))
  603. ;
  604. /* Set TA = 0, and also set the barrier */
  605. bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
  606. }
  607. /* Writes an number of bytes to SPI */
  608. void bcm2835_spi_writenb(const char* tbuf, uint32_t len)
  609. {
  610. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  611. volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4;
  612. uint32_t i;
  613. /* This is Polled transfer as per section 10.6.1
  614. // BUG ALERT: what happens if we get interupted in this section, and someone else
  615. // accesses a different peripheral?
  616. // Answer: an ISR is required to issue the required memory barriers.
  617. */
  618. /* Clear TX and RX fifos */
  619. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
  620. /* Set TA = 1 */
  621. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
  622. for (i = 0; i < len; i++)
  623. {
  624. /* Maybe wait for TXD */
  625. while (!(bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD))
  626. ;
  627. /* Write to FIFO, no barrier */
  628. bcm2835_peri_write_nb(fifo, tbuf[i]);
  629. /* Read from FIFO to prevent stalling */
  630. while (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD)
  631. (void) bcm2835_peri_read_nb(fifo);
  632. }
  633. /* Wait for DONE to be set */
  634. while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) {
  635. while (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD)
  636. (void) bcm2835_peri_read_nb(fifo);
  637. };
  638. /* Set TA = 0, and also set the barrier */
  639. bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
  640. }
  641. /* Writes (and reads) an number of bytes to SPI
  642. // Read bytes are copied over onto the transmit buffer
  643. */
  644. void bcm2835_spi_transfern(char* buf, uint32_t len)
  645. {
  646. bcm2835_spi_transfernb(buf, buf, len);
  647. }
  648. void bcm2835_spi_chipSelect(uint8_t cs)
  649. {
  650. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  651. /* Mask in the CS bits of CS */
  652. bcm2835_peri_set_bits(paddr, cs, BCM2835_SPI0_CS_CS);
  653. }
  654. void bcm2835_spi_setChipSelectPolarity(uint8_t cs, uint8_t active)
  655. {
  656. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  657. uint8_t shift = 21 + cs;
  658. /* Mask in the appropriate CSPOLn bit */
  659. bcm2835_peri_set_bits(paddr, active << shift, 1 << shift);
  660. }
  661. void bcm2835_spi_write(uint16_t data) {
  662. #if 0
  663. char buf[2];
  664. buf[0] = data >> 8;
  665. buf[1] = data & 0xFF;
  666. bcm2835_spi_transfern(buf, 2);
  667. #else
  668. volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
  669. volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4;
  670. /* Clear TX and RX fifos */
  671. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
  672. /* Set TA = 1 */
  673. bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
  674. /* Maybe wait for TXD */
  675. while (!(bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD))
  676. ;
  677. /* Write to FIFO */
  678. bcm2835_peri_write_nb(fifo, (uint32_t) data >> 8);
  679. bcm2835_peri_write_nb(fifo, data & 0xFF);
  680. /* Wait for DONE to be set */
  681. while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE))
  682. ;
  683. /* Set TA = 0, and also set the barrier */
  684. bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
  685. #endif
  686. }
  687. int bcm2835_aux_spi_begin(void) {
  688. volatile uint32_t* enable = bcm2835_aux + BCM2835_AUX_ENABLE/4;
  689. volatile uint32_t* cntl0 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL0/4;
  690. volatile uint32_t* cntl1 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL1/4;
  691. if (bcm2835_spi1 == MAP_FAILED)
  692. return 0; /* bcm2835_init() failed, or not root */
  693. /* Set the SPI pins to the Alt 4 function to enable SPI1 access on them */
  694. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_36, BCM2835_GPIO_FSEL_ALT4); /* SPI1_CE2_N */
  695. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_35, BCM2835_GPIO_FSEL_ALT4); /* SPI1_MISO */
  696. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_38, BCM2835_GPIO_FSEL_ALT4); /* SPI1_MOSI */
  697. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_40, BCM2835_GPIO_FSEL_ALT4); /* SPI1_SCLK */
  698. bcm2835_aux_spi_setClockDivider(bcm2835_aux_spi_CalcClockDivider(1000000)); // Default 1MHz SPI
  699. bcm2835_peri_write(enable, BCM2835_AUX_ENABLE_SPI0);
  700. bcm2835_peri_write(cntl1, 0);
  701. bcm2835_peri_write(cntl0, BCM2835_AUX_SPI_CNTL0_CLEARFIFO);
  702. return 1; /* OK */
  703. }
  704. void bcm2835_aux_spi_end(void) {
  705. /* Set all the SPI1 pins back to input */
  706. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_36, BCM2835_GPIO_FSEL_INPT); /* SPI1_CE2_N */
  707. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_35, BCM2835_GPIO_FSEL_INPT); /* SPI1_MISO */
  708. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_38, BCM2835_GPIO_FSEL_INPT); /* SPI1_MOSI */
  709. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_40, BCM2835_GPIO_FSEL_INPT); /* SPI1_SCLK */
  710. }
  711. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  712. uint16_t bcm2835_aux_spi_CalcClockDivider(uint32_t speed_hz) {
  713. uint16_t divider;
  714. if (speed_hz < (uint32_t) BCM2835_AUX_SPI_CLOCK_MIN) {
  715. speed_hz = (uint32_t) BCM2835_AUX_SPI_CLOCK_MIN;
  716. } else if (speed_hz > (uint32_t) BCM2835_AUX_SPI_CLOCK_MAX) {
  717. speed_hz = (uint32_t) BCM2835_AUX_SPI_CLOCK_MAX;
  718. }
  719. divider = (uint16_t) DIV_ROUND_UP(BCM2835_CORE_CLK_HZ, 2 * speed_hz) - 1;
  720. if (divider > (uint16_t) BCM2835_AUX_SPI_CNTL0_SPEED_MAX) {
  721. return (uint16_t) BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  722. }
  723. return divider;
  724. }
  725. static uint32_t spi1_speed;
  726. void bcm2835_aux_spi_setClockDivider(uint16_t divider) {
  727. spi1_speed = (uint32_t) divider;
  728. }
  729. void bcm2835_aux_spi_write(uint16_t data) {
  730. volatile uint32_t* cntl0 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL0/4;
  731. volatile uint32_t* cntl1 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL1/4;
  732. volatile uint32_t* stat = bcm2835_spi1 + BCM2835_AUX_SPI_STAT/4;
  733. volatile uint32_t* io = bcm2835_spi1 + BCM2835_AUX_SPI_IO/4;
  734. uint32_t _cntl0 = (spi1_speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT);
  735. _cntl0 |= BCM2835_AUX_SPI_CNTL0_CS2_N;
  736. _cntl0 |= BCM2835_AUX_SPI_CNTL0_ENABLE;
  737. _cntl0 |= BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  738. _cntl0 |= 16; // Shift length
  739. bcm2835_peri_write(cntl0, _cntl0);
  740. bcm2835_peri_write(cntl1, BCM2835_AUX_SPI_CNTL1_MSBF_IN);
  741. while (bcm2835_peri_read(stat) & BCM2835_AUX_SPI_STAT_TX_FULL)
  742. ;
  743. bcm2835_peri_write(io, (uint32_t) data << 16);
  744. }
  745. void bcm2835_aux_spi_writenb(const char *tbuf, uint32_t len) {
  746. volatile uint32_t* cntl0 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL0/4;
  747. volatile uint32_t* cntl1 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL1/4;
  748. volatile uint32_t* stat = bcm2835_spi1 + BCM2835_AUX_SPI_STAT/4;
  749. volatile uint32_t* txhold = bcm2835_spi1 + BCM2835_AUX_SPI_TXHOLD/4;
  750. volatile uint32_t* io = bcm2835_spi1 + BCM2835_AUX_SPI_IO/4;
  751. char *tx = (char *) tbuf;
  752. uint32_t tx_len = len;
  753. uint32_t count;
  754. uint32_t data;
  755. uint32_t i;
  756. uint8_t byte;
  757. uint32_t _cntl0 = (spi1_speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT);
  758. _cntl0 |= BCM2835_AUX_SPI_CNTL0_CS2_N;
  759. _cntl0 |= BCM2835_AUX_SPI_CNTL0_ENABLE;
  760. _cntl0 |= BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  761. _cntl0 |= BCM2835_AUX_SPI_CNTL0_VAR_WIDTH;
  762. bcm2835_peri_write(cntl0, _cntl0);
  763. bcm2835_peri_write(cntl1, BCM2835_AUX_SPI_CNTL1_MSBF_IN);
  764. while (tx_len > 0) {
  765. while (bcm2835_peri_read(stat) & BCM2835_AUX_SPI_STAT_TX_FULL)
  766. ;
  767. count = MIN(tx_len, 3);
  768. data = 0;
  769. for (i = 0; i < count; i++) {
  770. byte = (tx != NULL) ? (uint8_t) *tx++ : (uint8_t) 0;
  771. data |= byte << (8 * (2 - i));
  772. }
  773. data |= (count * 8) << 24;
  774. tx_len -= count;
  775. if (tx_len != 0) {
  776. bcm2835_peri_write(txhold, data);
  777. } else {
  778. bcm2835_peri_write(io, data);
  779. }
  780. while (bcm2835_peri_read(stat) & BCM2835_AUX_SPI_STAT_BUSY)
  781. ;
  782. (void) bcm2835_peri_read(io);
  783. }
  784. }
  785. void bcm2835_aux_spi_transfernb(const char *tbuf, char *rbuf, uint32_t len) {
  786. volatile uint32_t* cntl0 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL0/4;
  787. volatile uint32_t* cntl1 = bcm2835_spi1 + BCM2835_AUX_SPI_CNTL1/4;
  788. volatile uint32_t* stat = bcm2835_spi1 + BCM2835_AUX_SPI_STAT/4;
  789. volatile uint32_t* txhold = bcm2835_spi1 + BCM2835_AUX_SPI_TXHOLD/4;
  790. volatile uint32_t* io = bcm2835_spi1 + BCM2835_AUX_SPI_IO/4;
  791. char *tx = (char *)tbuf;
  792. char *rx = (char *)rbuf;
  793. uint32_t tx_len = len;
  794. uint32_t rx_len = len;
  795. uint32_t count;
  796. uint32_t data;
  797. uint32_t i;
  798. uint8_t byte;
  799. uint32_t _cntl0 = (spi1_speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT);
  800. _cntl0 |= BCM2835_AUX_SPI_CNTL0_CS2_N;
  801. _cntl0 |= BCM2835_AUX_SPI_CNTL0_ENABLE;
  802. _cntl0 |= BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  803. _cntl0 |= BCM2835_AUX_SPI_CNTL0_VAR_WIDTH;
  804. bcm2835_peri_write(cntl0, _cntl0);
  805. bcm2835_peri_write(cntl1, BCM2835_AUX_SPI_CNTL1_MSBF_IN);
  806. while ((tx_len > 0) || (rx_len > 0)) {
  807. while (!(bcm2835_peri_read(stat) & BCM2835_AUX_SPI_STAT_TX_FULL) && (tx_len > 0)) {
  808. count = MIN(tx_len, 3);
  809. data = 0;
  810. for (i = 0; i < count; i++) {
  811. byte = (tx != NULL) ? (uint8_t) *tx++ : (uint8_t) 0;
  812. data |= byte << (8 * (2 - i));
  813. }
  814. data |= (count * 8) << 24;
  815. tx_len -= count;
  816. if (tx_len != 0) {
  817. bcm2835_peri_write(txhold, data);
  818. } else {
  819. bcm2835_peri_write(io, data);
  820. }
  821. }
  822. while (!(bcm2835_peri_read(stat) & BCM2835_AUX_SPI_STAT_RX_EMPTY) && (rx_len > 0)) {
  823. count = MIN(rx_len, 3);
  824. data = bcm2835_peri_read(io);
  825. if (rbuf != NULL) {
  826. switch (count) {
  827. case 3:
  828. *rx++ = (char)((data >> 16) & 0xFF);
  829. /*@fallthrough@*/
  830. /* no break */
  831. case 2:
  832. *rx++ = (char)((data >> 8) & 0xFF);
  833. /*@fallthrough@*/
  834. /* no break */
  835. case 1:
  836. *rx++ = (char)((data >> 0) & 0xFF);
  837. }
  838. }
  839. rx_len -= count;
  840. }
  841. while (!(bcm2835_peri_read(stat) & BCM2835_AUX_SPI_STAT_BUSY) && (rx_len > 0)) {
  842. count = MIN(rx_len, 3);
  843. data = bcm2835_peri_read(io);
  844. if (rbuf != NULL) {
  845. switch (count) {
  846. case 3:
  847. *rx++ = (char)((data >> 16) & 0xFF);
  848. /*@fallthrough@*/
  849. /* no break */
  850. case 2:
  851. *rx++ = (char)((data >> 8) & 0xFF);
  852. /*@fallthrough@*/
  853. /* no break */
  854. case 1:
  855. *rx++ = (char)((data >> 0) & 0xFF);
  856. }
  857. }
  858. rx_len -= count;
  859. }
  860. }
  861. }
  862. void bcm2835_aux_spi_transfern(char *buf, uint32_t len) {
  863. bcm2835_aux_spi_transfernb(buf, buf, len);
  864. }
  865. int bcm2835_i2c_begin(void)
  866. {
  867. uint16_t cdiv;
  868. if ( bcm2835_bsc0 == MAP_FAILED
  869. || bcm2835_bsc1 == MAP_FAILED)
  870. return 0; /* bcm2835_init() failed, or not root */
  871. #ifdef I2C_V1
  872. volatile uint32_t* paddr = bcm2835_bsc0 + BCM2835_BSC_DIV/4;
  873. /* Set the I2C/BSC0 pins to the Alt 0 function to enable I2C access on them */
  874. bcm2835_gpio_fsel(RPI_GPIO_P1_03, BCM2835_GPIO_FSEL_ALT0); /* SDA */
  875. bcm2835_gpio_fsel(RPI_GPIO_P1_05, BCM2835_GPIO_FSEL_ALT0); /* SCL */
  876. #else
  877. volatile uint32_t* paddr = bcm2835_bsc1 + BCM2835_BSC_DIV/4;
  878. /* Set the I2C/BSC1 pins to the Alt 0 function to enable I2C access on them */
  879. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_03, BCM2835_GPIO_FSEL_ALT0); /* SDA */
  880. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_05, BCM2835_GPIO_FSEL_ALT0); /* SCL */
  881. #endif
  882. /* Read the clock divider register */
  883. cdiv = bcm2835_peri_read(paddr);
  884. /* Calculate time for transmitting one byte
  885. // 1000000 = micros seconds in a second
  886. // 9 = Clocks per byte : 8 bits + ACK
  887. */
  888. i2c_byte_wait_us = ((float)cdiv / BCM2835_CORE_CLK_HZ) * 1000000 * 9;
  889. return 1;
  890. }
  891. void bcm2835_i2c_end(void)
  892. {
  893. #ifdef I2C_V1
  894. /* Set all the I2C/BSC0 pins back to input */
  895. bcm2835_gpio_fsel(RPI_GPIO_P1_03, BCM2835_GPIO_FSEL_INPT); /* SDA */
  896. bcm2835_gpio_fsel(RPI_GPIO_P1_05, BCM2835_GPIO_FSEL_INPT); /* SCL */
  897. #else
  898. /* Set all the I2C/BSC1 pins back to input */
  899. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_03, BCM2835_GPIO_FSEL_INPT); /* SDA */
  900. bcm2835_gpio_fsel(RPI_V2_GPIO_P1_05, BCM2835_GPIO_FSEL_INPT); /* SCL */
  901. #endif
  902. }
  903. void bcm2835_i2c_setSlaveAddress(uint8_t addr)
  904. {
  905. /* Set I2C Device Address */
  906. #ifdef I2C_V1
  907. volatile uint32_t* paddr = bcm2835_bsc0 + BCM2835_BSC_A/4;
  908. #else
  909. volatile uint32_t* paddr = bcm2835_bsc1 + BCM2835_BSC_A/4;
  910. #endif
  911. bcm2835_peri_write(paddr, addr);
  912. }
  913. /* defaults to 0x5dc, should result in a 166.666 kHz I2C clock frequency.
  914. // The divisor must be a power of 2. Odd numbers
  915. // rounded down.
  916. */
  917. void bcm2835_i2c_setClockDivider(uint16_t divider)
  918. {
  919. #ifdef I2C_V1
  920. volatile uint32_t* paddr = bcm2835_bsc0 + BCM2835_BSC_DIV/4;
  921. #else
  922. volatile uint32_t* paddr = bcm2835_bsc1 + BCM2835_BSC_DIV/4;
  923. #endif
  924. bcm2835_peri_write(paddr, divider);
  925. /* Calculate time for transmitting one byte
  926. // 1000000 = micros seconds in a second
  927. // 9 = Clocks per byte : 8 bits + ACK
  928. */
  929. i2c_byte_wait_us = ((float)divider / BCM2835_CORE_CLK_HZ) * 1000000 * 9;
  930. }
  931. /* set I2C clock divider by means of a baudrate number */
  932. void bcm2835_i2c_set_baudrate(uint32_t baudrate)
  933. {
  934. uint32_t divider;
  935. /* use 0xFFFE mask to limit a max value and round down any odd number */
  936. divider = (BCM2835_CORE_CLK_HZ / baudrate) & 0xFFFE;
  937. bcm2835_i2c_setClockDivider( (uint16_t)divider );
  938. }
  939. /* Writes an number of bytes to I2C */
  940. uint8_t bcm2835_i2c_write(const char * buf, uint32_t len)
  941. {
  942. #ifdef I2C_V1
  943. volatile uint32_t* dlen = bcm2835_bsc0 + BCM2835_BSC_DLEN/4;
  944. volatile uint32_t* fifo = bcm2835_bsc0 + BCM2835_BSC_FIFO/4;
  945. volatile uint32_t* status = bcm2835_bsc0 + BCM2835_BSC_S/4;
  946. volatile uint32_t* control = bcm2835_bsc0 + BCM2835_BSC_C/4;
  947. #else
  948. volatile uint32_t* dlen = bcm2835_bsc1 + BCM2835_BSC_DLEN/4;
  949. volatile uint32_t* fifo = bcm2835_bsc1 + BCM2835_BSC_FIFO/4;
  950. volatile uint32_t* status = bcm2835_bsc1 + BCM2835_BSC_S/4;
  951. volatile uint32_t* control = bcm2835_bsc1 + BCM2835_BSC_C/4;
  952. #endif
  953. uint32_t remaining = len;
  954. uint32_t i = 0;
  955. uint8_t reason = BCM2835_I2C_REASON_OK;
  956. /* Clear FIFO */
  957. bcm2835_peri_set_bits(control, BCM2835_BSC_C_CLEAR_1 , BCM2835_BSC_C_CLEAR_1 );
  958. /* Clear Status */
  959. bcm2835_peri_write(status, BCM2835_BSC_S_CLKT | BCM2835_BSC_S_ERR | BCM2835_BSC_S_DONE);
  960. /* Set Data Length */
  961. bcm2835_peri_write(dlen, len);
  962. /* pre populate FIFO with max buffer */
  963. while( remaining && ( i < BCM2835_BSC_FIFO_SIZE ) )
  964. {
  965. bcm2835_peri_write_nb(fifo, buf[i]);
  966. i++;
  967. remaining--;
  968. }
  969. /* Enable device and start transfer */
  970. bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST);
  971. /* Transfer is over when BCM2835_BSC_S_DONE */
  972. while(!(bcm2835_peri_read(status) & BCM2835_BSC_S_DONE ))
  973. {
  974. while ( remaining && (bcm2835_peri_read(status) & BCM2835_BSC_S_TXD ))
  975. {
  976. /* Write to FIFO */
  977. bcm2835_peri_write(fifo, buf[i]);
  978. i++;
  979. remaining--;
  980. }
  981. }
  982. /* Received a NACK */
  983. if (bcm2835_peri_read(status) & BCM2835_BSC_S_ERR)
  984. {
  985. reason = BCM2835_I2C_REASON_ERROR_NACK;
  986. }
  987. /* Received Clock Stretch Timeout */
  988. else if (bcm2835_peri_read(status) & BCM2835_BSC_S_CLKT)
  989. {
  990. reason = BCM2835_I2C_REASON_ERROR_CLKT;
  991. }
  992. /* Not all data is sent */
  993. else if (remaining)
  994. {
  995. reason = BCM2835_I2C_REASON_ERROR_DATA;
  996. }
  997. bcm2835_peri_set_bits(control, BCM2835_BSC_S_DONE , BCM2835_BSC_S_DONE);
  998. return reason;
  999. }
  1000. /* Read an number of bytes from I2C */
  1001. uint8_t bcm2835_i2c_read(char* buf, uint32_t len)
  1002. {
  1003. #ifdef I2C_V1
  1004. volatile uint32_t* dlen = bcm2835_bsc0 + BCM2835_BSC_DLEN/4;
  1005. volatile uint32_t* fifo = bcm2835_bsc0 + BCM2835_BSC_FIFO/4;
  1006. volatile uint32_t* status = bcm2835_bsc0 + BCM2835_BSC_S/4;
  1007. volatile uint32_t* control = bcm2835_bsc0 + BCM2835_BSC_C/4;
  1008. #else
  1009. volatile uint32_t* dlen = bcm2835_bsc1 + BCM2835_BSC_DLEN/4;
  1010. volatile uint32_t* fifo = bcm2835_bsc1 + BCM2835_BSC_FIFO/4;
  1011. volatile uint32_t* status = bcm2835_bsc1 + BCM2835_BSC_S/4;
  1012. volatile uint32_t* control = bcm2835_bsc1 + BCM2835_BSC_C/4;
  1013. #endif
  1014. uint32_t remaining = len;
  1015. uint32_t i = 0;
  1016. uint8_t reason = BCM2835_I2C_REASON_OK;
  1017. /* Clear FIFO */
  1018. bcm2835_peri_set_bits(control, BCM2835_BSC_C_CLEAR_1 , BCM2835_BSC_C_CLEAR_1 );
  1019. /* Clear Status */
  1020. bcm2835_peri_write_nb(status, BCM2835_BSC_S_CLKT | BCM2835_BSC_S_ERR | BCM2835_BSC_S_DONE);
  1021. /* Set Data Length */
  1022. bcm2835_peri_write_nb(dlen, len);
  1023. /* Start read */
  1024. bcm2835_peri_write_nb(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST | BCM2835_BSC_C_READ);
  1025. /* wait for transfer to complete */
  1026. while (!(bcm2835_peri_read_nb(status) & BCM2835_BSC_S_DONE))
  1027. {
  1028. /* we must empty the FIFO as it is populated and not use any delay */
  1029. while (bcm2835_peri_read_nb(status) & BCM2835_BSC_S_RXD)
  1030. {
  1031. /* Read from FIFO, no barrier */
  1032. buf[i] = bcm2835_peri_read_nb(fifo);
  1033. i++;
  1034. remaining--;
  1035. }
  1036. }
  1037. /* transfer has finished - grab any remaining stuff in FIFO */
  1038. while (remaining && (bcm2835_peri_read_nb(status) & BCM2835_BSC_S_RXD))
  1039. {
  1040. /* Read from FIFO, no barrier */
  1041. buf[i] = bcm2835_peri_read_nb(fifo);
  1042. i++;
  1043. remaining--;
  1044. }
  1045. /* Received a NACK */
  1046. if (bcm2835_peri_read(status) & BCM2835_BSC_S_ERR)
  1047. {
  1048. reason = BCM2835_I2C_REASON_ERROR_NACK;
  1049. }
  1050. /* Received Clock Stretch Timeout */
  1051. else if (bcm2835_peri_read(status) & BCM2835_BSC_S_CLKT)
  1052. {
  1053. reason = BCM2835_I2C_REASON_ERROR_CLKT;
  1054. }
  1055. /* Not all data is received */
  1056. else if (remaining)
  1057. {
  1058. reason = BCM2835_I2C_REASON_ERROR_DATA;
  1059. }
  1060. bcm2835_peri_set_bits(control, BCM2835_BSC_S_DONE , BCM2835_BSC_S_DONE);
  1061. return reason;
  1062. }
  1063. /* Read an number of bytes from I2C sending a repeated start after writing
  1064. // the required register. Only works if your device supports this mode
  1065. */
  1066. uint8_t bcm2835_i2c_read_register_rs(char* regaddr, char* buf, uint32_t len)
  1067. {
  1068. #ifdef I2C_V1
  1069. volatile uint32_t* dlen = bcm2835_bsc0 + BCM2835_BSC_DLEN/4;
  1070. volatile uint32_t* fifo = bcm2835_bsc0 + BCM2835_BSC_FIFO/4;
  1071. volatile uint32_t* status = bcm2835_bsc0 + BCM2835_BSC_S/4;
  1072. volatile uint32_t* control = bcm2835_bsc0 + BCM2835_BSC_C/4;
  1073. #else
  1074. volatile uint32_t* dlen = bcm2835_bsc1 + BCM2835_BSC_DLEN/4;
  1075. volatile uint32_t* fifo = bcm2835_bsc1 + BCM2835_BSC_FIFO/4;
  1076. volatile uint32_t* status = bcm2835_bsc1 + BCM2835_BSC_S/4;
  1077. volatile uint32_t* control = bcm2835_bsc1 + BCM2835_BSC_C/4;
  1078. #endif
  1079. uint32_t remaining = len;
  1080. uint32_t i = 0;
  1081. uint8_t reason = BCM2835_I2C_REASON_OK;
  1082. /* Clear FIFO */
  1083. bcm2835_peri_set_bits(control, BCM2835_BSC_C_CLEAR_1 , BCM2835_BSC_C_CLEAR_1 );
  1084. /* Clear Status */
  1085. bcm2835_peri_write(status, BCM2835_BSC_S_CLKT | BCM2835_BSC_S_ERR | BCM2835_BSC_S_DONE);
  1086. /* Set Data Length */
  1087. bcm2835_peri_write(dlen, 1);
  1088. /* Enable device and start transfer */
  1089. bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN);
  1090. bcm2835_peri_write(fifo, regaddr[0]);
  1091. bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST);
  1092. /* poll for transfer has started */
  1093. while ( !( bcm2835_peri_read(status) & BCM2835_BSC_S_TA ) )
  1094. {
  1095. /* Linux may cause us to miss entire transfer stage */
  1096. if(bcm2835_peri_read(status) & BCM2835_BSC_S_DONE)
  1097. break;
  1098. }
  1099. /* Send a repeated start with read bit set in address */
  1100. bcm2835_peri_write(dlen, len);
  1101. bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST | BCM2835_BSC_C_READ );
  1102. /* Wait for write to complete and first byte back. */
  1103. bcm2835_delayMicroseconds(i2c_byte_wait_us * 3);
  1104. /* wait for transfer to complete */
  1105. while (!(bcm2835_peri_read(status) & BCM2835_BSC_S_DONE))
  1106. {
  1107. /* we must empty the FIFO as it is populated and not use any delay */
  1108. while (remaining && bcm2835_peri_read(status) & BCM2835_BSC_S_RXD)
  1109. {
  1110. /* Read from FIFO */
  1111. buf[i] = bcm2835_peri_read(fifo);
  1112. i++;
  1113. remaining--;
  1114. }
  1115. }
  1116. /* transfer has finished - grab any remaining stuff in FIFO */
  1117. while (remaining && (bcm2835_peri_read(status) & BCM2835_BSC_S_RXD))
  1118. {
  1119. /* Read from FIFO */
  1120. buf[i] = bcm2835_peri_read(fifo);
  1121. i++;
  1122. remaining--;
  1123. }
  1124. /* Received a NACK */
  1125. if (bcm2835_peri_read(status) & BCM2835_BSC_S_ERR)
  1126. {
  1127. reason = BCM2835_I2C_REASON_ERROR_NACK;
  1128. }
  1129. /* Received Clock Stretch Timeout */
  1130. else if (bcm2835_peri_read(status) & BCM2835_BSC_S_CLKT)
  1131. {
  1132. reason = BCM2835_I2C_REASON_ERROR_CLKT;
  1133. }
  1134. /* Not all data is sent */
  1135. else if (remaining)
  1136. {
  1137. reason = BCM2835_I2C_REASON_ERROR_DATA;
  1138. }
  1139. bcm2835_peri_set_bits(control, BCM2835_BSC_S_DONE , BCM2835_BSC_S_DONE);
  1140. return reason;
  1141. }
  1142. /* Sending an arbitrary number of bytes before issuing a repeated start
  1143. // (with no prior stop) and reading a response. Some devices require this behavior.
  1144. */
  1145. uint8_t bcm2835_i2c_write_read_rs(char* cmds, uint32_t cmds_len, char* buf, uint32_t buf_len)
  1146. {
  1147. #ifdef I2C_V1
  1148. volatile uint32_t* dlen = bcm2835_bsc0 + BCM2835_BSC_DLEN/4;
  1149. volatile uint32_t* fifo = bcm2835_bsc0 + BCM2835_BSC_FIFO/4;
  1150. volatile uint32_t* status = bcm2835_bsc0 + BCM2835_BSC_S/4;
  1151. volatile uint32_t* control = bcm2835_bsc0 + BCM2835_BSC_C/4;
  1152. #else
  1153. volatile uint32_t* dlen = bcm2835_bsc1 + BCM2835_BSC_DLEN/4;
  1154. volatile uint32_t* fifo = bcm2835_bsc1 + BCM2835_BSC_FIFO/4;
  1155. volatile uint32_t* status = bcm2835_bsc1 + BCM2835_BSC_S/4;
  1156. volatile uint32_t* control = bcm2835_bsc1 + BCM2835_BSC_C/4;
  1157. #endif
  1158. uint32_t remaining = cmds_len;
  1159. uint32_t i = 0;
  1160. uint8_t reason = BCM2835_I2C_REASON_OK;
  1161. /* Clear FIFO */
  1162. bcm2835_peri_set_bits(control, BCM2835_BSC_C_CLEAR_1 , BCM2835_BSC_C_CLEAR_1 );
  1163. /* Clear Status */
  1164. bcm2835_peri_write(status, BCM2835_BSC_S_CLKT | BCM2835_BSC_S_ERR | BCM2835_BSC_S_DONE);
  1165. /* Set Data Length */
  1166. bcm2835_peri_write(dlen, cmds_len);
  1167. /* pre populate FIFO with max buffer */
  1168. while( remaining && ( i < BCM2835_BSC_FIFO_SIZE ) )
  1169. {
  1170. bcm2835_peri_write_nb(fifo, cmds[i]);
  1171. i++;
  1172. remaining--;
  1173. }
  1174. /* Enable device and start transfer */
  1175. bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST);
  1176. /* poll for transfer has started (way to do repeated start, from BCM2835 datasheet) */
  1177. while ( !( bcm2835_peri_read(status) & BCM2835_BSC_S_TA ) )
  1178. {
  1179. /* Linux may cause us to miss entire transfer stage */
  1180. if(bcm2835_peri_read_nb(status) & BCM2835_BSC_S_DONE)
  1181. break;
  1182. }
  1183. remaining = buf_len;
  1184. i = 0;
  1185. /* Send a repeated start with read bit set in address */
  1186. bcm2835_peri_write(dlen, buf_len);
  1187. bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST | BCM2835_BSC_C_READ );
  1188. /* Wait for write to complete and first byte back. */
  1189. bcm2835_delayMicroseconds(i2c_byte_wait_us * (cmds_len + 1));
  1190. /* wait for transfer to complete */
  1191. while (!(bcm2835_peri_read_nb(status) & BCM2835_BSC_S_DONE))
  1192. {
  1193. /* we must empty the FIFO as it is populated and not use any delay */
  1194. while (remaining && bcm2835_peri_read(status) & BCM2835_BSC_S_RXD)
  1195. {
  1196. /* Read from FIFO, no barrier */
  1197. buf[i] = bcm2835_peri_read_nb(fifo);
  1198. i++;
  1199. remaining--;
  1200. }
  1201. }
  1202. /* transfer has finished - grab any remaining stuff in FIFO */
  1203. while (remaining && (bcm2835_peri_read(status) & BCM2835_BSC_S_RXD))
  1204. {
  1205. /* Read from FIFO */
  1206. buf[i] = bcm2835_peri_read(fifo);
  1207. i++;
  1208. remaining--;
  1209. }
  1210. /* Received a NACK */
  1211. if (bcm2835_peri_read(status) & BCM2835_BSC_S_ERR)
  1212. {
  1213. reason = BCM2835_I2C_REASON_ERROR_NACK;
  1214. }
  1215. /* Received Clock Stretch Timeout */
  1216. else if (bcm2835_peri_read(status) & BCM2835_BSC_S_CLKT)
  1217. {
  1218. reason = BCM2835_I2C_REASON_ERROR_CLKT;
  1219. }
  1220. /* Not all data is sent */
  1221. else if (remaining)
  1222. {
  1223. reason = BCM2835_I2C_REASON_ERROR_DATA;
  1224. }
  1225. bcm2835_peri_set_bits(control, BCM2835_BSC_S_DONE , BCM2835_BSC_S_DONE);
  1226. return reason;
  1227. }
  1228. /* Read the System Timer Counter (64-bits) */
  1229. uint64_t bcm2835_st_read(void)
  1230. {
  1231. volatile uint32_t* paddr;
  1232. uint32_t hi, lo;
  1233. uint64_t st;
  1234. if (bcm2835_st==MAP_FAILED)
  1235. return 0;
  1236. paddr = bcm2835_st + BCM2835_ST_CHI/4;
  1237. hi = bcm2835_peri_read(paddr);
  1238. paddr = bcm2835_st + BCM2835_ST_CLO/4;
  1239. lo = bcm2835_peri_read(paddr);
  1240. paddr = bcm2835_st + BCM2835_ST_CHI/4;
  1241. st = bcm2835_peri_read(paddr);
  1242. /* Test for overflow */
  1243. if (st == hi)
  1244. {
  1245. st <<= 32;
  1246. st += lo;
  1247. }
  1248. else
  1249. {
  1250. st <<= 32;
  1251. paddr = bcm2835_st + BCM2835_ST_CLO/4;
  1252. st += bcm2835_peri_read(paddr);
  1253. }
  1254. return st;
  1255. }
  1256. /* Delays for the specified number of microseconds with offset */
  1257. void bcm2835_st_delay(uint64_t offset_micros, uint64_t micros)
  1258. {
  1259. uint64_t compare = offset_micros + micros;
  1260. while(bcm2835_st_read() < compare)
  1261. ;
  1262. }
  1263. /* PWM */
  1264. void bcm2835_pwm_set_clock(uint32_t divisor)
  1265. {
  1266. if ( bcm2835_clk == MAP_FAILED
  1267. || bcm2835_pwm == MAP_FAILED)
  1268. return; /* bcm2835_init() failed or not root */
  1269. /* From Gerts code */
  1270. divisor &= 0xfff;
  1271. /* Stop PWM clock */
  1272. bcm2835_peri_write(bcm2835_clk + BCM2835_PWMCLK_CNTL, BCM2835_PWM_PASSWRD | 0x01);
  1273. bcm2835_delay(110); /* Prevents clock going slow */
  1274. /* Wait for the clock to be not busy */
  1275. while ((bcm2835_peri_read(bcm2835_clk + BCM2835_PWMCLK_CNTL) & 0x80) != 0)
  1276. bcm2835_delay(1);
  1277. /* set the clock divider and enable PWM clock */
  1278. bcm2835_peri_write(bcm2835_clk + BCM2835_PWMCLK_DIV, BCM2835_PWM_PASSWRD | (divisor << 12));
  1279. bcm2835_peri_write(bcm2835_clk + BCM2835_PWMCLK_CNTL, BCM2835_PWM_PASSWRD | 0x11); /* Source=osc and enable */
  1280. }
  1281. void bcm2835_pwm_set_mode(uint8_t channel, uint8_t markspace, uint8_t enabled)
  1282. {
  1283. if ( bcm2835_clk == MAP_FAILED
  1284. || bcm2835_pwm == MAP_FAILED)
  1285. return; /* bcm2835_init() failed or not root */
  1286. uint32_t control = bcm2835_peri_read(bcm2835_pwm + BCM2835_PWM_CONTROL);
  1287. if (channel == 0)
  1288. {
  1289. if (markspace)
  1290. control |= BCM2835_PWM0_MS_MODE;
  1291. else
  1292. control &= ~BCM2835_PWM0_MS_MODE;
  1293. if (enabled)
  1294. control |= BCM2835_PWM0_ENABLE;
  1295. else
  1296. control &= ~BCM2835_PWM0_ENABLE;
  1297. }
  1298. else if (channel == 1)
  1299. {
  1300. if (markspace)
  1301. control |= BCM2835_PWM1_MS_MODE;
  1302. else
  1303. control &= ~BCM2835_PWM1_MS_MODE;
  1304. if (enabled)
  1305. control |= BCM2835_PWM1_ENABLE;
  1306. else
  1307. control &= ~BCM2835_PWM1_ENABLE;
  1308. }
  1309. /* If you use the barrier here, wierd things happen, and the commands dont work */
  1310. bcm2835_peri_write_nb(bcm2835_pwm + BCM2835_PWM_CONTROL, control);
  1311. /* bcm2835_peri_write_nb(bcm2835_pwm + BCM2835_PWM_CONTROL, BCM2835_PWM0_ENABLE | BCM2835_PWM1_ENABLE | BCM2835_PWM0_MS_MODE | BCM2835_PWM1_MS_MODE); */
  1312. }
  1313. void bcm2835_pwm_set_range(uint8_t channel, uint32_t range)
  1314. {
  1315. if ( bcm2835_clk == MAP_FAILED
  1316. || bcm2835_pwm == MAP_FAILED)
  1317. return; /* bcm2835_init() failed or not root */
  1318. if (channel == 0)
  1319. bcm2835_peri_write_nb(bcm2835_pwm + BCM2835_PWM0_RANGE, range);
  1320. else if (channel == 1)
  1321. bcm2835_peri_write_nb(bcm2835_pwm + BCM2835_PWM1_RANGE, range);
  1322. }
  1323. void bcm2835_pwm_set_data(uint8_t channel, uint32_t data)
  1324. {
  1325. if ( bcm2835_clk == MAP_FAILED
  1326. || bcm2835_pwm == MAP_FAILED)
  1327. return; /* bcm2835_init() failed or not root */
  1328. if (channel == 0)
  1329. bcm2835_peri_write_nb(bcm2835_pwm + BCM2835_PWM0_DATA, data);
  1330. else if (channel == 1)
  1331. bcm2835_peri_write_nb(bcm2835_pwm + BCM2835_PWM1_DATA, data);
  1332. }
  1333. /* Allocate page-aligned memory. */
  1334. void *malloc_aligned(size_t size)
  1335. {
  1336. void *mem;
  1337. errno = posix_memalign(&mem, BCM2835_PAGE_SIZE, size);
  1338. return (errno ? NULL : mem);
  1339. }
  1340. /* Map 'size' bytes starting at 'off' in file 'fd' to memory.
  1341. // Return mapped address on success, MAP_FAILED otherwise.
  1342. // On error print message.
  1343. */
  1344. static void *mapmem(const char *msg, size_t size, int fd, off_t off)
  1345. {
  1346. void *map = mmap(NULL, size, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, off);
  1347. if (map == MAP_FAILED)
  1348. fprintf(stderr, "bcm2835_init: %s mmap failed: %s\n", msg, strerror(errno));
  1349. return map;
  1350. }
  1351. static void unmapmem(void **pmem, size_t size)
  1352. {
  1353. if (*pmem == MAP_FAILED) return;
  1354. munmap(*pmem, size);
  1355. *pmem = MAP_FAILED;
  1356. }
  1357. /* Initialise this library. */
  1358. int bcm2835_init(void)
  1359. {
  1360. int memfd;
  1361. int ok;
  1362. FILE *fp;
  1363. if (debug)
  1364. {
  1365. bcm2835_peripherals = (uint32_t*)BCM2835_PERI_BASE;
  1366. bcm2835_pads = bcm2835_peripherals + BCM2835_GPIO_PADS/4;
  1367. bcm2835_clk = bcm2835_peripherals + BCM2835_CLOCK_BASE/4;
  1368. bcm2835_gpio = bcm2835_peripherals + BCM2835_GPIO_BASE/4;
  1369. bcm2835_pwm = bcm2835_peripherals + BCM2835_GPIO_PWM/4;
  1370. bcm2835_spi0 = bcm2835_peripherals + BCM2835_SPI0_BASE/4;
  1371. bcm2835_bsc0 = bcm2835_peripherals + BCM2835_BSC0_BASE/4;
  1372. bcm2835_bsc1 = bcm2835_peripherals + BCM2835_BSC1_BASE/4;
  1373. bcm2835_st = bcm2835_peripherals + BCM2835_ST_BASE/4;
  1374. bcm2835_aux = bcm2835_peripherals + BCM2835_AUX_BASE/4;
  1375. bcm2835_spi1 = bcm2835_peripherals + BCM2835_SPI1_BASE/4;
  1376. return 1; /* Success */
  1377. }
  1378. /* Figure out the base and size of the peripheral address block
  1379. // using the device-tree. Required for RPi2, optional for RPi 1
  1380. */
  1381. if ((fp = fopen(BMC2835_RPI2_DT_FILENAME , "rb")))
  1382. {
  1383. unsigned char buf[4];
  1384. fseek(fp, BMC2835_RPI2_DT_PERI_BASE_ADDRESS_OFFSET, SEEK_SET);
  1385. if (fread(buf, 1, sizeof(buf), fp) == sizeof(buf))
  1386. bcm2835_peripherals_base = (uint32_t *)((long)buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0);
  1387. fseek(fp, BMC2835_RPI2_DT_PERI_SIZE_OFFSET, SEEK_SET);
  1388. if (fread(buf, 1, sizeof(buf), fp) == sizeof(buf))
  1389. bcm2835_peripherals_size = (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0);
  1390. fclose(fp);
  1391. }
  1392. /* else we are prob on RPi 1 with BCM2835, and use the hardwired defaults */
  1393. /* Now get ready to map the peripherals block
  1394. * If we are not root, try for the new /dev/gpiomem interface and accept
  1395. * the fact that we can only access GPIO
  1396. * else try for the /dev/mem interface and get access to everything
  1397. */
  1398. memfd = -1;
  1399. ok = 0;
  1400. if (geteuid() == 0)
  1401. {
  1402. /* Open the master /dev/mem device */
  1403. if ((memfd = open("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  1404. {
  1405. fprintf(stderr, "bcm2835_init: Unable to open /dev/mem: %s\n",
  1406. strerror(errno)) ;
  1407. goto exit;
  1408. }
  1409. /* Base of the peripherals block is mapped to VM */
  1410. bcm2835_peripherals = mapmem("gpio", bcm2835_peripherals_size, memfd, (off_t)bcm2835_peripherals_base);
  1411. if (bcm2835_peripherals == MAP_FAILED) goto exit;
  1412. /* Now compute the base addresses of various peripherals,
  1413. // which are at fixed offsets within the mapped peripherals block
  1414. // Caution: bcm2835_peripherals is uint32_t*, so divide offsets by 4
  1415. */
  1416. bcm2835_gpio = bcm2835_peripherals + BCM2835_GPIO_BASE/4;
  1417. bcm2835_pwm = bcm2835_peripherals + BCM2835_GPIO_PWM/4;
  1418. bcm2835_clk = bcm2835_peripherals + BCM2835_CLOCK_BASE/4;
  1419. bcm2835_pads = bcm2835_peripherals + BCM2835_GPIO_PADS/4;
  1420. bcm2835_spi0 = bcm2835_peripherals + BCM2835_SPI0_BASE/4;
  1421. bcm2835_bsc0 = bcm2835_peripherals + BCM2835_BSC0_BASE/4; /* I2C */
  1422. bcm2835_bsc1 = bcm2835_peripherals + BCM2835_BSC1_BASE/4; /* I2C */
  1423. bcm2835_st = bcm2835_peripherals + BCM2835_ST_BASE/4;
  1424. bcm2835_aux = bcm2835_peripherals + BCM2835_AUX_BASE/4;
  1425. bcm2835_spi1 = bcm2835_peripherals + BCM2835_SPI1_BASE/4;
  1426. ok = 1;
  1427. }
  1428. else
  1429. {
  1430. /* Not root, try /dev/gpiomem */
  1431. /* Open the master /dev/mem device */
  1432. if ((memfd = open("/dev/gpiomem", O_RDWR | O_SYNC) ) < 0)
  1433. {
  1434. fprintf(stderr, "bcm2835_init: Unable to open /dev/gpiomem: %s\n",
  1435. strerror(errno)) ;
  1436. goto exit;
  1437. }
  1438. /* Base of the peripherals block is mapped to VM */
  1439. bcm2835_peripherals_base = 0;
  1440. bcm2835_peripherals = mapmem("gpio", bcm2835_peripherals_size, memfd, (off_t)bcm2835_peripherals_base);
  1441. if (bcm2835_peripherals == MAP_FAILED) goto exit;
  1442. bcm2835_gpio = bcm2835_peripherals;
  1443. ok = 1;
  1444. }
  1445. exit:
  1446. if (memfd >= 0)
  1447. close(memfd);
  1448. if (!ok)
  1449. bcm2835_close();
  1450. return ok;
  1451. }
  1452. /* Close this library and deallocate everything */
  1453. int bcm2835_close(void)
  1454. {
  1455. if (debug) return 1; /* Success */
  1456. unmapmem((void**) &bcm2835_peripherals, bcm2835_peripherals_size);
  1457. bcm2835_peripherals = MAP_FAILED;
  1458. bcm2835_gpio = MAP_FAILED;
  1459. bcm2835_pwm = MAP_FAILED;
  1460. bcm2835_clk = MAP_FAILED;
  1461. bcm2835_pads = MAP_FAILED;
  1462. bcm2835_spi0 = MAP_FAILED;
  1463. bcm2835_bsc0 = MAP_FAILED;
  1464. bcm2835_bsc1 = MAP_FAILED;
  1465. bcm2835_st = MAP_FAILED;
  1466. bcm2835_aux = MAP_FAILED;
  1467. bcm2835_spi1 = MAP_FAILED;
  1468. return 1; /* Success */
  1469. }
  1470. #ifdef BCM2835_TEST
  1471. /* this is a simple test program that prints out what it will do rather than
  1472. // actually doing it
  1473. */
  1474. int main(int argc, char **argv)
  1475. {
  1476. /* Be non-destructive */
  1477. bcm2835_set_debug(1);
  1478. if (!bcm2835_init())
  1479. return 1;
  1480. /* Configure some GPIO pins fo some testing
  1481. // Set RPI pin P1-11 to be an output
  1482. */
  1483. bcm2835_gpio_fsel(RPI_GPIO_P1_11, BCM2835_GPIO_FSEL_OUTP);
  1484. /* Set RPI pin P1-15 to be an input */
  1485. bcm2835_gpio_fsel(RPI_GPIO_P1_15, BCM2835_GPIO_FSEL_INPT);
  1486. /* with a pullup */
  1487. bcm2835_gpio_set_pud(RPI_GPIO_P1_15, BCM2835_GPIO_PUD_UP);
  1488. /* And a low detect enable */
  1489. bcm2835_gpio_len(RPI_GPIO_P1_15);
  1490. /* and input hysteresis disabled on GPIOs 0 to 27 */
  1491. bcm2835_gpio_set_pad(BCM2835_PAD_GROUP_GPIO_0_27, BCM2835_PAD_SLEW_RATE_UNLIMITED|BCM2835_PAD_DRIVE_8mA);
  1492. #if 1
  1493. /* Blink */
  1494. while (1)
  1495. {
  1496. /* Turn it on */
  1497. bcm2835_gpio_write(RPI_GPIO_P1_11, HIGH);
  1498. /* wait a bit */
  1499. bcm2835_delay(500);
  1500. /* turn it off */
  1501. bcm2835_gpio_write(RPI_GPIO_P1_11, LOW);
  1502. /* wait a bit */
  1503. bcm2835_delay(500);
  1504. }
  1505. #endif
  1506. #if 0
  1507. /* Read input */
  1508. while (1)
  1509. {
  1510. /* Read some data */
  1511. uint8_t value = bcm2835_gpio_lev(RPI_GPIO_P1_15);
  1512. printf("read from pin 15: %d\n", value);
  1513. /* wait a bit */
  1514. bcm2835_delay(500);
  1515. }
  1516. #endif
  1517. #if 0
  1518. /* Look for a low event detection
  1519. // eds will be set whenever pin 15 goes low
  1520. */
  1521. while (1)
  1522. {
  1523. if (bcm2835_gpio_eds(RPI_GPIO_P1_15))
  1524. {
  1525. /* Now clear the eds flag by setting it to 1 */
  1526. bcm2835_gpio_set_eds(RPI_GPIO_P1_15);
  1527. printf("low event detect for pin 15\n");
  1528. }
  1529. /* wait a bit */
  1530. bcm2835_delay(500);
  1531. }
  1532. #endif
  1533. if (!bcm2835_close())
  1534. return 1;
  1535. return 0;
  1536. }
  1537. #endif