Class: PiPiper::Spi
- Inherits:
-
Object
- Object
- PiPiper::Spi
- Defined in:
- lib/pi_piper/spi.rb
Overview
class for SPI interfaces on the Raspberry Pi
Constant Summary collapse
- LSBFIRST =
Least signifigant bit first, e.g. 4 = 0b001
0
- MSBFIRST =
Most signifigant bit first, e.g. 4 = 0b100
1
- CHIP_SELECT_0 =
Select Chip 0
0
- CHIP_SELECT_1 =
Select Chip 1
1
- CHIP_SELECT_BOTH =
Select both chips (ie pins CS1 and CS2 are asserted)
2
- CHIP_SELECT_NONE =
No CS, control it yourself
3
- SPI_MODE0 =
SPI Modes
0
- SPI_MODE1 =
1
- SPI_MODE2 =
2
- SPI_MODE3 =
3
Class Method Summary collapse
-
.begin(chip = nil, &block) ⇒ Object
Begin an SPI block.
-
.end ⇒ Object
Not needed when #begin is called with a block.
-
.set_mode(cpol, cpha) ⇒ Object
Sets the SPI mode.
-
.spidev_out(array) ⇒ Object
Uses /dev/spidev0.0 to write to the SPI NOTE: Requires that you have /dev/spidev0.0 see: learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/overview most likely requires ‘chmod 666 /dev/spidev0.0`.
Instance Method Summary collapse
- #bit_order(order = MSBFIRST) ⇒ Object
-
#chip_select(chip = CHIP_SELECT_0) { ... } ⇒ Object
Activate a specific chip so that communication can begin.
-
#chip_select_active_low(active_low, chip = nil) ⇒ Object
Configure the active state of the chip select line.
-
#clock(frequency) ⇒ Object
Sets the SPI clock frequency.
-
#read(count = nil) ⇒ Number|Array
Read from the bus.
-
#write(*args) ⇒ Number|Array
Write to the bus.
Class Method Details
.begin(chip = nil, &block) ⇒ Object
Begin an SPI block. All SPI communications should be wrapped in a block.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pi_piper/spi.rb', line 34 def self.begin(chip=nil, &block) PiPiper.driver.spi_begin chip = CHIP_SELECT_0 if !chip && block_given? spi = new(chip) if block.arity > 0 block.call spi else spi.instance_exec &block end end |
.end ⇒ Object
Not needed when #begin is called with a block
47 48 49 |
# File 'lib/pi_piper/spi.rb', line 47 def self.end PiPiper.driver.spi_end end |
.set_mode(cpol, cpha) ⇒ Object
Sets the SPI mode. Defaults to mode (0,0).
25 26 27 28 29 30 31 |
# File 'lib/pi_piper/spi.rb', line 25 def self.set_mode(cpol, cpha) mode = SPI_MODE0 #default mode = SPI_MODE1 if cpol == 0 and cpha == 1 mode = SPI_MODE2 if cpol == 1 and cpha == 0 mode = SPI_MODE3 if cpol == 1 and cpha == 1 PiPiper.driver.spi_set_data_mode mode end |
.spidev_out(array) ⇒ Object
Uses /dev/spidev0.0 to write to the SPI NOTE: Requires that you have /dev/spidev0.0
see: http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/overview
most likely requires `chmod 666 /dev/spidev0.0`
59 60 61 |
# File 'lib/pi_piper/spi.rb', line 59 def self.spidev_out(array) PiPiper.driver.spidev_out(array) end |
Instance Method Details
#bit_order(order = MSBFIRST) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pi_piper/spi.rb', line 83 def bit_order(order=MSBFIRST) if order.is_a?(Range) if order.begin < order.end order = LSBFIRST else order = MSBFIRST end end PiPiper.driver.spi_bit_order(order) end |
#chip_select(chip = CHIP_SELECT_0) { ... } ⇒ Object
Activate a specific chip so that communication can begin
When a block is provided, the chip is automatically deactivated after the block completes. When a block is not provided, the user is responsible for calling chip_select(CHIP_SELECT_NONE)
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pi_piper/spi.rb', line 113 def chip_select(chip=CHIP_SELECT_0) chip = @chip if @chip PiPiper.driver.spi_chip_select(chip) if block_given? begin yield ensure PiPiper.driver.spi_chip_select(CHIP_SELECT_NONE) end end end |
#chip_select_active_low(active_low, chip = nil) ⇒ Object
Configure the active state of the chip select line
The default state for most chips is active low.
“active low” means the clock line is kept high during idle, and goes low when communicating.
“active high” means the clock line is kept low during idle, and goes high when communicating.
135 136 137 138 139 140 |
# File 'lib/pi_piper/spi.rb', line 135 def chip_select_active_low(active_low, chip=nil) chip = @chip if @chip chip = CHIP_SELECT_0 unless chip PiPiper.driver.spi_chip_select_polarity(chip, active_low ? 0 : 1) end |
#clock(frequency) ⇒ Object
Sets the SPI clock frequency
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pi_piper/spi.rb', line 64 def clock(frequency) = {4000 => 0, #4 kHz 8000 => 32768, #8 kHz 15625 => 16384, #15.625 kHz 31250 => 8192, #31.25 kHz 62500 => 4096, #62.5 kHz 125000 => 2048, #125 kHz 250000 => 1024, #250 kHz 500000 => 512, #500 kHz 1000000 => 256, #1 MHz 2000000 => 128, #2 MHz 4000000 => 64, #4 MHz 8000000 => 32, #8 MHz 20000000 => 16 #20 MHz } divider = [frequency] PiPiper.driver.spi_clock(divider) end |
#read(count = nil) ⇒ Number|Array
Read from the bus
155 156 157 158 159 160 161 |
# File 'lib/pi_piper/spi.rb', line 155 def read(count=nil) if count write([0xFF] * count) else enable { PiPiper.driver.spi_transfer(0) } end end |
#write(*args) ⇒ Number|Array
Write to the bus
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/pi_piper/spi.rb', line 172 def write(*args) case args.count when 0 raise ArgumentError, "missing arguments" when 1 data = args.first else data = args end enable do case data when Numeric PiPiper.driver.spi_transfer(data) when Enumerable PiPiper.driver.spi_transfer_bytes(data) else raise ArgumentError, "#{data.class} is not valid data. Use Numeric or an Enumerable of numbers" end end end |