Zephyr Project API  3.2.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
28#ifdef __cplusplus
29extern "C" {
30#endif
31
36#define SPI_OP_MODE_MASTER 0U
37#define SPI_OP_MODE_SLAVE BIT(0)
38#define SPI_OP_MODE_MASK 0x1U
39#define SPI_OP_MODE_GET(_operation_) ((_operation_) & SPI_OP_MODE_MASK)
52#define SPI_MODE_CPOL BIT(1)
53
61#define SPI_MODE_CPHA BIT(2)
62
68#define SPI_MODE_LOOP BIT(3)
69
70#define SPI_MODE_MASK (0xEU)
71#define SPI_MODE_GET(_mode_) \
72 ((_mode_) & SPI_MODE_MASK)
73
80#define SPI_TRANSFER_MSB (0U)
81#define SPI_TRANSFER_LSB BIT(4)
88#define SPI_WORD_SIZE_SHIFT (5U)
89#define SPI_WORD_SIZE_MASK (0x3FU << SPI_WORD_SIZE_SHIFT)
90#define SPI_WORD_SIZE_GET(_operation_) \
91 (((_operation_) & SPI_WORD_SIZE_MASK) >> SPI_WORD_SIZE_SHIFT)
92
93#define SPI_WORD_SET(_word_size_) \
94 ((_word_size_) << SPI_WORD_SIZE_SHIFT)
101/* Requests - if possible - to keep CS asserted after the transaction */
102#define SPI_HOLD_ON_CS BIT(12)
103/* Keep the device locked after the transaction for the current config.
104 * Use this with extreme caution (see spi_release() below) as it will
105 * prevent other callers to access the SPI device until spi_release() is
106 * properly called.
107 */
108#define SPI_LOCK_ON BIT(13)
109
110/* Active high logic on CS - Usually, and by default, CS logic is active
111 * low. However, some devices may require the reverse logic: active high.
112 * This bit will request the controller to use that logic. Note that not
113 * all controllers are able to handle that natively. In this case deferring
114 * the CS control to a gpio line through struct spi_cs_control would be
115 * the solution.
116 */
117#define SPI_CS_ACTIVE_HIGH BIT(14)
129#define SPI_LINES_SINGLE (0U << 16)
130#define SPI_LINES_DUAL (1U << 16)
131#define SPI_LINES_QUAD (2U << 16)
132#define SPI_LINES_OCTAL (3U << 16)
133
134#define SPI_LINES_MASK (0x3U << 16)
158};
159
197#define SPI_CS_GPIOS_DT_SPEC_GET(spi_dev) \
198 GPIO_DT_SPEC_GET_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
199
209#define SPI_CS_GPIOS_DT_SPEC_INST_GET(inst) \
210 SPI_CS_GPIOS_DT_SPEC_GET(DT_DRV_INST(inst))
211
212#ifndef __cplusplus
257#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
258 (&(struct spi_cs_control) { \
259 .gpio = SPI_CS_GPIOS_DT_SPEC_GET(node_id), \
260 .delay = (delay_), \
261 })
262
278#define SPI_CS_CONTROL_PTR_DT_INST(inst, delay_) \
279 SPI_CS_CONTROL_PTR_DT(DT_DRV_INST(inst), delay_)
280#endif
281
310#if defined(CONFIG_SPI_EXTENDED_MODES)
313 uint16_t _unused;
314#else
317#endif /* CONFIG_SPI_EXTENDED_MODES */
318
319 const struct spi_cs_control *cs;
320};
321
322#ifndef __cplusplus
342#define SPI_CONFIG_DT(node_id, operation_, delay_) \
343 { \
344 .frequency = DT_PROP(node_id, spi_max_frequency), \
345 .operation = (operation_) | \
346 DT_PROP(node_id, duplex) | \
347 DT_PROP(node_id, frame_format), \
348 .slave = DT_REG_ADDR(node_id), \
349 .cs = COND_CODE_1( \
350 DT_SPI_DEV_HAS_CS_GPIOS(node_id), \
351 (SPI_CS_CONTROL_PTR_DT(node_id, delay_)), \
352 (NULL)), \
353 }
354
368#define SPI_CONFIG_DT_INST(inst, operation_, delay_) \
369 SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)
370#endif
371
379 const struct device *bus;
381};
382
383#ifndef __cplusplus
403#define SPI_DT_SPEC_GET(node_id, operation_, delay_) \
404 { \
405 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
406 .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
407 }
408
422#define SPI_DT_SPEC_INST_GET(inst, operation_, delay_) \
423 SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)
424#endif
425
434struct spi_buf {
435 void *buf;
436 size_t len;
437};
438
446 const struct spi_buf *buffers;
447 size_t count;
448};
449
455typedef int (*spi_api_io)(const struct device *dev,
456 const struct spi_config *config,
457 const struct spi_buf_set *tx_bufs,
458 const struct spi_buf_set *rx_bufs);
459
467typedef void (*spi_callback_t)(const struct device *dev, int result, void *data);
468
474typedef int (*spi_api_io_async)(const struct device *dev,
475 const struct spi_config *config,
476 const struct spi_buf_set *tx_bufs,
477 const struct spi_buf_set *rx_bufs,
479 void *userdata);
480
486typedef int (*spi_api_release)(const struct device *dev,
487 const struct spi_config *config);
488
489
494__subsystem struct spi_driver_api {
496#ifdef CONFIG_SPI_ASYNC
498#endif /* CONFIG_SPI_ASYNC */
500};
501
510static inline bool spi_is_ready(const struct spi_dt_spec *spec)
511{
512 /* Validate bus is ready */
513 if (!device_is_ready(spec->bus)) {
514 return false;
515 }
516 /* Validate CS gpio port is ready, if it is used */
517 if (spec->config.cs &&
518 !device_is_ready(spec->config.cs->gpio.port)) {
519 return false;
520 }
521 return true;
522}
523
542__syscall int spi_transceive(const struct device *dev,
543 const struct spi_config *config,
544 const struct spi_buf_set *tx_bufs,
545 const struct spi_buf_set *rx_bufs);
546
547static inline int z_impl_spi_transceive(const struct device *dev,
548 const struct spi_config *config,
549 const struct spi_buf_set *tx_bufs,
550 const struct spi_buf_set *rx_bufs)
551{
552 const struct spi_driver_api *api =
553 (const struct spi_driver_api *)dev->api;
554
555 return api->transceive(dev, config, tx_bufs, rx_bufs);
556}
557
573static inline int spi_transceive_dt(const struct spi_dt_spec *spec,
574 const struct spi_buf_set *tx_bufs,
575 const struct spi_buf_set *rx_bufs)
576{
577 return spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
578}
579
596static inline int spi_read(const struct device *dev,
597 const struct spi_config *config,
598 const struct spi_buf_set *rx_bufs)
599{
600 return spi_transceive(dev, config, NULL, rx_bufs);
601}
602
615static inline int spi_read_dt(const struct spi_dt_spec *spec,
616 const struct spi_buf_set *rx_bufs)
617{
618 return spi_read(spec->bus, &spec->config, rx_bufs);
619}
620
637static inline int spi_write(const struct device *dev,
638 const struct spi_config *config,
639 const struct spi_buf_set *tx_bufs)
640{
641 return spi_transceive(dev, config, tx_bufs, NULL);
642}
643
656static inline int spi_write_dt(const struct spi_dt_spec *spec,
657 const struct spi_buf_set *tx_bufs)
658{
659 return spi_write(spec->bus, &spec->config, tx_bufs);
660}
661
662/* Doxygen defines this so documentation is generated. */
663#ifdef CONFIG_SPI_ASYNC
664
691static inline int spi_transceive_cb(const struct device *dev,
692 const struct spi_config *config,
693 const struct spi_buf_set *tx_bufs,
694 const struct spi_buf_set *rx_bufs,
695 spi_callback_t callback,
696 void *userdata)
697{
698 const struct spi_driver_api *api =
699 (const struct spi_driver_api *)dev->api;
700
701 return api->transceive_async(dev, config, tx_bufs, rx_bufs, callback, userdata);
702}
703
704#ifdef CONFIG_POLL
705
707void z_spi_transfer_signal_cb(const struct device *dev, int result, void *userdata);
735static inline int spi_transceive_signal(const struct device *dev,
736 const struct spi_config *config,
737 const struct spi_buf_set *tx_bufs,
738 const struct spi_buf_set *rx_bufs,
739 struct k_poll_signal *sig)
740{
741 const struct spi_driver_api *api =
742 (const struct spi_driver_api *)dev->api;
743 spi_callback_t cb = (sig == NULL) ? NULL : z_spi_transfer_signal_cb;
744
745 return api->transceive_async(dev, config, tx_bufs, rx_bufs, cb, sig);
746}
747
754__deprecated static inline int spi_transceive_async(const struct device *dev,
755 const struct spi_config *config,
756 const struct spi_buf_set *tx_bufs,
757 const struct spi_buf_set *rx_bufs,
758 struct k_poll_signal *sig)
759{
760 return spi_transceive_signal(dev, config, tx_bufs, rx_bufs, sig);
761}
762
786static inline int spi_read_signal(const struct device *dev,
787 const struct spi_config *config,
788 const struct spi_buf_set *rx_bufs,
789 struct k_poll_signal *sig)
790{
791 return spi_transceive_signal(dev, config, NULL, rx_bufs, sig);
792}
793
800__deprecated static inline int spi_read_async(const struct device *dev,
801 const struct spi_config *config,
802 const struct spi_buf_set *rx_bufs,
803 struct k_poll_signal *sig)
804{
805 return spi_read_signal(dev, config, rx_bufs, sig);
806}
807
831static inline int spi_write_signal(const struct device *dev,
832 const struct spi_config *config,
833 const struct spi_buf_set *tx_bufs,
834 struct k_poll_signal *sig)
835{
836 return spi_transceive_signal(dev, config, tx_bufs, NULL, sig);
837}
838
845__deprecated static inline int spi_write_async(const struct device *dev,
846 const struct spi_config *config,
847 const struct spi_buf_set *tx_bufs,
848 struct k_poll_signal *sig)
849{
850 return spi_write_signal(dev, config, tx_bufs, sig);
851}
852
853#endif /* CONFIG_POLL */
854
855#endif /* CONFIG_SPI_ASYNC */
856
877__syscall int spi_release(const struct device *dev,
878 const struct spi_config *config);
879
880static inline int z_impl_spi_release(const struct device *dev,
881 const struct spi_config *config)
882{
883 const struct spi_driver_api *api =
884 (const struct spi_driver_api *)dev->api;
885
886 return api->release(dev, config);
887}
888
900static inline int spi_release_dt(const struct spi_dt_spec *spec)
901{
902 return spi_release(spec->bus, &spec->config);
903}
904
905#ifdef __cplusplus
906}
907#endif
908
913#include <syscalls/spi.h>
914
915#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:474
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:467
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:656
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:596
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:573
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:691
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:615
static bool spi_is_ready(const struct spi_dt_spec *spec)
Validate that SPI bus is ready.
Definition: spi.h:510
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:637
static int spi_release_dt(const struct spi_dt_spec *spec)
Release the SPI device specified in spi_dt_spec.
Definition: spi.h:900
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:486
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:455
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.
__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:435
const void * api
Definition: device.h:441
Container for GPIO pin information specified in devicetree.
Definition: gpio.h:293
const struct device * port
Definition: gpio.h:295
Definition: kernel.h:5396
Definition: errno.c:37
SPI buffer array structure.
Definition: spi.h:445
const struct spi_buf * buffers
Definition: spi.h:446
size_t count
Definition: spi.h:447
SPI buffer structure.
Definition: spi.h:434
size_t len
Definition: spi.h:436
void * buf
Definition: spi.h:435
SPI controller configuration structure.
Definition: spi.h:308
uint16_t slave
Definition: spi.h:316
const struct spi_cs_control * cs
Definition: spi.h:319
uint32_t frequency
Definition: spi.h:309
uint16_t operation
Definition: spi.h:315
SPI Chip Select control structure.
Definition: spi.h:144
uint32_t delay
Definition: spi.h:157
struct gpio_dt_spec gpio
Definition: spi.h:152
SPI driver API This is the mandatory API any SPI driver needs to expose.
Definition: spi.h:494
spi_api_io transceive
Definition: spi.h:495
spi_api_release release
Definition: spi.h:499
spi_api_io_async transceive_async
Definition: spi.h:497
Complete SPI DT information.
Definition: spi.h:378
const struct device * bus
Definition: spi.h:379
struct spi_config config
Definition: spi.h:380
static fdata_t data[2]
Definition: test_fifo_contexts.c:15