Class: Beaglebone::SPIDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/beaglebone/spi.rb

Overview

Object Oriented SPI Implementation. This treats the SPI device as an object.

Instance Method Summary collapse

Constructor Details

#initialize(spi, mode = nil, speed = 1000000, bpw = 8) ⇒ SPIDevice

Initialize an SPI device. Returns an SPIDevice object

Examples:

spi = SPIDevice.new(:SPI0, SPI_MODE_0)

Parameters:

  • spi

    should be a symbol representing the SPI device

  • mode (defaults to: nil)

    optional, default 0, specifies the SPI mode :SPI_MODE_0 through 3

  • speed (defaults to: 1000000)

    optional, specifies the SPI communication speed

  • bpw (defaults to: 8)

    optional, specifies the bits per word



399
400
401
402
# 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

Instance Method Details

#disableObject

Note:

device trees cannot be unloaded at this time without kernel panic.

Disable the specified SPI device



457
458
459
# File 'lib/beaglebone/spi.rb', line 457

def disable
  SPI::disable(@spi)
end

#fileObject

Return the file descriptor to the open SPI device



428
429
430
# File 'lib/beaglebone/spi.rb', line 428

def file
  SPI::file(@spi)
end

#set_bpw(bpw) ⇒ Object

Set the bits per word of the SPI device

Parameters:

  • bpw

    should specify the bits per word



449
450
451
# File 'lib/beaglebone/spi.rb', line 449

def set_bpw(bpw)
  SPI::set_bpw(@spi, bpw)
end

#set_mode(mode) ⇒ Object

Set the communication speed of the SPI device

Parameters:

  • mode

    should be a valid SPI mode, e.g. :SPI_MODE_0 through 3



442
443
444
# File 'lib/beaglebone/spi.rb', line 442

def set_mode(mode)
  SPI::set_mode(@spi, mode)
end

#set_speed(speed) ⇒ Object

Set the communication speed of the SPI device

Parameters:

  • speed

    communication speed



435
436
437
# File 'lib/beaglebone/spi.rb', line 435

def set_speed(speed)
  SPI::set_speed(@spi, speed)
end

#xfer(tx_data, readbytes = 0, speed = nil, delay = nil, bpw = nil) ⇒ Object

Transfer data to and from the SPI device

Examples:

# 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]

Parameters:

  • tx_data

    data to transmit

  • readbytes (defaults to: 0)

    bytes to read, otherwise it sizeof tx_data is used

  • speed (defaults to: nil)

    optional, speed to xfer at

  • delay (defaults to: nil)

    optional delay

  • bpw (defaults to: nil)

    optional bits per word

Returns:

  • String data read from SPI device



423
424
425
# 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