> For the complete documentation index, see [llms.txt](https://mwx.twelite.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mwx.twelite.info/latest_en/api-reference/predefined_objs/spi.md).

# SPI

Reads and writes the SPI bus (as Controller).

## Constants

| Constant                                                                 | Meaning                |
| ------------------------------------------------------------------------ | ---------------------- |
| <p><code>const uint8\_t</code><br><code>SPI\_CONF::MSBFIRST</code></p>   | MSB as the first bit   |
| <p><code>const uint8\_t</code><br><code>SPI\_CONF::LSBFIRST</code></p>   | make LSB the first bit |
| <p><code>const uint8\_t</code><br><code>SPI\_CONF::SPI\_MODE0</code></p> | set SPI MODE 0         |
| <p><code>const uint8\_t</code><br><code>SPI\_CONF::SPI\_MODE1</code></p> | set to SPI MODE 1      |
| <p><code>const uint8\_t</code><br><code>SPI\_CONF::SPI\_MODE2</code></p> | set to SPI MODE 2      |
| <p><code>const uint8\_t</code><br><code>SPI\_CONF::SPI\_MODE3</code></p> | set to SPI MODE 3      |

## Initialization and termination

The procedure for using the SPI bus depends on the `begin()` method.

Reads and writes the SPI bus (MASTER).

### begin()

```cpp
void begin(uint8_t slave_select, SPISettings settings)
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
```

Initialize hardware.

{% hint style="warning" %}
This process is also required after sleep recovery.
{% endhint %}

| Parameters     | Description                                                                                                                                                                                                                                                                                                                                                                                        |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `slave_select` | Specify the SPI slave select pin to be used. `0 : DIO19`, `1 : DIO0` (DIO 19 is reserved), `2 : DIO1` (DIO 0,19 is reserved)                                                                                                                                                                                                                                                                       |
| `settings`     | <p>Specifies the SPI bus setting.<br><br>The <code>clock</code>\[hz] specifies the SPI bus frequency. A divisor closer to the specified frequency will be selected: 16Mhz or 16Mhz divided by an even number.<br><code>bitOrder</code> specifies <code>SPI\_CONF::MSBFIRST</code> or <code>SPI\_CONF::LSBFIRST</code>.<br><code>dataMode</code> specifies <code>SPI\_CONF::SPIMODE0..3</code>.</p> |

###

#### Example

```cpp
void setup() {
  ...
  SPI.begin(0, SPISettings(2000000, SPI_CONF::MSBFIRST, SPI_CONF::SPI_MODE3));
  ...
}

void wakeip() {
  ...
  SPI.begin(0, SPISettings(2000000, SPI_CONF::MSBFIRST, SPI_CONF::SPI_MODE3));
  ...
}
```

### end()

```cpp
void end()
```

Terminate the use of SPI hardware.

## Reading and Writing

There are two types of read/write procedures. Select and use one of them.

* [Member function version (input/output using the following member functions)](/latest_en/api-reference/predefined_objs/spi/spi-member.md) `beginTransaction(), endTransaction(), transfer(), transfer16(), transfer32()`
* [Helper class version (stream function available)](/latest_en/api-reference/predefined_objs/spi/spi-helperclass.md) `transceiver`
