Zephyr Project API  3.3.0
A Scalable Open Source RTOS
spi.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12#ifndef ZEPHYR_INCLUDE_DRIVERS_SPI_H_
13#define ZEPHYR_INCLUDE_DRIVERS_SPI_H_
14
22#include <zephyr/types.h>
23#include <stddef.h>
24#include <zephyr/device.h>
26#include <zephyr/drivers/gpio.h>
27#include <zephyr/kernel.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
37#define SPI_OP_MODE_MASTER 0U
38#define SPI_OP_MODE_SLAVE BIT(0)
39#define SPI_OP_MODE_MASK 0x1U
40#define SPI_OP_MODE_GET(_operation_) ((_operation_) & SPI_OP_MODE_MASK)
53#define SPI_MODE_CPOL BIT(1)
54
62#define SPI_MODE_CPHA BIT(2)
63
69#define SPI_MODE_LOOP BIT(3)
70
71#define SPI_MODE_MASK (0xEU)
72#define SPI_MODE_GET(_mode_) \
73 ((_mode_) & SPI_MODE_MASK)
74
81#define SPI_TRANSFER_MSB (0U)
82#define SPI_TRANSFER_LSB BIT(4)
89#define SPI_WORD_SIZE_SHIFT (5U)
90#define SPI_WORD_SIZE_MASK (0x3FU << SPI_WORD_SIZE_SHIFT)
91#define SPI_WORD_SIZE_GET(_operation_) \
92 (((_operation_) & SPI_WORD_SIZE_MASK) >> SPI_WORD_SIZE_SHIFT)
93
94#define SPI_WORD_SET(_word_size_) \
95 ((_word_size_) << SPI_WORD_SIZE_SHIFT)
102/* Requests - if possible - to keep CS asserted after the transaction */
103#define SPI_HOLD_ON_CS BIT(12)
104/* Keep the device locked after the transaction for the current config.
105 * Use this with extreme caution (see spi_release() below) as it will
106 * prevent other callers to access the SPI device until spi_release() is
107 * properly called.
108 */
109#define SPI_LOCK_ON BIT(13)
110
111/* Active high logic on CS - Usually, and by default, CS logic is active
112 * low. However, some devices may require the reverse logic: active high.
113 * This bit will request the controller to use that logic. Note that not
114 * all controllers are able to handle that natively. In this case deferring
115 * the CS control to a gpio line through struct spi_cs_control would be
116 * the solution.
117 */
118#define SPI_CS_ACTIVE_HIGH BIT(14)
130#define SPI_LINES_SINGLE (0U << 16)
131#define SPI_LINES_DUAL (1U << 16)
132#define SPI_LINES_QUAD (2U << 16)
133#define SPI_LINES_OCTAL (3U << 16)
134
135#define SPI_LINES_MASK (0x3U << 16)
159};
160
198#define SPI_CS_GPIOS_DT_SPEC_GET(spi_dev) \
199 GPIO_DT_SPEC_GET_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
200
210#define SPI_CS_GPIOS_DT_SPEC_INST_GET(inst) \
211 SPI_CS_GPIOS_DT_SPEC_GET(DT_DRV_INST(inst))
212
213#ifndef __cplusplus
258#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
259 (&(struct spi_cs_control) { \
260 .gpio = SPI_CS_GPIOS_DT_SPEC_GET(node_id), \
261 .delay = (delay_), \
262 })
263
279#define SPI_CS_CONTROL_PTR_DT_INST(inst, delay_) \
280 SPI_CS_CONTROL_PTR_DT(DT_DRV_INST(inst), delay_)
281#endif
282
311#if defined(CONFIG_SPI_EXTENDED_MODES)
314 uint16_t _unused;
315#else
318#endif /* CONFIG_SPI_EXTENDED_MODES */
319
320 const struct spi_cs_control *cs;
321};
322
323#ifndef __cplusplus
343#define SPI_CONFIG_DT(node_id, operation_, delay_) \
344 { \
345 .frequency = DT_PROP(node_id, spi_max_frequency), \
346 .operation = (operation_) | \
347 DT_PROP(node_id, duplex) | \
348 DT_PROP(node_id, frame_format), \
349 .slave = DT_REG_ADDR(node_id), \
350 .cs = COND_CODE_1( \
351 DT_SPI_DEV_HAS_CS_GPIOS(node_id), \
352 (SPI_CS_CONTROL_PTR_DT(node_id, delay_)), \
353 (NULL)), \
354 }
355
369#define SPI_CONFIG_DT_INST(inst, operation_, delay_) \
370 SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)
371#endif
372
380 const struct device *bus;
382};
383
384#ifndef __cplusplus
405#define SPI_DT_SPEC_GET(node_id, operation_, delay_) \
406 { \
407 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
408 .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
409 }
410
424#define SPI_DT_SPEC_INST_GET(inst, operation_, delay_) \
425 SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)
426#endif
427
436struct spi_buf {
437 void *buf;
438 size_t len;
439};
440
448 const struct spi_buf *buffers;
449 size_t count;
450};
451
457typedef int (*spi_api_io)(const struct device *dev,
458 const struct spi_config *config,
459 const struct spi_buf_set *tx_bufs,
460 const struct spi_buf_set *rx_bufs);
461
469typedef void (*spi_callback_t)(const struct device *dev, int result, void *data);
470
476typedef int (*spi_api_io_async)(const struct device *dev,
477 const struct spi_config *config,
478 const struct spi_buf_set *tx_bufs,
479 const struct spi_buf_set *rx_bufs,
481 void *userdata);
482
488typedef int (*spi_api_release)(const struct device *dev,
489 const struct spi_config *config);
490
491
496__subsystem struct spi_driver_api {
498#ifdef CONFIG_SPI_ASYNC
500#endif /* CONFIG_SPI_ASYNC */
502};
503
512__deprecated
513static inline bool spi_is_ready(const struct spi_dt_spec *spec)
514{
515 /* Validate bus is ready */
516 if (!device_is_ready(spec->bus)) {
517 return false;
518 }
519 /* Validate CS gpio port is ready, if it is used */
520 if (spec->config.cs &&
521 !device_is_ready(spec->config.cs->gpio.port)) {
522 return false;
523 }
524 return true;
525}
526
535static inline bool spi_is_ready_dt(const struct spi_dt_spec *spec)
536{
537 /* Validate bus is ready */
538 if (!device_is_ready(spec->bus)) {
539 return false;
540 }
541 /* Validate CS gpio port is ready, if it is used */
542 if (spec->config.cs &&
543 !device_is_ready(spec->config.cs->gpio.port)) {
544 return false;
545 }
546 return true;
547}
566__syscall int spi_transceive(const struct device *dev,
567 const struct spi_config *config,
568 const struct spi_buf_set *tx_bufs,
569 const struct spi_buf_set *rx_bufs);
570
571static inline int z_impl_spi_transceive(const struct device *dev,
572 const struct spi_config *config,
573 const struct spi_buf_set *tx_bufs,
574 const struct spi_buf_set *rx_bufs)
575{
576 const struct spi_driver_api *api =
577 (const struct spi_driver_api *)dev->api;
578
579 return api->transceive(dev, config, tx_bufs, rx_bufs);
580}
581
597static inline int spi_transceive_dt(const struct spi_dt_spec *spec,
598 const struct spi_buf_set *tx_bufs,
599 const struct spi_buf_set *rx_bufs)
600{
601 return spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
602}
603
620static inline int spi_read(const struct device *dev,
621 const struct spi_config *config,
622 const struct spi_buf_set *rx_bufs)
623{
624 return spi_transceive(dev, config, NULL, rx_bufs);
625}
626
639static inline int spi_read_dt(const struct spi_dt_spec *spec,
640 const struct spi_buf_set *rx_bufs)
641{
642 return spi_read(spec->bus, &spec->config, rx_bufs);
643}
644
661static inline int spi_write(const struct device *dev,
662 const struct spi_config *config,
663 const struct spi_buf_set *tx_bufs)
664{
665 return spi_transceive(dev, config, tx_bufs, NULL);
666}
667
680static inline int spi_write_dt(const struct spi_dt_spec *spec,
681 const struct spi_buf_set *tx_bufs)
682{
683 return spi_write(spec->bus, &spec->config, tx_bufs);
684}
685
686/* Doxygen defines this so documentation is generated. */
687#ifdef CONFIG_SPI_ASYNC
688
715static inline int spi_transceive_cb(const struct device *dev,
716 const struct spi_config *config,
717 const struct spi_buf_set *tx_bufs,
718 const struct spi_buf_set *rx_bufs,
719 spi_callback_t callback,
720 void *userdata)
721{
722 const struct spi_driver_api *api =
723 (const struct spi_driver_api *)dev->api;
724
725 return api->transceive_async(dev, config, tx_bufs, rx_bufs, callback, userdata);
726}
727
728#ifdef CONFIG_POLL
729
731void z_spi_transfer_signal_cb(const struct device *dev, int result, void *userdata);
759static inline int spi_transceive_signal(const struct device *dev,
760 const struct spi_config *config,
761 const struct spi_buf_set *tx_bufs,
762 const struct spi_buf_set *rx_bufs,
763 struct k_poll_signal *sig)
764{
765 const struct spi_driver_api *api =
766 (const struct spi_driver_api *)dev->api;
767 spi_callback_t cb = (sig == NULL) ? NULL : z_spi_transfer_signal_cb;
768
769 return api->transceive_async(dev, config, tx_bufs, rx_bufs, cb, sig);
770}
771
778__deprecated static inline int spi_transceive_async(const struct device *dev,
779 const struct spi_config *config,
780 const struct spi_buf_set *tx_bufs,
781 const struct spi_buf_set *rx_bufs,
782 struct k_poll_signal *sig)
783{
784 return spi_transceive_signal(dev, config, tx_bufs, rx_bufs, sig);
785}
786
810static inline int spi_read_signal(const struct device *dev,
811 const struct spi_config *config,
812 const struct spi_buf_set *rx_bufs,
813 struct k_poll_signal *sig)
814{
815 return spi_transceive_signal(dev, config, NULL, rx_bufs, sig);
816}
817
824__deprecated static inline int spi_read_async(const struct device *dev,
825 const struct spi_config *config,
826 const struct spi_buf_set *rx_bufs,
827 struct k_poll_signal *sig)
828{
829 return spi_read_signal(dev, config, rx_bufs, sig);
830}
831
855static inline int spi_write_signal(const struct device *dev,
856 const struct spi_config *config,
857 const struct spi_buf_set *tx_bufs,
858 struct k_poll_signal *sig)
859{
860 return spi_transceive_signal(dev, config, tx_bufs, NULL, sig);
861}
862
869__deprecated static inline int spi_write_async(const struct device *dev,
870 const struct spi_config *config,
871 const struct spi_buf_set *tx_bufs,
872 struct k_poll_signal *sig)
873{
874 return spi_write_signal(dev, config, tx_bufs, sig);
875}
876
877#endif /* CONFIG_POLL */
878
879#endif /* CONFIG_SPI_ASYNC */
880
901__syscall int spi_release(const struct device *dev,
902 const struct spi_config *config);
903
904static inline int z_impl_spi_release(const struct device *dev,
905 const struct spi_config *config)
906{
907 const struct spi_driver_api *api =
908 (const struct spi_driver_api *)dev->api;
909
910 return api->release(dev, config);
911}
912
924static inline int spi_release_dt(const struct spi_dt_spec *spec)
925{
926 return spi_release(spec->bus, &spec->config);
927}
928
929#ifdef __cplusplus
930}
931#endif
932
937#include <syscalls/spi.h>
938
939#endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_H_ */
Public APIs for GPIO drivers.
struct result result[2]
Definition: errno.c:42
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
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, spi_callback_t cb, void *userdata)
Definition: spi.h:476
int spi_release(const struct device *dev, const struct spi_config *config)
Release the SPI device locked on and/or the CS by the current config.
void(* spi_callback_t)(const struct device *dev, int result, void *data)
SPI callback for asynchronous transfer requests.
Definition: spi.h:469
static int spi_write_dt(const struct spi_dt_spec *spec, const struct spi_buf_set *tx_bufs)
Write data to a SPI bus specified in spi_dt_spec.
Definition: spi.h:680
static bool spi_is_ready_dt(const struct spi_dt_spec *spec)
Validate that SPI bus (and CS gpio if defined) is ready.
Definition: spi.h:535
static int spi_read(const struct device *dev, const struct spi_config *config, const struct spi_buf_set *rx_bufs)
Read the specified amount of data from the SPI driver.
Definition: spi.h:620
static int spi_transceive_dt(const struct spi_dt_spec *spec, const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs)
Read/write data from an SPI bus specified in spi_dt_spec.
Definition: spi.h:597
static int spi_transceive_cb(const struct device *dev, const struct spi_config *config, const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs, spi_callback_t callback, void *userdata)
Read/write the specified amount of data from the SPI driver.
Definition: spi.h:715
static int spi_read_dt(const struct spi_dt_spec *spec, const struct spi_buf_set *rx_bufs)
Read data from a SPI bus specified in spi_dt_spec.
Definition: spi.h:639
static bool spi_is_ready(const struct spi_dt_spec *spec)
Validate that SPI bus is ready.
Definition: spi.h:513
static int spi_write(const struct device *dev, const struct spi_config *config, const struct spi_buf_set *tx_bufs)
Write the specified amount of data from the SPI driver.
Definition: spi.h:661
static int spi_release_dt(const struct spi_dt_spec *spec)
Release the SPI device specified in spi_dt_spec.
Definition: spi.h:924
int(* spi_api_release)(const struct device *dev, const struct spi_config *config)
Callback API for unlocking SPI device. See spi_release() for argument descriptions.
Definition: spi.h:488
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)
Callback API for I/O See spi_transceive() for argument descriptions.
Definition: spi.h:457
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)
Read/write the specified amount of data from the SPI driver.
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition: device.h:378
const void * api
Definition: device.h:384
Container for GPIO pin information specified in devicetree.
Definition: gpio.h:271
const struct device * port
Definition: gpio.h:273
Definition: kernel.h:5459
Definition: errno.c:37
SPI buffer array structure.
Definition: spi.h:447
const struct spi_buf * buffers
Definition: spi.h:448
size_t count
Definition: spi.h:449
SPI buffer structure.
Definition: spi.h:436
size_t len
Definition: spi.h:438
void * buf
Definition: spi.h:437
SPI controller configuration structure.
Definition: spi.h:309
uint16_t slave
Definition: spi.h:317
const struct spi_cs_control * cs
Definition: spi.h:320
uint32_t frequency
Definition: spi.h:310
uint16_t operation
Definition: spi.h:316
SPI Chip Select control structure.
Definition: spi.h:145
uint32_t delay
Definition: spi.h:158
struct gpio_dt_spec gpio
Definition: spi.h:153
SPI driver API This is the mandatory API any SPI driver needs to expose.
Definition: spi.h:496
spi_api_io transceive
Definition: spi.h:497
spi_api_release release
Definition: spi.h:501
spi_api_io_async transceive_async
Definition: spi.h:499
Complete SPI DT information.
Definition: spi.h:379
const struct device * bus
Definition: spi.h:380
struct spi_config config
Definition: spi.h:381
static fdata_t data[2]
Definition: test_fifo_contexts.c:15