class Beaglebone::SPIDevice
Object Oriented SPI
Implementation. This treats the SPI
device as an object.
Public Class Methods
Initialize an SPI
device. Returns an SPIDevice
object
@param spi should be a symbol representing the SPI
device @param mode optional, default 0, specifies the SPI
mode :SPI_MODE_0 through 3 @param speed optional, specifies the SPI
communication speed @param bpw optional, specifies the bits per word
@example
spi = SPIDevice.new(:SPI0, SPI_MODE_0)
# File lib/beaglebone/spi.rb, line 399 def initialize(spi, mode=nil, speed=1000000, bpw=8) @spi = spi SPI::setup(@spi, mode, speed, bpw) end
Public Instance Methods
Disable the specified SPI
device
@note device trees cannot be unloaded at this time without kernel panic.
# File lib/beaglebone/spi.rb, line 457 def disable SPI::disable(@spi) end
Return the file descriptor to the open SPI
device
# File lib/beaglebone/spi.rb, line 428 def file SPI::file(@spi) end
Set the bits per word of the SPI
device
@param bpw should specify the bits per word
# File lib/beaglebone/spi.rb, line 449 def set_bpw(bpw) SPI::set_bpw(@spi, bpw) end
Set the communication speed of the SPI
device
@param speed communication speed
# File lib/beaglebone/spi.rb, line 435 def set_speed(speed) SPI::set_speed(@spi, speed) end
Transfer data to and from the SPI
device
@return String data read from SPI
device
@param tx_data data to transmit @param readbytes bytes to read, otherwise it sizeof tx_data is used @param speed optional, speed to xfer at @param delay optional delay @param bpw optional bits per word
@example
# communicate with MCP3008 # byte 1: start bit # byte 2: single(1)/diff(0),3 bites for channel, null pad # byte 3: don't care spi = SPIDevice.new(:SPI0) raw = spi.xfer([ 0b00000001, 0b10000000, 0].pack("C*")) data = raw.unpack("C*") val = ((data[1] & 0b00000011) << 8 ) | data[2]
# File lib/beaglebone/spi.rb, line 423 def xfer(tx_data, readbytes=0, speed=nil, delay=nil, bpw=nil) SPI::xfer(@spi, tx_data, readbytes, speed, delay, bpw) end