Class: SPI::Driver::SPIdev

Inherits:
Base
  • Object
show all
Defined in:
lib/spi/driver/spidev.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ SPIdev

Returns a new instance of SPIdev.

Raises:



10
11
12
13
14
15
16
17
# File 'lib/spi/driver/spidev.rb', line 10

def initialize(args={})
  raise SPIException, "No Device specified" if args[:device].nil?
  raise SPIException, "Device #{args[:device]} not found" unless File.exists?(args[:device])
  @device = File.open(args[:device])
  @mode=getMode
  @bits=getBits
  @speed=getSpeed
end

Instance Attribute Details

#bitsObject

Returns the value of attribute bits.



7
8
9
# File 'lib/spi/driver/spidev.rb', line 7

def bits
  @bits
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/spi/driver/spidev.rb', line 6

def mode
  @mode
end

#speedObject

Returns the value of attribute speed.



8
9
10
# File 'lib/spi/driver/spidev.rb', line 8

def speed
  @speed
end

Instance Method Details

#xfer(txdata: [], length: 0) ⇒ Object

By the length used will always be the greater of txdata.size and length. rxdata will be sized according to that maximum and txdata grown if needed



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spi/driver/spidev.rb', line 39

def xfer(txdata: [], length: 0)
  # Ensure tx and rx buffers are the same size
  length = txdata.size if txdata.size > length
  txdata += (Array.new(length-txdata.size) {0}) if length > txdata.size
  rxdata  = (Array.new(length) { 0 })
  
  # TODO Use the bitstruct gem ?
  # https://rubygems.org/gems/bit-struct/versions/0.15.0
  # Or create a class to wrap access and pack/unpack

  txdata_pack       = txdata.pack('C*')     # __u64 (pointer to data)
  rxdata_pack       = rxdata.pack('C*')     # __u64 (pointer to data)

  # length                                  # __u32
  speed             = 0                     # __u32

  delay_usecs       = 0                     # __u16
  bits_per_word     = 0                     # __u8
  cs_change         = 0                     # __u8
  tx_nbits          = 0                     # __u8
  rx_nbits          = 0                     # __u8
  pad               = 0                     # __u16

  txdata_p = [txdata_pack].pack('P').unpack('L!')[0] # 64bit pointer to txdata
  rxdata_p = [rxdata_pack].pack('P').unpack('L!')[0] # 64bit pointer to rxdata

  # We might need to do something special with the txdata and rxdata pointers to make them 64 bit
  data = [txdata_p, rxdata_p, length, speed, delay_usecs, bits_per_word, cs_change, tx_nbits, rx_nbits, pad].pack('QQLLSCCCCS')

  # We're only going to handle one message at a time for now
  @device.ioctl(SPI_IOC_MESSAGE(1),data)

  return rxdata_pack.unpack('C*')
    
end