Device::BCM2835

Perl extension for accesing GPIO pins on a Raspberry Pi via the BCM 2835 GPIO
Download

Device::BCM2835 Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Mike McCauley
  • Publisher web site:
  • http://search.cpan.org/~mikem/

Device::BCM2835 Tags


Device::BCM2835 Description

Device::BCM2835 is a Perl module that provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip as used on Raspberry Pi (RPi) http://www.raspberrypi.orgAllows access to the GPIO pins on the 26 pin IDE plug on the RPi board so you can control and interface with various external devices. It provides functions for reading digital inputs and setting digital outputs. Pin event detection is supported by polling (interrupts not supported).Building this module requires the bcm2835 library to be installed. You can get the latest version from http://www.open.com.au/mikem/bcm2835/my $ret = Device::BCM2835::init(); Initialise the library by opening /dev/mem and getting pointers to the internal memory for BCM 2835 device registers. You must call this (successfully) before calling any other functions in this library (except Device::BCM2835::set_debug). If Device::BCM2835::init() fails by returning 0, calling any other function may result in crashes or other failures. Prints messages to stderr in case of errors. return 1 if successful else 0Device::BCM2835::set_debug($debug); Sets the debug level of the library. A value of 1 prevents mapping to /dev/mem, and makes the library print out what it would do, rather than accessing the GPIO registers. A value of 0, the default, causes nomal operation. Call this before calling Device::BCM2835::init(); debug The new debug level. 1 means debugmy $value = Device::BCM2835::peri_read($paddr); Reads 32 bit value from a peripheral address The read is done twice, and is therefore always safe in terms of manual section 1.3 Peripheral access precautions for correct memory ordering paddr Physical address to read from. See BCM2835_GPIO_BASE etc. return the value read from the 32 bit registerDevice::BCM2835::peri_write($paddr, $value); Writes 32 bit value from a peripheral address The write is done twice, and is therefore always safe in terms of manual section 1.3 Peripheral access precautions for correct memory ordering paddr Physical address to read from. See BCM2835_GPIO_BASE etc. value The 32 bit value to writeDevice::BCM2835::peri_set_bits($paddr, $value, $mask); Alters a number of bits in a 32 peripheral regsiter. It reads the current valu and then alters the bits deines as 1 in mask, according to the bit value in value. All other bits that are 0 in the mask are unaffected. Use this to alter a subset of the bits in a register. The write is done twice, and is therefore always safe in terms of manual section 1.3 Peripheral access precautions for correct memory ordering paddr Physical address to read from. See BCM2835_GPIO_BASE etc. value The 32 bit value to write, masked in by mask. mask Bitmask that defines the bits that will be altered in the register.Device::BCM2835::gpio_fsel($pin, $mode); Sets the Function Select register for the given pin, which configures the pin as Input, Output or one of the 6 alternate functions. pin GPIO number, or one of RPI_GPIO_P1_* from RPiGPIOPin. mode Mode to set the pin to, one of BCM2835_GPIO_FSEL_* from \ref bcm2835FunctionSelectDevice::BCM2835::gpio_set($pin); Sets the specified pin output to HIGH. See Also Device::BCM2835::gpio_write() pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_clr($pin); Sets the specified pin output to LOW. See Also Device::BCM2835::gpio_write() pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.my $value = Device::BCM2835::gpio_lev($pin); Reads the current level on the specified pin and returns either HIGH or LOW. Works whether or not the pin is an input or an output. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin. return the current level either HIGH or LOWmy $value = Device::BCM2835::gpio_eds($pin); Event Detect Status. Tests whether the specified pin has detected a level or edge as requested by Device::BCM2835::gpio_ren(), Device::BCM2835::gpio_fen(), Device::BCM2835::gpio_hen(), Device::BCM2835::gpio_len(), Device::BCM2835::gpio_aren(), Device::BCM2835::gpio_afen(). Clear the flag for a given pin by calling Device::BCM2835::gpio_set_eds(pin); pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin. return HIGH if the event detect status for th given pin is true.Device::BCM2835::gpio_set_eds($pin); Sets the Event Detect Status register for a given pin to 1, which has the effect of clearing the flag. Use this afer seeing an Event Detect Status on the pin. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_ren($pin); Enable Rising Edge Detect Enable for the specified pin. When a rising edge is detected, sets the appropriate pin in Event Detect Status. The GPRENn registers use synchronous edge detection. This means the input signal is sampled using the system clock and then it is looking for a “011” pattern on the sampled signal. This has the effect of suppressing glitches. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_fen($pin); Enable Falling Edge Detect Enable for the specified pin. When a falling edge is detected, sets the appropriate pin in Event Detect Status. The GPRENn registers use synchronous edge detection. This means the input signal is sampled using the system clock and then it is looking for a “100” pattern on the sampled signal. This has the effect of suppressing glitches. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_hen($in); Enable High Detect Enable for the specified pin. When a HIGH level is detected on the pin, sets the appropriate pin in Event Detect Status. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_len($pin); Enable Low Detect Enable for the specified pin. When a LOW level is detected on the pin, sets the appropriate pin in Event Detect Status. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_aren($pin); Enable Asynchronous Rising Edge Detect Enable for the specified pin. When a rising edge is detected, sets the appropriate pin in Event Detect Status. Asynchronous means the incoming signal is not sampled by the system clock. As such rising edges of very short duration can be detected. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_afen($pin); Enable Asynchronous Falling Edge Detect Enable for the specified pin. When a falling edge is detected, sets the appropriate pin in Event Detect Status. Asynchronous means the incoming signal is not sampled by the system clock. As such falling edges of very short duration can be detected. pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin.Device::BCM2835::gpio_pud($pud); Sets the Pull-up/down register for the given pin. This is used with Device::BCM2835::gpio_pudclk() to set the Pull-up/down resistor for the given pin. However, it is usually more convenient to use Device::BCM2835::gpio_set_pud(). See Also: Device::BCM2835::gpio_set_pud() pud The desired Pull-up/down mode. One of BCM2835_GPIO_PUD_* from bcm2835PUDControlDevice::BCM2835::gpio_pudclk($pin, $on); Clocks the Pull-up/down value set earlier by Device::BCM2835::gpio_pud() into the pin. See Also: Device::BCM2835::gpio_set_pud() pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin. on HIGH to clock the value from Device::BCM2835::gpio_pud() into the pin. LOW to remove the clock.uint32_t Device::BCM2835::gpio_pad($group); Reads and returns the Pad Control for the given GPIO group. group The GPIO pad group number, one of BCM2835_PAD_GROUP_GPIO_* return Mask of bits from BCM2835_PAD_* from \ref bcm2835PadGroupDevice::BCM2835::gpio_set_pad($group, $control); Sets the Pad Control for the given GPIO group. group The GPIO pad group number, one of BCM2835_PAD_GROUP_GPIO_* control Mask of bits from BCM2835_PAD_* from \ref bcm2835PadGroupdelay ($millis); Delays for the specified number of milliseconds. Uses nanosleep(), and therefore does not use CPU until the time is up. millis Delay in millisecondsdelayMicroseconds ($micros); Delays for the specified number of microseconds. Uses nanosleep(), and therefore does not use CPU until the time is up. micros Delay in microsecondsDevice::BCM2835::gpio_write($pin, $on); Sets the output state of the specified pin pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin. on HIGH sets the output to HIGH and LOW to LOW.Device::BCM2835::gpio_set_pud($pin, $pud); Sets the Pull-up/down mode for the specified pin. This is more convenient than clocking the mode in with Device::BCM2835::gpio_pud() and Device::BCM2835::gpio_pudclk(). pin GPIO number, or one of RPI_GPIO_P1_* from \ref RPiGPIOPin. pud The desired Pull-up/down mode. One of BCM2835_GPIO_PUD_* from bcm2835PUDControlDevice::BCM2835::spi_begin(); 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 spi_end() when all SPI funcitons are complete to return the pins to their default functionsDevice::BCM2835::end(); 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.Device::BCM2835::setBitOrder($order); Sets the SPI bit order NOTE: has no effect. Not supported by SPI0. Defaults to order The desired bit order, one of BCM2835_SPI_BIT_ORDER_*, see \ref bcm2835SPIBitOrderDevice::BCM2835::setClockDivider($divider); Sets the SPI clock divider and therefore the SPI clock speed. divider The desired SPI clock divider, one of BCM2835_SPI_CLOCK_DIVIDER_*, see \ref bcm2835SPIClockDividerDevice::BCM2835::setDataMode($mode); Sets the SPI data mode Sets the clock polariy and phase mode The desired data mode, one of BCM2835_SPI_MODE*, see \ref bcm2835SPIModeDevice::BCM2835::chipSelect($cs); Sets the chip select pin(s) When an spi_transfer() is made, the selected pin(s) will be asserted during the transfer. cs Specifies the CS pins(s) that are used to activate the desired slave. One of BCM2835_SPI_CS*, see \ref bcm2835SPIChipSelectDevice::BCM2835::setChipSelectPolarity($cs, $active); Sets the chip select pin polarity for a given pin When an spi_transfer() occurs, the currently selected chip select pin(s) will be asserted to the value given by active. When transfers are not happening, the chip select pin(s) return to the complement (inactive) value. cs The chip select pin to affect active Whether the chip select pin is to be active HIGHmy $data = spi_transfer($value); Transfers one byte to and from the currently selected SPI slave. Asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect) during the transfer. Clocks the 8 bit value out on MOSI, and simultaneously clocks in data from MISO. Returns the read data byte from the slave. polled transfer as per section 10.6.1 of teh BCM 2835 ARM Peripherls manual value The 8 bit data byte to write to MOSI return The 8 bit byte simultaneously read from MISOspi_transfern($buf); Transfers any number of bytes to and from the currently selected SPI slave. Asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect) during the transfer. Clocks the len 8 bit bytes out on MOSI, and simultaneously clocks in data from MISO. The returned data from the slave replaces the transmitted data in the buffer. Uses polled transfer as per section 10.6.1 of teh BCM 2835 ARM Peripherls manual buf The buffer containing the bytes to be transmitted. ALl teh bytes in teh buffer will be sent, and the received data from the slave will replace the contentsSYNOPSIS use Device::BCM2835; # Library managment Device::BCM2835::set_debug(1); Device::BCM2835::init(); # Low level register access Device::BCM2835::peri_read(&Device::BCM2835::BCM2835_GPIO_BASE + &Device::BCM2835::BCM2835_GPFSEL1); Device::BCM2835::peri_write(&Device::BCM2835::BCM2835_GPIO_BASE + &Device::BCM2835::BCM2835_GPFSEL2, 0xdeadbeef) Device::BCM2835::peri_set_bits(&Device::BCM2835::BCM2835_GPIO_BASE + &Device::BCM2835::BCM2835_GPFSEL3, 0xdeadbeef, 0x1f); # GPIO register access Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_GPIO_P1_11, &Device::BCM2835::BCM2835_GPIO_FSEL_OUTP); Device::BCM2835::gpio_set(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_clr(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_lev(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_eds(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_set_eds(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_ren(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_fen(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_hen(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_len(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_aren(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_afen(&Device::BCM2835::RPI_GPIO_P1_11); Device::BCM2835::gpio_pud(&Device::BCM2835::BCM2835_GPIO_PUD_OFF); Device::BCM2835::gpio_pudclk(&Device::BCM2835::RPI_GPIO_P1_11, 1); my $pad = Device::BCM2835::gpio_pad(&Device::BCM2835::BCM2835_PAD_GROUP_GPIO_0_27); Device::BCM2835::gpio_set_pad(&Device::BCM2835::BCM2835_PAD_GROUP_GPIO_0_27, &Device::BCM2835::BCM2835_PAD_HYSTERESIS_ENABLED | &Device::BCM2835::BCM2835_PAD_DRIVE_10mA); # High level and convenience functions Device::BCM2835::delay(10); Device::BCM2835::delayMicroseconds(10); Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, 1); Device::BCM2835::gpio_set_pud(&Device::BCM2835::RPI_GPIO_P1_11, &Device::BCM2835::BCM2835_GPIO_PUD_UP);Product's homepage


Device::BCM2835 Related Software