Various projects using Raspberry Pi

bcm2835.c 36KB

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