spi: struct spi_dt_spec helpers

Add helper functions to simplify the usage of `struct spi_dt_spec`.
Implements helpers for the standard synchronous calls (transceive, read,
write).

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-06-23 22:16:00 +10:00 committed by Anas Nashif
commit 2946a535a0

View file

@ -473,6 +473,28 @@ static inline int z_impl_spi_transceive(const struct device *dev,
return api->transceive(dev, config, tx_bufs, rx_bufs); return api->transceive(dev, config, tx_bufs, rx_bufs);
} }
/**
* @brief Read/write data from an SPI bus specified in @p spi_dt_spec.
*
* This is equivalent to:
*
* spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
*
* @param spec SPI specification from devicetree
* @param tx_bufs Buffer array where data to be sent originates from,
* or NULL if none.
* @param rx_bufs Buffer array where data to be read will be written to,
* or NULL if none.
*
* @retval a value from spi_transceive()
*/
static inline int spi_transceive_dt(const struct spi_dt_spec *spec,
const struct spi_buf_set *tx_bufs,
const struct spi_buf_set *rx_bufs)
{
return spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
}
/** /**
* @brief Read the specified amount of data from the SPI driver. * @brief Read the specified amount of data from the SPI driver.
* *
@ -495,6 +517,24 @@ static inline int spi_read(const struct device *dev,
return spi_transceive(dev, config, NULL, rx_bufs); return spi_transceive(dev, config, NULL, rx_bufs);
} }
/**
* @brief Read data from a SPI bus specified in @p spi_dt_spec.
*
* This is equivalent to:
*
* spi_read(spec->bus, &spec->config, rx_bufs);
*
* @param spec SPI specification from devicetree
* @param rx_bufs Buffer array where data to be read will be written to.
*
* @retval a value from spi_read()
*/
static inline int spi_read_dt(const struct spi_dt_spec *spec,
const struct spi_buf_set *rx_bufs)
{
return spi_read(spec->bus, &spec->config, rx_bufs);
}
/** /**
* @brief Write the specified amount of data from the SPI driver. * @brief Write the specified amount of data from the SPI driver.
* *
@ -517,6 +557,24 @@ static inline int spi_write(const struct device *dev,
return spi_transceive(dev, config, tx_bufs, NULL); return spi_transceive(dev, config, tx_bufs, NULL);
} }
/**
* @brief Write data to a SPI bus specified in @p spi_dt_spec.
*
* This is equivalent to:
*
* spi_write(spec->bus, &spec->config, tx_bufs);
*
* @param spec SPI specification from devicetree
* @param tx_bufs Buffer array where data to be sent originates from.
*
* @retval a value from spi_write()
*/
static inline int spi_write_dt(const struct spi_dt_spec *spec,
const struct spi_buf_set *tx_bufs)
{
return spi_write(spec->bus, &spec->config, tx_bufs);
}
/* Doxygen defines this so documentation is generated. */ /* Doxygen defines this so documentation is generated. */
#ifdef CONFIG_SPI_ASYNC #ifdef CONFIG_SPI_ASYNC