The source files for the SPI driver library resides in the emdrv/spidrv folder, and are named spidrv.c and spidrv.h.
The SPI driver support the SPI capabilities of EFM32 USARTs. The driver is fully reentrant and several drivers can coexist. The driver does not buffer or queue data. The driver has SPI transfer functions for both master and slave SPI mode. Both synchronous and asynchronous transfer functions are present. Synchronous transfer functions are blocking and will not return to caller before the transfer has completed. Asynchronous transfer functions report transfer completion with callback functions. Transfers are done using DMA.
Some properties of the SPIDRV driver are compile-time configurable. These properties are stored in a file named spidrv_config.h. A template for this file, containing default values, resides in the emdrv/config folder. Currently the configuration options are:
To configure SPIDRV, provide your own configuration file. Here is a sample spidrv_config.h file:
#ifndef __SILICON_LABS_SPIDRV_CONFIG_H__ #define __SILICON_LABS_SPIDRV_CONFIG_H__ // SPIDRV configuration option. Use this define to include the // slave part of the SPIDRV API. #define EMDRV_SPIDRV_INCLUDE_SLAVE // SPIDRV configuration option. Set SPI transfer DMA IRQ priority. // Range is 0..7, 0 is highest priority. #define EMDRV_SPIDRV_DMA_IRQ_PRIORITY 4 #endif
The properties of each SPI driver instance are set at run-time via the SPIDRV_Init_t data structure input parameter to the SPIDRV_Init() function.
This section contain brief descriptions of the functions in the API. You will find detailed information on input and output parameters and return values by clicking on the hyperlinked function names. Most functions return an error code, ECODE_EMDRV_SPIDRV_OK is returned on success, see ecode.h and spidrv.h for other error codes.
Your application code must include one header file: spidrv.h.
SPIDRV_Init(), SPIDRV_DeInit()
These functions initializes or deinitializes the SPIDRV driver. Typically SPIDRV_Init() is called once in your startup code.
SPIDRV_GetTransferStatus()
Query the status of a transfer. Reports number of items (frames) transmitted and remaining.
SPIDRV_AbortTransfer()
Stop an ongoing transfer.
SPIDRV_SetBitrate(), SPIDRV_GetBitrate()
Set or query SPI bus bitrate.
SPIDRV_SetFramelength(), SPIDRV_GetFramelength()
Set or query SPI bus frame length.
SPIDRV_MReceive(), SPIDRV_MReceiveB()
SPIDRV_MTransfer(), SPIDRV_MTransferB(), SPIDRV_MTransferSingleItemB()
SPIDRV_MTransmit(), SPIDRV_MTransmitB()
SPIDRV_SReceive(), SPIDRV_SReceiveB()
SPIDRV_STransfer(), SPIDRV_STransferB()
SPIDRV_STransmit(), SPIDRV_STransmitB()
SPI transfer functions for SPI masters have an uppercase M in their name, the slave counterparts have an S.
Transfer functions come in both synchronous and asynchronous versions, the synchronous versions have an uppercase B (for Blocking) at the end of their function name. Synchronous functions will not return before the transfer has completed. The aynchronous functions signal transfer completion with a callback function.
Transmit functions discards received data, receive functions transmit a fixed data pattern set when the driver is initialized (SPIDRV_Init_t::dummyTxValue). Transfer functions both receive and transmit data.
All slave transfer functions have a millisecond timeout parameter. Use 0 for no (infinite) timeout.
#include "spidrv.h" SPIDRV_HandleData_t handleData; SPIDRV_Handle_t handle = &handleData; void TransferComplete( SPIDRV_Handle_t handle, Ecode_t transferStatus, int itemsTransferred) { if ( transferStatus == ECODE_EMDRV_SPIDRV_OK ) { // Success ! } } int main( void ) { uint8_t buffer[10]; SPIDRV_Init_t initData = SPIDRV_MASTER_USART2; // Initialize a SPI driver instance SPIDRV_Init( handle, &initData ); // Transmit data using a blocking transmit function SPIDRV_MTransmitB( handle, buffer, 10 ); // Transmit data using a callback to catch transfer completion. SPIDRV_MTransmit( handle, buffer, 10, TransferComplete ); }