diff --git a/drivers/bluetooth/hci/spi.c b/drivers/bluetooth/hci/spi.c index ca9c78f033a..72502215402 100644 --- a/drivers/bluetooth/hci/spi.c +++ b/drivers/bluetooth/hci/spi.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include diff --git a/drivers/can/can_mcp2515.c b/drivers/can/can_mcp2515.c index 904bda7a187..1f7fa75afe9 100644 --- a/drivers/can/can_mcp2515.c +++ b/drivers/can/can_mcp2515.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include #define LOG_LEVEL CONFIG_CAN_LOG_LEVEL diff --git a/drivers/display/display_ili9340.c b/drivers/display/display_ili9340.c index bb06c6f5082..149cf75ff4b 100644 --- a/drivers/display/display_ili9340.c +++ b/drivers/display/display_ili9340.c @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(display_ili9340); #include #include -#include +#include #include struct ili9340_data { diff --git a/drivers/display/ssd1673.c b/drivers/display/ssd1673.c index 477121a0f9f..df732711c5e 100644 --- a/drivers/display/ssd1673.c +++ b/drivers/display/ssd1673.c @@ -13,7 +13,7 @@ LOG_MODULE_REGISTER(ssd1673); #include #include #include -#include +#include #include #include "ssd1673_regs.h" diff --git a/drivers/ethernet/eth_enc28j60.c b/drivers/ethernet/eth_enc28j60.c index 2c9c6cc8845..23b5b6b4386 100644 --- a/drivers/ethernet/eth_enc28j60.c +++ b/drivers/ethernet/eth_enc28j60.c @@ -16,7 +16,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #include #include #include -#include +#include #include #include #include diff --git a/drivers/flash/spi_flash_w25qxxdv.c b/drivers/flash/spi_flash_w25qxxdv.c index ff44659621a..254686ec1dc 100644 --- a/drivers/flash/spi_flash_w25qxxdv.c +++ b/drivers/flash/spi_flash_w25qxxdv.c @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include "spi_flash_w25qxxdv_defs.h" diff --git a/drivers/flash/spi_nor.c b/drivers/flash/spi_nor.c index 30f2d4ae81b..db5f292e265 100644 --- a/drivers/flash/spi_nor.c +++ b/drivers/flash/spi_nor.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include "spi_nor.h" diff --git a/drivers/ieee802154/ieee802154_cc1200.c b/drivers/ieee802154/ieee802154_cc1200.c index fefecbbb382..7e9c545996f 100644 --- a/drivers/ieee802154/ieee802154_cc1200.c +++ b/drivers/ieee802154/ieee802154_cc1200.c @@ -26,7 +26,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #include #include -#include +#include #include #include diff --git a/drivers/ieee802154/ieee802154_cc1200.h b/drivers/ieee802154/ieee802154_cc1200.h index 697d7c43352..c4a588c327b 100644 --- a/drivers/ieee802154/ieee802154_cc1200.h +++ b/drivers/ieee802154/ieee802154_cc1200.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include diff --git a/drivers/ieee802154/ieee802154_cc2520.h b/drivers/ieee802154/ieee802154_cc2520.h index 6f68f0b7b39..4a589c5655b 100644 --- a/drivers/ieee802154/ieee802154_cc2520.h +++ b/drivers/ieee802154/ieee802154_cc2520.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include diff --git a/drivers/ieee802154/ieee802154_mcr20a.h b/drivers/ieee802154/ieee802154_mcr20a.h index 226bdbb24ed..60c3090ba5b 100644 --- a/drivers/ieee802154/ieee802154_mcr20a.h +++ b/drivers/ieee802154/ieee802154_mcr20a.h @@ -12,7 +12,7 @@ #include #include -#include +#include /* Runtime context structure *************************** diff --git a/drivers/led_strip/apa102.c b/drivers/led_strip/apa102.c index b94d9d1ebdb..23ebd2c1953 100644 --- a/drivers/led_strip/apa102.c +++ b/drivers/led_strip/apa102.c @@ -6,7 +6,7 @@ #include #include -#include +#include struct apa102_data { struct device *spi; diff --git a/drivers/led_strip/lpd880x.c b/drivers/led_strip/lpd880x.c index 8456d9333ce..9aedd32f915 100644 --- a/drivers/led_strip/lpd880x.c +++ b/drivers/led_strip/lpd880x.c @@ -29,7 +29,7 @@ LOG_MODULE_REGISTER(lpd880x); #include #include -#include +#include #include /* diff --git a/drivers/led_strip/ws2812.c b/drivers/led_strip/ws2812.c index 1d7d4a0dcde..0c43662c536 100644 --- a/drivers/led_strip/ws2812.c +++ b/drivers/led_strip/ws2812.c @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(ws2812); #include #include -#include +#include #include /* diff --git a/drivers/sensor/adxl362/adxl362.c b/drivers/sensor/adxl362/adxl362.c index 659a9e27e9b..03ac0b5051c 100644 --- a/drivers/sensor/adxl362/adxl362.c +++ b/drivers/sensor/adxl362/adxl362.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include "adxl362.h" diff --git a/drivers/sensor/adxl362/adxl362.h b/drivers/sensor/adxl362/adxl362.h index 97621b5f1d0..848ec875629 100644 --- a/drivers/sensor/adxl362/adxl362.h +++ b/drivers/sensor/adxl362/adxl362.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #define ADXL362_SLAVE_ID 1 diff --git a/drivers/sensor/adxl372/adxl372.c b/drivers/sensor/adxl372/adxl372.c index 0e3c0b842b2..eb2be036ba3 100644 --- a/drivers/sensor/adxl372/adxl372.c +++ b/drivers/sensor/adxl372/adxl372.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include diff --git a/drivers/sensor/adxl372/adxl372.h b/drivers/sensor/adxl372/adxl372.h index 7e0960a1176..1cfa39449a7 100644 --- a/drivers/sensor/adxl372/adxl372.h +++ b/drivers/sensor/adxl372/adxl372.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/drivers/sensor/bme280/bme280.c b/drivers/sensor/bme280/bme280.c index 65faf0554eb..f626d9baa2c 100644 --- a/drivers/sensor/bme280/bme280.c +++ b/drivers/sensor/bme280/bme280.c @@ -17,7 +17,7 @@ #ifdef DT_BOSCH_BME280_BUS_I2C #include #elif defined DT_BOSCH_BME280_BUS_SPI -#include +#include #endif #include diff --git a/drivers/sensor/bmi160/bmi160.h b/drivers/sensor/bmi160/bmi160.h index 1dedafab790..7825c1e8616 100644 --- a/drivers/sensor/bmi160/bmi160.h +++ b/drivers/sensor/bmi160/bmi160.h @@ -9,7 +9,7 @@ #define ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_ #include -#include +#include #include /* registers */ diff --git a/drivers/sensor/lis2dh/lis2dh.h b/drivers/sensor/lis2dh/lis2dh.h index 732f82535e5..768c101239f 100644 --- a/drivers/sensor/lis2dh/lis2dh.h +++ b/drivers/sensor/lis2dh/lis2dh.h @@ -19,7 +19,7 @@ #define LIS2DH_BUS_DEV_NAME DT_INST_0_ST_LIS2DH_BUS_NAME #if defined(DT_ST_LIS2DH_BUS_SPI) -#include +#include #define LIS2DH_SPI_READ_BIT BIT(7) #define LIS2DH_SPI_AUTOINC_ADDR BIT(6) diff --git a/drivers/sensor/lis2ds12/lis2ds12_spi.c b/drivers/sensor/lis2ds12/lis2ds12_spi.c index f3499d8fd4c..080e8abbc9b 100644 --- a/drivers/sensor/lis2ds12/lis2ds12_spi.c +++ b/drivers/sensor/lis2ds12/lis2ds12_spi.c @@ -10,7 +10,7 @@ #include -#include +#include #include "lis2ds12.h" #include diff --git a/drivers/sensor/lis2dw12/lis2dw12.c b/drivers/sensor/lis2dw12/lis2dw12.c index 9bda0739d8e..f18c398a81a 100644 --- a/drivers/sensor/lis2dw12/lis2dw12.c +++ b/drivers/sensor/lis2dw12/lis2dw12.c @@ -15,7 +15,7 @@ #include #if defined(DT_ST_LIS2DW12_BUS_SPI) -#include +#include #elif defined(DT_ST_LIS2DW12_BUS_I2C) #include #endif diff --git a/drivers/sensor/lis2dw12/lis2dw12.h b/drivers/sensor/lis2dw12/lis2dw12.h index b3f9f33c056..7ba36e140df 100644 --- a/drivers/sensor/lis2dw12/lis2dw12.h +++ b/drivers/sensor/lis2dw12/lis2dw12.h @@ -11,7 +11,7 @@ #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ #define ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ -#include +#include #include #include #include diff --git a/drivers/sensor/lsm6dsl/lsm6dsl_spi.c b/drivers/sensor/lsm6dsl/lsm6dsl_spi.c index 22e232f9b5b..db269f561d6 100644 --- a/drivers/sensor/lsm6dsl/lsm6dsl_spi.c +++ b/drivers/sensor/lsm6dsl/lsm6dsl_spi.c @@ -8,7 +8,7 @@ */ #include -#include +#include #include "lsm6dsl.h" #include diff --git a/drivers/spi/spi_cc13xx_cc26xx.c b/drivers/spi/spi_cc13xx_cc26xx.c index 46c257a568b..9496ba95211 100644 --- a/drivers/spi/spi_cc13xx_cc26xx.c +++ b/drivers/spi/spi_cc13xx_cc26xx.c @@ -8,7 +8,7 @@ #include LOG_MODULE_REGISTER(spi_cc13xx_cc26xx); -#include +#include #include #include diff --git a/drivers/spi/spi_context.h b/drivers/spi/spi_context.h index 2d01e8a0d02..3797cd62430 100644 --- a/drivers/spi/spi_context.h +++ b/drivers/spi/spi_context.h @@ -13,7 +13,7 @@ #define ZEPHYR_DRIVERS_SPI_SPI_CONTEXT_H_ #include -#include +#include #ifdef __cplusplus extern "C" { diff --git a/drivers/spi/spi_dw.c b/drivers/spi/spi_dw.c index 600ca20c3d0..336f46a7e38 100644 --- a/drivers/spi/spi_dw.c +++ b/drivers/spi/spi_dw.c @@ -40,7 +40,7 @@ LOG_MODULE_REGISTER(spi_dw); #include #endif -#include +#include #include "spi_dw.h" #include "spi_context.h" diff --git a/drivers/spi/spi_dw.h b/drivers/spi/spi_dw.h index cabea4509b2..51cb44c9b92 100644 --- a/drivers/spi/spi_dw.h +++ b/drivers/spi/spi_dw.h @@ -9,7 +9,7 @@ #ifndef ZEPHYR_DRIVERS_SPI_SPI_DW_H_ #define ZEPHYR_DRIVERS_SPI_SPI_DW_H_ -#include +#include #ifdef __cplusplus extern "C" { diff --git a/drivers/spi/spi_handlers.c b/drivers/spi/spi_handlers.c index 5644e5cfe71..63fcda356de 100644 --- a/drivers/spi/spi_handlers.c +++ b/drivers/spi/spi_handlers.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include diff --git a/drivers/spi/spi_intel.c b/drivers/spi/spi_intel.c index c19baf82c52..d90eddd22e8 100644 --- a/drivers/spi/spi_intel.c +++ b/drivers/spi/spi_intel.c @@ -23,7 +23,7 @@ LOG_MODULE_REGISTER(spi_intel); #include #include -#include +#include #include "spi_intel.h" #ifdef CONFIG_IOAPIC diff --git a/drivers/spi/spi_ll_stm32.c b/drivers/spi/spi_ll_stm32.c index 874d6ce52b6..0d72a7a5f58 100644 --- a/drivers/spi/spi_ll_stm32.c +++ b/drivers/spi/spi_ll_stm32.c @@ -12,7 +12,7 @@ LOG_MODULE_REGISTER(spi_ll_stm32); #include #include #include -#include +#include #include #include diff --git a/drivers/spi/spi_mcux_dspi.c b/drivers/spi/spi_mcux_dspi.c index d2a0238eae4..e65d371f26d 100644 --- a/drivers/spi/spi_mcux_dspi.c +++ b/drivers/spi/spi_mcux_dspi.c @@ -6,7 +6,7 @@ */ #include -#include +#include #include #include diff --git a/drivers/spi/spi_mcux_lpspi.c b/drivers/spi/spi_mcux_lpspi.c index 47b4ef60493..118712912f0 100644 --- a/drivers/spi/spi_mcux_lpspi.c +++ b/drivers/spi/spi_mcux_lpspi.c @@ -5,7 +5,7 @@ */ #include -#include +#include #include #include diff --git a/drivers/spi/spi_nrfx_spi.c b/drivers/spi/spi_nrfx_spi.c index df0d8d37264..27488b73e84 100644 --- a/drivers/spi/spi_nrfx_spi.c +++ b/drivers/spi/spi_nrfx_spi.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #define LOG_DOMAIN "spi_nrfx_spi" diff --git a/drivers/spi/spi_nrfx_spim.c b/drivers/spi/spi_nrfx_spim.c index 6230f9781ae..5708eea5c25 100644 --- a/drivers/spi/spi_nrfx_spim.c +++ b/drivers/spi/spi_nrfx_spim.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include diff --git a/drivers/spi/spi_nrfx_spis.c b/drivers/spi/spi_nrfx_spis.c index ea0e48607b9..f3c68260bda 100644 --- a/drivers/spi/spi_nrfx_spis.c +++ b/drivers/spi/spi_nrfx_spis.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #define LOG_DOMAIN "spi_nrfx_spis" diff --git a/drivers/spi/spi_sam.c b/drivers/spi/spi_sam.c index 5de188fbaff..54be46b6168 100644 --- a/drivers/spi/spi_sam.c +++ b/drivers/spi/spi_sam.c @@ -12,7 +12,7 @@ LOG_MODULE_REGISTER(spi_sam); #include "spi_context.h" #include #include -#include +#include #include #define SAM_SPI_CHIP_SELECT_COUNT 4 diff --git a/drivers/spi/spi_sam0.c b/drivers/spi/spi_sam0.c index c177d96dd77..3644532549f 100644 --- a/drivers/spi/spi_sam0.c +++ b/drivers/spi/spi_sam0.c @@ -11,7 +11,7 @@ LOG_MODULE_REGISTER(spi_sam0); #include "spi_context.h" #include #include -#include +#include #include #include diff --git a/drivers/spi/spi_sifive.h b/drivers/spi/spi_sifive.h index 7288f08e7ba..08b2ef192d2 100644 --- a/drivers/spi/spi_sifive.h +++ b/drivers/spi/spi_sifive.h @@ -11,7 +11,7 @@ #include #include -#include +#include #define SPI_CFG(dev) ((struct spi_sifive_cfg *) ((dev)->config->config_info)) #define SPI_DATA(dev) ((struct spi_sifive_data *) ((dev)->driver_data)) diff --git a/drivers/wifi/eswifi/eswifi_bus_spi.c b/drivers/wifi/eswifi/eswifi_bus_spi.c index 52dbb650f09..9205199bfdf 100644 --- a/drivers/wifi/eswifi/eswifi_bus_spi.c +++ b/drivers/wifi/eswifi/eswifi_bus_spi.c @@ -13,7 +13,7 @@ LOG_MODULE_REGISTER(wifi_eswifi_bus_spi); #include #include #include -#include +#include #include "eswifi.h" diff --git a/drivers/wifi/winc1500/wifi_winc1500_nm_bsp_internal.h b/drivers/wifi/winc1500/wifi_winc1500_nm_bsp_internal.h index c5cd0049c5e..fe11419697d 100644 --- a/drivers/wifi/winc1500/wifi_winc1500_nm_bsp_internal.h +++ b/drivers/wifi/winc1500/wifi_winc1500_nm_bsp_internal.h @@ -9,7 +9,7 @@ #include #include -#include +#include #include diff --git a/drivers/wifi/winc1500/wifi_winc1500_nm_bus_wrapper.c b/drivers/wifi/winc1500/wifi_winc1500_nm_bus_wrapper.c index 3d20a226a4f..58942d4da92 100644 --- a/drivers/wifi/winc1500/wifi_winc1500_nm_bus_wrapper.c +++ b/drivers/wifi/winc1500/wifi_winc1500_nm_bus_wrapper.c @@ -12,7 +12,7 @@ LOG_MODULE_REGISTER(winc1500); #include #include -#include +#include #include "wifi_winc1500_nm_bsp_internal.h" diff --git a/include/drivers/spi.h b/include/drivers/spi.h new file mode 100644 index 00000000000..23e8450703d --- /dev/null +++ b/include/drivers/spi.h @@ -0,0 +1,425 @@ +/* + * Copyright (c) 2015 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Public API for SPI drivers and applications + */ + +#ifndef ZEPHYR_INCLUDE_DRIVERS_SPI_H_ +#define ZEPHYR_INCLUDE_DRIVERS_SPI_H_ + +/** + * @brief SPI Interface + * @defgroup spi_interface SPI Interface + * @ingroup io_interfaces + * @{ + */ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief SPI operational mode + */ +#define SPI_OP_MODE_MASTER 0 +#define SPI_OP_MODE_SLAVE BIT(0) +#define SPI_OP_MODE_MASK 0x1 +#define SPI_OP_MODE_GET(_operation_) ((_operation_) & SPI_OP_MODE_MASK) + +/** + * @brief SPI Polarity & Phase Modes + */ + +/** + * Clock Polarity: if set, clock idle state will be 1 + * and active state will be 0. If untouched, the inverse will be true + * which is the default. + */ +#define SPI_MODE_CPOL BIT(1) + +/** + * Clock Phase: this dictates when is the data captured, and depends + * clock's polarity. When SPI_MODE_CPOL is set and this bit as well, + * capture will occur on low to high transition and high to low if + * this bit is not set (default). This is fully reversed if CPOL is + * not set. + */ +#define SPI_MODE_CPHA BIT(2) + +/** + * Whatever data is transmitted is looped-back to the receiving buffer of + * the controller. This is fully controller dependent as some may not + * support this, and can be used for testing purposes only. + */ +#define SPI_MODE_LOOP BIT(3) + +#define SPI_MODE_MASK (0xE) +#define SPI_MODE_GET(_mode_) \ + ((_mode_) & SPI_MODE_MASK) + +/** + * @brief SPI Transfer modes (host controller dependent) + */ +#define SPI_TRANSFER_MSB (0) +#define SPI_TRANSFER_LSB BIT(4) + +/** + * @brief SPI word size + */ +#define SPI_WORD_SIZE_SHIFT (5) +#define SPI_WORD_SIZE_MASK (0x3F << SPI_WORD_SIZE_SHIFT) +#define SPI_WORD_SIZE_GET(_operation_) \ + (((_operation_) & SPI_WORD_SIZE_MASK) >> SPI_WORD_SIZE_SHIFT) + +#define SPI_WORD_SET(_word_size_) \ + ((_word_size_) << SPI_WORD_SIZE_SHIFT) + +/** + * @brief SPI MISO lines + * + * Some controllers support dual, quad or octal MISO lines connected to slaves. + * Default is single, which is the case most of the time. + */ +#define SPI_LINES_SINGLE (0 << 11) +#define SPI_LINES_DUAL (1 << 11) +#define SPI_LINES_QUAD (2 << 11) +#define SPI_LINES_OCTAL (3 << 11) + +#define SPI_LINES_MASK (0x3 << 11) + +/** + * @brief Specific SPI devices control bits + */ +/* Requests - if possible - to keep CS asserted after the transaction */ +#define SPI_HOLD_ON_CS BIT(13) +/* Keep the device locked after the transaction for the current config. + * Use this with extreme caution (see spi_release() below) as it will + * prevent other callers to access the SPI device until spi_release() is + * properly called. + */ +#define SPI_LOCK_ON BIT(14) + +/* Active high logic on CS - Usually, and by default, CS logic is active + * low. However, some devices may require the reverse logic: active high. + * This bit will request the controller to use that logic. Note that not + * all controllers are able to handle that natively. In this case deferring + * the CS control to a gpio line through struct spi_cs_control would be + * the solution. + */ +#define SPI_CS_ACTIVE_HIGH BIT(15) + +/** + * @brief SPI Chip Select control structure + * + * This can be used to control a CS line via a GPIO line, instead of + * using the controller inner CS logic. + * + * @param gpio_dev is a valid pointer to an actual GPIO device. A NULL pointer + * can be provided to full inhibit CS control if necessary. + * @param gpio_pin is a number representing the gpio PIN that will be used + * to act as a CS line + * @param delay is a delay in microseconds to wait before starting the + * transmission and before releasing the CS line + */ +struct spi_cs_control { + struct device *gpio_dev; + u32_t gpio_pin; + u32_t delay; +}; + +/** + * @brief SPI controller configuration structure + * + * @param frequency is the bus frequency in Hertz + * @param operation is a bit field with the following parts: + * + * operational mode [ 0 ] - master or slave. + * mode [ 1 : 3 ] - Polarity, phase and loop mode. + * transfer [ 4 ] - LSB or MSB first. + * word_size [ 5 : 10 ] - Size of a data frame in bits. + * lines [ 11 : 12 ] - MISO lines: Single/Dual/Quad/Octal. + * cs_hold [ 13 ] - Hold on the CS line if possible. + * lock_on [ 14 ] - Keep resource locked for the caller. + * cs_active_high [ 15 ] - Active high CS logic. + * @param slave is the slave number from 0 to host controller slave limit. + * @param cs is a valid pointer on a struct spi_cs_control is CS line is + * emulated through a gpio line, or NULL otherwise. + * + * @note Only cs_hold and lock_on can be changed between consecutive + * transceive call. Rest of the attributes are not meant to be tweaked. + */ +struct spi_config { + u32_t frequency; + u16_t operation; + u16_t slave; + + const struct spi_cs_control *cs; +}; + +/** + * @brief SPI buffer structure + * + * @param buf is a valid pointer on a data buffer, or NULL otherwise. + * @param len is the length of the buffer or, if buf is NULL, will be the + * length which as to be sent as dummy bytes (as TX buffer) or + * the length of bytes that should be skipped (as RX buffer). + */ +struct spi_buf { + void *buf; + size_t len; +}; + +/** + * @brief SPI buffer array structure + * + * @param buffers is a valid pointer on an array of spi_buf, or NULL. + * @param count is the length of the array pointed by buffers. + */ +struct spi_buf_set { + const struct spi_buf *buffers; + size_t count; +}; + +/** + * @typedef spi_api_io + * @brief Callback API for I/O + * See spi_transceive() for argument descriptions + */ +typedef int (*spi_api_io)(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs, + const struct spi_buf_set *rx_bufs); + +/** + * @typedef spi_api_io + * @brief Callback API for asynchronous I/O + * See spi_transceive_async() for argument descriptions + */ +typedef int (*spi_api_io_async)(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs, + const struct spi_buf_set *rx_bufs, + struct k_poll_signal *async); + +/** + * @typedef spi_api_release + * @brief Callback API for unlocking SPI device. + * See spi_release() for argument descriptions + */ +typedef int (*spi_api_release)(struct device *dev, + const struct spi_config *config); + + +/** + * @brief SPI driver API + * This is the mandatory API any SPI driver needs to expose. + */ +struct spi_driver_api { + spi_api_io transceive; +#ifdef CONFIG_SPI_ASYNC + spi_api_io_async transceive_async; +#endif /* CONFIG_SPI_ASYNC */ + spi_api_release release; +}; + +/** + * @brief Read/write the specified amount of data from the SPI driver. + * + * Note: This function is synchronous. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + * @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 0 If successful, negative errno code otherwise. In case of slave + * transaction: if successful it will return the amount of frames + * received, negative errno code otherwise. + */ +__syscall int spi_transceive(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs, + const struct spi_buf_set *rx_bufs); + +static inline int z_impl_spi_transceive(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs, + const struct spi_buf_set *rx_bufs) +{ + const struct spi_driver_api *api = + (const struct spi_driver_api *)dev->driver_api; + + return api->transceive(dev, config, tx_bufs, rx_bufs); +} + +/** + * @brief Read the specified amount of data from the SPI driver. + * + * Note: This function is synchronous. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + * @param rx_bufs Buffer array where data to be read will be written to. + * + * @retval 0 If successful, negative errno code otherwise. + * + * @note This function is an helper function calling spi_transceive. + */ +static inline int spi_read(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *rx_bufs) +{ + return spi_transceive(dev, config, NULL, rx_bufs); +} + +/** + * @brief Write the specified amount of data from the SPI driver. + * + * Note: This function is synchronous. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + * @param tx_bufs Buffer array where data to be sent originates from. + * + * @retval 0 If successful, negative errno code otherwise. + * + * @note This function is an helper function calling spi_transceive. + */ +static inline int spi_write(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs) +{ + return spi_transceive(dev, config, tx_bufs, NULL); +} + +#ifdef CONFIG_SPI_ASYNC +/** + * @brief Read/write the specified amount of data from the SPI driver. + * + * Note: This function is asynchronous. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + * @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. + * @param async A pointer to a valid and ready to be signaled + * struct k_poll_signal. (Note: if NULL this function will not + * notify the end of the transaction, and whether it went + * successfully or not). + * + * @retval 0 If successful, negative errno code otherwise. In case of slave + * transaction: if successful it will return the amount of frames + * received, negative errno code otherwise. + */ +static inline int spi_transceive_async(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs, + const struct spi_buf_set *rx_bufs, + struct k_poll_signal *async) +{ + const struct spi_driver_api *api = + (const struct spi_driver_api *)dev->driver_api; + + return api->transceive_async(dev, config, tx_bufs, rx_bufs, async); +} + +/** + * @brief Read the specified amount of data from the SPI driver. + * + * Note: This function is asynchronous. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + * @param rx_bufs Buffer array where data to be read will be written to. + * @param async A pointer to a valid and ready to be signaled + * struct k_poll_signal. (Note: if NULL this function will not + * notify the end of the transaction, and whether it went + * successfully or not). + * + * @retval 0 If successful, negative errno code otherwise. + * + * @note This function is an helper function calling spi_transceive_async. + */ +static inline int spi_read_async(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *rx_bufs, + struct k_poll_signal *async) +{ + return spi_transceive_async(dev, config, NULL, rx_bufs, async); +} + +/** + * @brief Write the specified amount of data from the SPI driver. + * + * Note: This function is asynchronous. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + * @param tx_bufs Buffer array where data to be sent originates from. + * @param async A pointer to a valid and ready to be signaled + * struct k_poll_signal. (Note: if NULL this function will not + * notify the end of the transaction, and whether it went + * successfully or not). + * + * @retval 0 If successful, negative errno code otherwise. + * + * @note This function is an helper function calling spi_transceive_async. + */ +static inline int spi_write_async(struct device *dev, + const struct spi_config *config, + const struct spi_buf_set *tx_bufs, + struct k_poll_signal *async) +{ + return spi_transceive_async(dev, config, tx_bufs, NULL, async); +} +#endif /* CONFIG_SPI_ASYNC */ + +/** + * @brief Release the SPI device locked on by the current config + * + * Note: This synchronous function is used to release the lock on the SPI + * device that was kept if, and if only, given config parameter was + * the last one to be used (in any of the above functions) and if + * it has the SPI_LOCK_ON bit set into its operation bits field. + * This can be used if the caller needs to keep its hand on the SPI + * device for consecutive transactions. + * + * @param dev Pointer to the device structure for the driver instance + * @param config Pointer to a valid spi_config structure instance. + */ +__syscall int spi_release(struct device *dev, + const struct spi_config *config); + +static inline int z_impl_spi_release(struct device *dev, + const struct spi_config *config) +{ + const struct spi_driver_api *api = + (const struct spi_driver_api *)dev->driver_api; + + return api->release(dev, config); +} + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#include + +#endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_H_ */ diff --git a/include/spi.h b/include/spi.h index d2e78a81e78..3ae409dddda 100644 --- a/include/spi.h +++ b/include/spi.h @@ -1,425 +1,15 @@ /* - * Copyright (c) 2015 Intel Corporation + * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ - -/** - * @file - * @brief Public API for SPI drivers and applications - */ - #ifndef ZEPHYR_INCLUDE_SPI_H_ #define ZEPHYR_INCLUDE_SPI_H_ -/** - * @brief SPI Interface - * @defgroup spi_interface SPI Interface - * @ingroup io_interfaces - * @{ - */ - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { +#ifndef CONFIG_COMPAT_INCLUDES +#warning "This header file has moved, include instead." #endif -/** - * @brief SPI operational mode - */ -#define SPI_OP_MODE_MASTER 0 -#define SPI_OP_MODE_SLAVE BIT(0) -#define SPI_OP_MODE_MASK 0x1 -#define SPI_OP_MODE_GET(_operation_) ((_operation_) & SPI_OP_MODE_MASK) - -/** - * @brief SPI Polarity & Phase Modes - */ - -/** - * Clock Polarity: if set, clock idle state will be 1 - * and active state will be 0. If untouched, the inverse will be true - * which is the default. - */ -#define SPI_MODE_CPOL BIT(1) - -/** - * Clock Phase: this dictates when is the data captured, and depends - * clock's polarity. When SPI_MODE_CPOL is set and this bit as well, - * capture will occur on low to high transition and high to low if - * this bit is not set (default). This is fully reversed if CPOL is - * not set. - */ -#define SPI_MODE_CPHA BIT(2) - -/** - * Whatever data is transmitted is looped-back to the receiving buffer of - * the controller. This is fully controller dependent as some may not - * support this, and can be used for testing purposes only. - */ -#define SPI_MODE_LOOP BIT(3) - -#define SPI_MODE_MASK (0xE) -#define SPI_MODE_GET(_mode_) \ - ((_mode_) & SPI_MODE_MASK) - -/** - * @brief SPI Transfer modes (host controller dependent) - */ -#define SPI_TRANSFER_MSB (0) -#define SPI_TRANSFER_LSB BIT(4) - -/** - * @brief SPI word size - */ -#define SPI_WORD_SIZE_SHIFT (5) -#define SPI_WORD_SIZE_MASK (0x3F << SPI_WORD_SIZE_SHIFT) -#define SPI_WORD_SIZE_GET(_operation_) \ - (((_operation_) & SPI_WORD_SIZE_MASK) >> SPI_WORD_SIZE_SHIFT) - -#define SPI_WORD_SET(_word_size_) \ - ((_word_size_) << SPI_WORD_SIZE_SHIFT) - -/** - * @brief SPI MISO lines - * - * Some controllers support dual, quad or octal MISO lines connected to slaves. - * Default is single, which is the case most of the time. - */ -#define SPI_LINES_SINGLE (0 << 11) -#define SPI_LINES_DUAL (1 << 11) -#define SPI_LINES_QUAD (2 << 11) -#define SPI_LINES_OCTAL (3 << 11) - -#define SPI_LINES_MASK (0x3 << 11) - -/** - * @brief Specific SPI devices control bits - */ -/* Requests - if possible - to keep CS asserted after the transaction */ -#define SPI_HOLD_ON_CS BIT(13) -/* Keep the device locked after the transaction for the current config. - * Use this with extreme caution (see spi_release() below) as it will - * prevent other callers to access the SPI device until spi_release() is - * properly called. - */ -#define SPI_LOCK_ON BIT(14) - -/* Active high logic on CS - Usually, and by default, CS logic is active - * low. However, some devices may require the reverse logic: active high. - * This bit will request the controller to use that logic. Note that not - * all controllers are able to handle that natively. In this case deferring - * the CS control to a gpio line through struct spi_cs_control would be - * the solution. - */ -#define SPI_CS_ACTIVE_HIGH BIT(15) - -/** - * @brief SPI Chip Select control structure - * - * This can be used to control a CS line via a GPIO line, instead of - * using the controller inner CS logic. - * - * @param gpio_dev is a valid pointer to an actual GPIO device. A NULL pointer - * can be provided to full inhibit CS control if necessary. - * @param gpio_pin is a number representing the gpio PIN that will be used - * to act as a CS line - * @param delay is a delay in microseconds to wait before starting the - * transmission and before releasing the CS line - */ -struct spi_cs_control { - struct device *gpio_dev; - u32_t gpio_pin; - u32_t delay; -}; - -/** - * @brief SPI controller configuration structure - * - * @param frequency is the bus frequency in Hertz - * @param operation is a bit field with the following parts: - * - * operational mode [ 0 ] - master or slave. - * mode [ 1 : 3 ] - Polarity, phase and loop mode. - * transfer [ 4 ] - LSB or MSB first. - * word_size [ 5 : 10 ] - Size of a data frame in bits. - * lines [ 11 : 12 ] - MISO lines: Single/Dual/Quad/Octal. - * cs_hold [ 13 ] - Hold on the CS line if possible. - * lock_on [ 14 ] - Keep resource locked for the caller. - * cs_active_high [ 15 ] - Active high CS logic. - * @param slave is the slave number from 0 to host controller slave limit. - * @param cs is a valid pointer on a struct spi_cs_control is CS line is - * emulated through a gpio line, or NULL otherwise. - * - * @note Only cs_hold and lock_on can be changed between consecutive - * transceive call. Rest of the attributes are not meant to be tweaked. - */ -struct spi_config { - u32_t frequency; - u16_t operation; - u16_t slave; - - const struct spi_cs_control *cs; -}; - -/** - * @brief SPI buffer structure - * - * @param buf is a valid pointer on a data buffer, or NULL otherwise. - * @param len is the length of the buffer or, if buf is NULL, will be the - * length which as to be sent as dummy bytes (as TX buffer) or - * the length of bytes that should be skipped (as RX buffer). - */ -struct spi_buf { - void *buf; - size_t len; -}; - -/** - * @brief SPI buffer array structure - * - * @param buffers is a valid pointer on an array of spi_buf, or NULL. - * @param count is the length of the array pointed by buffers. - */ -struct spi_buf_set { - const struct spi_buf *buffers; - size_t count; -}; - -/** - * @typedef spi_api_io - * @brief Callback API for I/O - * See spi_transceive() for argument descriptions - */ -typedef int (*spi_api_io)(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs, - const struct spi_buf_set *rx_bufs); - -/** - * @typedef spi_api_io - * @brief Callback API for asynchronous I/O - * See spi_transceive_async() for argument descriptions - */ -typedef int (*spi_api_io_async)(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs, - const struct spi_buf_set *rx_bufs, - struct k_poll_signal *async); - -/** - * @typedef spi_api_release - * @brief Callback API for unlocking SPI device. - * See spi_release() for argument descriptions - */ -typedef int (*spi_api_release)(struct device *dev, - const struct spi_config *config); - - -/** - * @brief SPI driver API - * This is the mandatory API any SPI driver needs to expose. - */ -struct spi_driver_api { - spi_api_io transceive; -#ifdef CONFIG_SPI_ASYNC - spi_api_io_async transceive_async; -#endif /* CONFIG_SPI_ASYNC */ - spi_api_release release; -}; - -/** - * @brief Read/write the specified amount of data from the SPI driver. - * - * Note: This function is synchronous. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - * @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 0 If successful, negative errno code otherwise. In case of slave - * transaction: if successful it will return the amount of frames - * received, negative errno code otherwise. - */ -__syscall int spi_transceive(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs, - const struct spi_buf_set *rx_bufs); - -static inline int z_impl_spi_transceive(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs, - const struct spi_buf_set *rx_bufs) -{ - const struct spi_driver_api *api = - (const struct spi_driver_api *)dev->driver_api; - - return api->transceive(dev, config, tx_bufs, rx_bufs); -} - -/** - * @brief Read the specified amount of data from the SPI driver. - * - * Note: This function is synchronous. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - * @param rx_bufs Buffer array where data to be read will be written to. - * - * @retval 0 If successful, negative errno code otherwise. - * - * @note This function is an helper function calling spi_transceive. - */ -static inline int spi_read(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *rx_bufs) -{ - return spi_transceive(dev, config, NULL, rx_bufs); -} - -/** - * @brief Write the specified amount of data from the SPI driver. - * - * Note: This function is synchronous. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - * @param tx_bufs Buffer array where data to be sent originates from. - * - * @retval 0 If successful, negative errno code otherwise. - * - * @note This function is an helper function calling spi_transceive. - */ -static inline int spi_write(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs) -{ - return spi_transceive(dev, config, tx_bufs, NULL); -} - -#ifdef CONFIG_SPI_ASYNC -/** - * @brief Read/write the specified amount of data from the SPI driver. - * - * Note: This function is asynchronous. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - * @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. - * @param async A pointer to a valid and ready to be signaled - * struct k_poll_signal. (Note: if NULL this function will not - * notify the end of the transaction, and whether it went - * successfully or not). - * - * @retval 0 If successful, negative errno code otherwise. In case of slave - * transaction: if successful it will return the amount of frames - * received, negative errno code otherwise. - */ -static inline int spi_transceive_async(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs, - const struct spi_buf_set *rx_bufs, - struct k_poll_signal *async) -{ - const struct spi_driver_api *api = - (const struct spi_driver_api *)dev->driver_api; - - return api->transceive_async(dev, config, tx_bufs, rx_bufs, async); -} - -/** - * @brief Read the specified amount of data from the SPI driver. - * - * Note: This function is asynchronous. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - * @param rx_bufs Buffer array where data to be read will be written to. - * @param async A pointer to a valid and ready to be signaled - * struct k_poll_signal. (Note: if NULL this function will not - * notify the end of the transaction, and whether it went - * successfully or not). - * - * @retval 0 If successful, negative errno code otherwise. - * - * @note This function is an helper function calling spi_transceive_async. - */ -static inline int spi_read_async(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *rx_bufs, - struct k_poll_signal *async) -{ - return spi_transceive_async(dev, config, NULL, rx_bufs, async); -} - -/** - * @brief Write the specified amount of data from the SPI driver. - * - * Note: This function is asynchronous. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - * @param tx_bufs Buffer array where data to be sent originates from. - * @param async A pointer to a valid and ready to be signaled - * struct k_poll_signal. (Note: if NULL this function will not - * notify the end of the transaction, and whether it went - * successfully or not). - * - * @retval 0 If successful, negative errno code otherwise. - * - * @note This function is an helper function calling spi_transceive_async. - */ -static inline int spi_write_async(struct device *dev, - const struct spi_config *config, - const struct spi_buf_set *tx_bufs, - struct k_poll_signal *async) -{ - return spi_transceive_async(dev, config, tx_bufs, NULL, async); -} -#endif /* CONFIG_SPI_ASYNC */ - -/** - * @brief Release the SPI device locked on by the current config - * - * Note: This synchronous function is used to release the lock on the SPI - * device that was kept if, and if only, given config parameter was - * the last one to be used (in any of the above functions) and if - * it has the SPI_LOCK_ON bit set into its operation bits field. - * This can be used if the caller needs to keep its hand on the SPI - * device for consecutive transactions. - * - * @param dev Pointer to the device structure for the driver instance - * @param config Pointer to a valid spi_config structure instance. - */ -__syscall int spi_release(struct device *dev, - const struct spi_config *config); - -static inline int z_impl_spi_release(struct device *dev, - const struct spi_config *config) -{ - const struct spi_driver_api *api = - (const struct spi_driver_api *)dev->driver_api; - - return api->release(dev, config); -} - -#ifdef __cplusplus -} -#endif - -/** - * @} - */ - -#include +#include #endif /* ZEPHYR_INCLUDE_SPI_H_ */ diff --git a/samples/bluetooth/hci_spi/src/main.c b/samples/bluetooth/hci_spi/src/main.c index 88ab8ff533f..6639bb7c8fe 100644 --- a/samples/bluetooth/hci_spi/src/main.c +++ b/samples/bluetooth/hci_spi/src/main.c @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include diff --git a/samples/boards/96b_argonkey/sensors/src/main.c b/samples/boards/96b_argonkey/sensors/src/main.c index d2ed36e3785..8d4f8a1fc06 100644 --- a/samples/boards/96b_argonkey/sensors/src/main.c +++ b/samples/boards/96b_argonkey/sensors/src/main.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/samples/drivers/led_apa102/src/main.c b/samples/drivers/led_apa102/src/main.c index 2a68f11b5e2..64ce8ccb7ba 100644 --- a/samples/drivers/led_apa102/src/main.c +++ b/samples/drivers/led_apa102/src/main.c @@ -15,7 +15,7 @@ LOG_MODULE_REGISTER(main); #include #include #include -#include +#include #include /* diff --git a/samples/drivers/led_lpd8806/src/main.c b/samples/drivers/led_lpd8806/src/main.c index 68a3ab4f8ce..7eaf2f0b0c6 100644 --- a/samples/drivers/led_lpd8806/src/main.c +++ b/samples/drivers/led_lpd8806/src/main.c @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(main); #include #include #include -#include +#include #include /* diff --git a/samples/drivers/led_ws2812/src/main.c b/samples/drivers/led_ws2812/src/main.c index ca3ed3d0b99..231016b37c2 100644 --- a/samples/drivers/led_ws2812/src/main.c +++ b/samples/drivers/led_ws2812/src/main.c @@ -15,7 +15,7 @@ LOG_MODULE_REGISTER(main); #include #include #include -#include +#include #include /* diff --git a/samples/drivers/spi_fujitsu_fram/src/main.c b/samples/drivers/spi_fujitsu_fram/src/main.c index 676843e6c09..0609ea86ece 100644 --- a/samples/drivers/spi_fujitsu_fram/src/main.c +++ b/samples/drivers/spi_fujitsu_fram/src/main.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include /** * @file Sample app using the Fujitsu MB85RS64V FRAM through SPI. diff --git a/subsys/disk/disk_access_sdhc.c b/subsys/disk/disk_access_sdhc.c index 6207df7279a..67e03dd98c2 100644 --- a/subsys/disk/disk_access_sdhc.c +++ b/subsys/disk/disk_access_sdhc.c @@ -11,7 +11,7 @@ LOG_MODULE_REGISTER(sdhc, CONFIG_DISK_LOG_LEVEL); #include #include #include -#include +#include #include #define SDHC_SECTOR_SIZE 512 diff --git a/tests/application_development/cpp/src/main.cpp b/tests/application_development/cpp/src/main.cpp index 03ef8b7dfe1..ca9ca1d85b2 100644 --- a/tests/application_development/cpp/src/main.cpp +++ b/tests/application_development/cpp/src/main.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include diff --git a/tests/drivers/spi/spi_loopback/src/spi.c b/tests/drivers/spi/spi_loopback/src/spi.c index 1c50270d352..41b0b09de0b 100644 --- a/tests/drivers/spi/spi_loopback/src/spi.c +++ b/tests/drivers/spi/spi_loopback/src/spi.c @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(main); #include #include -#include +#include #define SPI_DRV_NAME CONFIG_SPI_LOOPBACK_DRV_NAME #define SPI_SLAVE CONFIG_SPI_LOOPBACK_SLAVE_NUMBER