Skip to main content

SPI

SPI

This library provides SPI communications, enabling communications with two or more microcontrollers, with the GR-LYCHEE board as the master device. It is shared with SPI communication of ESP32. When communicating with ESP32, specify PIN_ESP_IO15 (pin 37) as SS. A separate SPI channel (SPI2) is connected to the SD card. To use this library, specify #include <SPI.h>.

SPISettings

Description
Each SPI device can be configured once as an SPISettings object.
Syntax
SPISettings mySetting(speed, dataOrder, datamode)
Parameters
speed: The speed of the communication
dataOrder: MSBFIRST or LSBFIRST
dataMode: SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3
Returns
None

begin

Description
Initializes the SPI bus by setting SCK, MOSI and SS to outputs, pulling SCK and MOSI low, and SS high.
Syntax
SPI.begin()
Parameters
None
Returns
None

end

Description
Disables the SPI bus, and returns the pin to a general I/O port.
Syntax
SPI.end()
Parameters
None
Returns
None

beginTransaction()

Description
Initializes the SPI bus using the defined SPISettings.
Syntax
SPI.beginTransaction(mySettings)
Parameters
mySettings: The chosen settings according to SPISettings
Returns
None

endTransaction()

Description
Stop using the SPI bus. Normally, this is called after de-asserting the chip select, to allow other libraries to use the SPI bus.
Syntax
SPI.endTransaction()
Parameters
None
Returns
None

setBitOrder

Description
Sets the order of the bits shifted out of and into the SPI bus.
Syntax
SPI.setBitOrder(bitOrder)
Parameters
bitOrder: Bit order, either LSBFIRST or MSBFIRST. Default is MSBFIRST
Returns
None

setClockDivider

Description
Sets the SPI clock divider relative to the system clock.
Syntax
SPI.setClockDivider(divider)
Parameters
divider: Set one of the following:
  SPI_CLOCK_DIV2: 8MHz
  SPI_CLOCK_DIV4: 4MHz (default)
  SPI_CLOCK_DIV8: 2MHz
  SPI_CLOCK_DIV16: 1MHz
  SPI_CLOCK_DIV32: 500kHz
  SPI_CLOCK_DIV64: 250kHz
  SPI_CLOCK_DIV128: 125kHz
Returns
None

setDataMode

Description
Sets the SPI data mode: That is, clock polarity and phase.
Syntax
SPI.setDataMode(mode)
Parameters
mode: Set one of the following. Default is Mode 0. SDMMC is not available in Modes 2 and 3.
  SPI_MODE0: Clock is low during idling, samples data at rising edge
  SPI_MODE1: Clock is low during idling, samples data at falling edge
  SPI_MODE2: Clock is high during idling, samples data at rising edge
  SPI_MODE3: Clock is high during idling, samples data at falling edge
Returns
None

transfer

Description
Transfers one byte over the SPI bus, both sending and receiving.
Syntax
SPI.transfer(value)
Parameters
value: A byte of data
Returns
The byte read from the bus.

Sample Program


#include <Arduino.h>
#include <SPI.h>
void setup(){
    SPI.begin();
}
    
void loop(){
    //write
    SPI.transfer(0x55);
        
    //read
    uint8_t data = SPI.transfer(0xff);
}