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.h 76KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /* bcm2835.h
  2. C and C++ support for Broadcom BCM 2835 as used in Raspberry Pi
  3. Author: Mike McCauley
  4. Copyright (C) 2011-2013 Mike McCauley
  5. $Id: bcm2835.h,v 1.20 2015/03/31 04:55:41 mikem Exp mikem $
  6. */
  7. /*! \defgroup BCM2835grp C library for Broadcom BCM 2835 as used in Raspberry Pi
  8. \ingroup internals
  9. This is a C library for Raspberry Pi (RPi). It provides access to
  10. GPIO and other IO functions on the Broadcom BCM 2835 chip,
  11. allowing access to the GPIO pins on the
  12. 26 pin IDE plug on the RPi board so you can control and interface with various external devices.
  13. It provides functions for reading digital inputs and setting digital outputs, using SPI and I2C,
  14. and for accessing the system timers.
  15. Pin event detection is supported by polling (interrupts are not supported).
  16. It is C++ compatible, and installs as a header file and non-shared library on
  17. any Linux-based distro (but clearly is no use except on Raspberry Pi or another board with
  18. BCM 2835).
  19. The version of the package that this documentation refers to can be downloaded
  20. from http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz
  21. You can find the latest version at http://www.airspayce.com/mikem/bcm2835
  22. Several example programs are provided.
  23. Based on data in http://elinux.org/RPi_Low-level_peripherals and
  24. http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
  25. and http://www.scribd.com/doc/101830961/GPIO-Pads-Control2
  26. You can also find online help and discussion at http://groups.google.com/group/bcm2835
  27. Please use that group for all questions and discussions on this topic.
  28. Do not contact the author directly, unless it is to discuss commercial licensing.
  29. Before asking a question or reporting a bug, please read http://www.catb.org/esr/faqs/smart-questions.html
  30. Tested on debian6-19-04-2012, 2012-07-15-wheezy-raspbian, 2013-07-26-wheezy-raspbian
  31. and Occidentalisv01, 2016-02-09 Raspbian Jessie.
  32. CAUTION: it has been observed that when detect enables such as bcm2835_gpio_len()
  33. are used and the pin is pulled LOW
  34. it can cause temporary hangs on 2012-07-15-wheezy-raspbian, 2013-07-26-wheezy-raspbian
  35. and Occidentalisv01.
  36. Reason for this is not yet determined, but we suspect that an interrupt handler is
  37. hitting a hard loop on those OSs.
  38. If you must use bcm2835_gpio_len() and friends, make sure you disable the pins with
  39. bcm2835_gpio_clr_len() and friends after use.
  40. \par Running as root
  41. Prior to the release of Raspbian Jessie in Feb 2016, access to any
  42. peripheral device via /dev/mem on the RPi required the process to
  43. run as root. Raspbian Jessie permits non-root users to access the
  44. GPIO peripheral (only) via /dev/gpiomem, and this library supports
  45. that limited mode of operation.
  46. If the library runs with effective UID of 0 (ie root), then
  47. bcm2835_init() will attempt to open /dev/mem, and, if successful, it
  48. will permit use of all peripherals and library functions.
  49. If the library runs with any other effective UID (ie not root), then
  50. bcm2835_init() will attempt to open /dev/gpiomem, and, if
  51. successful, will only permit GPIO operations. In particular,
  52. bcm2835_spi_begin() and bcm2835_i2c_begin() will return false and all
  53. other non-gpio operations may fail silently or crash.
  54. \par Installation
  55. This library consists of a single non-shared library and header file, which will be
  56. installed in the usual places by make install
  57. \code
  58. # download the latest version of the library, say bcm2835-1.xx.tar.gz, then:
  59. tar zxvf bcm2835-1.xx.tar.gz
  60. cd bcm2835-1.xx
  61. ./configure
  62. make
  63. sudo make check
  64. sudo make install
  65. \endcode
  66. \par Physical Addresses
  67. The functions bcm2835_peri_read(), bcm2835_peri_write() and bcm2835_peri_set_bits()
  68. are low level peripheral register access functions. They are designed to use
  69. physical addresses as described in section 1.2.3 ARM physical addresses
  70. of the BCM2835 ARM Peripherals manual.
  71. Physical addresses range from 0x20000000 to 0x20FFFFFF for peripherals. The bus
  72. addresses for peripherals are set up to map onto the peripheral bus address range starting at
  73. 0x7E000000. Thus a peripheral advertised in the manual at bus address 0x7Ennnnnn is available at
  74. physical address 0x20nnnnnn.
  75. On RPI 2, the peripheral addresses are different and the bcm2835 library gets them
  76. from reading /proc/device-tree/soc/ranges. This is only availble with recent versions of the kernel on RPI 2.
  77. After initialisation, the base address of the various peripheral
  78. registers are available with the following
  79. externals:
  80. bcm2835_gpio
  81. bcm2835_pwm
  82. bcm2835_clk
  83. bcm2835_pads
  84. bcm2835_spio0
  85. bcm2835_st
  86. bcm2835_bsc0
  87. bcm2835_bsc1
  88. \par Raspberry Pi 2 (RPI2)
  89. For this library to work correctly on RPI2, you MUST have the device tree support enabled in the kernel.
  90. You should also ensure you are using the latest version of Linux. The library has been tested on RPI2
  91. with 2015-02-16-raspbian-wheezy and ArchLinuxARM-rpi-2 as of 2015-03-29.
  92. When device tree suport is enabled, the file /proc/device-tree/soc/ranges will appear in the file system,
  93. and the bcm2835 module relies on its presence to correctly run on RPI2 (it is optional for RPI1).
  94. Without device tree support enabled and the presence of this file, it will not work on RPI2.
  95. To enable device tree support:
  96. \code
  97. sudo raspi-config
  98. under Advanced Options - enable Device Tree
  99. Reboot.
  100. \endcode
  101. \par Pin Numbering
  102. The GPIO pin numbering as used by RPi is different to and inconsistent with the underlying
  103. BCM 2835 chip pin numbering. http://elinux.org/RPi_BCM2835_GPIOs
  104. RPi has a 26 pin IDE header that provides access to some of the GPIO pins on the BCM 2835,
  105. as well as power and ground pins. Not all GPIO pins on the BCM 2835 are available on the
  106. IDE header.
  107. RPi Version 2 also has a P5 connector with 4 GPIO pins, 5V, 3.3V and Gnd.
  108. The functions in this library are designed to be passed the BCM 2835 GPIO pin number and _not_
  109. the RPi pin number. There are symbolic definitions for each of the available pins
  110. that you should use for convenience. See \ref RPiGPIOPin.
  111. \par SPI Pins
  112. The bcm2835_spi_* functions allow you to control the BCM 2835 SPI0 interface,
  113. allowing you to send and received data by SPI (Serial Peripheral Interface).
  114. For more information about SPI, see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
  115. When bcm2835_spi_begin() is called it changes the bahaviour of the SPI interface pins from their
  116. default GPIO behaviour in order to support SPI. While SPI is in use, you will not be able
  117. to control the state of the SPI pins through the usual bcm2835_spi_gpio_write().
  118. When bcm2835_spi_end() is called, the SPI pins will all revert to inputs, and can then be
  119. configured and controled with the usual bcm2835_gpio_* calls.
  120. The Raspberry Pi GPIO pins used for SPI are:
  121. - P1-19 (MOSI)
  122. - P1-21 (MISO)
  123. - P1-23 (CLK)
  124. - P1-24 (CE0)
  125. - P1-26 (CE1)
  126. \par I2C Pins
  127. The bcm2835_i2c_* functions allow you to control the BCM 2835 BSC interface,
  128. allowing you to send and received data by I2C ("eye-squared cee"; generically referred to as "two-wire interface") .
  129. For more information about I?C, see http://en.wikipedia.org/wiki/I%C2%B2C
  130. The Raspberry Pi V2 GPIO pins used for I2C are:
  131. - P1-03 (SDA)
  132. - P1-05 (SLC)
  133. \par PWM
  134. The BCM2835 supports hardware PWM on a limited subset of GPIO pins. This bcm2835 library provides
  135. functions for configuring and controlling PWM output on these pins.
  136. The BCM2835 contains 2 independent PWM channels (0 and 1), each of which be connnected to a limited subset of
  137. GPIO pins. The following GPIO pins may be connected to the following PWM channels (from section 9.5):
  138. \code
  139. GPIO PIN RPi pin PWM Channel ALT FUN
  140. 12 0 0
  141. 13 1 0
  142. 18 1-12 0 5
  143. 19 1 5
  144. 40 0 0
  145. 41 1 0
  146. 45 1 0
  147. 52 0 1
  148. 53 1 1
  149. \endcode
  150. In order for a GPIO pin to emit output from its PWM channel, it must be set to the Alt Function given above.
  151. Note carefully that current versions of the Raspberry Pi only expose one of these pins (GPIO 18 = RPi Pin 1-12)
  152. on the IO headers, and therefore this is the only IO pin on the RPi that can be used for PWM.
  153. Further it must be set to ALT FUN 5 to get PWM output.
  154. Both PWM channels are driven by the same PWM clock, whose clock dvider can be varied using
  155. bcm2835_pwm_set_clock(). Each channel can be separately enabled with bcm2835_pwm_set_mode().
  156. The average output of the PWM channel is determined by the ratio of DATA/RANGE for that channel.
  157. Use bcm2835_pwm_set_range() to set the range and bcm2835_pwm_set_data() to set the data in that ratio
  158. Each PWM channel can run in either Balanced or Mark-Space mode. In Balanced mode, the hardware
  159. sends a combination of clock pulses that results in an overall DATA pulses per RANGE pulses.
  160. In Mark-Space mode, the hardware sets the output HIGH for DATA clock pulses wide, followed by
  161. LOW for RANGE-DATA clock pulses.
  162. The PWM clock can be set to control the PWM pulse widths. The PWM clock is derived from
  163. a 19.2MHz clock. You can set any divider, but some common ones are provided by the BCM2835_PWM_CLOCK_DIVIDER_*
  164. values of \ref bcm2835PWMClockDivider.
  165. For example, say you wanted to drive a DC motor with PWM at about 1kHz,
  166. and control the speed in 1/1024 increments from
  167. 0/1024 (stopped) through to 1024/1024 (full on). In that case you might set the
  168. clock divider to be 16, and the RANGE to 1024. The pulse repetition frequency will be
  169. 1.2MHz/1024 = 1171.875Hz.
  170. \par SPI
  171. In order for bcm2835 library SPI to work, you may need to disable the SPI kernel module using:
  172. \code
  173. sudo raspi-config
  174. under Advanced Options - enable Device Tree
  175. under Advanced Options - disable SPI
  176. Reboot.
  177. \endcode
  178. \par Real Time performance constraints
  179. The bcm2835 is a library for user programs (i.e. they run in 'userland').
  180. Such programs are not part of the kernel and are usually
  181. subject to paging and swapping by the kernel while it does other things besides running your program.
  182. This means that you should not expect to get real-time performance or
  183. real-time timing constraints from such programs. In particular, there is no guarantee that the
  184. bcm2835_delay() and bcm2835_delayMicroseconds() will return after exactly the time requested.
  185. In fact, depending on other activity on the host, IO etc, you might get significantly longer delay times
  186. than the one you asked for. So please dont expect to get exactly the time delay you request.
  187. Arjan reports that you can prevent swapping on Linux with the following code fragment:
  188. \code
  189. struct sched_param sp;
  190. memset(&sp, 0, sizeof(sp));
  191. sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
  192. sched_setscheduler(0, SCHED_FIFO, &sp);
  193. mlockall(MCL_CURRENT | MCL_FUTURE);
  194. \endcode
  195. \par Bindings to other languages
  196. mikem has made Perl bindings available at CPAN:
  197. http://search.cpan.org/~mikem/Device-BCM2835-1.9/lib/Device/BCM2835.pm
  198. Matthew Baker has kindly made Python bindings available at:
  199. https: github.com/mubeta06/py-libbcm2835
  200. Gary Marks has created a Serial Peripheral Interface (SPI) command-line utility
  201. for Raspberry Pi, based on the bcm2835 library. The
  202. utility, spincl, is licensed under Open Source GNU GPLv3 by iP Solutions (http://ipsolutionscorp.com), as a
  203. free download with source included: http://ipsolutionscorp.com/raspberry-pi-spi-utility/
  204. \par Open Source Licensing GPL V2
  205. This is the appropriate option if you want to share the source code of your
  206. application with everyone you distribute it to, and you also want to give them
  207. the right to share who uses it. If you wish to use this software under Open
  208. Source Licensing, you must contribute all your source code to the open source
  209. community in accordance with the GPL Version 2 when your application is
  210. distributed. See http://www.gnu.org/copyleft/gpl.html and COPYING
  211. \par Acknowledgements
  212. Some of this code has been inspired by Dom and Gert.
  213. The I2C code has been inspired by Alan Barr.
  214. \par Revision History
  215. \version 1.0 Initial release
  216. \version 1.1 Minor bug fixes
  217. \version 1.2 Added support for SPI
  218. \version 1.3 Added bcm2835_spi_transfern()
  219. \version 1.4 Fixed a problem that prevented SPI CE1 being used. Reported by David Robinson.
  220. \version 1.5 Added bcm2835_close() to deinit the library. Suggested by C?sar Ortiz
  221. \version 1.6 Document testing on 2012-07-15-wheezy-raspbian and Occidentalisv01
  222. Functions bcm2835_gpio_ren(), bcm2835_gpio_fen(), bcm2835_gpio_hen()
  223. bcm2835_gpio_len(), bcm2835_gpio_aren() and bcm2835_gpio_afen() now
  224. changes only the pin specified. Other pins that were already previously
  225. enabled stay enabled.
  226. Added bcm2835_gpio_clr_ren(), bcm2835_gpio_clr_fen(), bcm2835_gpio_clr_hen()
  227. bcm2835_gpio_clr_len(), bcm2835_gpio_clr_aren(), bcm2835_gpio_clr_afen()
  228. to clear the enable for individual pins, suggested by Andreas Sundstrom.
  229. \version 1.7 Added bcm2835_spi_transfernb to support different buffers for read and write.
  230. \version 1.8 Improvements to read barrier, as suggested by maddin.
  231. \version 1.9 Improvements contributed by mikew:
  232. I noticed that it was mallocing memory for the mmaps on /dev/mem.
  233. It's not necessary to do that, you can just mmap the file directly,
  234. so I've removed the mallocs (and frees).
  235. I've also modified delayMicroseconds() to use nanosleep() for long waits,
  236. and a busy wait on a high resolution timer for the rest. This is because
  237. I've found that calling nanosleep() takes at least 100-200 us.
  238. You need to link using '-lrt' using this version.
  239. I've added some unsigned casts to the debug prints to silence compiler
  240. warnings I was getting, fixed some typos, and changed the value of
  241. BCM2835_PAD_HYSTERESIS_ENABLED to 0x08 as per Gert van Loo's doc at
  242. http://www.scribd.com/doc/101830961/GPIO-Pads-Control2
  243. Also added a define for the passwrd value that Gert says is needed to
  244. change pad control settings.
  245. \version 1.10 Changed the names of the delay functions to bcm2835_delay()
  246. and bcm2835_delayMicroseconds() to prevent collisions with wiringPi.
  247. Macros to map delay()-> bcm2835_delay() and
  248. Macros to map delayMicroseconds()-> bcm2835_delayMicroseconds(), which
  249. can be disabled by defining BCM2835_NO_DELAY_COMPATIBILITY
  250. \version 1.11 Fixed incorrect link to download file
  251. \version 1.12 New GPIO pin definitions for RPi version 2 (which has a different GPIO mapping)
  252. \version 1.13 New GPIO pin definitions for RPi version 2 plug P5
  253. Hardware base pointers are now available (after initialisation) externally as bcm2835_gpio
  254. bcm2835_pwm bcm2835_clk bcm2835_pads bcm2835_spi0.
  255. \version 1.14 Now compiles even if CLOCK_MONOTONIC_RAW is not available, uses CLOCK_MONOTONIC instead.
  256. Fixed errors in documentation of SPI divider frequencies based on 250MHz clock.
  257. Reported by Ben Simpson.
  258. \version 1.15 Added bcm2835_close() to end of examples as suggested by Mark Wolfe.
  259. \version 1.16 Added bcm2835_gpio_set_multi, bcm2835_gpio_clr_multi and bcm2835_gpio_write_multi
  260. to allow a mask of pins to be set all at once. Requested by Sebastian Loncar.
  261. \version 1.17 Added bcm2835_gpio_write_mask. Requested by Sebastian Loncar.
  262. \version 1.18 Added bcm2835_i2c_* functions. Changes to bcm2835_delayMicroseconds:
  263. now uses the RPi system timer counter, instead of clock_gettime, for improved accuracy.
  264. No need to link with -lrt now. Contributed by Arjan van Vught.
  265. \version 1.19 Removed inlines added by previous patch since they don't seem to work everywhere.
  266. Reported by olly.
  267. \version 1.20 Patch from Mark Dootson to close /dev/mem after access to the peripherals has been granted.
  268. \version 1.21 delayMicroseconds is now not susceptible to 32 bit timer overruns.
  269. Patch courtesy Jeremy Mortis.
  270. \version 1.22 Fixed incorrect definition of BCM2835_GPFEN0 which broke the ability to set
  271. falling edge events. Reported by Mark Dootson.
  272. \version 1.23 Added bcm2835_i2c_set_baudrate and bcm2835_i2c_read_register_rs.
  273. Improvements to bcm2835_i2c_read and bcm2835_i2c_write functions
  274. to fix ocasional reads not completing. Patched by Mark Dootson.
  275. \version 1.24 Mark Dootson p[atched a problem with his previously submitted code
  276. under high load from other processes.
  277. \version 1.25 Updated author and distribution location details to airspayce.com
  278. \version 1.26 Added missing unmapmem for pads in bcm2835_close to prevent a memory leak.
  279. Reported by Hartmut Henkel.
  280. \version 1.27 bcm2835_gpio_set_pad() no longer needs BCM2835_PAD_PASSWRD: it is
  281. now automatically included.
  282. Added suport for PWM mode with bcm2835_pwm_* functions.
  283. \version 1.28 Fixed a problem where bcm2835_spi_writenb() would have problems with transfers of more than
  284. 64 bytes dues to read buffer filling. Patched by Peter Würtz.
  285. \version 1.29 Further fix to SPI from Peter Würtz.
  286. \version 1.30 10 microsecond delays from bcm2835_spi_transfer and bcm2835_spi_transfern for
  287. significant performance improvements, Patch by Alan Watson.
  288. \version 1.31 Fix a GCC warning about dummy variable, patched by Alan Watson. Thanks.
  289. \version 1.32 Added option I2C_V1 definition to compile for version 1 RPi.
  290. By default I2C code is generated for the V2 RPi which has SDA1 and SCL1 connected.
  291. Contributed by Malcolm Wiles based on work by Arvi Govindaraj.
  292. \version 1.33 Added command line utilities i2c and gpio to examples. Contributed by Shahrooz Shahparnia.
  293. \version 1.34 Added bcm2835_i2c_write_read_rs() which writes an arbitrary number of bytes,
  294. sends a repeat start, and reads from the device. Contributed by Eduardo Steinhorst.
  295. \version 1.35 Fix build errors when compiled under Qt. Also performance improvements with SPI transfers. Contributed b Udo Klaas.
  296. \version 1.36 Make automake's test runner detect that we're skipping tests when not root, the second
  297. one makes us skip the test when using fakeroot (as used when building
  298. Debian packages). Contributed by Guido Günther.
  299. \version 1.37 Moved confiure.in to configure.ac as receommnded by autoreconf.<br>
  300. Improvements to bcm2835_st_read to account for possible timer overflow, contributed by 'Ed'.<br>
  301. Added definitions for Raspberry Pi B+ J8 header GPIO pins.<br>
  302. \version 1.38 Added bcm2835_regbase for the benefit of C# wrappers, patch by Frank Hommers <br>
  303. \version 1.39 Beta version of RPi2 compatibility. Not tested here on RPi2 hardware.
  304. Testers please confirm correct operation on RPi2.<br>
  305. Unneccessary 'volatile' qualifiers removed from all variables and signatures.<br>
  306. Removed unsupportable PWM dividers, based on a report from Christophe Cecillon.<br>
  307. Minor improvements to spi.c example.<br>
  308. \version 1.40 Correct operation on RPi2 has been confirmed.<br>
  309. Fixed a number of compiler errors and warnings that occur when bcm2835.h is included
  310. in code compiled with -Wall -Woverflow -Wstrict-overflow -Wshadow -Wextra -pedantic.
  311. Reported by tlhackque.<br>
  312. Fixed a problem where calling bcm2835_delayMicroseconds loops forever when debug is set. Reported by tlhackque.<br>
  313. Reinstated use of volatile in 2 functions where there was a danger of lost reads or writes. Reported by tlhackque.<br>
  314. \version 1.41 Added BCM2835_VERSION macro and new function bcm2835_version(); Requested by tlhackque.<br>
  315. Improvements to peripheral memory barriers as suggested by tlhackque.<br>
  316. Reinstated some necessary volatile declarations as requested by tlhackque.<br>
  317. \version 1.42 Further improvements to memory barriers with the patient assistance and patches of tlhackque.<br>
  318. \version 1.43 Fixed problems with compiling barriers on RPI 2 with Arch Linux and gcc 4.9.2.
  319. Reported and patched by Lars Christensen.<br>
  320. Testing on RPI 2, with ArchLinuxARM-rpi-2-latest and 2015-02-16-raspbian-wheezy.<br>
  321. \version 1.44 Added documention about the need for device tree to be enabled on RPI2.<br>
  322. Improvements to detection of availablity of DMB instruction based on value of __ARM_ARCH macro.<br>
  323. \version 1.45 Fixed an error in the pad group offsets that would prevent bcm2835_gpio_set_pad()
  324. and bcm2835_gpio_pad() working correctly with non-0 pad groups. Reported by Guido.
  325. \version 1.46 2015-09-18
  326. Added symbolic definitions for remaining pins on 40 pin GPIO header on RPi 2. <br>
  327. \version 1.47 2015-11-18
  328. Fixed possibly incorrect reads in bcm2835_i2c_read_register_rs, patch from Eckhardt Ulrich.<br>
  329. \version 1.48 2015-12-08
  330. Added patch from Eckhardt Ulrich that fixed problems that could cause hanging with bcm2835_i2c_read_register_rs
  331. and others.
  332. \version 1.49 2016-01-05
  333. Added patch from Jonathan Perkin with new functions bcm2835_gpio_eds_multi() and bcm2835_gpio_set_eds_multi().
  334. \version 1.50 2016-02-28
  335. Added support for running as non-root, permitting access to GPIO only. Functions
  336. bcm2835_spi_begin() and bcm2835_i2c_begin() will now return 0 if not running as root
  337. (which prevents access to the SPI and I2C peripherals, amongst others).
  338. Testing on Raspbian Jessie.
  339. \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS
  340. Modified September 2016 by Marcelo Aquino <marceloaqno@gmail.org>
  341. */
  342. /* Defines for BCM2835 */
  343. #ifndef BCM2835_H
  344. #define BCM2835_H
  345. #include <stdint.h>
  346. #define BCM2835_VERSION 10050 /* Version 1.50 */
  347. /* RPi 2 is ARM v7, and has DMB instruction for memory barriers.
  348. Older RPis are ARM v6 and don't, so a coprocessor instruction must be used instead.
  349. However, not all versions of gcc in all distros support the dmb assembler instruction even on conmpatible processors.
  350. This test is so any ARMv7 or higher processors with suitable GCC will use DMB.
  351. */
  352. #if __ARM_ARCH >= 7
  353. #define BCM2835_HAVE_DMB
  354. #endif
  355. /*! \defgroup BCM2835constantsgrp Constants for passing to and from library functions
  356. \ingroup BCM2835grp
  357. The values here are designed to be passed to various functions in the bcm2835 library.
  358. @{
  359. */
  360. /*! This means pin HIGH, true, 3.3volts on a pin. */
  361. #define HIGH 0x1
  362. /*! This means pin LOW, false, 0volts on a pin. */
  363. #define LOW 0x0
  364. /*! Speed of the core clock core_clk */
  365. #define BCM2835_CORE_CLK_HZ 250000000 /*!< 250 MHz */
  366. /*! On RPi2 with BCM2836, and all recent OSs, the base of the peripherals is read from a /proc file */
  367. #define BMC2835_RPI2_DT_FILENAME "/proc/device-tree/soc/ranges"
  368. /*! Offset into BMC2835_RPI2_DT_FILENAME for the peripherals base address */
  369. #define BMC2835_RPI2_DT_PERI_BASE_ADDRESS_OFFSET 4
  370. /*! Offset into BMC2835_RPI2_DT_FILENAME for the peripherals size address */
  371. #define BMC2835_RPI2_DT_PERI_SIZE_OFFSET 8
  372. /*! Physical addresses for various peripheral register sets
  373. Base Physical Address of the BCM 2835 peripheral registers
  374. Note this is different for the RPi2 BCM2836, where this is derived from /proc/device-tree/soc/ranges
  375. If /proc/device-tree/soc/ranges exists on a RPi 1 OS, it would be expected to contain the
  376. following numbers:
  377. */
  378. /*! Peripherals block base address on RPi 1 */
  379. #define BCM2835_PERI_BASE 0x20000000
  380. /*! Size of the peripherals block on RPi 1 */
  381. #define BCM2835_PERI_SIZE 0x01000000
  382. /*! Offsets for the bases of various peripherals within the peripherals block
  383. / Base Address of the System Timer registers
  384. */
  385. #define BCM2835_ST_BASE 0x3000
  386. /*! Base Address of the Pads registers */
  387. #define BCM2835_GPIO_PADS 0x100000
  388. /*! Base Address of the Clock/timer registers */
  389. #define BCM2835_CLOCK_BASE 0x101000
  390. /*! Base Address of the GPIO registers */
  391. #define BCM2835_GPIO_BASE 0x200000
  392. /*! Base Address of the SPI0 registers */
  393. #define BCM2835_SPI0_BASE 0x204000
  394. /*! Base Address of the BSC0 registers */
  395. #define BCM2835_BSC0_BASE 0x205000
  396. /*! Base Address of the PWM registers */
  397. #define BCM2835_GPIO_PWM 0x20C000
  398. /*! Base Address of the BSC1 registers */
  399. #define BCM2835_BSC1_BASE 0x804000
  400. /*! Physical address and size of the peripherals block
  401. May be overridden on RPi2
  402. */
  403. extern uint32_t *bcm2835_peripherals_base;
  404. /*! Size of the peripherals block to be mapped */
  405. extern uint32_t bcm2835_peripherals_size;
  406. /*! Virtual memory address of the mapped peripherals block */
  407. extern uint32_t *bcm2835_peripherals;
  408. /*! Base of the ST (System Timer) registers.
  409. Available after bcm2835_init has been called (as root)
  410. */
  411. extern volatile uint32_t *bcm2835_st;
  412. /*! Base of the GPIO registers.
  413. Available after bcm2835_init has been called
  414. */
  415. extern volatile uint32_t *bcm2835_gpio;
  416. /*! Base of the PWM registers.
  417. Available after bcm2835_init has been called (as root)
  418. */
  419. extern volatile uint32_t *bcm2835_pwm;
  420. /*! Base of the CLK registers.
  421. Available after bcm2835_init has been called (as root)
  422. */
  423. extern volatile uint32_t *bcm2835_clk;
  424. /*! Base of the PADS registers.
  425. Available after bcm2835_init has been called (as root)
  426. */
  427. extern volatile uint32_t *bcm2835_pads;
  428. /*! Base of the SPI0 registers.
  429. Available after bcm2835_init has been called (as root)
  430. */
  431. extern volatile uint32_t *bcm2835_spi0;
  432. /*! Base of the BSC0 registers.
  433. Available after bcm2835_init has been called (as root)
  434. */
  435. extern volatile uint32_t *bcm2835_bsc0;
  436. /*! Base of the BSC1 registers.
  437. Available after bcm2835_init has been called (as root)
  438. */
  439. extern volatile uint32_t *bcm2835_bsc1;
  440. /*! \brief bcm2835RegisterBase
  441. Register bases for bcm2835_regbase()
  442. */
  443. typedef enum {
  444. BCM2835_REGBASE_ST = 1, /*!< Base of the ST (System Timer) registers. */
  445. BCM2835_REGBASE_GPIO = 2, /*!< Base of the GPIO registers. */
  446. BCM2835_REGBASE_PWM = 3, /*!< Base of the PWM registers. */
  447. BCM2835_REGBASE_CLK = 4, /*!< Base of the CLK registers. */
  448. BCM2835_REGBASE_PADS = 5, /*!< Base of the PADS registers. */
  449. BCM2835_REGBASE_SPI0 = 6, /*!< Base of the SPI0 registers. */
  450. BCM2835_REGBASE_BSC0 = 7, /*!< Base of the BSC0 registers. */
  451. BCM2835_REGBASE_BSC1 = 8 /*!< Base of the BSC1 registers. */
  452. } bcm2835RegisterBase;
  453. /*! Size of memory page on RPi */
  454. #define BCM2835_PAGE_SIZE (4*1024)
  455. /*! Size of memory block on RPi */
  456. #define BCM2835_BLOCK_SIZE (4*1024)
  457. /* Defines for GPIO
  458. The BCM2835 has 54 GPIO pins.
  459. BCM2835 data sheet, Page 90 onwards.
  460. */
  461. /*! GPIO register offsets from BCM2835_GPIO_BASE.
  462. Offsets into the GPIO Peripheral block in bytes per 6.1 Register View
  463. */
  464. #define BCM2835_GPFSEL0 0x0000 /*!< GPIO Function Select 0 */
  465. #define BCM2835_GPFSEL1 0x0004 /*!< GPIO Function Select 1 */
  466. #define BCM2835_GPFSEL2 0x0008 /*!< GPIO Function Select 2 */
  467. #define BCM2835_GPFSEL3 0x000c /*!< GPIO Function Select 3 */
  468. #define BCM2835_GPFSEL4 0x0010 /*!< GPIO Function Select 4 */
  469. #define BCM2835_GPFSEL5 0x0014 /*!< GPIO Function Select 5 */
  470. #define BCM2835_GPSET0 0x001c /*!< GPIO Pin Output Set 0 */
  471. #define BCM2835_GPSET1 0x0020 /*!< GPIO Pin Output Set 1 */
  472. #define BCM2835_GPCLR0 0x0028 /*!< GPIO Pin Output Clear 0 */
  473. #define BCM2835_GPCLR1 0x002c /*!< GPIO Pin Output Clear 1 */
  474. #define BCM2835_GPLEV0 0x0034 /*!< GPIO Pin Level 0 */
  475. #define BCM2835_GPLEV1 0x0038 /*!< GPIO Pin Level 1 */
  476. #define BCM2835_GPEDS0 0x0040 /*!< GPIO Pin Event Detect Status 0 */
  477. #define BCM2835_GPEDS1 0x0044 /*!< GPIO Pin Event Detect Status 1 */
  478. #define BCM2835_GPREN0 0x004c /*!< GPIO Pin Rising Edge Detect Enable 0 */
  479. #define BCM2835_GPREN1 0x0050 /*!< GPIO Pin Rising Edge Detect Enable 1 */
  480. #define BCM2835_GPFEN0 0x0058 /*!< GPIO Pin Falling Edge Detect Enable 0 */
  481. #define BCM2835_GPFEN1 0x005c /*!< GPIO Pin Falling Edge Detect Enable 1 */
  482. #define BCM2835_GPHEN0 0x0064 /*!< GPIO Pin High Detect Enable 0 */
  483. #define BCM2835_GPHEN1 0x0068 /*!< GPIO Pin High Detect Enable 1 */
  484. #define BCM2835_GPLEN0 0x0070 /*!< GPIO Pin Low Detect Enable 0 */
  485. #define BCM2835_GPLEN1 0x0074 /*!< GPIO Pin Low Detect Enable 1 */
  486. #define BCM2835_GPAREN0 0x007c /*!< GPIO Pin Async. Rising Edge Detect 0 */
  487. #define BCM2835_GPAREN1 0x0080 /*!< GPIO Pin Async. Rising Edge Detect 1 */
  488. #define BCM2835_GPAFEN0 0x0088 /*!< GPIO Pin Async. Falling Edge Detect 0 */
  489. #define BCM2835_GPAFEN1 0x008c /*!< GPIO Pin Async. Falling Edge Detect 1 */
  490. #define BCM2835_GPPUD 0x0094 /*!< GPIO Pin Pull-up/down Enable */
  491. #define BCM2835_GPPUDCLK0 0x0098 /*!< GPIO Pin Pull-up/down Enable Clock 0 */
  492. #define BCM2835_GPPUDCLK1 0x009c /*!< GPIO Pin Pull-up/down Enable Clock 1 */
  493. /*! \brief bcm2835PortFunction
  494. Port function select modes for bcm2835_gpio_fsel()
  495. */
  496. typedef enum {
  497. BCM2835_GPIO_FSEL_INPT = 0x00, /*!< Input 0b000 */
  498. BCM2835_GPIO_FSEL_OUTP = 0x01, /*!< Output 0b001 */
  499. BCM2835_GPIO_FSEL_ALT0 = 0x04, /*!< Alternate function 0 0b100 */
  500. BCM2835_GPIO_FSEL_ALT1 = 0x05, /*!< Alternate function 1 0b101 */
  501. BCM2835_GPIO_FSEL_ALT2 = 0x06, /*!< Alternate function 2 0b110, */
  502. BCM2835_GPIO_FSEL_ALT3 = 0x07, /*!< Alternate function 3 0b111 */
  503. BCM2835_GPIO_FSEL_ALT4 = 0x03, /*!< Alternate function 4 0b011 */
  504. BCM2835_GPIO_FSEL_ALT5 = 0x02, /*!< Alternate function 5 0b010 */
  505. BCM2835_GPIO_FSEL_MASK = 0x07 /*!< Function select bits mask 0b111 */
  506. } bcm2835FunctionSelect;
  507. /*! \brief bcm2835PUDControl
  508. Pullup/Pulldown defines for bcm2835_gpio_pud()
  509. */
  510. typedef enum {
  511. BCM2835_GPIO_PUD_OFF = 0x00, /*!< Off ? disable pull-up/down 0b00 */
  512. BCM2835_GPIO_PUD_DOWN = 0x01, /*!< Enable Pull Down control 0b01 */
  513. BCM2835_GPIO_PUD_UP = 0x02 /*!< Enable Pull Up control 0b10 */
  514. } bcm2835PUDControl;
  515. /*! Pad control register offsets from BCM2835_GPIO_PADS */
  516. #define BCM2835_PADS_GPIO_0_27 0x002c /*!< Pad control register for pads 0 to 27 */
  517. #define BCM2835_PADS_GPIO_28_45 0x0030 /*!< Pad control register for pads 28 to 45 */
  518. #define BCM2835_PADS_GPIO_46_53 0x0034 /*!< Pad control register for pads 46 to 53 */
  519. /*! Pad Control masks */
  520. #define BCM2835_PAD_PASSWRD (0x5A << 24) /*!< Password to enable setting pad mask */
  521. #define BCM2835_PAD_SLEW_RATE_UNLIMITED 0x10 /*!< Slew rate unlimited */
  522. #define BCM2835_PAD_HYSTERESIS_ENABLED 0x08 /*!< Hysteresis enabled */
  523. #define BCM2835_PAD_DRIVE_2mA 0x00 /*!< 2mA drive current */
  524. #define BCM2835_PAD_DRIVE_4mA 0x01 /*!< 4mA drive current */
  525. #define BCM2835_PAD_DRIVE_6mA 0x02 /*!< 6mA drive current */
  526. #define BCM2835_PAD_DRIVE_8mA 0x03 /*!< 8mA drive current */
  527. #define BCM2835_PAD_DRIVE_10mA 0x04 /*!< 10mA drive current */
  528. #define BCM2835_PAD_DRIVE_12mA 0x05 /*!< 12mA drive current */
  529. #define BCM2835_PAD_DRIVE_14mA 0x06 /*!< 14mA drive current */
  530. #define BCM2835_PAD_DRIVE_16mA 0x07 /*!< 16mA drive current */
  531. /*! \brief bcm2835PadGroup
  532. Pad group specification for bcm2835_gpio_pad()
  533. */
  534. typedef enum {
  535. BCM2835_PAD_GROUP_GPIO_0_27 = 0, /*!< Pad group for GPIO pads 0 to 27 */
  536. BCM2835_PAD_GROUP_GPIO_28_45 = 1, /*!< Pad group for GPIO pads 28 to 45 */
  537. BCM2835_PAD_GROUP_GPIO_46_53 = 2 /*!< Pad group for GPIO pads 46 to 53 */
  538. } bcm2835PadGroup;
  539. /*! \brief GPIO Pin Numbers
  540. Here we define Raspberry Pin GPIO pins on P1 in terms of the underlying BCM GPIO pin numbers.
  541. These can be passed as a pin number to any function requiring a pin.
  542. Not all pins on the RPi 26 bin IDE plug are connected to GPIO pins
  543. and some can adopt an alternate function.
  544. RPi version 2 has some slightly different pinouts, and these are values RPI_V2_*.
  545. RPi B+ has yet differnet pinouts and these are defined in RPI_BPLUS_*.
  546. At bootup, pins 8 and 10 are set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
  547. When SPI0 is in use (ie after bcm2835_spi_begin()), SPI0 pins are dedicated to SPI
  548. and cant be controlled independently.
  549. If you are using the RPi Compute Module, just use the GPIO number: there is no need to use one of these
  550. symbolic names
  551. */
  552. typedef enum {
  553. RPI_GPIO_P1_03 = 0, /*!< Version 1, Pin P1-03 */
  554. RPI_GPIO_P1_05 = 1, /*!< Version 1, Pin P1-05 */
  555. RPI_GPIO_P1_07 = 4, /*!< Version 1, Pin P1-07 */
  556. RPI_GPIO_P1_08 = 14, /*!< Version 1, Pin P1-08, defaults to alt function 0 UART0_TXD */
  557. RPI_GPIO_P1_10 = 15, /*!< Version 1, Pin P1-10, defaults to alt function 0 UART0_RXD */
  558. RPI_GPIO_P1_11 = 17, /*!< Version 1, Pin P1-11 */
  559. RPI_GPIO_P1_12 = 18, /*!< Version 1, Pin P1-12, can be PWM channel 0 in ALT FUN 5 */
  560. RPI_GPIO_P1_13 = 21, /*!< Version 1, Pin P1-13 */
  561. RPI_GPIO_P1_15 = 22, /*!< Version 1, Pin P1-15 */
  562. RPI_GPIO_P1_16 = 23, /*!< Version 1, Pin P1-16 */
  563. RPI_GPIO_P1_18 = 24, /*!< Version 1, Pin P1-18 */
  564. RPI_GPIO_P1_19 = 10, /*!< Version 1, Pin P1-19, MOSI when SPI0 in use */
  565. RPI_GPIO_P1_21 = 9, /*!< Version 1, Pin P1-21, MISO when SPI0 in use */
  566. RPI_GPIO_P1_22 = 25, /*!< Version 1, Pin P1-22 */
  567. RPI_GPIO_P1_23 = 11, /*!< Version 1, Pin P1-23, CLK when SPI0 in use */
  568. RPI_GPIO_P1_24 = 8, /*!< Version 1, Pin P1-24, CE0 when SPI0 in use */
  569. RPI_GPIO_P1_26 = 7, /*!< Version 1, Pin P1-26, CE1 when SPI0 in use */
  570. /* RPi Version 2 */
  571. RPI_V2_GPIO_P1_03 = 2, /*!< Version 2, Pin P1-03 */
  572. RPI_V2_GPIO_P1_05 = 3, /*!< Version 2, Pin P1-05 */
  573. RPI_V2_GPIO_P1_07 = 4, /*!< Version 2, Pin P1-07 */
  574. RPI_V2_GPIO_P1_08 = 14, /*!< Version 2, Pin P1-08, defaults to alt function 0 UART0_TXD */
  575. RPI_V2_GPIO_P1_10 = 15, /*!< Version 2, Pin P1-10, defaults to alt function 0 UART0_RXD */
  576. RPI_V2_GPIO_P1_11 = 17, /*!< Version 2, Pin P1-11 */
  577. RPI_V2_GPIO_P1_12 = 18, /*!< Version 2, Pin P1-12, can be PWM channel 0 in ALT FUN 5 */
  578. RPI_V2_GPIO_P1_13 = 27, /*!< Version 2, Pin P1-13 */
  579. RPI_V2_GPIO_P1_15 = 22, /*!< Version 2, Pin P1-15 */
  580. RPI_V2_GPIO_P1_16 = 23, /*!< Version 2, Pin P1-16 */
  581. RPI_V2_GPIO_P1_18 = 24, /*!< Version 2, Pin P1-18 */
  582. RPI_V2_GPIO_P1_19 = 10, /*!< Version 2, Pin P1-19, MOSI when SPI0 in use */
  583. RPI_V2_GPIO_P1_21 = 9, /*!< Version 2, Pin P1-21, MISO when SPI0 in use */
  584. RPI_V2_GPIO_P1_22 = 25, /*!< Version 2, Pin P1-22 */
  585. RPI_V2_GPIO_P1_23 = 11, /*!< Version 2, Pin P1-23, CLK when SPI0 in use */
  586. RPI_V2_GPIO_P1_24 = 8, /*!< Version 2, Pin P1-24, CE0 when SPI0 in use */
  587. RPI_V2_GPIO_P1_26 = 7, /*!< Version 2, Pin P1-26, CE1 when SPI0 in use */
  588. RPI_V2_GPIO_P1_29 = 5, /*!< Version 2, Pin P1-29 */
  589. RPI_V2_GPIO_P1_31 = 6, /*!< Version 2, Pin P1-31 */
  590. RPI_V2_GPIO_P1_32 = 12, /*!< Version 2, Pin P1-32 */
  591. RPI_V2_GPIO_P1_33 = 13, /*!< Version 2, Pin P1-33 */
  592. RPI_V2_GPIO_P1_35 = 19, /*!< Version 2, Pin P1-35 */
  593. RPI_V2_GPIO_P1_36 = 16, /*!< Version 2, Pin P1-36 */
  594. RPI_V2_GPIO_P1_37 = 26, /*!< Version 2, Pin P1-37 */
  595. RPI_V2_GPIO_P1_38 = 20, /*!< Version 2, Pin P1-38 */
  596. RPI_V2_GPIO_P1_40 = 21, /*!< Version 2, Pin P1-40 */
  597. /* RPi Version 2, new plug P5 */
  598. RPI_V2_GPIO_P5_03 = 28, /*!< Version 2, Pin P5-03 */
  599. RPI_V2_GPIO_P5_04 = 29, /*!< Version 2, Pin P5-04 */
  600. RPI_V2_GPIO_P5_05 = 30, /*!< Version 2, Pin P5-05 */
  601. RPI_V2_GPIO_P5_06 = 31, /*!< Version 2, Pin P5-06 */
  602. /* RPi B+ J8 header, also RPi 2 40 pin GPIO header */
  603. RPI_BPLUS_GPIO_J8_03 = 2, /*!< B+, Pin J8-03 */
  604. RPI_BPLUS_GPIO_J8_05 = 3, /*!< B+, Pin J8-05 */
  605. RPI_BPLUS_GPIO_J8_07 = 4, /*!< B+, Pin J8-07 */
  606. RPI_BPLUS_GPIO_J8_08 = 14, /*!< B+, Pin J8-08, defaults to alt function 0 UART0_TXD */
  607. RPI_BPLUS_GPIO_J8_10 = 15, /*!< B+, Pin J8-10, defaults to alt function 0 UART0_RXD */
  608. RPI_BPLUS_GPIO_J8_11 = 17, /*!< B+, Pin J8-11 */
  609. RPI_BPLUS_GPIO_J8_12 = 18, /*!< B+, Pin J8-12, can be PWM channel 0 in ALT FUN 5 */
  610. RPI_BPLUS_GPIO_J8_13 = 27, /*!< B+, Pin J8-13 */
  611. RPI_BPLUS_GPIO_J8_15 = 22, /*!< B+, Pin J8-15 */
  612. RPI_BPLUS_GPIO_J8_16 = 23, /*!< B+, Pin J8-16 */
  613. RPI_BPLUS_GPIO_J8_18 = 24, /*!< B+, Pin J8-18 */
  614. RPI_BPLUS_GPIO_J8_19 = 10, /*!< B+, Pin J8-19, MOSI when SPI0 in use */
  615. RPI_BPLUS_GPIO_J8_21 = 9, /*!< B+, Pin J8-21, MISO when SPI0 in use */
  616. RPI_BPLUS_GPIO_J8_22 = 25, /*!< B+, Pin J8-22 */
  617. RPI_BPLUS_GPIO_J8_23 = 11, /*!< B+, Pin J8-23, CLK when SPI0 in use */
  618. RPI_BPLUS_GPIO_J8_24 = 8, /*!< B+, Pin J8-24, CE0 when SPI0 in use */
  619. RPI_BPLUS_GPIO_J8_26 = 7, /*!< B+, Pin J8-26, CE1 when SPI0 in use */
  620. RPI_BPLUS_GPIO_J8_29 = 5, /*!< B+, Pin J8-29, */
  621. RPI_BPLUS_GPIO_J8_31 = 6, /*!< B+, Pin J8-31, */
  622. RPI_BPLUS_GPIO_J8_32 = 12, /*!< B+, Pin J8-32, */
  623. RPI_BPLUS_GPIO_J8_33 = 13, /*!< B+, Pin J8-33, */
  624. RPI_BPLUS_GPIO_J8_35 = 19, /*!< B+, Pin J8-35, */
  625. RPI_BPLUS_GPIO_J8_36 = 16, /*!< B+, Pin J8-36, */
  626. RPI_BPLUS_GPIO_J8_37 = 26, /*!< B+, Pin J8-37, */
  627. RPI_BPLUS_GPIO_J8_38 = 20, /*!< B+, Pin J8-38, */
  628. RPI_BPLUS_GPIO_J8_40 = 21 /*!< B+, Pin J8-40, */
  629. } RPiGPIOPin;
  630. /* Defines for SPI
  631. GPIO register offsets from BCM2835_SPI0_BASE.
  632. Offsets into the SPI Peripheral block in bytes per 10.5 SPI Register Map
  633. */
  634. #define BCM2835_SPI0_CS 0x0000 /*!< SPI Master Control and Status */
  635. #define BCM2835_SPI0_FIFO 0x0004 /*!< SPI Master TX and RX FIFOs */
  636. #define BCM2835_SPI0_CLK 0x0008 /*!< SPI Master Clock Divider */
  637. #define BCM2835_SPI0_DLEN 0x000c /*!< SPI Master Data Length */
  638. #define BCM2835_SPI0_LTOH 0x0010 /*!< SPI LOSSI mode TOH */
  639. #define BCM2835_SPI0_DC 0x0014 /*!< SPI DMA DREQ Controls */
  640. /* Register masks for SPI0_CS */
  641. #define BCM2835_SPI0_CS_LEN_LONG 0x02000000 /*!< Enable Long data word in Lossi mode if DMA_LEN is set */
  642. #define BCM2835_SPI0_CS_DMA_LEN 0x01000000 /*!< Enable DMA mode in Lossi mode */
  643. #define BCM2835_SPI0_CS_CSPOL2 0x00800000 /*!< Chip Select 2 Polarity */
  644. #define BCM2835_SPI0_CS_CSPOL1 0x00400000 /*!< Chip Select 1 Polarity */
  645. #define BCM2835_SPI0_CS_CSPOL0 0x00200000 /*!< Chip Select 0 Polarity */
  646. #define BCM2835_SPI0_CS_RXF 0x00100000 /*!< RXF - RX FIFO Full */
  647. #define BCM2835_SPI0_CS_RXR 0x00080000 /*!< RXR RX FIFO needs Reading (full) */
  648. #define BCM2835_SPI0_CS_TXD 0x00040000 /*!< TXD TX FIFO can accept Data */
  649. #define BCM2835_SPI0_CS_RXD 0x00020000 /*!< RXD RX FIFO contains Data */
  650. #define BCM2835_SPI0_CS_DONE 0x00010000 /*!< Done transfer Done */
  651. #define BCM2835_SPI0_CS_TE_EN 0x00008000 /*!< Unused */
  652. #define BCM2835_SPI0_CS_LMONO 0x00004000 /*!< Unused */
  653. #define BCM2835_SPI0_CS_LEN 0x00002000 /*!< LEN LoSSI enable */
  654. #define BCM2835_SPI0_CS_REN 0x00001000 /*!< REN Read Enable */
  655. #define BCM2835_SPI0_CS_ADCS 0x00000800 /*!< ADCS Automatically Deassert Chip Select */
  656. #define BCM2835_SPI0_CS_INTR 0x00000400 /*!< INTR Interrupt on RXR */
  657. #define BCM2835_SPI0_CS_INTD 0x00000200 /*!< INTD Interrupt on Done */
  658. #define BCM2835_SPI0_CS_DMAEN 0x00000100 /*!< DMAEN DMA Enable */
  659. #define BCM2835_SPI0_CS_TA 0x00000080 /*!< Transfer Active */
  660. #define BCM2835_SPI0_CS_CSPOL 0x00000040 /*!< Chip Select Polarity */
  661. #define BCM2835_SPI0_CS_CLEAR 0x00000030 /*!< Clear FIFO Clear RX and TX */
  662. #define BCM2835_SPI0_CS_CLEAR_RX 0x00000020 /*!< Clear FIFO Clear RX */
  663. #define BCM2835_SPI0_CS_CLEAR_TX 0x00000010 /*!< Clear FIFO Clear TX */
  664. #define BCM2835_SPI0_CS_CPOL 0x00000008 /*!< Clock Polarity */
  665. #define BCM2835_SPI0_CS_CPHA 0x00000004 /*!< Clock Phase */
  666. #define BCM2835_SPI0_CS_CS 0x00000003 /*!< Chip Select */
  667. /*! \brief bcm2835SPIBitOrder SPI Bit order
  668. Specifies the SPI data bit ordering for bcm2835_spi_setBitOrder()
  669. */
  670. typedef enum {
  671. BCM2835_SPI_BIT_ORDER_LSBFIRST = 0, /*!< LSB First */
  672. BCM2835_SPI_BIT_ORDER_MSBFIRST = 1 /*!< MSB First */
  673. } bcm2835SPIBitOrder;
  674. /*! \brief SPI Data mode
  675. Specify the SPI data mode to be passed to bcm2835_spi_setDataMode()
  676. */
  677. typedef enum {
  678. BCM2835_SPI_MODE0 = 0, /*!< CPOL = 0, CPHA = 0 */
  679. BCM2835_SPI_MODE1 = 1, /*!< CPOL = 0, CPHA = 1 */
  680. BCM2835_SPI_MODE2 = 2, /*!< CPOL = 1, CPHA = 0 */
  681. BCM2835_SPI_MODE3 = 3 /*!< CPOL = 1, CPHA = 1 */
  682. } bcm2835SPIMode;
  683. /*! \brief bcm2835SPIChipSelect
  684. Specify the SPI chip select pin(s)
  685. */
  686. typedef enum {
  687. BCM2835_SPI_CS0 = 0, /*!< Chip Select 0 */
  688. BCM2835_SPI_CS1 = 1, /*!< Chip Select 1 */
  689. BCM2835_SPI_CS2 = 2, /*!< Chip Select 2 (ie pins CS1 and CS2 are asserted) */
  690. BCM2835_SPI_CS_NONE = 3 /*!< No CS, control it yourself */
  691. } bcm2835SPIChipSelect;
  692. /*! \brief bcm2835SPIClockDivider
  693. Specifies the divider used to generate the SPI clock from the system clock.
  694. Figures below give the divider, clock period and clock frequency.
  695. Clock divided is based on nominal base clock rate of 250MHz
  696. It is reported that (contrary to the documentation) any even divider may used.
  697. The frequencies shown for each divider have been confirmed by measurement.
  698. */
  699. typedef enum {
  700. BCM2835_SPI_CLOCK_DIVIDER_65536 = 0, /*!< 65536 = 262.144us = 3.814697260kHz */
  701. BCM2835_SPI_CLOCK_DIVIDER_32768 = 32768, /*!< 32768 = 131.072us = 7.629394531kHz */
  702. BCM2835_SPI_CLOCK_DIVIDER_16384 = 16384, /*!< 16384 = 65.536us = 15.25878906kHz */
  703. BCM2835_SPI_CLOCK_DIVIDER_8192 = 8192, /*!< 8192 = 32.768us = 30/51757813kHz */
  704. BCM2835_SPI_CLOCK_DIVIDER_4096 = 4096, /*!< 4096 = 16.384us = 61.03515625kHz */
  705. BCM2835_SPI_CLOCK_DIVIDER_2048 = 2048, /*!< 2048 = 8.192us = 122.0703125kHz */
  706. BCM2835_SPI_CLOCK_DIVIDER_1024 = 1024, /*!< 1024 = 4.096us = 244.140625kHz */
  707. BCM2835_SPI_CLOCK_DIVIDER_512 = 512, /*!< 512 = 2.048us = 488.28125kHz */
  708. BCM2835_SPI_CLOCK_DIVIDER_256 = 256, /*!< 256 = 1.024us = 976.5625kHz */
  709. BCM2835_SPI_CLOCK_DIVIDER_128 = 128, /*!< 128 = 512ns = = 1.953125MHz */
  710. BCM2835_SPI_CLOCK_DIVIDER_64 = 64, /*!< 64 = 256ns = 3.90625MHz */
  711. BCM2835_SPI_CLOCK_DIVIDER_32 = 32, /*!< 32 = 128ns = 7.8125MHz */
  712. BCM2835_SPI_CLOCK_DIVIDER_16 = 16, /*!< 16 = 64ns = 15.625MHz */
  713. BCM2835_SPI_CLOCK_DIVIDER_8 = 8, /*!< 8 = 32ns = 31.25MHz */
  714. BCM2835_SPI_CLOCK_DIVIDER_4 = 4, /*!< 4 = 16ns = 62.5MHz */
  715. BCM2835_SPI_CLOCK_DIVIDER_2 = 2, /*!< 2 = 8ns = 125MHz, fastest you can get */
  716. BCM2835_SPI_CLOCK_DIVIDER_1 = 1 /*!< 1 = 262.144us = 3.814697260kHz, same as 0/65536 */
  717. } bcm2835SPIClockDivider;
  718. /* Defines for I2C
  719. GPIO register offsets from BCM2835_BSC*_BASE.
  720. Offsets into the BSC Peripheral block in bytes per 3.1 BSC Register Map
  721. */
  722. #define BCM2835_BSC_C 0x0000 /*!< BSC Master Control */
  723. #define BCM2835_BSC_S 0x0004 /*!< BSC Master Status */
  724. #define BCM2835_BSC_DLEN 0x0008 /*!< BSC Master Data Length */
  725. #define BCM2835_BSC_A 0x000c /*!< BSC Master Slave Address */
  726. #define BCM2835_BSC_FIFO 0x0010 /*!< BSC Master Data FIFO */
  727. #define BCM2835_BSC_DIV 0x0014 /*!< BSC Master Clock Divider */
  728. #define BCM2835_BSC_DEL 0x0018 /*!< BSC Master Data Delay */
  729. #define BCM2835_BSC_CLKT 0x001c /*!< BSC Master Clock Stretch Timeout */
  730. /* Register masks for BSC_C */
  731. #define BCM2835_BSC_C_I2CEN 0x00008000 /*!< I2C Enable, 0 = disabled, 1 = enabled */
  732. #define BCM2835_BSC_C_INTR 0x00000400 /*!< Interrupt on RX */
  733. #define BCM2835_BSC_C_INTT 0x00000200 /*!< Interrupt on TX */
  734. #define BCM2835_BSC_C_INTD 0x00000100 /*!< Interrupt on DONE */
  735. #define BCM2835_BSC_C_ST 0x00000080 /*!< Start transfer, 1 = Start a new transfer */
  736. #define BCM2835_BSC_C_CLEAR_1 0x00000020 /*!< Clear FIFO Clear */
  737. #define BCM2835_BSC_C_CLEAR_2 0x00000010 /*!< Clear FIFO Clear */
  738. #define BCM2835_BSC_C_READ 0x00000001 /*!< Read transfer */
  739. /* Register masks for BSC_S */
  740. #define BCM2835_BSC_S_CLKT 0x00000200 /*!< Clock stretch timeout */
  741. #define BCM2835_BSC_S_ERR 0x00000100 /*!< ACK error */
  742. #define BCM2835_BSC_S_RXF 0x00000080 /*!< RXF FIFO full, 0 = FIFO is not full, 1 = FIFO is full */
  743. #define BCM2835_BSC_S_TXE 0x00000040 /*!< TXE FIFO full, 0 = FIFO is not full, 1 = FIFO is full */
  744. #define BCM2835_BSC_S_RXD 0x00000020 /*!< RXD FIFO contains data */
  745. #define BCM2835_BSC_S_TXD 0x00000010 /*!< TXD FIFO can accept data */
  746. #define BCM2835_BSC_S_RXR 0x00000008 /*!< RXR FIFO needs reading (full) */
  747. #define BCM2835_BSC_S_TXW 0x00000004 /*!< TXW FIFO needs writing (full) */
  748. #define BCM2835_BSC_S_DONE 0x00000002 /*!< Transfer DONE */
  749. #define BCM2835_BSC_S_TA 0x00000001 /*!< Transfer Active */
  750. #define BCM2835_BSC_FIFO_SIZE 16 /*!< BSC FIFO size */
  751. /*! \brief bcm2835I2CClockDivider
  752. Specifies the divider used to generate the I2C clock from the system clock.
  753. Clock divided is based on nominal base clock rate of 250MHz
  754. */
  755. typedef enum {
  756. BCM2835_I2C_CLOCK_DIVIDER_2500 = 2500, /*!< 2500 = 10us = 100 kHz */
  757. BCM2835_I2C_CLOCK_DIVIDER_626 = 626, /*!< 622 = 2.504us = 399.3610 kHz */
  758. BCM2835_I2C_CLOCK_DIVIDER_150 = 150, /*!< 150 = 60ns = 1.666 MHz (default at reset) */
  759. BCM2835_I2C_CLOCK_DIVIDER_148 = 148 /*!< 148 = 59ns = 1.689 MHz */
  760. } bcm2835I2CClockDivider;
  761. /*! \brief bcm2835I2CReasonCodes
  762. Specifies the reason codes for the bcm2835_i2c_write and bcm2835_i2c_read functions.
  763. */
  764. typedef enum {
  765. BCM2835_I2C_REASON_OK = 0x00, /*!< Success */
  766. BCM2835_I2C_REASON_ERROR_NACK = 0x01, /*!< Received a NACK */
  767. BCM2835_I2C_REASON_ERROR_CLKT = 0x02, /*!< Received Clock Stretch Timeout */
  768. BCM2835_I2C_REASON_ERROR_DATA = 0x04 /*!< Not all data is sent / received */
  769. } bcm2835I2CReasonCodes;
  770. /* Defines for ST
  771. GPIO register offsets from BCM2835_ST_BASE.
  772. Offsets into the ST Peripheral block in bytes per 12.1 System Timer Registers
  773. The System Timer peripheral provides four 32-bit timer channels and a single 64-bit free running counter.
  774. BCM2835_ST_CLO is the System Timer Counter Lower bits register.
  775. The system timer free-running counter lower register is a read-only register that returns the current value
  776. of the lower 32-bits of the free running counter.
  777. BCM2835_ST_CHI is the System Timer Counter Upper bits register.
  778. The system timer free-running counter upper register is a read-only register that returns the current value
  779. of the upper 32-bits of the free running counter.
  780. */
  781. #define BCM2835_ST_CS 0x0000 /*!< System Timer Control/Status */
  782. #define BCM2835_ST_CLO 0x0004 /*!< System Timer Counter Lower 32 bits */
  783. #define BCM2835_ST_CHI 0x0008 /*!< System Timer Counter Upper 32 bits */
  784. /* Defines for PWM, word offsets (ie 4 byte multiples) */
  785. #define BCM2835_PWM_CONTROL 0 /*!< PWM Control register */
  786. #define BCM2835_PWM_STATUS 1 /*!< PWM Status register */
  787. #define BCM2835_PWM_DMAC 2 /*!< PWM DMA Configuration */
  788. #define BCM2835_PWM0_RANGE 4 /*!< PWM Channel 0 Range */
  789. #define BCM2835_PWM0_DATA 5 /*!< PWM Channel 0 Data */
  790. #define BCM2835_PWM_FIF1 6 /*!< PWM FIFO Input */
  791. #define BCM2835_PWM1_RANGE 8 /*!< PWM Channel 1 Range */
  792. #define BCM2835_PWM1_DATA 9 /*!< PWM Channel 1 Data */
  793. /* Defines for PWM Clock, word offsets (ie 4 byte multiples) */
  794. #define BCM2835_PWMCLK_CNTL 40 /*!< PWM Clock Control */
  795. #define BCM2835_PWMCLK_DIV 41 /*!< PWM Clock Divider */
  796. #define BCM2835_PWM_PASSWRD (0x5A << 24) /*!< Password to enable setting PWM clock */
  797. #define BCM2835_PWM1_MS_MODE 0x8000 /*!< Run in Mark/Space mode */
  798. #define BCM2835_PWM1_USEFIFO 0x2000 /*!< Data from FIFO */
  799. #define BCM2835_PWM1_REVPOLAR 0x1000 /*!< Reverse polarity */
  800. #define BCM2835_PWM1_OFFSTATE 0x0800 /*!< Ouput Off state */
  801. #define BCM2835_PWM1_REPEATFF 0x0400 /*!< Repeat last value if FIFO empty */
  802. #define BCM2835_PWM1_SERIAL 0x0200 /*!< Run in serial mode */
  803. #define BCM2835_PWM1_ENABLE 0x0100 /*!< Channel Enable */
  804. #define BCM2835_PWM0_MS_MODE 0x0080 /*!< Run in Mark/Space mode */
  805. #define BCM2835_PWM_CLEAR_FIFO 0x0040 /*!< Clear FIFO */
  806. #define BCM2835_PWM0_USEFIFO 0x0020 /*!< Data from FIFO */
  807. #define BCM2835_PWM0_REVPOLAR 0x0010 /*!< Reverse polarity */
  808. #define BCM2835_PWM0_OFFSTATE 0x0008 /*!< Ouput Off state */
  809. #define BCM2835_PWM0_REPEATFF 0x0004 /*!< Repeat last value if FIFO empty */
  810. #define BCM2835_PWM0_SERIAL 0x0002 /*!< Run in serial mode */
  811. #define BCM2835_PWM0_ENABLE 0x0001 /*!< Channel Enable */
  812. /*! \brief bcm2835PWMClockDivider
  813. Specifies the divider used to generate the PWM clock from the system clock.
  814. Figures below give the divider, clock period and clock frequency.
  815. Clock divided is based on nominal PWM base clock rate of 19.2MHz
  816. The frequencies shown for each divider have been confirmed by measurement
  817. */
  818. typedef enum {
  819. BCM2835_PWM_CLOCK_DIVIDER_2048 = 2048, /*!< 2048 = 9.375kHz */
  820. BCM2835_PWM_CLOCK_DIVIDER_1024 = 1024, /*!< 1024 = 18.75kHz */
  821. BCM2835_PWM_CLOCK_DIVIDER_512 = 512, /*!< 512 = 37.5kHz */
  822. BCM2835_PWM_CLOCK_DIVIDER_256 = 256, /*!< 256 = 75kHz */
  823. BCM2835_PWM_CLOCK_DIVIDER_128 = 128, /*!< 128 = 150kHz */
  824. BCM2835_PWM_CLOCK_DIVIDER_64 = 64, /*!< 64 = 300kHz */
  825. BCM2835_PWM_CLOCK_DIVIDER_32 = 32, /*!< 32 = 600.0kHz */
  826. BCM2835_PWM_CLOCK_DIVIDER_16 = 16, /*!< 16 = 1.2MHz */
  827. BCM2835_PWM_CLOCK_DIVIDER_8 = 8, /*!< 8 = 2.4MHz */
  828. BCM2835_PWM_CLOCK_DIVIDER_4 = 4, /*!< 4 = 4.8MHz */
  829. BCM2835_PWM_CLOCK_DIVIDER_2 = 2, /*!< 2 = 9.6MHz, fastest you can get */
  830. BCM2835_PWM_CLOCK_DIVIDER_1 = 1 /*!< 1 = 4.6875kHz, same as divider 4096 */
  831. } bcm2835PWMClockDivider;
  832. /*! @} */
  833. /* Historical name compatibility */
  834. #ifndef BCM2835_NO_DELAY_COMPATIBILITY
  835. #define delay(x) bcm2835_delay(x)
  836. #define delayMicroseconds(x) bcm2835_delayMicroseconds(x)
  837. #endif
  838. #ifdef __cplusplus
  839. extern "C" {
  840. #endif
  841. /*! \defgroup BCM2835initgrp Library initialisation and management
  842. \ingroup BCM2835grp
  843. These functions allow you to intialise and control the bcm2835 library
  844. @{
  845. */
  846. /*! Initialise the library by opening /dev/mem (if you are root)
  847. or /dev/gpiomem (if you are not)
  848. and getting pointers to the
  849. internal memory for BCM 2835 device registers. You must call this (successfully)
  850. before calling any other
  851. functions in this library (except bcm2835_set_debug).
  852. If bcm2835_init() fails by returning 0,
  853. calling any other function may result in crashes or other failures.
  854. If bcm2835_init() succeeds but you are not running as root, then only gpio operations
  855. are permitted, and calling any other functions may result in crashes or other failures. .
  856. Prints messages to stderr in case of errors.
  857. \return 1 if successful else 0
  858. */
  859. extern int bcm2835_init(void);
  860. /*! Close the library, deallocating any allocated memory and closing /dev/mem
  861. \return 1 if successful else 0
  862. */
  863. extern int bcm2835_close(void);
  864. /*! Sets the debug level of the library.
  865. A value of 1 prevents mapping to /dev/mem, and makes the library print out
  866. what it would do, rather than accessing the GPIO registers.
  867. A value of 0, the default, causes normal operation.
  868. Call this before calling bcm2835_init();
  869. \param[in] debug The new debug level. 1 means debug
  870. */
  871. extern void bcm2835_set_debug(uint8_t debug);
  872. /*! Returns the version number of the library, same as BCM2835_VERSION
  873. \return the current library version number
  874. */
  875. extern unsigned int bcm2835_version(void);
  876. /*! @} */
  877. /*! \defgroup BCM2835lowlevelgrp Low level register access
  878. \ingroup BCM2835grp
  879. These functions provide low level register access, and should not generally
  880. need to be used
  881. @{
  882. */
  883. /*! Gets the base of a register
  884. \param[in] regbase You can use one of the common values BCM2835_REGBASE_*
  885. in \ref bcm2835RegisterBase
  886. \return the register base
  887. \sa Physical Addresses
  888. */
  889. extern uint32_t* bcm2835_regbase(uint8_t regbase);
  890. /*! Reads 32 bit value from a peripheral address WITH a memory barrier before and after each read.
  891. This is safe, but slow. The MB before protects this read from any in-flight reads that didn't
  892. use a MB. The MB after protects subsequent reads from another peripheral.
  893. \param[in] paddr Physical address to read from. See BCM2835_GPIO_BASE etc.
  894. \return the value read from the 32 bit register
  895. \sa Physical Addresses
  896. */
  897. extern uint32_t bcm2835_peri_read(volatile uint32_t* paddr);
  898. /*! Reads 32 bit value from a peripheral address WITHOUT the read barriers
  899. You should only use this when:
  900. o your code has previously called bcm2835_peri_read() for a register
  901. within the same peripheral, and no read or write to another peripheral has occurred since.
  902. o your code has called bcm2835_memory_barrier() since the last access to ANOTHER peripheral.
  903. \param[in] paddr Physical address to read from. See BCM2835_GPIO_BASE etc.
  904. \return the value read from the 32 bit register
  905. \sa Physical Addresses
  906. */
  907. extern uint32_t bcm2835_peri_read_nb(volatile uint32_t* paddr);
  908. /*! Writes 32 bit value from a peripheral address WITH a memory barrier before and after each write
  909. This is safe, but slow. The MB before ensures that any in-flight write to another peripheral
  910. completes before this write is issued. The MB after ensures that subsequent reads and writes
  911. to another peripheral will see the effect of this write.
  912. This is a tricky optimization; if you aren't sure, use the barrier version.
  913. \param[in] paddr Physical address to read from. See BCM2835_GPIO_BASE etc.
  914. \param[in] value The 32 bit value to write
  915. \sa Physical Addresses
  916. */
  917. extern void bcm2835_peri_write(volatile uint32_t* paddr, uint32_t value);
  918. /*! Writes 32 bit value from a peripheral address without the write barrier
  919. You should only use this when:
  920. o your code has previously called bcm2835_peri_write() for a register
  921. within the same peripheral, and no other peripheral access has occurred since.
  922. o your code has called bcm2835_memory_barrier() since the last access to ANOTHER peripheral.
  923. This is a tricky optimization; if you aren't sure, use the barrier version.
  924. \param[in] paddr Physical address to read from. See BCM2835_GPIO_BASE etc.
  925. \param[in] value The 32 bit value to write
  926. \sa Physical Addresses
  927. */
  928. extern void bcm2835_peri_write_nb(volatile uint32_t* paddr, uint32_t value);
  929. /*! Alters a number of bits in a 32 peripheral regsiter.
  930. It reads the current valu and then alters the bits defines as 1 in mask,
  931. according to the bit value in value.
  932. All other bits that are 0 in the mask are unaffected.
  933. Use this to alter a subset of the bits in a register.
  934. Memory barriers are used. Note that this is not atomic; an interrupt
  935. routine can cause unexpected results.
  936. \param[in] paddr Physical address to read from. See BCM2835_GPIO_BASE etc.
  937. \param[in] value The 32 bit value to write, masked in by mask.
  938. \param[in] mask Bitmask that defines the bits that will be altered in the register.
  939. \sa Physical Addresses
  940. */
  941. extern void bcm2835_peri_set_bits(volatile uint32_t* paddr, uint32_t value, uint32_t mask);
  942. /*! @} end of lowlevel */
  943. /*! \defgroup BCM2835gpiogrp GPIO register access
  944. \ingroup BCM2835grp
  945. These functions allow you to control the GPIO interface. You can set the
  946. function of each GPIO pin, read the input state and set the output state.
  947. @{
  948. */
  949. /*! Sets the Function Select register for the given pin, which configures
  950. the pin as Input, Output or one of the 6 alternate functions.
  951. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  952. \param[in] mode Mode to set the pin to, one of BCM2835_GPIO_FSEL_* from \ref bcm2835FunctionSelect
  953. */
  954. extern void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode);
  955. /*! Sets the specified pin output to
  956. HIGH.
  957. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  958. \sa bcm2835_gpio_write()
  959. */
  960. extern void bcm2835_gpio_set(uint8_t pin);
  961. /*! Sets the specified pin output to
  962. LOW.
  963. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  964. \sa bcm2835_gpio_write()
  965. */
  966. extern void bcm2835_gpio_clr(uint8_t pin);
  967. /*! Sets any of the first 32 GPIO output pins specified in the mask to
  968. HIGH.
  969. \param[in] mask Mask of pins to affect. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  970. \sa bcm2835_gpio_write_multi()
  971. */
  972. extern void bcm2835_gpio_set_multi(uint32_t mask);
  973. /*! Sets any of the first 32 GPIO output pins specified in the mask to
  974. LOW.
  975. \param[in] mask Mask of pins to affect. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  976. \sa bcm2835_gpio_write_multi()
  977. */
  978. extern void bcm2835_gpio_clr_multi(uint32_t mask);
  979. /*! Reads the current level on the specified
  980. pin and returns either HIGH or LOW. Works whether or not the pin
  981. is an input or an output.
  982. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  983. \return the current level either HIGH or LOW
  984. */
  985. extern uint8_t bcm2835_gpio_lev(uint8_t pin);
  986. /*! Event Detect Status.
  987. Tests whether the specified pin has detected a level or edge
  988. as requested by bcm2835_gpio_ren(), bcm2835_gpio_fen(), bcm2835_gpio_hen(),
  989. bcm2835_gpio_len(), bcm2835_gpio_aren(), bcm2835_gpio_afen().
  990. Clear the flag for a given pin by calling bcm2835_gpio_set_eds(pin);
  991. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  992. \return HIGH if the event detect status for the given pin is true.
  993. */
  994. extern uint8_t bcm2835_gpio_eds(uint8_t pin);
  995. /*! Same as bcm2835_gpio_eds() but checks if any of the pins specified in
  996. the mask have detected a level or edge.
  997. \param[in] mask Mask of pins to check. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  998. \return Mask of pins HIGH if the event detect status for the given pin is true.
  999. */
  1000. extern uint32_t bcm2835_gpio_eds_multi(uint32_t mask);
  1001. /*! Sets the Event Detect Status register for a given pin to 1,
  1002. which has the effect of clearing the flag. Use this afer seeing
  1003. an Event Detect Status on the pin.
  1004. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1005. */
  1006. extern void bcm2835_gpio_set_eds(uint8_t pin);
  1007. /*! Same as bcm2835_gpio_set_eds() but clears the flag for any pin which
  1008. is set in the mask.
  1009. \param[in] mask Mask of pins to clear. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  1010. */
  1011. extern void bcm2835_gpio_set_eds_multi(uint32_t mask);
  1012. /*! Enable Rising Edge Detect Enable for the specified pin.
  1013. When a rising edge is detected, sets the appropriate pin in Event Detect Status.
  1014. The GPRENn registers use
  1015. synchronous edge detection. This means the input signal is sampled using the
  1016. system clock and then it is looking for a ?011? pattern on the sampled signal. This
  1017. has the effect of suppressing glitches.
  1018. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1019. */
  1020. extern void bcm2835_gpio_ren(uint8_t pin);
  1021. /*! Disable Rising Edge Detect Enable for the specified pin.
  1022. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1023. */
  1024. extern void bcm2835_gpio_clr_ren(uint8_t pin);
  1025. /*! Enable Falling Edge Detect Enable for the specified pin.
  1026. When a falling edge is detected, sets the appropriate pin in Event Detect Status.
  1027. The GPRENn registers use
  1028. synchronous edge detection. This means the input signal is sampled using the
  1029. system clock and then it is looking for a ?100? pattern on the sampled signal. This
  1030. has the effect of suppressing glitches.
  1031. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1032. */
  1033. extern void bcm2835_gpio_fen(uint8_t pin);
  1034. /*! Disable Falling Edge Detect Enable for the specified pin.
  1035. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1036. */
  1037. extern void bcm2835_gpio_clr_fen(uint8_t pin);
  1038. /*! Enable High Detect Enable for the specified pin.
  1039. When a HIGH level is detected on the pin, sets the appropriate pin in Event Detect Status.
  1040. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1041. */
  1042. extern void bcm2835_gpio_hen(uint8_t pin);
  1043. /*! Disable High Detect Enable for the specified pin.
  1044. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1045. */
  1046. extern void bcm2835_gpio_clr_hen(uint8_t pin);
  1047. /*! Enable Low Detect Enable for the specified pin.
  1048. When a LOW level is detected on the pin, sets the appropriate pin in Event Detect Status.
  1049. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1050. */
  1051. extern void bcm2835_gpio_len(uint8_t pin);
  1052. /*! Disable Low Detect Enable for the specified pin.
  1053. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1054. */
  1055. extern void bcm2835_gpio_clr_len(uint8_t pin);
  1056. /*! Enable Asynchronous Rising Edge Detect Enable for the specified pin.
  1057. When a rising edge is detected, sets the appropriate pin in Event Detect Status.
  1058. Asynchronous means the incoming signal is not sampled by the system clock. As such
  1059. rising edges of very short duration can be detected.
  1060. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1061. */
  1062. extern void bcm2835_gpio_aren(uint8_t pin);
  1063. /*! Disable Asynchronous Rising Edge Detect Enable for the specified pin.
  1064. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1065. */
  1066. extern void bcm2835_gpio_clr_aren(uint8_t pin);
  1067. /*! Enable Asynchronous Falling Edge Detect Enable for the specified pin.
  1068. When a falling edge is detected, sets the appropriate pin in Event Detect Status.
  1069. Asynchronous means the incoming signal is not sampled by the system clock. As such
  1070. falling edges of very short duration can be detected.
  1071. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1072. */
  1073. extern void bcm2835_gpio_afen(uint8_t pin);
  1074. /*! Disable Asynchronous Falling Edge Detect Enable for the specified pin.
  1075. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1076. */
  1077. extern void bcm2835_gpio_clr_afen(uint8_t pin);
  1078. /*! Sets the Pull-up/down register for the given pin. This is
  1079. used with bcm2835_gpio_pudclk() to set the Pull-up/down resistor for the given pin.
  1080. However, it is usually more convenient to use bcm2835_gpio_set_pud().
  1081. \param[in] pud The desired Pull-up/down mode. One of BCM2835_GPIO_PUD_* from bcm2835PUDControl
  1082. \sa bcm2835_gpio_set_pud()
  1083. */
  1084. extern void bcm2835_gpio_pud(uint8_t pud);
  1085. /*! Clocks the Pull-up/down value set earlier by bcm2835_gpio_pud() into the pin.
  1086. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1087. \param[in] on HIGH to clock the value from bcm2835_gpio_pud() into the pin.
  1088. LOW to remove the clock.
  1089. \sa bcm2835_gpio_set_pud()
  1090. */
  1091. extern void bcm2835_gpio_pudclk(uint8_t pin, uint8_t on);
  1092. /*! Reads and returns the Pad Control for the given GPIO group.
  1093. \param[in] group The GPIO pad group number, one of BCM2835_PAD_GROUP_GPIO_*
  1094. \return Mask of bits from BCM2835_PAD_* from \ref bcm2835PadGroup
  1095. */
  1096. extern uint32_t bcm2835_gpio_pad(uint8_t group);
  1097. /*! Sets the Pad Control for the given GPIO group.
  1098. \param[in] group The GPIO pad group number, one of BCM2835_PAD_GROUP_GPIO_*
  1099. \param[in] control Mask of bits from BCM2835_PAD_* from \ref bcm2835PadGroup. Note
  1100. that it is not necessary to include BCM2835_PAD_PASSWRD in the mask as this
  1101. is automatically included.
  1102. */
  1103. extern void bcm2835_gpio_set_pad(uint8_t group, uint32_t control);
  1104. /*! Delays for the specified number of milliseconds.
  1105. Uses nanosleep(), and therefore does not use CPU until the time is up.
  1106. However, you are at the mercy of nanosleep(). From the manual for nanosleep():
  1107. If the interval specified in req is not an exact multiple of the granularity
  1108. underlying clock (see time(7)), then the interval will be
  1109. rounded up to the next multiple. Furthermore, after the sleep completes,
  1110. there may still be a delay before the CPU becomes free to once
  1111. again execute the calling thread.
  1112. \param[in] millis Delay in milliseconds
  1113. */
  1114. extern void bcm2835_delay (unsigned int millis);
  1115. /*! Delays for the specified number of microseconds.
  1116. Uses a combination of nanosleep() and a busy wait loop on the BCM2835 system timers,
  1117. However, you are at the mercy of nanosleep(). From the manual for nanosleep():
  1118. If the interval specified in req is not an exact multiple of the granularity
  1119. underlying clock (see time(7)), then the interval will be
  1120. rounded up to the next multiple. Furthermore, after the sleep completes,
  1121. there may still be a delay before the CPU becomes free to once
  1122. again execute the calling thread.
  1123. For times less than about 450 microseconds, uses a busy wait on the System Timer.
  1124. It is reported that a delay of 0 microseconds on RaspberryPi will in fact
  1125. result in a delay of about 80 microseconds. Your mileage may vary.
  1126. \param[in] micros Delay in microseconds
  1127. */
  1128. extern void bcm2835_delayMicroseconds (uint64_t micros);
  1129. /*! Sets the output state of the specified pin
  1130. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1131. \param[in] on HIGH sets the output to HIGH and LOW to LOW.
  1132. */
  1133. extern void bcm2835_gpio_write(uint8_t pin, uint8_t on);
  1134. /*! Sets any of the first 32 GPIO output pins specified in the mask to the state given by on
  1135. \param[in] mask Mask of pins to affect. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  1136. \param[in] on HIGH sets the output to HIGH and LOW to LOW.
  1137. */
  1138. extern void bcm2835_gpio_write_multi(uint32_t mask, uint8_t on);
  1139. /*! Sets the first 32 GPIO output pins specified in the mask to the value given by value
  1140. \param[in] value values required for each bit masked in by mask, eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  1141. \param[in] mask Mask of pins to affect. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
  1142. */
  1143. extern void bcm2835_gpio_write_mask(uint32_t value, uint32_t mask);
  1144. /*! Sets the Pull-up/down mode for the specified pin. This is more convenient than
  1145. clocking the mode in with bcm2835_gpio_pud() and bcm2835_gpio_pudclk().
  1146. \param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
  1147. \param[in] pud The desired Pull-up/down mode. One of BCM2835_GPIO_PUD_* from bcm2835PUDControl
  1148. */
  1149. extern void bcm2835_gpio_set_pud(uint8_t pin, uint8_t pud);
  1150. /*! @} */
  1151. /*! \defgroup BCM2835spigrp SPI access
  1152. \ingroup BCM2835grp
  1153. These functions let you use SPI0 (Serial Peripheral Interface) to
  1154. interface with an external SPI device.
  1155. @{
  1156. */
  1157. /*! Start SPI operations.
  1158. Forces RPi SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1)
  1159. to alternate function ALT0, which enables those pins for SPI interface.
  1160. You should call bcm2835_spi_end() when all SPI funcitons are complete to return the pins to
  1161. their default functions.
  1162. \sa bcm2835_spi_end()
  1163. \return 1 if successful, 0 otherwise (perhaps because you are not running as root)
  1164. */
  1165. extern int bcm2835_spi_begin(void);
  1166. /*! End SPI operations.
  1167. SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1)
  1168. are returned to their default INPUT behaviour.
  1169. */
  1170. extern void bcm2835_spi_end(void);
  1171. /*! Sets the SPI bit order
  1172. NOTE: has no effect. Not supported by SPI0.
  1173. Defaults to
  1174. \param[in] order The desired bit order, one of BCM2835_SPI_BIT_ORDER_*,
  1175. see \ref bcm2835SPIBitOrder
  1176. */
  1177. extern void bcm2835_spi_setBitOrder(uint8_t order);
  1178. /*! Sets the SPI clock divider and therefore the
  1179. SPI clock speed.
  1180. \param[in] divider The desired SPI clock divider, one of BCM2835_SPI_CLOCK_DIVIDER_*,
  1181. see \ref bcm2835SPIClockDivider
  1182. */
  1183. extern void bcm2835_spi_setClockDivider(uint16_t divider);
  1184. /*! Sets the SPI data mode
  1185. Sets the clock polariy and phase
  1186. \param[in] mode The desired data mode, one of BCM2835_SPI_MODE*,
  1187. see \ref bcm2835SPIMode
  1188. */
  1189. extern void bcm2835_spi_setDataMode(uint8_t mode);
  1190. /*! Sets the chip select pin(s)
  1191. When an bcm2835_spi_transfer() is made, the selected pin(s) will be asserted during the
  1192. transfer.
  1193. \param[in] cs Specifies the CS pins(s) that are used to activate the desired slave.
  1194. One of BCM2835_SPI_CS*, see \ref bcm2835SPIChipSelect
  1195. */
  1196. extern void bcm2835_spi_chipSelect(uint8_t cs);
  1197. /*! Sets the chip select pin polarity for a given pin
  1198. When an bcm2835_spi_transfer() occurs, the currently selected chip select pin(s)
  1199. will be asserted to the
  1200. value given by active. When transfers are not happening, the chip select pin(s)
  1201. return to the complement (inactive) value.
  1202. \param[in] cs The chip select pin to affect
  1203. \param[in] active Whether the chip select pin is to be active HIGH
  1204. */
  1205. extern void bcm2835_spi_setChipSelectPolarity(uint8_t cs, uint8_t active);
  1206. /*! Transfers one byte to and from the currently selected SPI slave.
  1207. Asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect)
  1208. during the transfer.
  1209. Clocks the 8 bit value out on MOSI, and simultaneously clocks in data from MISO.
  1210. Returns the read data byte from the slave.
  1211. Uses polled transfer as per section 10.6.1 of the BCM 2835 ARM Peripherls manual
  1212. \param[in] value The 8 bit data byte to write to MOSI
  1213. \return The 8 bit byte simultaneously read from MISO
  1214. \sa bcm2835_spi_transfern()
  1215. */
  1216. extern uint8_t bcm2835_spi_transfer(uint8_t value);
  1217. /*! Transfers any number of bytes to and from the currently selected SPI slave.
  1218. Asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect)
  1219. during the transfer.
  1220. Clocks the len 8 bit bytes out on MOSI, and simultaneously clocks in data from MISO.
  1221. The data read read from the slave is placed into rbuf. rbuf must be at least len bytes long
  1222. Uses polled transfer as per section 10.6.1 of the BCM 2835 ARM Peripherls manual
  1223. \param[in] tbuf Buffer of bytes to send.
  1224. \param[out] rbuf Received bytes will by put in this buffer
  1225. \param[in] len Number of bytes in the tbuf buffer, and the number of bytes to send/received
  1226. \sa bcm2835_spi_transfer()
  1227. */
  1228. extern void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len);
  1229. /*! Transfers any number of bytes to and from the currently selected SPI slave
  1230. using bcm2835_spi_transfernb.
  1231. The returned data from the slave replaces the transmitted data in the buffer.
  1232. \param[in,out] buf Buffer of bytes to send. Received bytes will replace the contents
  1233. \param[in] len Number of bytes int eh buffer, and the number of bytes to send/received
  1234. \sa bcm2835_spi_transfer()
  1235. */
  1236. extern void bcm2835_spi_transfern(char* buf, uint32_t len);
  1237. /*! Transfers any number of bytes to the currently selected SPI slave.
  1238. Asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect)
  1239. during the transfer.
  1240. \param[in] buf Buffer of bytes to send.
  1241. \param[in] len Number of bytes in the tbuf buffer, and the number of bytes to send
  1242. */
  1243. extern void bcm2835_spi_writenb(char* buf, uint32_t len);
  1244. /*! @} */
  1245. /*! \defgroup BCM2835i2cgrp I2C access
  1246. \ingroup BCM2835grp
  1247. These functions let you use I2C (The Broadcom Serial Control bus with the Philips
  1248. I2C bus/interface version 2.1 January 2000.) to interface with an external I2C device.
  1249. @{
  1250. */
  1251. /*! Start I2C operations.
  1252. Forces RPi I2C pins P1-03 (SDA) and P1-05 (SCL)
  1253. to alternate function ALT0, which enables those pins for I2C interface.
  1254. You should call bcm2835_i2c_end() when all I2C functions are complete to return the pins to
  1255. their default functions
  1256. \return 1 if successful, 0 otherwise (perhaps because you are not running as root)
  1257. \sa bcm2835_i2c_end()
  1258. */
  1259. extern int bcm2835_i2c_begin(void);
  1260. /*! End I2C operations.
  1261. I2C pins P1-03 (SDA) and P1-05 (SCL)
  1262. are returned to their default INPUT behaviour.
  1263. */
  1264. extern void bcm2835_i2c_end(void);
  1265. /*! Sets the I2C slave address.
  1266. \param[in] addr The I2C slave address.
  1267. */
  1268. extern void bcm2835_i2c_setSlaveAddress(uint8_t addr);
  1269. /*! Sets the I2C clock divider and therefore the I2C clock speed.
  1270. \param[in] divider The desired I2C clock divider, one of BCM2835_I2C_CLOCK_DIVIDER_*,
  1271. see \ref bcm2835I2CClockDivider
  1272. */
  1273. extern void bcm2835_i2c_setClockDivider(uint16_t divider);
  1274. /*! Sets the I2C clock divider by converting the baudrate parameter to
  1275. the equivalent I2C clock divider. ( see \sa bcm2835_i2c_setClockDivider)
  1276. For the I2C standard 100khz you would set baudrate to 100000
  1277. The use of baudrate corresponds to its use in the I2C kernel device
  1278. driver. (Of course, bcm2835 has nothing to do with the kernel driver)
  1279. */
  1280. extern void bcm2835_i2c_set_baudrate(uint32_t baudrate);
  1281. /*! Transfers any number of bytes to the currently selected I2C slave.
  1282. (as previously set by \sa bcm2835_i2c_setSlaveAddress)
  1283. \param[in] buf Buffer of bytes to send.
  1284. \param[in] len Number of bytes in the buf buffer, and the number of bytes to send.
  1285. \return reason see \ref bcm2835I2CReasonCodes
  1286. */
  1287. extern uint8_t bcm2835_i2c_write(const char * buf, uint32_t len);
  1288. /*! Transfers any number of bytes from the currently selected I2C slave.
  1289. (as previously set by \sa bcm2835_i2c_setSlaveAddress)
  1290. \param[in] buf Buffer of bytes to receive.
  1291. \param[in] len Number of bytes in the buf buffer, and the number of bytes to received.
  1292. \return reason see \ref bcm2835I2CReasonCodes
  1293. */
  1294. extern uint8_t bcm2835_i2c_read(char* buf, uint32_t len);
  1295. /*! Allows reading from I2C slaves that require a repeated start (without any prior stop)
  1296. to read after the required slave register has been set. For example, the popular
  1297. MPL3115A2 pressure and temperature sensor. Note that your device must support or
  1298. require this mode. If your device does not require this mode then the standard
  1299. combined:
  1300. \sa bcm2835_i2c_write
  1301. \sa bcm2835_i2c_read
  1302. are a better choice.
  1303. Will read from the slave previously set by \sa bcm2835_i2c_setSlaveAddress
  1304. \param[in] regaddr Buffer containing the slave register you wish to read from.
  1305. \param[in] buf Buffer of bytes to receive.
  1306. \param[in] len Number of bytes in the buf buffer, and the number of bytes to received.
  1307. \return reason see \ref bcm2835I2CReasonCodes
  1308. */
  1309. extern uint8_t bcm2835_i2c_read_register_rs(char* regaddr, char* buf, uint32_t len);
  1310. /*! Allows sending an arbitrary number of bytes to I2C slaves before issuing a repeated
  1311. start (with no prior stop) and reading a response.
  1312. Necessary for devices that require such behavior, such as the MLX90620.
  1313. Will write to and read from the slave previously set by \sa bcm2835_i2c_setSlaveAddress
  1314. \param[in] cmds Buffer containing the bytes to send before the repeated start condition.
  1315. \param[in] cmds_len Number of bytes to send from cmds buffer
  1316. \param[in] buf Buffer of bytes to receive.
  1317. \param[in] buf_len Number of bytes to receive in the buf buffer.
  1318. \return reason see \ref bcm2835I2CReasonCodes
  1319. */
  1320. extern uint8_t bcm2835_i2c_write_read_rs(char* cmds, uint32_t cmds_len, char* buf,
  1321. uint32_t buf_len);
  1322. /*! @} */
  1323. /*! \defgroup BCM2835stgrp System Timer access
  1324. \ingroup BCM2835grp
  1325. Allows access to and delays using the System Timer Counter.
  1326. @{
  1327. */
  1328. /*! Read the System Timer Counter register.
  1329. \return the value read from the System Timer Counter Lower 32 bits register
  1330. */
  1331. extern uint64_t bcm2835_st_read(void);
  1332. /*! Delays for the specified number of microseconds with offset.
  1333. \param[in] offset_micros Offset in microseconds
  1334. \param[in] micros Delay in microseconds
  1335. */
  1336. extern void bcm2835_st_delay(uint64_t offset_micros, uint64_t micros);
  1337. /*! @} */
  1338. /*! \defgroup BCM2835pwmgrp Pulse Width Modulation
  1339. \ingroup BCM2835grp
  1340. Allows control of 2 independent PWM channels. A limited subset of GPIO pins
  1341. can be connected to one of these 2 channels, allowing PWM control of GPIO pins.
  1342. You have to set the desired pin into a particular Alt Fun to PWM output. See the PWM
  1343. documentation on the Main Page.
  1344. @{
  1345. */
  1346. /*! Sets the PWM clock divisor,
  1347. to control the basic PWM pulse widths.
  1348. \param[in] divisor Divides the basic 19.2MHz PWM clock. You can use one of the common
  1349. values BCM2835_PWM_CLOCK_DIVIDER_* in \ref bcm2835PWMClockDivider
  1350. */
  1351. extern void bcm2835_pwm_set_clock(uint32_t divisor);
  1352. /*! Sets the mode of the given PWM channel,
  1353. allowing you to control the PWM mode and enable/disable that channel
  1354. \param[in] channel The PWM channel. 0 or 1.
  1355. \param[in] markspace Set true if you want Mark-Space mode. 0 for Balanced mode.
  1356. \param[in] enabled Set true to enable this channel and produce PWM pulses.
  1357. */
  1358. extern void bcm2835_pwm_set_mode(uint8_t channel, uint8_t markspace, uint8_t enabled);
  1359. /*! Sets the maximum range of the PWM output.
  1360. The data value can vary between 0 and this range to control PWM output
  1361. \param[in] channel The PWM channel. 0 or 1.
  1362. \param[in] range The maximum value permitted for DATA.
  1363. */
  1364. extern void bcm2835_pwm_set_range(uint8_t channel, uint32_t range);
  1365. /*! Sets the PWM pulse ratio to emit to DATA/RANGE,
  1366. where RANGE is set by bcm2835_pwm_set_range().
  1367. \param[in] channel The PWM channel. 0 or 1.
  1368. \param[in] data Controls the PWM output ratio as a fraction of the range.
  1369. Can vary from 0 to RANGE.
  1370. */
  1371. extern void bcm2835_pwm_set_data(uint8_t channel, uint32_t data);
  1372. /*! @} */
  1373. #ifdef __cplusplus
  1374. }
  1375. #endif
  1376. #endif /* BCM2835_H */