Module: Beaglebone::I2C

Defined in:
lib/beaglebone/i2c.rb

Overview

I2C

Procedural methods for I2C control

Summary

#setup is called to initialize an I2C device

Constant Summary collapse

I2C_SLAVE =
0x0703

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.i2cmutexObject

Returns the value of attribute i2cmutex.



16
17
18
# File 'lib/beaglebone/i2c.rb', line 16

def i2cmutex
  @i2cmutex
end

.i2cstatusObject

Returns the value of attribute i2cstatus.



16
17
18
# File 'lib/beaglebone/i2c.rb', line 16

def i2cstatus
  @i2cstatus
end

Class Method Details

.cleanupObject

Disable all active I2C interfaces



130
131
132
133
# File 'lib/beaglebone/i2c.rb', line 130

def cleanup
  #reset all i2cs we've used and unload the device tree
  i2cstatus.clone.keys.each { |i2c| disable(i2c)}
end

.disable(i2c) ⇒ Object

Note:

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

Disable the specified I2C device.

Parameters:

  • i2c

    should be a symbol representing the I2C device



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/beaglebone/i2c.rb', line 115

def disable(i2c)
  check_i2c_valid(i2c)
  check_i2c_enabled(i2c)

  disable_i2c_pin(I2CS[i2c][:sda]) if I2CS[i2c][:sda]
  disable_i2c_pin(I2CS[i2c][:scl]) if I2CS[i2c][:scl]

  delete_i2c_status(i2c)

  #removing i2c tree causes a crash... can't really disable.
  #Beaglebone::device_tree_unload("#{I2CS[i2c][:devicetree]}") if I2CS[i2c][:devicetree]

end

.file(i2c) ⇒ Object

Return the file descriptor to the open I2C device

Parameters:

  • i2c

    should be a symbol representing the I2C device



105
106
107
108
# File 'lib/beaglebone/i2c.rb', line 105

def file(i2c)
  check_i2c_enabled(i2c)
  get_i2c_status(i2c, :fd_i2c)
end

.read(i2c, address, bytes = 1, register = nil) ⇒ Object

Read data from an I2C device

Examples:

# read 3 big endian signed shorts starting at register 0x03
data = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
  x,z,y = raw.unpack("s>*")

Parameters:

  • i2c

    should be a symbol representing the I2C device

  • address

    the address of the slave device

  • bytes (defaults to: 1)

    bytes to read

  • register (defaults to: nil)

    optional register to read from



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/beaglebone/i2c.rb', line 84

def read(i2c, address, bytes=1, register=nil)
  check_i2c_enabled(i2c)

  data = ''
  lock_i2c(i2c) do
    i2c_fd = get_i2c_status(i2c, :fd_i2c)

    #set the slave address to communicate with
    i2c_fd.ioctl(I2C_SLAVE, address)

    i2c_fd.syswrite(register) if register

    data = i2c_fd.sysread(bytes)
  end

  data
end

.setup(i2c) ⇒ Object

Initialize an I2C device

Examples:

I2C.setup(:I2C2)

Parameters:

  • i2c

    should be a symbol representing the I2C device



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/beaglebone/i2c.rb', line 24

def setup(i2c)
  check_i2c_valid(i2c)

  #make sure i2c not already enabled
  return if get_i2c_status(i2c)

  i2cinfo = I2CS[i2c]

  #ensure dtb is loaded
  Beaglebone::device_tree_load("#{i2cinfo[:devicetree]}") if i2cinfo[:devicetree]

  #open the i2c device
  i2c_fd = File.open(i2cinfo[:dev], 'r+')

  Beaglebone::set_pin_status(i2cinfo[:scl], :i2c, i2cinfo[:id])
  Beaglebone::set_pin_status(i2cinfo[:scl], :type, :i2c)
  Beaglebone::set_pin_status(i2cinfo[:scl], :fd_i2c, i2c_fd)

  Beaglebone::set_pin_status(i2cinfo[:sda], :i2c, i2cinfo[:id])
  Beaglebone::set_pin_status(i2cinfo[:sda], :type, :i2c)
  Beaglebone::set_pin_status(i2cinfo[:sda], :fd_i2c, i2c_fd)

  set_i2c_status(i2c, :fd_i2c, i2c_fd)
  set_i2c_status(i2c, :mutex, Mutex.new)
end

.write(i2c, address, data) ⇒ Object

Write data to an I2C device

Examples:

I2C.write(:I2C2, 0x1e, [0x00, 0b10010000].pack("C*") )

Parameters:

  • i2c

    should be a symbol representing the I2C device

  • address

    the address of the slave device

  • data

    the data to write

Returns:

  • Integer the number of bytes written



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/beaglebone/i2c.rb', line 60

def write(i2c, address, data)
  check_i2c_enabled(i2c)

  lock_i2c(i2c) do
    i2c_fd = get_i2c_status(i2c, :fd_i2c)

    #set the slave address to communicate with
    i2c_fd.ioctl(I2C_SLAVE, address)

    i2c_fd.syswrite(data)
  end
end