zephyr/include/devicetree/spi.h
Martí Bolívar c3ec5db6d2 devicetree.h: add accessor API for nodes and properties
This is joint work with Kumar Gala (see signed-off-by).

Add helper macros which abstract the "true names" of each of the four
types of node identifier we intend to support (e.g. DT_ALIAS(),
DT_INST()).

These can be passed to a new DT_PROP() macro which can be used to read
the value of a devicetree property given a node identifier from one of
these four other macros, and the as-a-c-token name of the property.
Add other accessor macros and tests as well.

Add some convenience APIs for writing device drivers based on instance
numbers as well. Drivers can "#define DT_DRV_COMPAT driver_compatible"
at the top of the file, then utilize these DT_INST_* macros to access
various property defines.

For example, the uart_sifive driver can do:

  #define DT_DRV_COMPAT sifive_uart0

Then use DT_INST macros like:

  .port         = DT_INST_REG_ADDR(0),
  .sys_clk_freq = DT_INST_PROP(0, clock_frequency),

For convenience working with specific hardware, also add:

  <devicetree/gpio.h>
  <devicetree/adc.h>
  <devicetree/spi.h>

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-03-24 10:11:20 -05:00

122 lines
3.8 KiB
C

/**
* @file
* @brief SPI Devicetree macro public API header file.
*/
/*
* Copyright (c) 2020 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DEVICETREE_SPI_H_
#define ZEPHYR_INCLUDE_DEVICETREE_SPI_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup devicetree-spi Devicetree SPI API
* @{
*/
/**
* @brief Does a SPI controller have chip select GPIOs configured?
* The commonly used "cs-gpios" property is used for this test.
* @param spi node identifier for a SPI bus controller
* @return 1 if it has a cs-gpios property, 0 otherwise
*/
#define DT_SPI_HAS_CS(spi) DT_NODE_HAS_PROP(spi, cs_gpios)
/**
* @brief The number of chip select GPIOs in a SPI controller
* @param spi node identifier for a SPI bus controller
* @return The length of its cs-gpios, or 0 if it doesn't have one
*/
#define DT_SPI_NUM_CS(spi) \
COND_CODE_1(DT_SPI_HAS_CS(spi), \
(DT_PROP_LEN(spi, cs_gpios)), (0))
/**
* @brief Does a SPI device have a chip select line in DT?
* @param spi_dev node identifier for a SPI device
* @return 1 if the SPI device's bus DT_BUS(spi_dev) has a CS
* pin at index DT_REG_ADDR(spi_dev), 0 otherwise
*/
#define DT_SPI_DEV_HAS_CS(spi_dev) DT_SPI_HAS_CS(DT_BUS(spi_dev))
/**
* @brief Get GPIO controller name for a SPI device's chip select
* DT_SPI_DEV_HAS_CS(spi_dev) must expand to 1.
* @brief spi_dev a SPI device node identifier
* @return label property of spi_dev's chip select GPIO controller
*/
#define DT_SPI_DEV_CS_GPIO_LABEL(spi_dev) \
DT_GPIO_LABEL_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
/**
* @brief Get GPIO specifier 'pin' value for a SPI device's chip select
* It's an error if the GPIO specifier for spi_dev's entry in its
* bus node's cs-gpios property has no 'pin' value.
* @brief spi_dev a SPI device node identifier
* @return pin number of spi_dev's chip select GPIO
*/
#define DT_SPI_DEV_CS_GPIO_PIN(spi_dev) \
DT_GPIO_PIN_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
/**
* @brief Get GPIO specifier 'flags' value for a SPI device's chip select
* It's an error if the GPIO specifier for spi_dev's entry in its
* bus node's cs-gpios property has no 'flags' value.
* @brief spi_dev a SPI device node identifier
* @return flags value of spi_dev's chip select GPIO specifier
*/
#define DT_SPI_DEV_CS_GPIO_FLAGS(spi_dev) \
DT_GPIO_FLAGS_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
/**
* @brief Equivalent to DT_SPI_DEV_HAS_CS(DT_DRV_INST(inst))
* @param inst instance number
* @return 1 if the instance's bus has a CS pin at index
* DT_INST_REG_ADDR(inst), 0 otherwise
*/
#define DT_INST_SPI_DEV_HAS_CS(inst) DT_SPI_DEV_HAS_CS(DT_DRV_INST(inst))
/**
* @brief Get GPIO controller name for a SPI device instance
* This is equivalent to DT_SPI_DEV_CS_GPIO_LABEL(DT_DRV_INST(inst)).
* @brief inst instance number
* @return label property of the instance's chip select GPIO controller
*/
#define DT_INST_SPI_DEV_CS_GPIO_LABEL(inst) \
DT_SPI_DEV_CS_GPIO_LABEL(DT_DRV_INST(inst))
/**
* @brief Get GPIO specifier "pin" value for a SPI device instance
* This is equivalent to DT_SPI_DEV_CS_GPIO_PIN(DT_DRV_INST(inst)).
* @brief inst a SPI device instance number
* @return pin number of the instance's chip select GPIO
*/
#define DT_INST_SPI_DEV_CS_GPIO_PIN(inst) \
DT_SPI_DEV_CS_GPIO_PIN(DT_DRV_INST(inst))
/**
* @brief Get GPIO specifier "flags" value for a SPI device instance
* This is equivalent to DT_SPI_DEV_CS_GPIO_FLAGS(DT_DRV_INST(inst)).
* @brief inst a SPI device instance number
* @return flags value of the instance's chip select GPIO specifier
*/
#define DT_INST_SPI_DEV_CS_GPIO_FLAGS(inst) \
DT_SPI_DEV_CS_GPIO_FLAGS(DT_DRV_INST(inst))
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_DEVICETREE_SPI_H_ */