Module: Beaglebone::SPI
- Defined in:
- lib/beaglebone/spi.rb
Overview
SPI
Procedural methods for SPI control
Summary
#setup is called to initialize an SPI device
Constant Summary collapse
- IOC_NONE =
some ioctl defines
0- IOC_WRITE =
1- IOC_READ =
2- IOC_NRBITS =
8- IOC_TYPEBITS =
8- IOC_SIZEBITS =
14- IOC_DIRBITS =
2- IOC_NRSHIFT =
0- IOC_TYPESHIFT =
IOC_NRSHIFT+IOC_NRBITS
- IOC_SIZESHIFT =
IOC_TYPESHIFT+IOC_TYPEBITS
- IOC_DIRSHIFT =
IOC_SIZESHIFT+IOC_SIZEBITS
- SPI_CPHA =
spi defines
0x01- SPI_CPOL =
0x02- SPI_MODE_0 =
(0|0)
- SPI_MODE_1 =
(0|SPI_CPHA)
- SPI_MODE_2 =
(SPI_CPOL|0)
- SPI_MODE_3 =
(SPI_CPOL|SPI_CPHA)
- SPI_CS_HIGH =
0x04- SPI_LSB_FIRST =
0x08- SPI_3WIRE =
0x10- SPI_LOOP =
0x20- SPI_NO_CS =
0x40- SPI_READY =
0x80- SPI_IOC_MAGIC =
'k'.ord
- SPI_IOC_RD_MODE =
ior(SPI_IOC_MAGIC, 1, 1)
2147576577- SPI_IOC_WR_MODE =
iow(SPI_IOC_MAGIC, 1, 1)
1073834753- SPI_IOC_RD_LSB_FIRST =
ior(SPI_IOC_MAGIC, 2, 1)
2147576578- SPI_IOC_WR_LSB_FIRST =
iow(SPI_IOC_MAGIC, 2, 1)
1073834754- SPI_IOC_RD_BITS_PER_WORD =
ior(SPI_IOC_MAGIC, 3, 1)
2147576579- SPI_IOC_WR_BITS_PER_WORD =
iow(SPI_IOC_MAGIC, 3, 1)
1073834755- SPI_IOC_RD_MAX_SPEED_HZ =
ior(SPI_IOC_MAGIC, 4, 4)
2147773188- SPI_IOC_WR_MAX_SPEED_HZ =
iow(SPI_IOC_MAGIC, 4, 4)
1074031364- SPI_IOC_MESSAGE_1 =
1075866368- SPI_IOC_TRANSFER_STRUCT_SIZE =
32
Class Attribute Summary collapse
-
.spimutex ⇒ Object
Returns the value of attribute spimutex.
-
.spistatus ⇒ Object
Returns the value of attribute spistatus.
Class Method Summary collapse
-
.cleanup ⇒ Object
Disable all active SPI devices.
-
.disable(spi) ⇒ Object
Disable the specified SPI device.
-
.file(spi) ⇒ Object
Return the file descriptor to the open SPI device.
-
.set_bpw(spi, bpw) ⇒ Object
Set the bits per word of the specified SPI device.
-
.set_mode(spi, mode) ⇒ Object
Set the communication speed of the specified SPI device.
-
.set_speed(spi, speed) ⇒ Object
Set the communication speed of the specified SPI device.
-
.setup(spi, mode = nil, speed = 1000000, bpw = 8) ⇒ Object
Initialize an SPI device.
-
.xfer(spi, tx_data, readbytes = 0, speed = nil, delay = nil, bpw = nil) ⇒ Object
Transfer data to and from the SPI device.
Class Attribute Details
.spimutex ⇒ Object
Returns the value of attribute spimutex.
63 64 65 |
# File 'lib/beaglebone/spi.rb', line 63 def spimutex @spimutex end |
.spistatus ⇒ Object
Returns the value of attribute spistatus.
63 64 65 |
# File 'lib/beaglebone/spi.rb', line 63 def spistatus @spistatus end |
Class Method Details
.cleanup ⇒ Object
Disable all active SPI devices
248 249 250 251 |
# File 'lib/beaglebone/spi.rb', line 248 def cleanup #reset all spis we've used and unload the device tree spistatus.clone.keys.each { |spi| disable(spi)} end |
.disable(spi) ⇒ Object
device trees cannot be unloaded at this time without kernel panic.
Disable the specified SPI device
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/beaglebone/spi.rb', line 232 def disable(spi) check_spi_valid(spi) check_spi_enabled(spi) SPIS[spi][:pins].each do |pin| disable_spi_pin(pin) end delete_spi_status(spi) #removing spi tree causes a crash... can't really disable. #Beaglebone::device_tree_unload("#{SPIS[spi][:devicetree]}") if SPIS[spi][:devicetree] end |
.file(spi) ⇒ Object
Return the file descriptor to the open SPI device
177 178 179 180 |
# File 'lib/beaglebone/spi.rb', line 177 def file(spi) check_spi_enabled(spi) get_spi_status(spi, :fd_spi) end |
.set_bpw(spi, bpw) ⇒ Object
Set the bits per word of the specified SPI device
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/beaglebone/spi.rb', line 215 def set_bpw(spi, bpw) bpw = bpw.to_i raise ArgumentError, "BPW (#{bpw.to_s}) must be a positive integer" unless bpw > 0 check_spi_enabled(spi) spi_fd = get_spi_status(spi, :fd_spi) spi_fd.ioctl(SPI_IOC_WR_BITS_PER_WORD, [bpw].pack('C')) spi_fd.ioctl(SPI_IOC_RD_BITS_PER_WORD, [bpw].pack('C')) set_spi_status(spi, :bpw, bpw) end |
.set_mode(spi, mode) ⇒ Object
Set the communication speed of the specified SPI device
202 203 204 205 206 207 208 209 |
# File 'lib/beaglebone/spi.rb', line 202 def set_mode(spi, mode) check_spi_enabled(spi) raise ArgumentError, "Mode (#{mode.to_s}) is unknown" unless [SPI_MODE_0, SPI_MODE_1, SPI_MODE_2, SPI_MODE_3].include?(mode) spi_fd = get_spi_status(spi, :fd_spi) spi_fd.ioctl(SPI_IOC_WR_MODE, [mode].pack('C')) spi_fd.ioctl(SPI_IOC_RD_MODE, [mode].pack('C')) end |
.set_speed(spi, speed) ⇒ Object
Set the communication speed of the specified SPI device
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/beaglebone/spi.rb', line 186 def set_speed(spi, speed) speed = speed.to_i raise ArgumentError, "Speed (#{speed.to_s}) must be a positive integer" unless speed > 0 check_spi_enabled(spi) spi_fd = get_spi_status(spi, :fd_spi) spi_fd.ioctl(SPI_IOC_WR_MAX_SPEED_HZ, [speed].pack('L')) spi_fd.ioctl(SPI_IOC_RD_MAX_SPEED_HZ, [speed].pack('L')) set_spi_status(spi, :speed, speed) end |
.setup(spi, mode = nil, speed = 1000000, bpw = 8) ⇒ Object
Initialize an SPI device
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/beaglebone/spi.rb', line 74 def setup(spi, mode=nil, speed=1000000, bpw=8) check_spi_valid(spi) #make sure spi not already enabled return if get_spi_status(spi) mode = mode || SPI_MODE_0 spiinfo = SPIS[spi] #ensure dtb is loaded Beaglebone::device_tree_load("#{spiinfo[:devicetree]}") if spiinfo[:devicetree] #open the spi device. spi_fd = File.open("#{spiinfo[:dev]}#{SPIS[:counter]}.0", 'r+') set_spi_status(spi, :fd_spi, spi_fd) set_spi_status(spi, :mutex, Mutex.new) set_mode(spi, mode) set_bpw(spi, bpw) set_speed(spi, speed) SPIS[:counter] += 1 spiinfo[:pins].each do |pin| Beaglebone::set_pin_status(pin, :spi, spiinfo[:id]) Beaglebone::set_pin_status(pin, :type, :spi) Beaglebone::set_pin_status(pin, :fd_spi, spi_fd) end end |
.xfer(spi, tx_data, readbytes = 0, speed = nil, delay = nil, bpw = nil) ⇒ Object
Transfer data to and from the SPI device
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/beaglebone/spi.rb', line 127 def xfer(spi, tx_data, readbytes=0, speed=nil, delay=nil, bpw=nil) check_spi_enabled(spi) speed = speed || get_spi_status(spi, :speed) delay = delay || 0 bpw = bpw || get_spi_status(spi, :bpw) if tx_data.size > readbytes readbytes = tx_data.size end rx_data = ' ' * readbytes lock_spi(spi) do spi_fd = get_spi_status(spi, :fd_spi) ### SPI IOC transfer structure # __u64 tx_buf; # __u64 rx_buf; # # __u32 len; # __u32 speed_hz; # # __u16 delay_usecs; # __u8 bits_per_word; # __u8 cs_change; # __u32 pad; ### msg = [ tx_data, 0, rx_data, 0, readbytes, speed, delay, bpw, 0, 0].pack('pLpLLLSCCL') #ioctl call to begin data transfer spi_fd.ioctl(SPI_IOC_MESSAGE_1, msg) #speedup with defined int #spi_fd.ioctl(spi_ioc_message(1), msg) end rx_data end |