A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://arm-software.github.io/CMSIS_5/Driver/html/group__spi__interface__gr.html below:

SPI Interface

Driver API for SPI Bus Peripheral (Driver_SPI.h) More...

Driver API for SPI Bus Peripheral (Driver_SPI.h)

The Serial Peripheral Interface Bus (SPI) implements a synchronous serial bus for data exchange. In microcontroller (MCU) applications, the interface is often used to connect peripheral components at board (PCB) level. SPI devices can operate as Master (SCLK and SS are outputs) or Slave (SCLK and SS are inputs). Wikipedia offers more information about the Serial Peripheral Interface Bus.

Block Diagram

The SPI Driver API defines a SPI interface for middleware components. The SPI Driver supports multiple slaves, but if only one slave is connected, then the Slave Select signal can be omitted.

SPI Master connected to a single slave

SPI Master connected to 3 slaves

The SPI Driver functions control the following SPI signal lines.

Signal Name Description SS Slave Select (active low) Selects the slave. This signal can be part of the SPI peripheral or implemented using a GPIO pin. MOSI Master Out, Slave In MOSI output of the Master connects to MOSI input of the Slave. SCLK Serial Clock Serial clock output from Master. Controls the transfer speed and when data are sent and read. MISO Master In, Slave Out MISO input of the Master connects to MISO output of the Slave.

SPI API

The following header files define the Application Programming Interface (API) for the SPI interface:

The driver implementation is a typical part of the Device Family Pack (DFP) that supports the peripherals of the microcontroller family.

Driver Functions

The driver functions are published in the access struct as explained in Common Driver Functions

Example Code

The following example code shows the usage of the SPI interface.

#include "cmsis_os.h"

void mySPI_Thread(void const *argument);

osThreadId tid_mySPI_Thread;

void mySPI_callback(uint32_t event)

{

switch (event)

{

osSignalSet(tid_mySPI_Thread, 0x01);

break;

__breakpoint(0);

break;

__breakpoint(0);

break;

}

}

const uint8_t testdata_out[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };

uint8_t testdata_in [8];

void mySPI_Thread(void const* arg)

{

osEvent evt;

#ifdef DEBUG

if

(version.

api

< 0x200)

{

return;

}

{

return;

}

#endif

while (1)

{

SPIdrv->

Send

(testdata_out,

sizeof

(testdata_out));

evt = osSignalWait(0x01, 100);

if (evt.status == osEventTimeout) {

__breakpoint(0);

}

evt = osSignalWait(0x01, 100);

if (evt.status == osEventTimeout) {

__breakpoint(0);

}

}

}

Access structure of the SPI Driver.

The functions of the SPI driver are accessed by function pointers exposed by this structure. Refer to Common Driver Functions for overview information.

Each instance of a SPI interface provides such an access structure. The instance is identified by a postfix number in the symbol name of the access structure, for example:

A middleware configuration setting allows connecting the middleware to a specific driver instance Driver_SPIn. The default is 0, which connects a middleware to the first instance of a driver.

int32_t(* Send)(const void *data, uint32_t num)

Pointer to ARM_SPI_Send : Start sending data to SPI Interface.

int32_t(* Receive)(void *data, uint32_t num)

Pointer to ARM_SPI_Receive : Start receiving data from SPI Interface.

int32_t(* Transfer)(const void *data_out, void *data_in, uint32_t num)

Pointer to ARM_SPI_Transfer : Start sending/receiving data to/from SPI.

int32_t(* Control)(uint32_t control, uint32_t arg)

Pointer to ARM_SPI_Control : Control SPI Interface.

struct ARM_SPI_CAPABILITIES

SPI Driver Capabilities.

A SPI driver can be implemented with different capabilities. The data fields of this structure encode the capabilities implemented by this driver.

Returned by:

Data Fields uint32_t simplex: 1 supports Simplex Mode (Master and Slave)
Deprecated:
Reserved (must be zero)
uint32_t ti_ssi: 1 supports TI Synchronous Serial Interface uint32_t microwire: 1 supports Microwire Interface uint32_t event_mode_fault: 1 Signal Mode Fault event: ARM_SPI_EVENT_MODE_FAULT. uint32_t reserved: 28 Reserved (must be zero)

SPI Status.

Structure with information about the status of the SPI. The data fields encode busy flag and error flags.

Returned by:

Data Fields uint32_t busy: 1 Transmitter/Receiver busy flag. uint32_t data_lost: 1 Data lost: Receive overflow / Transmit underflow (cleared on start of transfer operation) uint32_t mode_fault: 1 Mode fault detected; optional (cleared on start of transfer operation) uint32_t reserved: 29

Get driver version.

Returns
ARM_DRIVER_VERSION

The function ARM_SPI_GetVersion returns version information of the driver implementation in ARM_DRIVER_VERSION

Example:

void setup_spi (void) {

drv_info = &Driver_SPI0;

if

(version.

api

< 0x10A) {

return;

}

}

Get driver capabilities.

Returns
ARM_SPI_CAPABILITIES

The function ARM_SPI_GetCapabilities returns information about the capabilities in this driver implementation. The data fields of the structure ARM_SPI_CAPABILITIES encode various capabilities, for example supported modes.

Example:

void read_capabilities (void) {

drv_info = &Driver_SPI0;

}

Initialize SPI Interface.

Parameters
Returns
Status Error Codes

The function ARM_SPI_Initialize initializes the SPI interface.

The parameter cb_event is a pointer to the ARM_SPI_SignalEvent callback function; use a NULL pointer when no callback signals are required.

The function is called when the middleware component starts operation and performs the following:

Example:

int32_t ARM_SPI_Uninitialize ( void  )

De-initialize SPI Interface.

Returns
Status Error Codes

The function ARM_SPI_Uninitialize de-initializes the resources of SPI interface.

It is called when the middleware component stops operation and releases the software resources used by the interface.

Control SPI Interface Power.

Parameters
Returns
Status Error Codes

The function ARM_SPI_PowerControl controls the power modes of the SPI interface.

The parameter state sets the operation and can have the following values:

Refer to Function Call Sequence for more information.

int32_t ARM_SPI_Send ( const void *  data, uint32_t  num  )

Start sending data to SPI transmitter.

Parameters
[in] data Pointer to buffer with data to send to SPI transmitter [in] num Number of data items to send
Returns
Status Error Codes

This function ARM_SPI_Send is used to send data to the SPI transmitter (received data is ignored).

The parameter data specifies the data buffer.
The parameter num specifies the number of items to send.
The item size is defined by the data type, which depends on the configured number of data bits.

Data type is:

Calling the function ARM_SPI_Send only starts the send operation. When in slave mode, the operation is only registered and started when the master starts the transfer. The function is non-blocking and returns as soon as the driver has started the operation (driver typically configures DMA or the interrupt system for continuous transfer). During the operation it is not allowed to call this function or any other data transfer function again. Also the data buffer must stay allocated and the contents of unsent data must not be modified. When send operation is completed (requested number of items sent), the ARM_SPI_EVENT_TRANSFER_COMPLETE event is generated. Progress of send operation can also be monitored by reading the number of items already sent by calling ARM_SPI_GetDataCount.

Status of the transmitter can also be monitored by calling the ARM_SPI_GetStatus and checking the busy data field, which indicates if transmission is still in progress or pending.

When in master mode and configured to monitor slave select and the slave select gets deactivated during transfer, then the SPI mode changes to inactive and the ARM_SPI_EVENT_MODE_FAULT event is generated (instead of ARM_SPI_EVENT_TRANSFER_COMPLETE).

When in slave mode but send/receive/transfer operation is not started and data is sent/requested by the master, then the ARM_SPI_EVENT_DATA_LOST event is generated.

Send operation can be aborted by calling ARM_SPI_Control with ARM_SPI_ABORT_TRANSFER as the control parameter.

int32_t ARM_SPI_Receive ( void *  data, uint32_t  num  )

Start receiving data from SPI receiver.

Parameters
[out] data Pointer to buffer for data to receive from SPI receiver [in] num Number of data items to receive
Returns
Status Error Codes

The function ARM_SPI_Receive is used to receive data (transmits the default value as specified by ARM_SPI_Control with ARM_SPI_SET_DEFAULT_TX_VALUE as control parameter).

The parameter data specifies the data buffer.
The parameter num specifies the number of items to receive.
The item size is defined by the data type, which depends on the configured number of data bits.

Data type is:

Calling the function ARM_SPI_Receive only starts the receive operation. The function is non-blocking and returns as soon as the driver has started the operation (driver typically configures DMA or the interrupt system for continuous transfer). When in slave mode, the operation is only registered and started when the master starts the transfer. During the operation it is not allowed to call this function or any other data transfer function again. Also the data buffer must stay allocated. When receive operation is completed (requested number of items received), the ARM_SPI_EVENT_TRANSFER_COMPLETE event is generated. Progress of receive operation can also be monitored by reading the number of items already received by calling ARM_SPI_GetDataCount.

Status of the receiver can also be monitored by calling the ARM_SPI_GetStatus and checking the busy data field, which indicates if reception is still in progress or pending.

When in master mode and configured to monitor slave select and the slave select gets deactivated during transfer, then the SPI mode changes to inactive and the ARM_SPI_EVENT_MODE_FAULT event is generated (instead of ARM_SPI_EVENT_TRANSFER_COMPLETE).

When in slave mode but send/receive/transfer operation is not started and data is sent/requested by the master, then the ARM_SPI_EVENT_DATA_LOST event is generated.

Receive operation can be aborted by calling ARM_SPI_Control with ARM_SPI_ABORT_TRANSFER as the control parameter.

int32_t ARM_SPI_Transfer ( const void *  data_out, void *  data_in, uint32_t  num  )

Start sending/receiving data to/from SPI transmitter/receiver.

Parameters
[in] data_out Pointer to buffer with data to send to SPI transmitter [out] data_in Pointer to buffer for data to receive from SPI receiver [in] num Number of data items to transfer
Returns
Status Error Codes

The function ARM_SPI_Transfer transfers data via SPI. It synchronously sends data to the SPI transmitter and receives data from the SPI receiver.

The parameter data_out is a pointer to the buffer with data to send.
The parameter data_in is a pointer to the buffer which receives data.
The parameter num specifies the number of items to transfer.
The item size is defined by the data type which depends on the configured number of data bits.

Data type is:

Calling the function ARM_SPI_Transfer only starts the transfer operation. The function is non-blocking and returns as soon as the driver has started the operation (driver typically configures DMA or the interrupt system for continuous transfer). When in slave mode, the operation is only registered and started when the master starts the transfer. During the operation it is not allowed to call this function or any other data transfer function again. Also the data buffers must stay allocated and the contents of unsent data must not be modified. When transfer operation is completed (requested number of items transferred), the ARM_SPI_EVENT_TRANSFER_COMPLETE event is generated. Progress of transfer operation can also be monitored by reading the number of items already transferred by calling ARM_SPI_GetDataCount.

Status of the transmitter and receiver can also be monitored by calling the ARM_SPI_GetStatus and checking the busy flag.

When in master mode and configured to monitor slave select and the slave select gets deactivated during transfer, then the SPI mode changes to inactive and the ARM_SPI_EVENT_MODE_FAULT event is generated (instead of ARM_SPI_EVENT_TRANSFER_COMPLETE).

When in slave mode but send/receive/transfer operation is not started and data is sent/requested by the master, then the ARM_SPI_EVENT_DATA_LOST event is generated.

Transfer operation can also be aborted by calling ARM_SPI_Control with ARM_SPI_ABORT_TRANSFER as the control parameter.

uint32_t ARM_SPI_GetDataCount ( void  )

Get transferred data count.

Returns
number of data items transferred

The function ARM_SPI_GetDataCount returns the number of currently transferred data items during ARM_SPI_Send, ARM_SPI_Receive and ARM_SPI_Transfer operation.

int32_t ARM_SPI_Control ( uint32_t  control, uint32_t  arg  )

Control SPI Interface.

Parameters
[in] control Operation [in] arg Argument of operation (optional)
Returns
common Status Error Codes and driver specific Status Error Codes

The function ARM_SPI_Control controls the SPI interface settings and executes various operations.

The parameter control is a bit mask that specifies various operations.

The parameter arg provides (depending on the parameter control) additional information, for example the Bus Speed.

Parameter control Bit Category Description ARM_SPI_MODE_INACTIVE 0..7 Mode Controls Set SPI to inactive. ARM_SPI_MODE_MASTER Set the SPI Master (Output on MOSI, and the Input on MISO); arg = Bus Speed in bps ARM_SPI_MODE_SLAVE Set the SPI Slave (Output on MISO, and the Input on MOSI) ARM_SPI_CPOL0_CPHA0 (default) 8..11 Clock Polarity
(Frame Format) CPOL=0 and CPHA=0: Clock Polarity 0, Clock Phase 0 ARM_SPI_CPOL0_CPHA1 CPOL=0 and CPHA=1: Clock Polarity 0, Clock Phase 1 ARM_SPI_CPOL1_CPHA0 CPOL=1 and CPHA=0: Clock Polarity 1, Clock Phase 0 ARM_SPI_CPOL1_CPHA1 CPOL=1 and CPHA=1: Clock Polarity 1, Clock Phase 1 ARM_SPI_TI_SSI Specifies that the frame format corresponds to the Texas Instruments Frame Format ARM_SPI_MICROWIRE Specifies that the frame format corresponds to the National Semiconductor Microwire Frame Format ARM_SPI_DATA_BITS(n) 12..17 Data Bits Set the number of bits per SPI frame; range for n = 1..32. This is the minimum required parameter. ARM_SPI_MSB_LSB (default) 18 Bit Order Set the bit order from MSB to LSB ARM_SPI_LSB_MSB Set the bit order from LSB to MSB ARM_SPI_SS_MASTER_UNUSED (default) 19..21 Slave Select
when Master Must be used with the corresponding master or slave controls from category Mode Controls. Slave Select
when Slave Set the Slave Select mode for the master to Not used. Used with Mode Control ARM_SPI_MODE_MASTER. Master does not drive or monitor the SS line. For example, when connecting to a single slave, which has the SS line connected to a fixed low level. ARM_SPI_SS_MASTER_SW Set the Slave Select mode for the master to Software controlled. Used with Mode Control ARM_SPI_MODE_MASTER. The Slave Select line is configured as output and controlled via the Miscellaneous Control ARM_SPI_CONTROL_SS. By default, the line it is not active (high), and is not affected by transfer-, send-, or receive functions. ARM_SPI_SS_MASTER_HW_OUTPUT Set the Slave Select mode for the master to Hardware controlled Output. Used with Mode Control ARM_SPI_MODE_MASTER. The Slave Select line is configured as output and controlled by hardware. The line gets activated or deactivated automatically by the hardware for transfers and is not controlled by the Miscellaneous Control ARM_SPI_CONTROL_SS. When exactly the line is activated or deactivated is hardware dependent. Typically, the hardware will activate the line before starting the transfer and deactivate it after the transfer completes. Some hardware will keep the line active as long as the SPI stays master.
Note
Some devices require that the SS signal is strictly defined regarding transfers. Refer to the documentation of your device.
ARM_SPI_SS_MASTER_HW_INPUT Set the Slave Select mode for the master to Hardware monitored Input. Used with Mode Control ARM_SPI_MODE_MASTER. Used in multi-master configuration where a master does not drive the Slave Select when driving the bus, but rather monitors it. When another master activates this line, the active master backs off. This is called Mode Fault. Slave Select is configured as input and hardware only monitors the line. When the line is activated externally while we are master, it presents a Mode Fault (ARM_SPI_EVENT_MODE_FAULT) and the SPI switches to inactive mode. ARM_SPI_SS_SLAVE_HW (default) Set the Slave Select mode for the slave to Hardware monitored. Used with Mode Control ARM_SPI_MODE_SLAVE. Hardware monitors the Slave Select line and accepts transfers only when the line is active. Transfers are ignored while the Slave Select line is inactive. ARM_SPI_SS_SLAVE_SW Set the Slave Select mode for the slave to Software controlled. Used with Mode Control ARM_SPI_MODE_SLAVE. Used only when the Slave Select line is not used. For example, when a single master and slave are connected in the system then the Slave Select line is not needed. Software controls if the slave is responding or not (by default it is not responding). Software enables or disables transfers by using the Miscellaneous Control ARM_SPI_CONTROL_SS. ARM_SPI_SET_BUS_SPEED 0..21 Miscellaneous Controls
(cannot be ORed) Set the bus speed; arg= Bus Speed in bps ARM_SPI_GET_BUS_SPEED Get the bus speed; Return values >= 0 represent the bus speed in bps. Negative values are Status Error Codes. ARM_SPI_SET_DEFAULT_TX_VALUE Set the default transmission value; the parameter arg sets the value ARM_SPI_CONTROL_SS Control the Slave Select signal (SS); the values for the parameter arg are: ARM_SPI_SS_INACTIVE; ARM_SPI_SS_ACTIVE ARM_SPI_ABORT_TRANSFER Abort the current data transfer

Example

Get SPI status.

Returns
SPI status ARM_SPI_STATUS

The function ARM_SPI_GetStatus returns the current SPI interface status.

void ARM_SPI_SignalEvent ( uint32_t  event )

Signal SPI Events.

Parameters
Returns
none

The function ARM_SPI_SignalEvent is a callback function registered by the function ARM_SPI_Initialize.

The parameter event indicates one or more events that occurred during driver operation. Each event is encoded in a separate bit and therefore it is possible to signal multiple events within the same call.

Not every event is necessarily generated by the driver. This depends on the implemented capabilities stored in the data fields of the structure ARM_SPI_CAPABILITIES, which can be retrieved with the function ARM_SPI_GetCapabilities.

The following events can be generated:

Parameter event Bit Description supported when ARM_SPI_CAPABILITIES ARM_SPI_EVENT_TRANSFER_COMPLETE 0 Occurs after call to ARM_SPI_Send, ARM_SPI_Receive, or ARM_SPI_Transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation. always supported ARM_SPI_EVENT_DATA_LOST 1 Occurs in slave mode when data is requested/sent by master but send/receive/transfer operation has not been started and indicates that data is lost. Occurs also in master mode when driver cannot transfer data fast enough. always supported ARM_SPI_EVENT_MODE_FAULT 2 Occurs in master mode when Slave Select is deactivated and indicates Master Mode Fault. The driver is ready for the next transfer operation. data field event_mode_fault = 1

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4