Skip to content
rseetham edited this page Jul 12, 2014 · 11 revisions

This library allows you to communicate with SPI devices. The BeagleBone Black (BBB) has two SPI buses. #######SPI0 : CS P9_17
DO P9_21
DI P9_18
SCLK P9_22
#######SPI1 : CS P9_17
DO P9_21
DI P9_18
SCLK P9_22

The 2 SPI buses are accessed through pre-initialized objects named SPI0 and SPI1. Each is used with the following methods by calling SPI0.begin() and SPI1.begin().


##Functions

SPIx.begin()

Must be called at the start to initialise the bus. Connects the BBB to the SPI Bus specified. Initialises both the devices on the bus.

SPIx.end()

Disconnects the bus from the interface

SPIx.read(chip_select, no_of_bytes_to_be_read)

chip_select must be either 0 or 1 based on the device to read from. Read no_of_bytes_to_be_read bytes from SPI device selected by chip_select. Returns the data read from the device as a list whose each element contains 1 byte.

SPIx.write(chip_select,[list])

Writes each element in the list to SPI device specified by the chip_select. Does not return anything.

SPIx.transfer(chip_select,[list])

Performs SPI transaction. Writes each element in the list to SPI device given by the chip_select. Simultaneously reads from the SPI device specified by the chip_select. CS will be held active between blocks. Returns the data read from the device as a list whose each element contains 1 byte.

SPIx.setDataMode(chip_select,mode)

Sets the Mode of the SPI device specified by chip_select. SPI mode as two bit pattern of Clock Polarity and Phase [CPOL|CPHA]. It can take a value between 0<= chip_select <=3. Mode 0 : base value of the clock is zero, data are 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 are 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 are 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 are captured on clock's rising edge and data is propagated on a falling edge.

SPIx.getDataMode(chip_select)

Gets the Mode of the SPI device specified by chip_select. SPI mode has two bit pattern of Clock Polarity and Phase [CPOL|CPHA]. Mode 0 : base value of the clock is zero, data are 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 are 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 are 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 are captured on clock's rising edge and data is propagated on a falling edge. Returns the mode of SPI device.

SPIx.setBitOrder(chip_select,order)

Sets the BitOrder of the SPI device specified by chip_select to LSB First or MSB First. order must be either LSBFIRST or MSBFIRST. Default is MSBFIRST. NOTE : SPIx.getBitOrder(MSBFIRST) gives an error. Returns the mode of SPI device.

SPIx.getBitOrder(chip_select)

Returns the Bit Order of SPI device specified by chip_select.

SPIx.setMaxFreq(chip_select,freq)

Sets the maximum frequency of the SPI device specified by chip_select to freq in HZ. frequency has to be specified in Hz.

SPIx.getMaxFreq(chip_select)

Gets the maximum frequency of the SPI device specified by chip_select maximum speed in Hz. Returns the maximum frequency in Hz.

SPIx.setCSActiveHigh(chip_select)

Makes Chip Select pin of the SPI device given by chip_select active High.

SPIx.setCSActiveLow(chip_select)

Makes Chip Select pin of the SPI device given by chip_select Active Low.

Clone this wiki locally