SPI Data Transfer

Description

SPI master clock and data (SCLK, MOSI and MISO)

This resource implements Serial Peripheral Interface (SPI) master clock and data lines, which allows Sensor Controller task code to perform SPI data transfers.

  • CC13x0/CC26x0: Uses bit-banging.
  • CC13x2/CC26x2 and CC13x4/CC26x4: Uses the AUX SPI Master peripheral.

The interface consists of the following signals:

  • SCLK output - Serial clock
  • MOSI output - Master output / slave input
  • MISO input - Master input / slave output

The pins are mapped in the I/O Mapping Panel .

One or more usages of the SPI Chip Select resource must be used to implement chip select (CSN).

General features:

  • 8-bit and 16-bit data transfers
  • Most significant bit is shifted out first
  • Configurable polarity and phase
  • Configurable bit rates:
    • Maximum: 2 MHz TX only, 1.7 MHz RX only, 1.1 MHz RX+TX
    • Balanced: 1.1 MHz TX only, RX only and TX+RX

It is assumed that the SPI signals are not shared with another, external SPI master. The SCLK and MOSI lines are driven while CSN is deasserted to prevent these lines from floating. The MISO line can be configured as no pull, pull-up or pull-down. Note that the MISO input buffer is disabled while CSN is deasserted, and will not cause leakage if the pin is left floating in this state (with no pull).

Examples

Write Multiple SPI Device Configuration Registers

// MACRO: Writes an 8-bit value to an accelerometer register with 8-bit address
macro spiWriteAccelReg(addr, value) {
    spiBegin(SPI_POL0_PHA0, AUXIO_SPI_CSN_ACCEL);
    spiTx8bit(SPI_POL0_PHA0, ACCEL_SPI_WRITE);
    spiTx8bit(SPI_POL0_PHA0, addr);
    spiTx8bit(SPI_POL0_PHA0, value);
    spiEnd(SPI_POL0_PHA0, AUXIO_SPI_CSN_ACCEL);
}

// Perform soft reset of the accelerometer, and wait for 50 ms
spiWriteAccelReg(ACCEL_REG_SOFT_RESET, ACCEL_SR_KEY);
fwDelayUs(50000, FW_DELAY_RANGE_50_MS);

// Perform one-time configuration of the accelerometer
spiWriteAccelReg(ACCEL_REG_INTMAP1, ACCEL_IM_DATA_READY);
spiWriteAccelReg(ACCEL_REG_POWER_CTL, ACCEL_PC_LOW_NOISE_0 | ACCEL_PC_MEASURE_ON);

Read Array of SPI Device Registers

// Read accelerometer data:
// - Write 8-bit register address
// - Read 16-bit value for each axis, most significant byte first (no need to swap endianess)
spiBegin(SPI_POL0_PHA0, AUXIO_SPI_CSN_ACCEL);
spiTx8bit(SPI_POL0_PHA0, 0x42);
spiRx16bit(SPI_POL0_PHA0; output.x);
spiRx16bit(SPI_POL0_PHA0; output.y);
spiRx16bit(SPI_POL0_PHA0; output.z);
spiEnd(SPI_POL0_PHA0, AUXIO_SPI_CSN_ACCEL);

Read Array of SPI Device Registers with Endianess Conversion

// MACRO: Reads a 16-bit value over SPI, and swaps endianess (LSB is received before MSB)
macro spiRx16bitSwapEndianess(value) {
    U16 valueLsbFirst;
    spiRx16bit(SPI_POL0_PHA0; valueLsbFirst);
    utilSwapEndianess(valueLsbFirst; value);
}

// Read accelerometer data:
// - Write 8-bit register address
// - Read 16-bit value for each axis, least significant byte first (need to swap endianess)
spiBegin(SPI_POL0_PHA0, AUXIO_SPI_CSN_ACCEL);
spiTx8bit(SPI_POL0_PHA0, 0x42);
spiRx16bitSwapEndianess(output.x);
spiRx16bitSwapEndianess(output.y);
spiRx16bitSwapEndianess(output.z);
spiEnd(SPI_POL0_PHA0, AUXIO_SPI_CSN_ACCEL);

Procedures Overview

Name Brief description
spiRx16bit() Transmits 0x0000 over SPI, and returns the received 16-bit value (big-endian). More …
spiRx8bit() Transmits 0x00 over SPI, and returns the received 8-bit value. More …
spiTx16bit() Transmits a 16-bit value over SPI (big-endian). More …
spiTx8bit() Transmits an 8-bit value over SPI. More …
spiTxRx16bit() Transmits a 16-bit value over SPI (big-endian), and returns the received 16-bit value (big-endian). More …
spiTxRx8bit() Transmits an 8-bit value over SPI, and returns the received 8-bit value. More …

Constants

Name Description
SPI_POL0_PHA0 First edge of SCLK is rising, MOSI/MISO sampled at first edge of SCLK
SPI_POL0_PHA1 First edge of SCLK is rising, MOSI/MISO propagated at first edge of SCLK
SPI_POL1_PHA0 First edge of SCLK is falling, MOSI/MISO sampled at first edge of SCLK
SPI_POL1_PHA1 First edge of SCLK is falling, MOSI/MISO propagated at first edge of SCLK

Global Variables

None.

Procedures

spiRx16bit

Prototype: spiRx16bit(#cfg; rxValue)

Transmits 0x0000 over SPI, and returns the received 16-bit value (big-endian).

Parameter value(s)

  • #cfg : SCLK polarity and phase configuration (SPI_POLx_PHAy)

Return value(s)

  • rxValue : The received 16-bit value ([15:8] = first byte, [7:0] = second byte)

spiRx8bit

Prototype: spiRx8bit(#cfg; rxValue)

Transmits 0x00 over SPI, and returns the received 8-bit value.

Parameter value(s)

  • #cfg : SCLK polarity and phase configuration (SPI_POLx_PHAy)

Return value(s)

  • rxValue : The received 8-bit value

spiTx16bit

Prototype: spiTx16bit(#cfg, txValue)

Transmits a 16-bit value over SPI (big-endian). The received bytes are ignored.

The procedure returns when the bytes have been transferred.

Parameter value(s)

  • #cfg : SCLK polarity and phase configuration (SPI_POLx_PHAy)
  • txValue : The 16-bit value to be transmitted ([15:8] = first byte, [7:0] = second byte)

spiTx8bit

Prototype: spiTx8bit(#cfg, txValue)

Transmits an 8-bit value over SPI. The received byte is ignored.

The procedure returns when the byte has been transferred.

Parameter value(s)

  • #cfg : SCLK polarity and phase configuration (SPI_POLx_PHAy)
  • txValue : The 8-bit value to be transmitted

spiTxRx16bit

Prototype: spiTxRx16bit(#cfg, txValue; rxValue)

Transmits a 16-bit value over SPI (big-endian), and returns the received 16-bit value (big-endian).

Parameter value(s)

  • #cfg : SCLK polarity and phase configuration (SPI_POLx_PHAy)
  • txValue : The 16-bit value to be transmitted ([15:8] = first byte, [7:0] = second byte)

Return value(s)

  • rxValue : The received 16-bit value ([15:8] = first byte, [7:0] = second byte)

spiTxRx8bit

Prototype: spiTxRx8bit(#cfg, txValue; rxValue)

Transmits an 8-bit value over SPI, and returns the received 8-bit value.

Parameter value(s)

  • #cfg : SCLK polarity and phase configuration (SPI_POLx_PHAy)
  • txValue : The 8-bit value to be transmitted

Return value(s)

  • rxValue : The received 8-bit value