ChibiOS/RT
2.5.1
SPI Driver
Collaboration diagram for SPI Driver:

Detailed Description

Generic SPI Driver.

This module implements a generic SPI (Serial Peripheral Interface) driver allowing bidirectional and monodirectional transfers, complex atomic transactions are supported as well.

Precondition:
In order to use the SPI driver the HAL_USE_SPI option must be enabled in halconf.h.

Driver State Machine

The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).

dot_inline_dotgraph_2.png

The driver is not thread safe for performance reasons, if you need to access the SPI bus from multiple threads then use the spiAcquireBus() and spiReleaseBus() APIs in order to gain exclusive access.

Data Structures

struct  SPIConfig
 Driver configuration structure. More...
struct  SPIDriver
 Structure representing a SPI driver. More...

Functions

void spiInit (void)
 SPI Driver initialization.
void spiObjectInit (SPIDriver *spip)
 Initializes the standard part of a SPIDriver structure.
void spiStart (SPIDriver *spip, const SPIConfig *config)
 Configures and activates the SPI peripheral.
void spiStop (SPIDriver *spip)
 Deactivates the SPI peripheral.
void spiSelect (SPIDriver *spip)
 Asserts the slave select signal and prepares for transfers.
void spiUnselect (SPIDriver *spip)
 Deasserts the slave select signal.
void spiStartIgnore (SPIDriver *spip, size_t n)
 Ignores data on the SPI bus.
void spiStartExchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf)
 Exchanges data on the SPI bus.
void spiStartSend (SPIDriver *spip, size_t n, const void *txbuf)
 Sends data over the SPI bus.
void spiStartReceive (SPIDriver *spip, size_t n, void *rxbuf)
 Receives data from the SPI bus.
void spiIgnore (SPIDriver *spip, size_t n)
 Ignores data on the SPI bus.
void spiExchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf)
 Exchanges data on the SPI bus.
void spiSend (SPIDriver *spip, size_t n, const void *txbuf)
 Sends data over the SPI bus.
void spiReceive (SPIDriver *spip, size_t n, void *rxbuf)
 Receives data from the SPI bus.
void spiAcquireBus (SPIDriver *spip)
 Gains exclusive access to the SPI bus.
void spiReleaseBus (SPIDriver *spip)
 Releases exclusive access to the SPI bus.
 CH_IRQ_HANDLER (10)
 IRQ 10 service routine.
void spi_lld_init (void)
 Low level SPI driver initialization.
void spi_lld_start (SPIDriver *spip)
 Configures and activates the SPI peripheral.
void spi_lld_stop (SPIDriver *spip)
 Deactivates the SPI peripheral.
void spi_lld_select (SPIDriver *spip)
 Asserts the slave select signal and prepares for transfers.
void spi_lld_unselect (SPIDriver *spip)
 Deasserts the slave select signal.
void spi_lld_ignore (SPIDriver *spip, size_t n)
 Ignores data on the SPI bus.
void spi_lld_exchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf)
 Exchanges data on the SPI bus.
void spi_lld_send (SPIDriver *spip, size_t n, const void *txbuf)
 Sends data over the SPI bus.
void spi_lld_receive (SPIDriver *spip, size_t n, void *rxbuf)
 Receives data from the SPI bus.
uint8_t spi_lld_polled_exchange (SPIDriver *spip, uint8_t frame)
 Exchanges one frame using a polled wait.

Variables

SPIDriver SPID1
 SPI1 driver identifier.

SPI configuration options

#define SPI_USE_WAIT   TRUE
 Enables synchronous APIs.
#define SPI_USE_MUTUAL_EXCLUSION   TRUE
 Enables the spiAcquireBus() and spiReleaseBus() APIs.

Macro Functions

#define spiSelectI(spip)
 Asserts the slave select signal and prepares for transfers.
#define spiUnselectI(spip)
 Deasserts the slave select signal.
#define spiStartIgnoreI(spip, n)
 Ignores data on the SPI bus.
#define spiStartExchangeI(spip, n, txbuf, rxbuf)
 Exchanges data on the SPI bus.
#define spiStartSendI(spip, n, txbuf)
 Sends data over the SPI bus.
#define spiStartReceiveI(spip, n, rxbuf)
 Receives data from the SPI bus.
#define spiPolledExchange(spip, frame)   spi_lld_polled_exchange(spip, frame)
 Exchanges one frame using a polled wait.

Low Level driver helper macros

#define _spi_wait_s(spip)
 Waits for operation completion.
#define _spi_wakeup_isr(spip)
 Wakes up the waiting thread.
#define _spi_isr_code(spip)
 Common ISR code.

Defines

#define STM8S_SPI_USE_SPI   TRUE
 SPI1 driver enable switch.
#define STM8S_SPI_ERROR_HOOK(spip)   chSysHalt()
 Overflow error hook.

Typedefs

typedef struct SPIDriver SPIDriver
 Type of a structure representing an SPI driver.
typedef void(* spicallback_t )(SPIDriver *spip)
 SPI notification callback type.

Enumerations

enum  spistate_t {
  SPI_UNINIT = 0, SPI_STOP = 1, SPI_READY = 2, SPI_ACTIVE = 3,
  SPI_COMPLETE = 4
}
 Driver state machine possible states. More...

Function Documentation

void spiInit ( void  )

SPI Driver initialization.

Note:
This function is implicitly invoked by halInit(), there is no need to explicitly initialize the driver.
Function Class:
Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 61 of file spi.c.

References spi_lld_init().

Referenced by halInit().

Here is the call graph for this function:

void spiObjectInit ( SPIDriver spip)

Initializes the standard part of a SPIDriver structure.

Parameters:
[out]spippointer to the SPIDriver object
Function Class:
Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 73 of file spi.c.

References SPIDriver::config, SPIDriver::mutex, SPI_STOP, SPIDriver::state, and SPIDriver::thread.

Referenced by spi_lld_init().

void spiStart ( SPIDriver spip,
const SPIConfig config 
)

Configures and activates the SPI peripheral.

Parameters:
[in]spippointer to the SPIDriver object
[in]configpointer to the SPIConfig object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 100 of file spi.c.

References SPIDriver::config, spi_lld_start(), SPI_READY, SPI_STOP, and SPIDriver::state.

Here is the call graph for this function:

void spiStop ( SPIDriver spip)

Deactivates the SPI peripheral.

Note:
Deactivating the peripheral also enforces a release of the slave select line.
Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 122 of file spi.c.

References spi_lld_stop(), spi_lld_unselect(), SPI_READY, SPI_STOP, and SPIDriver::state.

Here is the call graph for this function:

void spiSelect ( SPIDriver spip)

Asserts the slave select signal and prepares for transfers.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 142 of file spi.c.

References SPI_READY, spiSelectI, and SPIDriver::state.

void spiUnselect ( SPIDriver spip)

Deasserts the slave select signal.

The previously selected peripheral is unselected.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 160 of file spi.c.

References SPI_READY, spiUnselectI, and SPIDriver::state.

void spiStartIgnore ( SPIDriver spip,
size_t  n 
)

Ignores data on the SPI bus.

This asynchronous function starts the transmission of a series of idle words on the SPI bus and ignores the received data.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be ignored
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 183 of file spi.c.

References SPI_READY, spiStartIgnoreI, and SPIDriver::state.

void spiStartExchange ( SPIDriver spip,
size_t  n,
const void *  txbuf,
void *  rxbuf 
)

Exchanges data on the SPI bus.

This asynchronous function starts a simultaneous transmit/receive operation.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be exchanged
[in]txbufthe pointer to the transmit buffer
[out]rxbufthe pointer to the receive buffer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 210 of file spi.c.

References SPI_READY, spiStartExchangeI, and SPIDriver::state.

void spiStartSend ( SPIDriver spip,
size_t  n,
const void *  txbuf 
)

Sends data over the SPI bus.

This asynchronous function starts a transmit operation.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to send
[in]txbufthe pointer to the transmit buffer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 237 of file spi.c.

References SPI_READY, spiStartSendI, and SPIDriver::state.

void spiStartReceive ( SPIDriver spip,
size_t  n,
void *  rxbuf 
)

Receives data from the SPI bus.

This asynchronous function starts a receive operation.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to receive
[out]rxbufthe pointer to the receive buffer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 263 of file spi.c.

References SPI_READY, spiStartReceiveI, and SPIDriver::state.

void spiIgnore ( SPIDriver spip,
size_t  n 
)

Ignores data on the SPI bus.

This synchronous function performs the transmission of a series of idle words on the SPI bus and ignores the received data.

Precondition:
In order to use this function the option SPI_USE_WAIT must be enabled.
In order to use this function the driver must have been configured without callbacks (end_cb = NULL).
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be ignored
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 289 of file spi.c.

References _spi_wait_s, SPIDriver::config, SPIConfig::end_cb, SPI_READY, spiStartIgnoreI, and SPIDriver::state.

void spiExchange ( SPIDriver spip,
size_t  n,
const void *  txbuf,
void *  rxbuf 
)

Exchanges data on the SPI bus.

This synchronous function performs a simultaneous transmit/receive operation.

Precondition:
In order to use this function the option SPI_USE_WAIT must be enabled.
In order to use this function the driver must have been configured without callbacks (end_cb = NULL).
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be exchanged
[in]txbufthe pointer to the transmit buffer
[out]rxbufthe pointer to the receive buffer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 319 of file spi.c.

References _spi_wait_s, SPIDriver::config, SPIConfig::end_cb, SPI_READY, spiStartExchangeI, and SPIDriver::state.

void spiSend ( SPIDriver spip,
size_t  n,
const void *  txbuf 
)

Sends data over the SPI bus.

This synchronous function performs a transmit operation.

Precondition:
In order to use this function the option SPI_USE_WAIT must be enabled.
In order to use this function the driver must have been configured without callbacks (end_cb = NULL).
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to send
[in]txbufthe pointer to the transmit buffer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 350 of file spi.c.

References _spi_wait_s, SPIDriver::config, SPIConfig::end_cb, SPI_READY, spiStartSendI, and SPIDriver::state.

void spiReceive ( SPIDriver spip,
size_t  n,
void *  rxbuf 
)

Receives data from the SPI bus.

This synchronous function performs a receive operation.

Precondition:
In order to use this function the option SPI_USE_WAIT must be enabled.
In order to use this function the driver must have been configured without callbacks (end_cb = NULL).
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to receive
[out]rxbufthe pointer to the receive buffer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 378 of file spi.c.

References _spi_wait_s, SPIDriver::config, SPIConfig::end_cb, SPI_READY, spiStartReceiveI, and SPIDriver::state.

void spiAcquireBus ( SPIDriver spip)

Gains exclusive access to the SPI bus.

This function tries to gain ownership to the SPI bus, if the bus is already being used then the invoking thread is queued.

Precondition:
In order to use this function the option SPI_USE_MUTUAL_EXCLUSION must be enabled.
Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 405 of file spi.c.

References SPIDriver::mutex.

void spiReleaseBus ( SPIDriver spip)

Releases exclusive access to the SPI bus.

Precondition:
In order to use this function the option SPI_USE_MUTUAL_EXCLUSION must be enabled.
Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 425 of file spi.c.

CH_IRQ_HANDLER ( 10  )

IRQ 10 service routine.

Function Class:
Interrupt handler, this function should not be directly invoked.

Definition at line 61 of file spi_lld.c.

References _spi_isr_code, SPIDriver::rxcnt, SPIDriver::rxptr, STM8S_SPI_ERROR_HOOK, SPIDriver::txcnt, and SPIDriver::txptr.

void spi_lld_init ( void  )

Low level SPI driver initialization.

Function Class:
Not an API, this function is for internal use only.

Definition at line 113 of file spi_lld.c.

References spiObjectInit().

Referenced by spiInit().

Here is the call graph for this function:

void spi_lld_start ( SPIDriver spip)

Configures and activates the SPI peripheral.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 127 of file spi_lld.c.

References SPIDriver::config, and SPIConfig::cr1.

Referenced by spiStart().

void spi_lld_stop ( SPIDriver spip)

Deactivates the SPI peripheral.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 146 of file spi_lld.c.

Referenced by spiStop().

void spi_lld_select ( SPIDriver spip)

Asserts the slave select signal and prepares for transfers.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 166 of file spi_lld.c.

References SPIDriver::config, palClearPad, SPIConfig::sspad, and SPIConfig::ssport.

void spi_lld_unselect ( SPIDriver spip)

Deasserts the slave select signal.

The previously selected peripheral is unselected.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 179 of file spi_lld.c.

References SPIDriver::config, palSetPad, SPIConfig::sspad, and SPIConfig::ssport.

Referenced by spiStop().

void spi_lld_ignore ( SPIDriver spip,
size_t  n 
)

Ignores data on the SPI bus.

This function transmits a series of idle words on the SPI bus and ignores the received data. This function can be invoked even when a slave select signal has not been yet asserted.

Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be ignored
Function Class:
Not an API, this function is for internal use only.

Definition at line 195 of file spi_lld.c.

References SPIDriver::rxcnt, SPIDriver::rxptr, SPIDriver::txcnt, and SPIDriver::txptr.

void spi_lld_exchange ( SPIDriver spip,
size_t  n,
const void *  txbuf,
void *  rxbuf 
)

Exchanges data on the SPI bus.

This asynchronous function starts a simultaneous transmit/receive operation.

Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be exchanged
[in]txbufthe pointer to the transmit buffer
[out]rxbufthe pointer to the receive buffer
Function Class:
Not an API, this function is for internal use only.

Definition at line 218 of file spi_lld.c.

References SPIDriver::rxcnt, SPIDriver::rxptr, SPIDriver::txcnt, and SPIDriver::txptr.

void spi_lld_send ( SPIDriver spip,
size_t  n,
const void *  txbuf 
)

Sends data over the SPI bus.

This asynchronous function starts a transmit operation.

Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to send
[in]txbufthe pointer to the transmit buffer
Function Class:
Not an API, this function is for internal use only.

Definition at line 240 of file spi_lld.c.

References SPIDriver::rxcnt, SPIDriver::rxptr, SPIDriver::txcnt, and SPIDriver::txptr.

void spi_lld_receive ( SPIDriver spip,
size_t  n,
void *  rxbuf 
)

Receives data from the SPI bus.

This asynchronous function starts a receive operation.

Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to receive
[out]rxbufthe pointer to the receive buffer
Function Class:
Not an API, this function is for internal use only.

Definition at line 261 of file spi_lld.c.

References SPIDriver::rxcnt, SPIDriver::rxptr, SPIDriver::txcnt, and SPIDriver::txptr.

uint8_t spi_lld_polled_exchange ( SPIDriver spip,
uint8_t  frame 
)

Exchanges one frame using a polled wait.

This synchronous function exchanges one frame using a polled synchronization method. This function is useful when exchanging small amount of data on high speed channels, usually in this situation is much more efficient just wait for completion using polling than suspending the thread waiting for an interrupt.

Parameters:
[in]spippointer to the SPIDriver object
[in]framethe data frame to send over the SPI bus
Returns:
The received data frame from the SPI bus.

Definition at line 281 of file spi_lld.c.


Variable Documentation

SPI1 driver identifier.

Definition at line 40 of file spi_lld.c.


Define Documentation

#define SPI_USE_WAIT   TRUE

Enables synchronous APIs.

Note:
Disabling this option saves both code and data space.

Definition at line 51 of file spi.h.

#define SPI_USE_MUTUAL_EXCLUSION   TRUE

Enables the spiAcquireBus() and spiReleaseBus() APIs.

Note:
Disabling this option saves both code and data space.

Definition at line 59 of file spi.h.

#define spiSelectI (   spip)
Value:
{                                                  \
  spi_lld_select(spip);                                                     \
}

Asserts the slave select signal and prepares for transfers.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 103 of file spi.h.

Referenced by spiSelect().

#define spiUnselectI (   spip)
Value:
{                                                \
  spi_lld_unselect(spip);                                                   \
}

Deasserts the slave select signal.

The previously selected peripheral is unselected.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 115 of file spi.h.

Referenced by spiUnselect().

#define spiStartIgnoreI (   spip,
 
)
Value:
{                                          \
  (spip)->state = SPI_ACTIVE;                                               \
  spi_lld_ignore(spip, n);                                                  \
}

Ignores data on the SPI bus.

This asynchronous function starts the transmission of a series of idle words on the SPI bus and ignores the received data.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be ignored
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 132 of file spi.h.

Referenced by spiIgnore(), and spiStartIgnore().

#define spiStartExchangeI (   spip,
  n,
  txbuf,
  rxbuf 
)
Value:
{                          \
  (spip)->state = SPI_ACTIVE;                                               \
  spi_lld_exchange(spip, n, txbuf, rxbuf);                                  \
}

Exchanges data on the SPI bus.

This asynchronous function starts a simultaneous transmit/receive operation.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to be exchanged
[in]txbufthe pointer to the transmit buffer
[out]rxbufthe pointer to the receive buffer
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 154 of file spi.h.

Referenced by spiExchange(), and spiStartExchange().

#define spiStartSendI (   spip,
  n,
  txbuf 
)
Value:
{                                     \
  (spip)->state = SPI_ACTIVE;                                               \
  spi_lld_send(spip, n, txbuf);                                             \
}

Sends data over the SPI bus.

This asynchronous function starts a transmit operation.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to send
[in]txbufthe pointer to the transmit buffer
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 174 of file spi.h.

Referenced by spiSend(), and spiStartSend().

#define spiStartReceiveI (   spip,
  n,
  rxbuf 
)
Value:
{                                  \
  (spip)->state = SPI_ACTIVE;                                               \
  spi_lld_receive(spip, n, rxbuf);                                          \
}

Receives data from the SPI bus.

This asynchronous function starts a receive operation.

Precondition:
A slave must have been selected using spiSelect() or spiSelectI().
Postcondition:
At the end of the operation the configured callback is invoked.
Note:
The buffers are organized as uint8_t arrays for data sizes below or equal to 8 bits else it is organized as uint16_t arrays.
Parameters:
[in]spippointer to the SPIDriver object
[in]nnumber of words to receive
[out]rxbufthe pointer to the receive buffer
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 194 of file spi.h.

Referenced by spiReceive(), and spiStartReceive().

#define spiPolledExchange (   spip,
  frame 
)    spi_lld_polled_exchange(spip, frame)

Exchanges one frame using a polled wait.

This synchronous function exchanges one frame using a polled synchronization method. This function is useful when exchanging small amount of data on high speed channels, usually in this situation is much more efficient just wait for completion using polling than suspending the thread waiting for an interrupt.

Note:
This API is implemented as a macro in order to minimize latency.
Parameters:
[in]spippointer to the SPIDriver object
[in]framethe data frame to send over the SPI bus
Returns:
The received data frame from the SPI bus.

Definition at line 212 of file spi.h.

#define _spi_wait_s (   spip)
Value:
{                                                 \
  chDbgAssert((spip)->thread == NULL,                                       \
              "_spi_wait(), #1", "already waiting");                        \
  (spip)->thread = chThdSelf();                                             \
  chSchGoSleepS(THD_STATE_SUSPENDED);                                       \
}

Waits for operation completion.

This function waits for the driver to complete the current operation.

Precondition:
An operation must be running while the function is invoked.
Note:
No more than one thread can wait on a SPI driver using this function.
Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 232 of file spi.h.

Referenced by spiExchange(), spiIgnore(), spiReceive(), and spiSend().

#define _spi_wakeup_isr (   spip)
Value:
{                                             \
  if ((spip)->thread != NULL) {                                             \
    Thread *tp = (spip)->thread;                                            \
    (spip)->thread = NULL;                                                  \
    chSysLockFromIsr();                                                     \
    chSchReadyI(tp);                                                        \
    chSysUnlockFromIsr();                                                   \
  }                                                                         \
}

Wakes up the waiting thread.

Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 246 of file spi.h.

#define _spi_isr_code (   spip)
Value:
{                                               \
  if ((spip)->config->end_cb) {                                             \
    (spip)->state = SPI_COMPLETE;                                           \
    (spip)->config->end_cb(spip);                                           \
    if ((spip)->state == SPI_COMPLETE)                                      \
      (spip)->state = SPI_READY;                                            \
  }                                                                         \
  else                                                                      \
    (spip)->state = SPI_READY;                                              \
  _spi_wakeup_isr(spip);                                                    \
}

Common ISR code.

This code handles the portable part of the ISR code:

  • Callback invocation.
  • Waiting thread wakeup, if any.
  • Driver state transitions.
Note:
This macro is meant to be used in the low level drivers implementation only.
Parameters:
[in]spippointer to the SPIDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 274 of file spi.h.

Referenced by CH_IRQ_HANDLER().

#define STM8S_SPI_USE_SPI   TRUE

SPI1 driver enable switch.

If set to TRUE the support for device SSP0 is included.

Note:
The default is TRUE.

Definition at line 48 of file spi_lld.h.

#define STM8S_SPI_ERROR_HOOK (   spip)    chSysHalt()

Overflow error hook.

The default action is to stop the system.

Definition at line 56 of file spi_lld.h.

Referenced by CH_IRQ_HANDLER().


Typedef Documentation

typedef struct SPIDriver SPIDriver

Type of a structure representing an SPI driver.

Definition at line 74 of file spi_lld.h.

typedef void(* spicallback_t)(SPIDriver *spip)

SPI notification callback type.

Parameters:
[in]spippointer to the SPIDriver object triggering the callback

Definition at line 82 of file spi_lld.h.


Enumeration Type Documentation

enum spistate_t

Driver state machine possible states.

Enumerator:
SPI_UNINIT 

Not initialized.

SPI_STOP 

Stopped.

SPI_READY 

Ready.

SPI_ACTIVE 

Exchanging data.

SPI_COMPLETE 

Asynchronous operation complete.

Definition at line 78 of file spi.h.