HardwareSPI

This class is used for creating objects to manage the Maple’s built-in SPI ports. The Maple has two SPI ports. The relevant pins corresponding to each port’s logic signals are documented in the following table (and on the Maple silkscreen):

Port number NSS MOSI MISO SCK
1 10 11 12 13
2 31 32 33 34

If you use a SPI port, you cannot simultaneously use its associated pins for other purposes.

Library Documentation

HardwareSPI Class Reference

You can use the SPI interface by including a declaration HardwareSPI Spi(number); at the start of the sketch or program. The number must be either 1 or 2 and specifies which port to use. Once this is done, you can call any of the methods documented here using Spi.method(arguments); for example, Spi.send(0x13) would send the value 0x13 into the port buffer to be transmitted as soon as possible.

class HardwareSPI

Class for interacting with SPI.

HardwareSPI::HardwareSPI(uint32 spi_num)

Construct an object for managing a SPI peripheral. spi_num must be 1 or 2; see the table above for pinout information.

void HardwareSPI::begin(SPIFrequency freq, uint32 endianness, uint32 mode)

Configure the baudrate of the given SPI port and set up the header pins appropriately.

Parameters:

  • freq: one of the SPIFrequency values, given below.

  • endianness: either LSBFIRST (little-endian) or MSBFIRST (big-endian).

  • mode: one of 0, 1, 2, or 3, and specifies which SPI mode is used. The mode number determines a combination of polarity and phase according to the following table:

    Mode

    Polarity

    Phase

    0

    0

    0

    1

    0

    1

    2

    1

    0

    3

    1

    1

    For more information on polarity and phase, see the external references, below.

void HardwareSPI::begin()

A convenience begin(), equivalent to begin(SPI_1_125MHZ, MSBFIRST, 0).

uint8 HardwareSpi::send(uint8* data, uint32 length)

Writes data into the port buffer to be transmitted as soon as possible, where length is the number of bytes to send from data. Returns the last byte shifted back from slave.

uint8 HardwareSpi::send(uint8 data)

Writes the single byte data into the port buffer to be transmitted as soon as possible. Returns the data byte shifted back from the slave.

uint8 HardwareSpi::recv()

Reads a byte from the peripheral. Returns the next byte in the buffer.

SPI Speeds

The possible SPI speeds are configured using the SPIFrequency enum:

SPIFrequency enum

HardwareSPI definitions.

Defines the possible SPI communication speeds.

Values:

  • SPI_18MHZ = 0 -

    18 MHz

  • SPI_9MHZ = 1 -

    9 MHz

  • SPI_4_5MHZ = 2 -

    4.5 MHz

  • SPI_2_25MHZ = 3 -

    2.25 MHZ

  • SPI_1_125MHZ = 4 -

    1.125 MHz

  • SPI_562_500KHZ = 5 -

    562.500 KHz

  • SPI_281_250KHZ = 6 -

    281.250 KHz

  • SPI_140_625KHZ = 7 -

    140.625 KHz

  • MAX_SPI_FREQS = 8 -

    The number of SPI frequencies.

See Also