Zephyr Project API  3.1.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)
152 union {
154 struct {
155 const struct device *gpio_dev __deprecated;
156 gpio_pin_t gpio_pin __deprecated;
158 };
159 };
165};
166
204#define SPI_CS_GPIOS_DT_SPEC_GET(spi_dev) \
205 GPIO_DT_SPEC_GET_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
206
207#ifndef __cplusplus
252#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
253 (&(struct spi_cs_control) { \
254 { \
255 .gpio = SPI_CS_GPIOS_DT_SPEC_GET(node_id),\
256 }, \
257 .delay = (delay_), \
258 })
259
275#define SPI_CS_CONTROL_PTR_DT_INST(inst, delay_) \
276 SPI_CS_CONTROL_PTR_DT(DT_DRV_INST(inst), delay_)
277#endif
278
307#if defined(CONFIG_SPI_EXTENDED_MODES)
310 uint16_t _unused;
311#else
314#endif /* CONFIG_SPI_EXTENDED_MODES */
315
316 const struct spi_cs_control *cs;
317};
318
319#ifndef __cplusplus
339#define SPI_CONFIG_DT(node_id, operation_, delay_) \
340 { \
341 .frequency = DT_PROP(node_id, spi_max_frequency), \
342 .operation = (operation_) | \
343 DT_PROP(node_id, duplex) | \
344 DT_PROP(node_id, frame_format), \
345 .slave = DT_REG_ADDR(node_id), \
346 .cs = COND_CODE_1( \
347 DT_SPI_DEV_HAS_CS_GPIOS(node_id), \
348 (SPI_CS_CONTROL_PTR_DT(node_id, delay_)), \
349 (NULL)), \
350 }
351
365#define SPI_CONFIG_DT_INST(inst, operation_, delay_) \
366 SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)
367#endif
368
376 const struct device *bus;
378};
379
380#ifndef __cplusplus
400#define SPI_DT_SPEC_GET(node_id, operation_, delay_) \
401 { \
402 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
403 .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
404 }
405
419#define SPI_DT_SPEC_INST_GET(inst, operation_, delay_) \
420 SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)
421#endif
422
431struct spi_buf {
432 void *buf;
433 size_t len;
434};
435
443 const struct spi_buf *buffers;
444 size_t count;
445};
446
452typedef int (*spi_api_io)(const struct device *dev,
453 const struct spi_config *config,
454 const struct spi_buf_set *tx_bufs,
455 const struct spi_buf_set *rx_bufs);
456
462typedef int (*spi_api_io_async)(const struct device *dev,
463 const struct spi_config *config,
464 const struct spi_buf_set *tx_bufs,
465 const struct spi_buf_set *rx_bufs,
466 struct k_poll_signal *async);
467
473typedef int (*spi_api_release)(const struct device *dev,
474 const struct spi_config *config);
475
476
481__subsystem struct spi_driver_api {
483#ifdef CONFIG_SPI_ASYNC
485#endif /* CONFIG_SPI_ASYNC */
487};
488
497static inline bool spi_is_ready(const struct spi_dt_spec *spec)
498{
499 /* Validate bus is ready */
500 if (!device_is_ready(spec->bus)) {
501 return false;
502 }
503 /* Validate CS gpio port is ready, if it is used */
504 if (spec->config.cs &&
505 !device_is_ready(spec->config.cs->gpio.port)) {
506 return false;
507 }
508 return true;
509}
510
529__syscall int spi_transceive(const struct device *dev,
530 const struct spi_config *config,
531 const struct spi_buf_set *tx_bufs,
532 const struct spi_buf_set *rx_bufs);
533
534static inline int z_impl_spi_transceive(const struct device *dev,
535 const struct spi_config *config,
536 const struct spi_buf_set *tx_bufs,
537 const struct spi_buf_set *rx_bufs)
538{
539 const struct spi_driver_api *api =
540 (const struct spi_driver_api *)dev->api;
541
542 return api->transceive(dev, config, tx_bufs, rx_bufs);
543}
544
560static inline int spi_transceive_dt(const struct spi_dt_spec *spec,
561 const struct spi_buf_set *tx_bufs,
562 const struct spi_buf_set *rx_bufs)
563{
564 return spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
565}
566
583static inline int spi_read(const struct device *dev,
584 const struct spi_config *config,
585 const struct spi_buf_set *rx_bufs)
586{
587 return spi_transceive(dev, config, NULL, rx_bufs);
588}
589
602static inline int spi_read_dt(const struct spi_dt_spec *spec,
603 const struct spi_buf_set *rx_bufs)
604{
605 return spi_read(spec->bus, &spec->config, rx_bufs);
606}
607
624static inline int spi_write(const struct device *dev,
625 const struct spi_config *config,
626 const struct spi_buf_set *tx_bufs)
627{
628 return spi_transceive(dev, config, tx_bufs, NULL);
629}
630
643static inline int spi_write_dt(const struct spi_dt_spec *spec,
644 const struct spi_buf_set *tx_bufs)
645{
646 return spi_write(spec->bus, &spec->config, tx_bufs);
647}
648
649/* Doxygen defines this so documentation is generated. */
650#ifdef CONFIG_SPI_ASYNC
651
677static inline int spi_transceive_async(const struct device *dev,
678 const struct spi_config *config,
679 const struct spi_buf_set *tx_bufs,
680 const struct spi_buf_set *rx_bufs,
681 struct k_poll_signal *async)
682{
683 const struct spi_driver_api *api =
684 (const struct spi_driver_api *)dev->api;
685
686 return api->transceive_async(dev, config, tx_bufs, rx_bufs, async);
687}
688
712static inline int spi_read_async(const struct device *dev,
713 const struct spi_config *config,
714 const struct spi_buf_set *rx_bufs,
715 struct k_poll_signal *async)
716{
717 return spi_transceive_async(dev, config, NULL, rx_bufs, async);
718}
719
743static inline int spi_write_async(const struct device *dev,
744 const struct spi_config *config,
745 const struct spi_buf_set *tx_bufs,
746 struct k_poll_signal *async)
747{
748 return spi_transceive_async(dev, config, tx_bufs, NULL, async);
749}
750#endif /* CONFIG_SPI_ASYNC */
751
772__syscall int spi_release(const struct device *dev,
773 const struct spi_config *config);
774
775static inline int z_impl_spi_release(const struct device *dev,
776 const struct spi_config *config)
777{
778 const struct spi_driver_api *api =
779 (const struct spi_driver_api *)dev->api;
780
781 return api->release(dev, config);
782}
783
795static inline int spi_release_dt(const struct spi_dt_spec *spec)
796{
797 return spi_release(spec->bus, &spec->config);
798}
799
800#ifdef __cplusplus
801}
802#endif
803
808#include <syscalls/spi.h>
809
810#endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_H_ */
Public APIs for GPIO drivers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition: gpio.h:259
uint16_t gpio_dt_flags_t
Provides a type to hold GPIO devicetree flags.
Definition: gpio.h:271
static 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)
Write the specified amount of data from the SPI driver.
Definition: spi.h:743
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.
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)
Definition: spi.h:462
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:643
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:583
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:560
static 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)
Read/write the specified amount of data from the SPI driver.
Definition: spi.h:677
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:602
static bool spi_is_ready(const struct spi_dt_spec *spec)
Validate that SPI bus is ready.
Definition: spi.h:497
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:624
static int spi_release_dt(const struct spi_dt_spec *spec)
Release the SPI device specified in spi_dt_spec.
Definition: spi.h:795
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:473
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:452
static 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)
Read the specified amount of data from the SPI driver.
Definition: spi.h:712
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:456
const void * api
Definition: device.h:462
Container for GPIO pin information specified in devicetree.
Definition: gpio.h:293
const struct device * port
Definition: gpio.h:295
Definition: kernel.h:5459
SPI buffer array structure.
Definition: spi.h:442
const struct spi_buf * buffers
Definition: spi.h:443
size_t count
Definition: spi.h:444
SPI buffer structure.
Definition: spi.h:431
size_t len
Definition: spi.h:433
void * buf
Definition: spi.h:432
SPI controller configuration structure.
Definition: spi.h:305
uint16_t slave
Definition: spi.h:313
const struct spi_cs_control * cs
Definition: spi.h:316
uint32_t frequency
Definition: spi.h:306
uint16_t operation
Definition: spi.h:312
SPI Chip Select control structure.
Definition: spi.h:144
uint32_t delay
Definition: spi.h:164
gpio_pin_t gpio_pin
Definition: spi.h:156
gpio_dt_flags_t gpio_dt_flags
Definition: spi.h:157
struct gpio_dt_spec gpio
Definition: spi.h:153
const struct device * gpio_dev
Definition: spi.h:155
SPI driver API This is the mandatory API any SPI driver needs to expose.
Definition: spi.h:481
spi_api_io transceive
Definition: spi.h:482
spi_api_release release
Definition: spi.h:486
spi_api_io_async transceive_async
Definition: spi.h:484
Complete SPI DT information.
Definition: spi.h:375
const struct device * bus
Definition: spi.h:376
struct spi_config config
Definition: spi.h:377