SPI Library Functions

SPI.begin()

Description

Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to SPI.begin(), the pin is configured to be directly managed by the SPI interface.
Note that once the pin is configured, you can't use it anymore as a general I/O, unless you call the SPI.end() method on the same pin.
The only pins that can be configured to be managed by SPI interface are the Arduino Due's Slave Select pins: 4, 10, 52 and 78.

Syntax

SPI.begin()
SPI.begin(slaveSelectPin) (Arduino Due only)

Parameters

slaveSelectPin: slave device SS pin (Arduino Due only)

Returns

None



SPI.end()

Description

Disables the SPI bus (leaving pin modes unchanged).
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to end(), the specified pin is disconnected from the SPI interface and is available again as a general I/O. 

Syntax

SPI.end()
SPI.end(slaveSelectPin) (Arduino Due only)

Parameters

slaveSelectPin: slave device SS pin (Arduino Due only)

Returns

 None


SPI.setBitOrder()

Description

Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first).
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to setBitOrder(), the bit order setting is applied only to the device connected to the specified SS pin. 

Syntax

SPI.setBitOrder(order)
SPI.setBitOrder(slaveSelectPin, order) (Arduino Due only)

Parameters

order: either LSBFIRST or MSBFIRST

Returns

None


SPI.setClockDivider()

Description

Sets the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter the frequency of the system clock (4 Mhz for the boards at 16 MHz).
Arduino Due
On the Due, the system clock can be divided by values from 1 to 255. The default value is 21, which sets the clock to 4 MHz like other Arduino boards.
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to setClockDivider(), the clock setting is applied only to the device connected to the specified SS pin.

Syntax

SPI.setClockDivider(divider)
SPI.setClockDivider(slaveSelectPin, divider) (Arduino Due only)

Parameters

divider:
  • SPI_CLOCK_DIV2
  • SPI_CLOCK_DIV4
  • SPI_CLOCK_DIV8
  • SPI_CLOCK_DIV16
  • SPI_CLOCK_DIV32
  • SPI_CLOCK_DIV64
  • SPI_CLOCK_DIV128
(On AVR boards)
slaveSelectPin: slave device SS pin (Arduino Due only)
divider: a number from 1 to 255 (Arduino Due only)

Returns

None


SPI.setDataMode()

Description

Sets the SPI data mode: that is, clock polarity and phase. See the Wikipedia article on SPI for details.
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to setDataMode(), the data mode setting is applied only to the device connected to the specified SS pin. 

Syntax

SPI.setDataMode(mode)
SPI.setDataMode(slaveSelectPin, mode) (Arduino Due only)

Parameters

mode:
  • SPI_MODE0
  • SPI_MODE1
  • SPI_MODE2
  • SPI_MODE3
slaveSelectPin slave device SS pin (Arduino Due only)

Returns

None


SPI.transfer()

Description

Transfers one byte over the SPI bus, both sending and receiving.
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to SPI.transfer(), the specified pin is activated (pulled low) before the transfer occurs and deactivated (pulled high) when the transfer is finished.
You can use an additional SPI_CONTINUE or SPI_LAST parameter to manage the Slave Select line after the transfer. SPI_CONTINUE keeps the specified SS pin active (low) after the transfer to allow the send of additional bytes via the transfer() function in the same SPI transaction. The last byte to be transferred should be accompanied by the SPI_LAST parameter. By default, if you don't specify a third parameter, SPI_LAST is used. When a transfer is complete with SPI_LAST, the slave select pin returns inactive (high).

Syntax

SPI.transfer(val)
SPI.transfer(slaveSelectPin, val) (Arduino Due only)
SPI.transfer(slaveSelectPin, val, transferMode) (Arduino Due only)

Parameters

val: the byte to send out over the bus
slaveSelectPin: slave device SS pin (Arduino Due only)
transferMode:
  • SPI_CONTINUE: keeps the SS pin low, allowing a subsequent byte transfer.
  • SPI_LAST: default if not specified the SS pin returns to high after one byte has been transferred.
(Optional, Arduino Due only)

Returns

the byte read from the bus.