HiPi-BCM2835
view release on metacpan or search on metacpan
BCM2835/src/src/bcm2835.h view on Meta::CPAN
Gary Marks has created a Serial Peripheral Interface (SPI) command-line utility
for Raspberry Pi, based on the bcm2835 library. The
utility, spincl, is licensed under Open Source GNU GPLv3 by iP Solutions (http://ipsolutionscorp.com), as a
free download with source included: http://ipsolutionscorp.com/raspberry-pi-spi-utility/
Bindings for Ada are available courtesy Tama McGlinn at https://github.com/TamaMcGlinn/ada_raspio
\par Open Source Licensing GPL V3
This is the appropriate option if you want to share the source code of your
application with everyone you distribute it to, and you also want to give them
the right to share who uses it. If you wish to use this software under Open
Source Licensing, you must contribute all your source code to the open source
community in accordance with the GPL Version 3 when your application is
distributed. See https://www.gnu.org/licenses/gpl-3.0.html and COPYING
\par Commercial Licensing
This is the appropriate option if you are creating proprietary applications
and you are not prepared to distribute and share the source code of your
application. To purchase a commercial license, contact info@airspayce.com
\par Acknowledgements
Some of this code has been inspired by Dom and Gert.
The I2C code has been inspired by Alan Barr.
\par Revision History
\version 1.0 Initial release
\version 1.1 Minor bug fixes
\version 1.2 Added support for SPI
\version 1.3 Added bcm2835_spi_transfern()
\version 1.4 Fixed a problem that prevented SPI CE1 being used. Reported by David Robinson.
\version 1.5 Added bcm2835_close() to deinit the library. Suggested by C?sar Ortiz
\version 1.6 Document testing on 2012-07-15-wheezy-raspbian and Occidentalisv01
Functions bcm2835_gpio_ren(), bcm2835_gpio_fen(), bcm2835_gpio_hen()
bcm2835_gpio_len(), bcm2835_gpio_aren() and bcm2835_gpio_afen() now
changes only the pin specified. Other pins that were already previously
enabled stay enabled.
Added bcm2835_gpio_clr_ren(), bcm2835_gpio_clr_fen(), bcm2835_gpio_clr_hen()
bcm2835_gpio_clr_len(), bcm2835_gpio_clr_aren(), bcm2835_gpio_clr_afen()
to clear the enable for individual pins, suggested by Andreas Sundstrom.
\version 1.7 Added bcm2835_spi_transfernb to support different buffers for read and write.
\version 1.8 Improvements to read barrier, as suggested by maddin.
\version 1.9 Improvements contributed by mikew:
I noticed that it was mallocing memory for the mmaps on /dev/mem.
It's not necessary to do that, you can just mmap the file directly,
so I've removed the mallocs (and frees).
I've also modified delayMicroseconds() to use nanosleep() for long waits,
and a busy wait on a high resolution timer for the rest. This is because
I've found that calling nanosleep() takes at least 100-200 us.
You need to link using '-lrt' using this version.
I've added some unsigned casts to the debug prints to silence compiler
warnings I was getting, fixed some typos, and changed the value of
BCM2835_PAD_HYSTERESIS_ENABLED to 0x08 as per Gert van Loo's doc at
http://www.scribd.com/doc/101830961/GPIO-Pads-Control2
Also added a define for the passwrd value that Gert says is needed to
change pad control settings.
\version 1.10 Changed the names of the delay functions to bcm2835_delay()
and bcm2835_delayMicroseconds() to prevent collisions with wiringPi.
Macros to map delay()-> bcm2835_delay() and
Macros to map delayMicroseconds()-> bcm2835_delayMicroseconds(), which
can be disabled by defining BCM2835_NO_DELAY_COMPATIBILITY
\version 1.11 Fixed incorrect link to download file
\version 1.12 New GPIO pin definitions for RPi version 2 (which has a different GPIO mapping)
\version 1.13 New GPIO pin definitions for RPi version 2 plug P5
Hardware base pointers are now available (after initialisation) externally as bcm2835_gpio
bcm2835_pwm bcm2835_clk bcm2835_pads bcm2835_spi0.
\version 1.14 Now compiles even if CLOCK_MONOTONIC_RAW is not available, uses CLOCK_MONOTONIC instead.
Fixed errors in documentation of SPI divider frequencies based on 250MHz clock.
Reported by Ben Simpson.
\version 1.15 Added bcm2835_close() to end of examples as suggested by Mark Wolfe.
\version 1.16 Added bcm2835_gpio_set_multi, bcm2835_gpio_clr_multi and bcm2835_gpio_write_multi
to allow a mask of pins to be set all at once. Requested by Sebastian Loncar.
\version 1.17 Added bcm2835_gpio_write_mask. Requested by Sebastian Loncar.
\version 1.18 Added bcm2835_i2c_* functions. Changes to bcm2835_delayMicroseconds:
now uses the RPi system timer counter, instead of clock_gettime, for improved accuracy.
No need to link with -lrt now. Contributed by Arjan van Vught.
\version 1.19 Removed inlines added by previous patch since they don't seem to work everywhere.
Reported by olly.
\version 1.20 Patch from Mark Dootson to close /dev/mem after access to the peripherals has been granted.
\version 1.21 delayMicroseconds is now not susceptible to 32 bit timer overruns.
Patch courtesy Jeremy Mortis.
\version 1.22 Fixed incorrect definition of BCM2835_GPFEN0 which broke the ability to set
falling edge events. Reported by Mark Dootson.
\version 1.23 Added bcm2835_i2c_set_baudrate and bcm2835_i2c_read_register_rs.
Improvements to bcm2835_i2c_read and bcm2835_i2c_write functions
to fix ocasional reads not completing. Patched by Mark Dootson.
\version 1.24 Mark Dootson p[atched a problem with his previously submitted code
under high load from other processes.
\version 1.25 Updated author and distribution location details to airspayce.com
\version 1.26 Added missing unmapmem for pads in bcm2835_close to prevent a memory leak.
Reported by Hartmut Henkel.
BCM2835/src/src/bcm2835.h view on Meta::CPAN
\param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
*/
extern void bcm2835_gpio_afen(uint8_t pin);
/*! Disable Asynchronous Falling Edge Detect Enable for the specified pin.
\param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
*/
extern void bcm2835_gpio_clr_afen(uint8_t pin);
/*! Sets the Pull-up/down register for the given pin. This is
used with bcm2835_gpio_pudclk() to set the Pull-up/down resistor for the given pin.
However, it is usually more convenient to use bcm2835_gpio_set_pud().
\param[in] pud The desired Pull-up/down mode. One of BCM2835_GPIO_PUD_* from bcm2835PUDControl
On the RPI 4, although this function and bcm2835_gpio_pudclk() are supported for backward
compatibility, new code should always use bcm2835_gpio_set_pud().
\sa bcm2835_gpio_set_pud()
*/
extern void bcm2835_gpio_pud(uint8_t pud);
/*! Clocks the Pull-up/down value set earlier by bcm2835_gpio_pud() into the pin.
\param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
\param[in] on HIGH to clock the value from bcm2835_gpio_pud() into the pin.
LOW to remove the clock.
On the RPI 4, although this function and bcm2835_gpio_pud() are supported for backward
compatibility, new code should always use bcm2835_gpio_set_pud().
\sa bcm2835_gpio_set_pud()
*/
extern void bcm2835_gpio_pudclk(uint8_t pin, uint8_t on);
/*! Reads and returns the Pad Control for the given GPIO group.
Caution: requires root access.
\param[in] group The GPIO pad group number, one of BCM2835_PAD_GROUP_GPIO_*
\return Mask of bits from BCM2835_PAD_* from \ref bcm2835PadGroup
*/
extern uint32_t bcm2835_gpio_pad(uint8_t group);
/*! Sets the Pad Control for the given GPIO group.
Caution: requires root access.
\param[in] group The GPIO pad group number, one of BCM2835_PAD_GROUP_GPIO_*
\param[in] control Mask of bits from BCM2835_PAD_* from \ref bcm2835PadGroup. Note
that it is not necessary to include BCM2835_PAD_PASSWRD in the mask as this
is automatically included.
*/
extern void bcm2835_gpio_set_pad(uint8_t group, uint32_t control);
/*! Delays for the specified number of milliseconds.
Uses nanosleep(), and therefore does not use CPU until the time is up.
However, you are at the mercy of nanosleep(). From the manual for nanosleep():
If the interval specified in req is not an exact multiple of the granularity
underlying clock (see time(7)), then the interval will be
rounded up to the next multiple. Furthermore, after the sleep completes,
there may still be a delay before the CPU becomes free to once
again execute the calling thread.
\param[in] millis Delay in milliseconds
*/
extern void bcm2835_delay (unsigned int millis);
/*! Delays for the specified number of microseconds.
Uses a combination of nanosleep() and a busy wait loop on the BCM2835 system timers,
However, you are at the mercy of nanosleep(). From the manual for nanosleep():
If the interval specified in req is not an exact multiple of the granularity
underlying clock (see time(7)), then the interval will be
rounded up to the next multiple. Furthermore, after the sleep completes,
there may still be a delay before the CPU becomes free to once
again execute the calling thread.
For times less than about 450 microseconds, uses a busy wait on the System Timer.
It is reported that a delay of 0 microseconds on RaspberryPi will in fact
result in a delay of about 80 microseconds. Your mileage may vary.
\param[in] micros Delay in microseconds
*/
extern void bcm2835_delayMicroseconds (uint64_t micros);
/*! Sets the output state of the specified pin
\param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
\param[in] on HIGH sets the output to HIGH and LOW to LOW.
*/
extern void bcm2835_gpio_write(uint8_t pin, uint8_t on);
/*! Sets any of the first 32 GPIO output pins specified in the mask to the state given by on
\param[in] mask Mask of pins to affect. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
\param[in] on HIGH sets the output to HIGH and LOW to LOW.
*/
extern void bcm2835_gpio_write_multi(uint32_t mask, uint8_t on);
/*! Sets the first 32 GPIO output pins specified in the mask to the value given by value
\param[in] value values required for each bit masked in by mask, eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
\param[in] mask Mask of pins to affect. Use eg: (1 << RPI_GPIO_P1_03) | (1 << RPI_GPIO_P1_05)
*/
extern void bcm2835_gpio_write_mask(uint32_t value, uint32_t mask);
/*! Sets the Pull-up/down mode for the specified pin. This is more convenient than
clocking the mode in with bcm2835_gpio_pud() and bcm2835_gpio_pudclk().
\param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
\param[in] pud The desired Pull-up/down mode. One of BCM2835_GPIO_PUD_* from bcm2835PUDControl
*/
extern void bcm2835_gpio_set_pud(uint8_t pin, uint8_t pud);
/*! On the BCM2711 based RPI 4, gets the current Pull-up/down mode for the specified pin.
Returns one of BCM2835_GPIO_PUD_* from bcm2835PUDControl.
On earlier RPI versions not based on the BCM2711, returns BCM2835_GPIO_PUD_ERROR
\param[in] pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.
*/
extern uint8_t bcm2835_gpio_get_pud(uint8_t pin);
/*! @} */
/*! \defgroup spi SPI access
These functions let you use SPI0 (Serial Peripheral Interface) to
interface with an external SPI device.
@{
*/
/*! Start SPI operations.
Forces RPi SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1)
to alternate function ALT0, which enables those pins for SPI interface.
You should call bcm2835_spi_end() when all SPI funcitons are complete to return the pins to
their default functions.
\sa bcm2835_spi_end()
\return 1 if successful, 0 otherwise (perhaps because you are not running as root)
*/
extern int bcm2835_spi_begin(void);
/*! End SPI operations.
SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1)
are returned to their default INPUT behaviour.
( run in 1.138 second using v1.01-cache-2.11-cpan-39bf76dae61 )