123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- #ifndef ZEPHYR_INCLUDE_DRIVERS_SPI_H_
- #define ZEPHYR_INCLUDE_DRIVERS_SPI_H_
- #include <zephyr/types.h>
- #include <stddef.h>
- #include <device.h>
- #include <drivers/gpio.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #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)
- #define SPI_MODE_CPOL BIT(1)
- #define SPI_MODE_CPHA BIT(2)
- #define SPI_MODE_LOOP BIT(3)
- #define SPI_MODE_MASK (0xE)
- #define SPI_MODE_GET(_mode_) \
- ((_mode_) & SPI_MODE_MASK)
- #define SPI_TRANSFER_MSB (0)
- #define SPI_TRANSFER_LSB BIT(4)
- #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)
- #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)
- #define SPI_HOLD_ON_CS BIT(13)
- #define SPI_LOCK_ON BIT(14)
- #define SPI_CS_ACTIVE_HIGH BIT(15)
- struct spi_cs_control {
- const struct device *gpio_dev;
- uint32_t delay;
- gpio_pin_t gpio_pin;
- gpio_dt_flags_t gpio_dt_flags;
- };
- #ifndef __cplusplus
- #define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
- (&(struct spi_cs_control) { \
- .gpio_dev = DEVICE_DT_GET( \
- DT_SPI_DEV_CS_GPIOS_CTLR(node_id)), \
- .delay = (delay_), \
- .gpio_pin = DT_SPI_DEV_CS_GPIOS_PIN(node_id), \
- .gpio_dt_flags = DT_SPI_DEV_CS_GPIOS_FLAGS(node_id), \
- })
- #define SPI_CS_CONTROL_PTR_DT_INST(inst, delay_) \
- SPI_CS_CONTROL_PTR_DT(DT_DRV_INST(inst), delay_)
- #endif
- struct spi_config {
- uint32_t frequency;
- uint16_t operation;
- uint16_t slave;
- const struct spi_cs_control *cs;
- };
- #ifndef __cplusplus
- #define SPI_CONFIG_DT(node_id, operation_, delay_) \
- { \
- .frequency = DT_PROP(node_id, spi_max_frequency), \
- .operation = (operation_), \
- .slave = DT_REG_ADDR(node_id), \
- .cs = COND_CODE_1( \
- DT_SPI_DEV_HAS_CS_GPIOS(node_id), \
- (SPI_CS_CONTROL_PTR_DT(node_id, delay_)), \
- (NULL)), \
- }
- #define SPI_CONFIG_DT_INST(inst, operation_, delay_) \
- SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)
- #endif
- struct spi_dt_spec {
- const struct device *bus;
- struct spi_config config;
- };
- #ifndef __cplusplus
- #define SPI_DT_SPEC_GET(node_id, operation_, delay_) \
- { \
- .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
- .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
- }
- #define SPI_DT_SPEC_INST_GET(inst, operation_, delay_) \
- SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)
- #endif
- struct spi_buf {
- void *buf;
- size_t len;
- };
- struct spi_buf_set {
- const struct spi_buf *buffers;
- size_t count;
- };
- typedef int (*spi_api_io)(const struct device *dev,
- const struct spi_config *config,
- const struct spi_buf_set *tx_bufs,
- const struct spi_buf_set *rx_bufs);
- typedef int (*spi_api_io_async)(const 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 int (*spi_api_release)(const struct device *dev,
- const struct spi_config *config);
- __subsystem struct spi_driver_api {
- spi_api_io transceive;
- #ifdef CONFIG_SPI_ASYNC
- spi_api_io_async transceive_async;
- #endif
- spi_api_release release;
- };
- static inline bool spi_is_ready(const struct spi_dt_spec *spec)
- {
-
- if (!device_is_ready(spec->bus)) {
- return false;
- }
-
- if (spec->config.cs &&
- !device_is_ready(spec->config.cs->gpio_dev)) {
- return false;
- }
- return true;
- }
- __syscall int spi_transceive(const 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(const 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->api;
- return api->transceive(dev, config, tx_bufs, rx_bufs);
- }
- 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);
- }
- static inline int spi_read(const struct device *dev,
- const struct spi_config *config,
- const struct spi_buf_set *rx_bufs)
- {
- return spi_transceive(dev, config, NULL, rx_bufs);
- }
- 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);
- }
- static inline int spi_write(const struct device *dev,
- const struct spi_config *config,
- const struct spi_buf_set *tx_bufs)
- {
- return spi_transceive(dev, config, tx_bufs, NULL);
- }
- 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);
- }
- #ifdef CONFIG_SPI_ASYNC
- static inline int spi_transceive_async(const 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->api;
- return api->transceive_async(dev, config, tx_bufs, rx_bufs, async);
- }
- static inline int spi_read_async(const 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);
- }
- static inline int spi_write_async(const 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
- __syscall int spi_release(const struct device *dev,
- const struct spi_config *config);
- static inline int z_impl_spi_release(const struct device *dev,
- const struct spi_config *config)
- {
- const struct spi_driver_api *api =
- (const struct spi_driver_api *)dev->api;
- return api->release(dev, config);
- }
- static inline int spi_release_dt(const struct spi_dt_spec *spec)
- {
- return spi_release(spec->bus, &spec->config);
- }
- #ifdef __cplusplus
- }
- #endif
- #include <syscalls/spi.h>
- #endif
|