ChibiOS  21.6.0
SPI Driver

Generic SPI Driver. More...

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_18.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.

Macros

#define SPI_SUPPORTS_CIRCULAR   TRUE
 Circular mode support flag. More...
 
#define spi_lld_driver_fields
 Low level fields of the SPI driver structure. More...
 
#define spi_lld_config_fields
 Low level fields of the SPI configuration structure. More...
 

Chip Select modes

#define SPI_SELECT_MODE_NONE
 
#define SPI_SELECT_MODE_PAD   1 /** @brief Legacy mode. */
 
#define SPI_SELECT_MODE_PORT   2 /** @brief Fastest mode. */
 
#define SPI_SELECT_MODE_LINE   3 /** @brief Packed mode. */
 
#define SPI_SELECT_MODE_LLD   4 /** @brief LLD-defined mode.*/
 

SPI configuration options

#define SPI_USE_WAIT   TRUE
 Enables synchronous APIs. More...
 
#define SPI_USE_CIRCULAR   FALSE
 Enables circular transfers APIs. More...
 
#define SPI_USE_MUTUAL_EXCLUSION   TRUE
 Enables the spiAcquireBus() and spiReleaseBus() APIs. More...
 
#define SPI_SELECT_MODE   SPI_SELECT_MODE_PAD
 Handling method for SPI CS line. More...
 

Macro Functions

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

Low level driver helper macros

#define _spi_wakeup_isr(spip)
 Wakes up the waiting thread. More...
 
#define _spi_isr_code(spip)
 Common ISR code when circular mode is not supported. More...
 
#define _spi_isr_half_code(spip)
 Half buffer filled ISR code in circular mode. More...
 
#define _spi_isr_full_code(spip)
 Full buffer filled ISR code in circular mode. More...
 

PLATFORM configuration options

#define PLATFORM_SPI_USE_SPI1   FALSE
 SPI1 driver enable switch. More...
 

Typedefs

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

Data Structures

struct  hal_spi_config
 Driver configuration structure. More...
 
struct  hal_spi_driver
 Structure representing an SPI driver. More...
 

Functions

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

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...
 

Variables

SPIDriver SPID1
 SPI1 driver identifier. More...
 

Macro Definition Documentation

◆ SPI_USE_WAIT

#define SPI_USE_WAIT   TRUE

Enables synchronous APIs.

Note
Disabling this option saves both code and data space.

Definition at line 58 of file hal_spi.h.

◆ SPI_USE_CIRCULAR

#define SPI_USE_CIRCULAR   FALSE

Enables circular transfers APIs.

Note
Disabling this option saves both code and data space.

Definition at line 66 of file hal_spi.h.

◆ SPI_USE_MUTUAL_EXCLUSION

#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 74 of file hal_spi.h.

◆ SPI_SELECT_MODE

#define SPI_SELECT_MODE   SPI_SELECT_MODE_PAD

Handling method for SPI CS line.

Note
Disabling this option saves both code and data space.

Definition at line 82 of file hal_spi.h.

◆ spiIsBufferComplete

#define spiIsBufferComplete (   spip)    ((bool)((spip)->state == SPI_COMPLETE))

Buffer state.

Note
This function is meant to be called from the SPI callback only.
Parameters
[in]spippointer to the SPIDriver object
Returns
The buffer state.
Return values
falseif the driver filled/sent the first half of the buffer.
trueif the driver filled/sent the second half of the buffer.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 239 of file hal_spi.h.

◆ spiSelectI

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

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 249 of file hal_spi.h.

◆ spiUnselectI

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

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 262 of file hal_spi.h.

◆ spiStartIgnoreI

#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 319 of file hal_spi.h.

◆ spiStartExchangeI

#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 341 of file hal_spi.h.

◆ spiStartSendI

#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 361 of file hal_spi.h.

◆ spiStartReceiveI

#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 381 of file hal_spi.h.

◆ spiPolledExchange

#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 399 of file hal_spi.h.

◆ _spi_wakeup_isr

#define _spi_wakeup_isr (   spip)
Value:
{ \
osalSysLockFromISR(); \
osalThreadResumeI(&(spip)->thread, MSG_OK); \
osalSysUnlockFromISR(); \
}

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 414 of file hal_spi.h.

◆ _spi_isr_code

#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 when circular mode is not supported.

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 437 of file hal_spi.h.

◆ _spi_isr_half_code

#define _spi_isr_half_code (   spip)
Value:
{ \
if ((spip)->config->end_cb) { \
(spip)->config->end_cb(spip); \
} \
}

Half buffer filled ISR code in circular mode.

This code handles the portable part of the ISR code:

  • Callback invocation.
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 461 of file hal_spi.h.

◆ _spi_isr_full_code

#define _spi_isr_full_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_ACTIVE; \
} \
}

Full buffer filled ISR code in circular mode.

This code handles the portable part of the ISR code:

  • Callback invocation.
  • 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 480 of file hal_spi.h.

◆ SPI_SUPPORTS_CIRCULAR

#define SPI_SUPPORTS_CIRCULAR   TRUE

Circular mode support flag.

Definition at line 37 of file hal_spi_lld.h.

◆ PLATFORM_SPI_USE_SPI1

#define PLATFORM_SPI_USE_SPI1   FALSE

SPI1 driver enable switch.

If set to TRUE the support for SPI1 is included.

Note
The default is FALSE.

Definition at line 53 of file hal_spi_lld.h.

◆ spi_lld_driver_fields

#define spi_lld_driver_fields
Value:
/* Dummy field, it is not needed.*/ \
uint32_t dummy

Low level fields of the SPI driver structure.

Definition at line 72 of file hal_spi_lld.h.

◆ spi_lld_config_fields

#define spi_lld_config_fields
Value:
/* Dummy configuration, it is not needed.*/ \
uint32_t dummy

Low level fields of the SPI configuration structure.

Definition at line 79 of file hal_spi_lld.h.

Typedef Documentation

◆ SPIDriver

typedef struct hal_spi_driver SPIDriver

Type of a structure representing an SPI driver.

Definition at line 125 of file hal_spi.h.

◆ SPIConfig

typedef struct hal_spi_config SPIConfig

Type of a SPI driver configuration structure.

Definition at line 129 of file hal_spi.h.

◆ spicallback_t

typedef void(* spicallback_t) (SPIDriver *spip)

SPI notification callback type.

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

Definition at line 137 of file hal_spi.h.

Enumeration Type Documentation

◆ spistate_t

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 114 of file hal_spi.h.

Function Documentation

◆ spiInit()

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 56 of file hal_spi.c.

References spi_lld_init().

Referenced by halInit().

Here is the call graph for this function:

◆ spiObjectInit()

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 68 of file hal_spi.c.

References hal_spi_driver::config, hal_spi_driver::mutex, osalMutexObjectInit(), SPI_STOP, hal_spi_driver::state, and hal_spi_driver::thread.

Referenced by spi_lld_init().

Here is the call graph for this function:

◆ spiStart()

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 91 of file hal_spi.c.

References hal_spi_driver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), spi_lld_start(), SPI_READY, SPI_STOP, and hal_spi_driver::state.

Referenced by acc_read_raw(), gyro_read_raw(), l3gd20Stop(), lis302dlStop(), lis3dshStop(), mmcConnect(), mmcDisconnect(), mmcStartSequentialRead(), mmcStartSequentialWrite(), and mmcSync().

Here is the call graph for this function:

◆ spiStop()

void spiStop ( SPIDriver spip)

Deactivates the SPI peripheral.

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 111 of file hal_spi.c.

References hal_spi_driver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), spi_lld_stop(), SPI_READY, SPI_STOP, and hal_spi_driver::state.

Referenced by mmcDisconnect(), mmcSequentialWrite(), and mmcStop().

Here is the call graph for this function:

◆ spiSelect()

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 134 of file hal_spi.c.

References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiSelectI, and hal_spi_driver::state.

Referenced by lis3dshSPIWriteRegister(), mmcStartSequentialRead(), mmcStartSequentialWrite(), read_CxD(), send_command_R1(), send_command_R3(), and sync().

Here is the call graph for this function:

◆ spiUnselect()

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 152 of file hal_spi.c.

References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiUnselectI, and hal_spi_driver::state.

Referenced by lis3dshSPIWriteRegister(), mmcSequentialWrite(), mmcStopSequentialWrite(), read_CxD(), send_command_R1(), send_command_R3(), and sync().

Here is the call graph for this function:

◆ spiStartIgnore()

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 175 of file hal_spi.c.

References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartIgnoreI, and hal_spi_driver::state.

Here is the call graph for this function:

◆ spiStartExchange()

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 202 of file hal_spi.c.

References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartExchangeI, and hal_spi_driver::state.

Here is the call graph for this function:

◆ spiStartSend()

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 229 of file hal_spi.c.

References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartSendI, and hal_spi_driver::state.

Here is the call graph for this function:

◆ spiStartReceive()

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 254 of file hal_spi.c.

References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartReceiveI, and hal_spi_driver::state.

Here is the call graph for this function:

◆ spiAbortI()

void spiAbortI ( SPIDriver spip)

Aborts the ongoing SPI operation.

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 272 of file hal_spi.c.

References MSG_OK, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, osalThreadResumeI(), SPI_ACTIVE, SPI_COMPLETE, spi_lld_abort(), SPI_READY, hal_spi_driver::state, and hal_spi_driver::thread.

Referenced by spiAbort().

Here is the call graph for this function:

◆ spiAbort()

void spiAbort ( SPIDriver spip)

Aborts the ongoing SPI operation, if any.

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 294 of file hal_spi.c.

References osalDbgAssert, osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), SPI_ACTIVE, SPI_READY, spiAbortI(), and hal_spi_driver::state.

Here is the call graph for this function:

◆ spiIgnore()

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 322 of file hal_spi.c.

References hal_spi_config::circular, hal_spi_driver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartIgnoreI, hal_spi_driver::state, and hal_spi_driver::thread.

Referenced by mmcConnect(), and mmcSequentialWrite().

Here is the call graph for this function:

◆ spiExchange()

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 354 of file hal_spi.c.

References hal_spi_config::circular, hal_spi_driver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartExchangeI, hal_spi_driver::state, and hal_spi_driver::thread.

Here is the call graph for this function:

◆ spiSend()

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 386 of file hal_spi.c.

References hal_spi_config::circular, hal_spi_driver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartSendI, hal_spi_driver::state, and hal_spi_driver::thread.

Referenced by lis3dshSPIWriteRegister(), mmcSequentialWrite(), mmcStopSequentialWrite(), and send_hdr().

Here is the call graph for this function:

◆ spiReceive()

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 416 of file hal_spi.c.

References hal_spi_config::circular, hal_spi_driver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartReceiveI, hal_spi_driver::state, and hal_spi_driver::thread.

Referenced by mmcSequentialWrite(), recvr1(), recvr3(), sync(), and wait().

Here is the call graph for this function:

◆ spiAcquireBus()

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 443 of file hal_spi.c.

References hal_spi_driver::mutex, osalDbgCheck, and osalMutexLock().

Referenced by acc_read_raw(), gyro_read_raw(), l3gd20Stop(), lis302dlStop(), and lis3dshStop().

Here is the call graph for this function:

◆ spiReleaseBus()

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 459 of file hal_spi.c.

References hal_spi_driver::mutex, osalDbgCheck, and osalMutexUnlock().

Here is the call graph for this function:

◆ spi_lld_init()

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 65 of file hal_spi_lld.c.

References SPID1, and spiObjectInit().

Referenced by spiInit().

Here is the call graph for this function:

◆ spi_lld_start()

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 80 of file hal_spi_lld.c.

References SPI_STOP, SPID1, and hal_spi_driver::state.

Referenced by spiStart().

◆ spi_lld_stop()

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 101 of file hal_spi_lld.c.

References SPI_READY, SPID1, and hal_spi_driver::state.

Referenced by spiStop().

◆ spi_lld_select()

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 120 of file hal_spi_lld.c.

◆ spi_lld_unselect()

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 134 of file hal_spi_lld.c.

◆ spi_lld_ignore()

void spi_lld_ignore ( 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.

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:
Not an API, this function is for internal use only.

Definition at line 151 of file hal_spi_lld.c.

◆ spi_lld_exchange()

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 173 of file hal_spi_lld.c.

◆ spi_lld_send()

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 196 of file hal_spi_lld.c.

◆ spi_lld_receive()

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 217 of file hal_spi_lld.c.

◆ spi_lld_abort()

void spi_lld_abort ( SPIDriver spip)

Aborts the ongoing SPI operation, if any.

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

Definition at line 233 of file hal_spi_lld.c.

Referenced by spiAbortI().

◆ spi_lld_polled_exchange()

uint16_t spi_lld_polled_exchange ( SPIDriver spip,
uint16_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.
Function Class:
Not an API, this function is for internal use only.

Definition at line 253 of file hal_spi_lld.c.

Variable Documentation

◆ SPID1

SPIDriver SPID1

SPI1 driver identifier.

Definition at line 41 of file hal_spi_lld.c.

Referenced by spi_lld_init(), spi_lld_start(), and spi_lld_stop().

SPI_READY
@ SPI_READY
Definition: hal_spi.h:117
SPI_ACTIVE
@ SPI_ACTIVE
Definition: hal_spi.h:118
MSG_OK
#define MSG_OK
Normal wakeup message.
Definition: chschd.h:39
SPI_COMPLETE
@ SPI_COMPLETE
Definition: hal_spi.h:119