Loading...
Searching...
No Matches

SPI abstraction layer RIOT adaption. More...

Detailed Description

SPI abstraction layer RIOT adaption.

Author
Francisco Molina franc.nosp@m.ois-.nosp@m.xavie.nosp@m.r.mo.nosp@m.lina@.nosp@m.inri.nosp@m.a.fr

Definition in file hal_spi.h.

#include "periph/spi.h"
+ Include dependency graph for hal_spi.h:

Go to the source code of this file.

Data Structures

struct  hal_spi_settings
 since one spi device can control multiple devices, some configuration can be changed on the fly from the hal More...
 

Macros

#define HAL_SPI_MODE0   (SPI_MODE_0)
 SPI mode 0.
 
#define HAL_SPI_MODE1   (SPI_MODE_1)
 SPI mode 1.
 
#define HAL_SPI_MODE2   (SPI_MODE_2)
 SPI mode 2.
 
#define HAL_SPI_MODE3   (SPI_MODE_3)
 SPI mode 3.
 

Typedefs

typedef void(* hal_spi_txrx_cb) (void *arg, int len)
 Prototype for tx/rx callback.
 

Functions

int hal_spi_config (int spi_num, struct hal_spi_settings *psettings)
 Configure the spi.
 
int hal_spi_set_txrx_cb (int spi_num, hal_spi_txrx_cb txrx_cb, void *arg)
 Sets the txrx callback (executed at interrupt context) when the buffer is transferred by the master or the slave using the non-blocking API.
 
int hal_spi_enable (int spi_num)
 Enables the SPI.
 
int hal_spi_disable (int spi_num)
 Disables the SPI.
 
int hal_spi_txrx (int spi_num, void *txbuf, void *rxbuf, int cnt)
 Blocking interface to send a buffer and store the received values from the slave.
 
int hal_spi_txrx_noblock (int spi_num, void *txbuf, void *rxbuf, int cnt)
 Non-blocking interface to send a buffer and store received values.
 

Macro Definition Documentation

◆ HAL_SPI_MODE0

#define HAL_SPI_MODE0   (SPI_MODE_0)

SPI mode 0.

Definition at line 30 of file hal_spi.h.

◆ HAL_SPI_MODE1

#define HAL_SPI_MODE1   (SPI_MODE_1)

SPI mode 1.

Definition at line 32 of file hal_spi.h.

◆ HAL_SPI_MODE2

#define HAL_SPI_MODE2   (SPI_MODE_2)

SPI mode 2.

Definition at line 34 of file hal_spi.h.

◆ HAL_SPI_MODE3

#define HAL_SPI_MODE3   (SPI_MODE_3)

SPI mode 3.

Definition at line 36 of file hal_spi.h.

Typedef Documentation

◆ hal_spi_txrx_cb

typedef void(* hal_spi_txrx_cb) (void *arg, int len)

Prototype for tx/rx callback.

Definition at line 41 of file hal_spi.h.

Function Documentation

◆ hal_spi_config()

int hal_spi_config ( int  spi_num,
struct hal_spi_settings psettings 
)

Configure the spi.

Must be called after the spi is initialized (after hal_spi_init is called) and when the spi is disabled (user must call hal_spi_disable if the spi has been enabled through hal_spi_enable prior to calling this function). Can also be used to reconfigure an initialized SPI (assuming it is disabled as described previously).

Parameters
spi_numThe number of the SPI to configure.
psettingsThe settings to configure this SPI with
Returns
int 0 on success, non-zero error code on failure.

◆ hal_spi_disable()

int hal_spi_disable ( int  spi_num)

Disables the SPI.

Used for power mgmt. It will halt any current SPI transfers in progress.

Parameters
spi_num
Returns
int 0 on success, non-zero error code on failure.

◆ hal_spi_enable()

int hal_spi_enable ( int  spi_num)

Enables the SPI.

This does not start a transmit or receive operation; it is used for power mgmt. Cannot be called when a SPI transfer is in progress.

Parameters
spi_num
Returns
int 0 on success, non-zero error code on failure.

◆ hal_spi_set_txrx_cb()

int hal_spi_set_txrx_cb ( int  spi_num,
hal_spi_txrx_cb  txrx_cb,
void *  arg 
)

Sets the txrx callback (executed at interrupt context) when the buffer is transferred by the master or the slave using the non-blocking API.

Cannot be called when the spi is enabled. This callback will also be called when chip select is de-asserted on the slave.

NOTE: This callback is only used for the non-blocking interface and must be called prior to using the non-blocking API.

Parameters
spi_numSPI interface on which to set callback
txrx_cbCallback function
argArgument to be passed to callback function
Returns
int 0 on success, non-zero error code on failure.

◆ hal_spi_txrx()

int hal_spi_txrx ( int  spi_num,
void *  txbuf,
void *  rxbuf,
int  cnt 
)

Blocking interface to send a buffer and store the received values from the slave.

The transmit and receive buffers are either arrays of 8-bit (uint8_t) values or 16-bit values depending on whether the spi is configured for 8 bit data or more than 8 bits per value. The 'cnt' parameter is the number of 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).

NOTE: these buffers are in the native endian-ness of the platform.

MASTER: master sends all the values in the buffer and stores the
        stores the values in the receive buffer if rxbuf is not NULL.
        The txbuf parameter cannot be NULL.
SLAVE: cannot be called for a slave; returns -1
Parameters
spi_numSPI interface to use
txbufPointer to buffer where values to transmit are stored.
rxbufPointer to buffer to store values received from peer.
cntNumber of 8-bit or 16-bit values to be transferred.
Returns
int 0 on success, non-zero error code on failure.

◆ hal_spi_txrx_noblock()

int hal_spi_txrx_noblock ( int  spi_num,
void *  txbuf,
void *  rxbuf,
int  cnt 
)

Non-blocking interface to send a buffer and store received values.

Can be used for both master and slave SPI types. The user must configure the callback (using hal_spi_set_txrx_cb); the txrx callback is executed at interrupt context when the buffer is sent.

The transmit and receive buffers are either arrays of 8-bit (uint8_t) values or 16-bit values depending on whether the spi is configured for 8 bit data or more than 8 bits per value. The 'cnt' parameter is the number of 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).

NOTE: these buffers are in the native endian-ness of the platform.

MASTER: master sends all the values in the buffer and stores the
        stores the values in the receive buffer if rxbuf is not NULL.
        The txbuf parameter cannot be NULL
SLAVE: Slave "preloads" the data to be sent to the master (values
       stored in txbuf) and places received data from master in rxbuf
       (if not NULL). The txrx callback occurs when len values are
       transferred or master de-asserts chip select. If txbuf is NULL,
       the slave transfers its default byte. Both rxbuf and txbuf cannot
       be NULL.
Parameters
spi_numSPI interface to use
txbufPointer to buffer where values to transmit are stored.
rxbufPointer to buffer to store values received from peer.
cntNumber of 8-bit or 16-bit values to be transferred.
Returns
int 0 on success, non-zero error code on failure.