WPILibC++ 2024.3.2
SPI Functions

Namespaces

namespace  hal
 WPILib Hardware Abstraction Layer (HAL) namespace.
 

Enumerations

enum  HAL_SPIPort : int32_t {
  HAL_SPI_kInvalid = -1 , HAL_SPI_kOnboardCS0 , HAL_SPI_kOnboardCS1 , HAL_SPI_kOnboardCS2 ,
  HAL_SPI_kOnboardCS3 , HAL_SPI_kMXP
}
 
enum  HAL_SPIMode : int32_t { HAL_SPI_kMode0 = 0 , HAL_SPI_kMode1 = 1 , HAL_SPI_kMode2 = 2 , HAL_SPI_kMode3 = 3 }
 

Functions

void HAL_InitializeSPI (HAL_SPIPort port, int32_t *status)
 Initializes the SPI port. More...
 
int32_t HAL_TransactionSPI (HAL_SPIPort port, const uint8_t *dataToSend, uint8_t *dataReceived, int32_t size)
 Performs an SPI send/receive transaction. More...
 
int32_t HAL_WriteSPI (HAL_SPIPort port, const uint8_t *dataToSend, int32_t sendSize)
 Executes a write transaction with the device. More...
 
int32_t HAL_ReadSPI (HAL_SPIPort port, uint8_t *buffer, int32_t count)
 Executes a read from the device. More...
 
void HAL_CloseSPI (HAL_SPIPort port)
 Closes the SPI port. More...
 
void HAL_SetSPISpeed (HAL_SPIPort port, int32_t speed)
 Sets the clock speed for the SPI bus. More...
 
void HAL_SetSPIMode (HAL_SPIPort port, HAL_SPIMode mode)
 Sets the SPI Mode. More...
 
HAL_SPIMode HAL_GetSPIMode (HAL_SPIPort port)
 Gets the SPI Mode. More...
 
void HAL_SetSPIChipSelectActiveHigh (HAL_SPIPort port, int32_t *status)
 Sets the CS Active high for a SPI port. More...
 
void HAL_SetSPIChipSelectActiveLow (HAL_SPIPort port, int32_t *status)
 Sets the CS Active low for a SPI port. More...
 
int32_t HAL_GetSPIHandle (HAL_SPIPort port)
 Gets the stored handle for a SPI port. More...
 
void HAL_SetSPIHandle (HAL_SPIPort port, int32_t handle)
 Sets the stored handle for a SPI port. More...
 
void HAL_InitSPIAuto (HAL_SPIPort port, int32_t bufferSize, int32_t *status)
 Initializes the SPI automatic accumulator. More...
 
void HAL_FreeSPIAuto (HAL_SPIPort port, int32_t *status)
 Frees an SPI automatic accumulator. More...
 
void HAL_StartSPIAutoRate (HAL_SPIPort port, double period, int32_t *status)
 Sets the period for automatic SPI accumulation. More...
 
void HAL_StartSPIAutoTrigger (HAL_SPIPort port, HAL_Handle digitalSourceHandle, HAL_AnalogTriggerType analogTriggerType, HAL_Bool triggerRising, HAL_Bool triggerFalling, int32_t *status)
 Starts the auto SPI accumulator on a specific trigger. More...
 
void HAL_StopSPIAuto (HAL_SPIPort port, int32_t *status)
 Stops an automatic SPI accumulation. More...
 
void HAL_SetSPIAutoTransmitData (HAL_SPIPort port, const uint8_t *dataToSend, int32_t dataSize, int32_t zeroSize, int32_t *status)
 Sets the data to be transmitted to the device to initiate a read. More...
 
void HAL_ForceSPIAutoRead (HAL_SPIPort port, int32_t *status)
 Immediately forces an SPI read to happen. More...
 
int32_t HAL_ReadSPIAutoReceivedData (HAL_SPIPort port, uint32_t *buffer, int32_t numToRead, double timeout, int32_t *status)
 Reads data received by the SPI accumulator. More...
 
int32_t HAL_GetSPIAutoDroppedCount (HAL_SPIPort port, int32_t *status)
 Gets the count of how many SPI accumulations have been missed. More...
 
void HAL_ConfigureSPIAutoStall (HAL_SPIPort port, int32_t csToSclkTicks, int32_t stallTicks, int32_t pow2BytesPerRead, int32_t *status)
 Configure the Auto SPI Stall time between reads. More...
 

Detailed Description

Enumeration Type Documentation

◆ HAL_SPIMode

enum HAL_SPIMode : int32_t
Enumerator
HAL_SPI_kMode0 
HAL_SPI_kMode1 
HAL_SPI_kMode2 
HAL_SPI_kMode3 

◆ HAL_SPIPort

enum HAL_SPIPort : int32_t
Enumerator
HAL_SPI_kInvalid 
HAL_SPI_kOnboardCS0 
HAL_SPI_kOnboardCS1 
HAL_SPI_kOnboardCS2 
HAL_SPI_kOnboardCS3 
HAL_SPI_kMXP 

Function Documentation

◆ HAL_CloseSPI()

void HAL_CloseSPI ( HAL_SPIPort  port)

Closes the SPI port.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP

◆ HAL_ConfigureSPIAutoStall()

void HAL_ConfigureSPIAutoStall ( HAL_SPIPort  port,
int32_t  csToSclkTicks,
int32_t  stallTicks,
int32_t  pow2BytesPerRead,
int32_t *  status 
)

Configure the Auto SPI Stall time between reads.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[in]csToSclkTicksthe number of ticks to wait before asserting the cs pin
[in]stallTicksthe number of ticks to stall for
[in]pow2BytesPerReadthe number of bytes to read before stalling
[out]statusthe error code, or 0 for success

◆ HAL_ForceSPIAutoRead()

void HAL_ForceSPIAutoRead ( HAL_SPIPort  port,
int32_t *  status 
)

Immediately forces an SPI read to happen.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[out]statusthe error code, or 0 for success

◆ HAL_FreeSPIAuto()

void HAL_FreeSPIAuto ( HAL_SPIPort  port,
int32_t *  status 
)

Frees an SPI automatic accumulator.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[out]statusthe error code, or 0 for success

◆ HAL_GetSPIAutoDroppedCount()

int32_t HAL_GetSPIAutoDroppedCount ( HAL_SPIPort  port,
int32_t *  status 
)

Gets the count of how many SPI accumulations have been missed.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[out]statusthe error code, or 0 for success
Returns
The number of missed accumulations.

◆ HAL_GetSPIHandle()

int32_t HAL_GetSPIHandle ( HAL_SPIPort  port)

Gets the stored handle for a SPI port.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
Returns
The stored handle for the SPI port. 0 represents no stored handle.

◆ HAL_GetSPIMode()

HAL_SPIMode HAL_GetSPIMode ( HAL_SPIPort  port)

Gets the SPI Mode.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
Returns
The SPI mode currently set

◆ HAL_InitializeSPI()

void HAL_InitializeSPI ( HAL_SPIPort  port,
int32_t *  status 
)

Initializes the SPI port.

Opens the port if necessary and saves the handle.

If opening the MXP port, also sets up the channel functions appropriately.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS3, 4 for MXP
[out]statusthe error code, or 0 for success

◆ HAL_InitSPIAuto()

void HAL_InitSPIAuto ( HAL_SPIPort  port,
int32_t  bufferSize,
int32_t *  status 
)

Initializes the SPI automatic accumulator.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[in]bufferSizeThe accumulator buffer size.
[out]statusthe error code, or 0 for success

◆ HAL_ReadSPI()

int32_t HAL_ReadSPI ( HAL_SPIPort  port,
uint8_t *  buffer,
int32_t  count 
)

Executes a read from the device.

This method does not write any data out to the device.

Most spi devices will require a register address to be written before they begin returning data.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
bufferA pointer to the array of bytes to store the data read from the device.
countThe number of bytes to read in the transaction. [1..7]
Returns
Number of bytes read. -1 for error.

◆ HAL_ReadSPIAutoReceivedData()

int32_t HAL_ReadSPIAutoReceivedData ( HAL_SPIPort  port,
uint32_t *  buffer,
int32_t  numToRead,
double  timeout,
int32_t *  status 
)

Reads data received by the SPI accumulator.

Each received data sequence consists of a timestamp followed by the received data bytes, one byte per word (in the least significant byte). The length of each received data sequence is the same as the combined dataSize + zeroSize set in HAL_SetSPIAutoTransmitData.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[out]bufferThe buffer to store the data into.
[in]numToReadThe number of words to read.
[in]timeoutThe read timeout (in seconds).
[out]statusthe error code, or 0 for success
Returns
The number of words actually read.

◆ HAL_SetSPIAutoTransmitData()

void HAL_SetSPIAutoTransmitData ( HAL_SPIPort  port,
const uint8_t *  dataToSend,
int32_t  dataSize,
int32_t  zeroSize,
int32_t *  status 
)

Sets the data to be transmitted to the device to initiate a read.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[in]dataToSendPointer to the data to send (Gets copied for continue use, so no need to keep alive).
[in]dataSizeThe length of the data to send.
[in]zeroSizeThe number of zeros to send after the data.
[out]statusthe error code, or 0 for success

◆ HAL_SetSPIChipSelectActiveHigh()

void HAL_SetSPIChipSelectActiveHigh ( HAL_SPIPort  port,
int32_t *  status 
)

Sets the CS Active high for a SPI port.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
[out]statusthe error code, or 0 for success

◆ HAL_SetSPIChipSelectActiveLow()

void HAL_SetSPIChipSelectActiveLow ( HAL_SPIPort  port,
int32_t *  status 
)

Sets the CS Active low for a SPI port.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
[out]statusthe error code, or 0 for success

◆ HAL_SetSPIHandle()

void HAL_SetSPIHandle ( HAL_SPIPort  port,
int32_t  handle 
)

Sets the stored handle for a SPI port.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
handleThe value of the handle for the port.

◆ HAL_SetSPIMode()

void HAL_SetSPIMode ( HAL_SPIPort  port,
HAL_SPIMode  mode 
)

Sets the SPI Mode.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
modeThe SPI mode to use

◆ HAL_SetSPISpeed()

void HAL_SetSPISpeed ( HAL_SPIPort  port,
int32_t  speed 
)

Sets the clock speed for the SPI bus.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
speedThe speed in Hz (500KHz-10MHz)

◆ HAL_StartSPIAutoRate()

void HAL_StartSPIAutoRate ( HAL_SPIPort  port,
double  period,
int32_t *  status 
)

Sets the period for automatic SPI accumulation.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[in]periodThe accumulation period (seconds).
[out]statusthe error code, or 0 for success

◆ HAL_StartSPIAutoTrigger()

void HAL_StartSPIAutoTrigger ( HAL_SPIPort  port,
HAL_Handle  digitalSourceHandle,
HAL_AnalogTriggerType  analogTriggerType,
HAL_Bool  triggerRising,
HAL_Bool  triggerFalling,
int32_t *  status 
)

Starts the auto SPI accumulator on a specific trigger.

Note that triggering on both rising and falling edges is a valid configuration.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[in]digitalSourceHandleThe trigger source to use (Either HAL_AnalogTriggerHandle or HAL_DigitalHandle).
[in]analogTriggerTypeThe analog trigger type, if the source is an analog trigger.
[in]triggerRisingTrigger on the rising edge if true.
[in]triggerFallingTrigger on the falling edge if true.
[out]statusthe error code, or 0 for success

◆ HAL_StopSPIAuto()

void HAL_StopSPIAuto ( HAL_SPIPort  port,
int32_t *  status 
)

Stops an automatic SPI accumulation.

Parameters
[in]portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.
[out]statusthe error code, or 0 for success

◆ HAL_TransactionSPI()

int32_t HAL_TransactionSPI ( HAL_SPIPort  port,
const uint8_t *  dataToSend,
uint8_t *  dataReceived,
int32_t  size 
)

Performs an SPI send/receive transaction.

This is a lower-level interface to the spi hardware giving you more control over each transaction.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
dataToSendBuffer of data to send as part of the transaction.
dataReceivedBuffer to read data into.
sizeNumber of bytes to transfer. [0..7]
Returns
Number of bytes transferred, -1 for error

◆ HAL_WriteSPI()

int32_t HAL_WriteSPI ( HAL_SPIPort  port,
const uint8_t *  dataToSend,
int32_t  sendSize 
)

Executes a write transaction with the device.

Writes to a device and wait until the transaction is complete.

Parameters
portThe number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
dataToSendThe data to write to the register on the device.
sendSizeThe number of bytes to be written
Returns
The number of bytes written. -1 for an error