Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

bcm2835.c 42KB

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