-
Notifications
You must be signed in to change notification settings - Fork 87
The SPI objects allow you to interface with SPI devices. The beagleBone Black has two SPI interfaces:
####SPI0 :
- CS0 : P9_17
- MISO : P9_21
- MOSI : P9_18
- SCLK : P9_22
####SPI1 :
- CS0 : P9_28
- CS1 : P9_42
- MISO : P9_29
- MOSI : P9_30
- SCLK : P9_31
The 2 SPI interfaces are controlled using the two pre-initialized objects SPI0 and SPI1. They are activated by calling SPI0.begin()
and SPI1.begin()
respectively, and are controlled using the methods described below.
SPI0 has one chip select (CS0), and SPI1 has two (CS0 and CS1). The chip select is passed to each of the methods as an integer (e.g. SPI0.read(0, 5)
to read 5 words from SPI0 using its CS0, or SPI1.read(1, 5)
to read 5 words from SPI1 using CS1).
Note: The BeagleBone's SPI kernel driver doesn't currently support disabling the SPI module's chip selects, so if you're using GPIO pins for chip select signals, the SPI module will still be toggling the specified chip select. To avoid activating two SPI devices at the same time you should not combine GPIO chip selects and the built-in chip selects on the same bus.
##Methods
####SPIx.begin() Initializes the SPI interface. Must be called before using any other methods.
####SPIx.open() Another name for begin().
####SPIx.end() Closes the connection to the spidev interface. (This doesn't typically need to be called)
####SPIx.close() Another name for end().
####SPIx.read(cs, num_words)
Reads num_words words from the SPI interface using the given chip select and returns them as a list on integers.
Will only read up to a maximum of 4096 bytes.
####SPIx.write(cs, [words])
Writes each word in the given list to the SPI interface using the given chip select. The list must only contain integers.
Will only write up to a maximum of 4096 bytes.
####SPIx.transfer(cs, [words])
Performs SPI transaction.
Writes each word in the given list to the SPI interface using the given chip select, while simultaneously reading words.
The received words will be returned as a list of integers.
Will only transfer up to a maximum of 4096 bytes.
####SPIx.setClockMode(cs, mode)
Sets the SPI clock mode to use for the given chip select.
The SPI clock mode consists of two bits, setting the Clock Polarity and Phase [CPOL|CPHA].
mode = 0 : base value of the clock is zero, data is captured on the clock's rising edge and data is propagated on a falling edge.
mode = 1 : the base value of the clock is zero, data is captured on the clock's falling edge and data is propagated on a rising edge.
mode = 2 : the base value of the clock is one, data is captured on clock's falling edge and data is propagated on a rising edge.
mode = 3 : the base value of the clock is one, data is captured on clock's rising edge and data is propagated on a falling edge.
####SPIx.setMaxFrequency(cs, frequency) Sets the maximum SPI clock frequency (in Hz) to use for the given chip select.
####SPIx.setBitsPerWord(cs, bits_per_word) Sets the SPI bits per word for the given chip select to the given value.
####SPIx.setMSBFirst(cs)
Sets the SPI bit order for the given chip select to be most significant bit first.
Raises an exception if unable to set the bit order.
####SPIx.setLSBFirst(cs)
Sets the SPI bit order for the given chip select to be least significant bit first.
Raises an exception if unable to set the bit order.
####SPIx.setCSActiveLow(cs) Sets the SPI interface to use a low level when activating the given chip select. (default)
####SPIx.setCSActiveHigh(cs) Sets the SPI interface to use a high level when activating the given chip select. (inverted)