Zephyr API Documentation  2.7.0-rc2
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 <device.h>
25#include <drivers/gpio.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
34#define SPI_OP_MODE_MASTER 0
35#define SPI_OP_MODE_SLAVE BIT(0)
36#define SPI_OP_MODE_MASK 0x1
37#define SPI_OP_MODE_GET(_operation_) ((_operation_) & SPI_OP_MODE_MASK)
38
48#define SPI_MODE_CPOL BIT(1)
49
57#define SPI_MODE_CPHA BIT(2)
58
64#define SPI_MODE_LOOP BIT(3)
65
66#define SPI_MODE_MASK (0xE)
67#define SPI_MODE_GET(_mode_) \
68 ((_mode_) & SPI_MODE_MASK)
69
73#define SPI_TRANSFER_MSB (0)
74#define SPI_TRANSFER_LSB BIT(4)
75
79#define SPI_WORD_SIZE_SHIFT (5)
80#define SPI_WORD_SIZE_MASK (0x3F << SPI_WORD_SIZE_SHIFT)
81#define SPI_WORD_SIZE_GET(_operation_) \
82 (((_operation_) & SPI_WORD_SIZE_MASK) >> SPI_WORD_SIZE_SHIFT)
83
84#define SPI_WORD_SET(_word_size_) \
85 ((_word_size_) << SPI_WORD_SIZE_SHIFT)
86
93#define SPI_LINES_SINGLE (0 << 11)
94#define SPI_LINES_DUAL (1 << 11)
95#define SPI_LINES_QUAD (2 << 11)
96#define SPI_LINES_OCTAL (3 << 11)
97
98#define SPI_LINES_MASK (0x3 << 11)
99
103/* Requests - if possible - to keep CS asserted after the transaction */
104#define SPI_HOLD_ON_CS BIT(13)
105/* Keep the device locked after the transaction for the current config.
106 * Use this with extreme caution (see spi_release() below) as it will
107 * prevent other callers to access the SPI device until spi_release() is
108 * properly called.
109 */
110#define SPI_LOCK_ON BIT(14)
111
112/* Active high logic on CS - Usually, and by default, CS logic is active
113 * low. However, some devices may require the reverse logic: active high.
114 * This bit will request the controller to use that logic. Note that not
115 * all controllers are able to handle that natively. In this case deferring
116 * the CS control to a gpio line through struct spi_cs_control would be
117 * the solution.
118 */
119#define SPI_CS_ACTIVE_HIGH BIT(15)
120
139 const struct device *gpio_dev;
143};
144
145#ifndef __cplusplus
190#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
191 (&(struct spi_cs_control) { \
192 .gpio_dev = DEVICE_DT_GET( \
193 DT_SPI_DEV_CS_GPIOS_CTLR(node_id)), \
194 .delay = (delay_), \
195 .gpio_pin = DT_SPI_DEV_CS_GPIOS_PIN(node_id), \
196 .gpio_dt_flags = DT_SPI_DEV_CS_GPIOS_FLAGS(node_id), \
197 })
198
214#define SPI_CS_CONTROL_PTR_DT_INST(inst, delay_) \
215 SPI_CS_CONTROL_PTR_DT(DT_DRV_INST(inst), delay_)
216#endif
217
248
249 const struct spi_cs_control *cs;
250};
251
252#ifndef __cplusplus
272#define SPI_CONFIG_DT(node_id, operation_, delay_) \
273 { \
274 .frequency = DT_PROP(node_id, spi_max_frequency), \
275 .operation = (operation_), \
276 .slave = DT_REG_ADDR(node_id), \
277 .cs = COND_CODE_1( \
278 DT_SPI_DEV_HAS_CS_GPIOS(node_id), \
279 (SPI_CS_CONTROL_PTR_DT(node_id, delay_)), \
280 (NULL)), \
281 }
282
296#define SPI_CONFIG_DT_INST(inst, operation_, delay_) \
297 SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)
298#endif
299
307 const struct device *bus;
309};
310
311#ifndef __cplusplus
331#define SPI_DT_SPEC_GET(node_id, operation_, delay_) \
332 { \
333 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
334 .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
335 }
336
350#define SPI_DT_SPEC_INST_GET(inst, operation_, delay_) \
351 SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)
352#endif
353
362struct spi_buf {
363 void *buf;
364 size_t len;
365};
366
374 const struct spi_buf *buffers;
375 size_t count;
376};
377
383typedef int (*spi_api_io)(const struct device *dev,
384 const struct spi_config *config,
385 const struct spi_buf_set *tx_bufs,
386 const struct spi_buf_set *rx_bufs);
387
393typedef int (*spi_api_io_async)(const struct device *dev,
394 const struct spi_config *config,
395 const struct spi_buf_set *tx_bufs,
396 const struct spi_buf_set *rx_bufs,
397 struct k_poll_signal *async);
398
404typedef int (*spi_api_release)(const struct device *dev,
405 const struct spi_config *config);
406
407
412__subsystem struct spi_driver_api {
414#ifdef CONFIG_SPI_ASYNC
416#endif /* CONFIG_SPI_ASYNC */
418};
419
428static inline bool spi_is_ready(const struct spi_dt_spec *spec)
429{
430 /* Validate bus is ready */
431 if (!device_is_ready(spec->bus)) {
432 return false;
433 }
434 /* Validate CS gpio port is ready, if it is used */
435 if (spec->config.cs &&
437 return false;
438 }
439 return true;
440}
441
460__syscall int spi_transceive(const struct device *dev,
461 const struct spi_config *config,
462 const struct spi_buf_set *tx_bufs,
463 const struct spi_buf_set *rx_bufs);
464
465static inline int z_impl_spi_transceive(const struct device *dev,
466 const struct spi_config *config,
467 const struct spi_buf_set *tx_bufs,
468 const struct spi_buf_set *rx_bufs)
469{
470 const struct spi_driver_api *api =
471 (const struct spi_driver_api *)dev->api;
472
473 return api->transceive(dev, config, tx_bufs, rx_bufs);
474}
475
491static inline int spi_transceive_dt(const struct spi_dt_spec *spec,
492 const struct spi_buf_set *tx_bufs,
493 const struct spi_buf_set *rx_bufs)
494{
495 return spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
496}
497
514static inline int spi_read(const struct device *dev,
515 const struct spi_config *config,
516 const struct spi_buf_set *rx_bufs)
517{
518 return spi_transceive(dev, config, NULL, rx_bufs);
519}
520
533static inline int spi_read_dt(const struct spi_dt_spec *spec,
534 const struct spi_buf_set *rx_bufs)
535{
536 return spi_read(spec->bus, &spec->config, rx_bufs);
537}
538
555static inline int spi_write(const struct device *dev,
556 const struct spi_config *config,
557 const struct spi_buf_set *tx_bufs)
558{
559 return spi_transceive(dev, config, tx_bufs, NULL);
560}
561
574static inline int spi_write_dt(const struct spi_dt_spec *spec,
575 const struct spi_buf_set *tx_bufs)
576{
577 return spi_write(spec->bus, &spec->config, tx_bufs);
578}
579
580/* Doxygen defines this so documentation is generated. */
581#ifdef CONFIG_SPI_ASYNC
582
608static inline int spi_transceive_async(const struct device *dev,
609 const struct spi_config *config,
610 const struct spi_buf_set *tx_bufs,
611 const struct spi_buf_set *rx_bufs,
612 struct k_poll_signal *async)
613{
614 const struct spi_driver_api *api =
615 (const struct spi_driver_api *)dev->api;
616
617 return api->transceive_async(dev, config, tx_bufs, rx_bufs, async);
618}
619
643static inline int spi_read_async(const struct device *dev,
644 const struct spi_config *config,
645 const struct spi_buf_set *rx_bufs,
646 struct k_poll_signal *async)
647{
648 return spi_transceive_async(dev, config, NULL, rx_bufs, async);
649}
650
674static inline int spi_write_async(const struct device *dev,
675 const struct spi_config *config,
676 const struct spi_buf_set *tx_bufs,
677 struct k_poll_signal *async)
678{
679 return spi_transceive_async(dev, config, tx_bufs, NULL, async);
680}
681#endif /* CONFIG_SPI_ASYNC */
682
701__syscall int spi_release(const struct device *dev,
702 const struct spi_config *config);
703
704static inline int z_impl_spi_release(const struct device *dev,
705 const struct spi_config *config)
706{
707 const struct spi_driver_api *api =
708 (const struct spi_driver_api *)dev->api;
709
710 return api->release(dev, config);
711}
712
724static inline int spi_release_dt(const struct spi_dt_spec *spec)
725{
726 return spi_release(spec->bus, &spec->config);
727}
728
729#ifdef __cplusplus
730}
731#endif
732
737#include <syscalls/spi.h>
738
739#endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_H_ */
Public APIs for GPIO drivers.
static bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
Definition: device.h:686
uint8_t gpio_dt_flags_t
Provides a type to hold GPIO devicetree flags.
Definition: gpio.h:304
uint8_t gpio_pin_t
Provides a type to hold a GPIO pin index.
Definition: gpio.h:295
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:674
int spi_release(const struct device *dev, const struct spi_config *config)
Release the SPI device locked on 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:393
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:574
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:514
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:491
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:608
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:533
static bool spi_is_ready(const struct spi_dt_spec *spec)
Validate that SPI bus is ready.
Definition: spi.h:428
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:555
static int spi_release_dt(const struct spi_dt_spec *spec)
Release the SPI device specified in spi_dt_spec.
Definition: spi.h:724
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:404
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:383
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:643
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:60
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
Definition: kernel.h:5405
SPI buffer array structure.
Definition: spi.h:373
const struct spi_buf * buffers
Definition: spi.h:374
size_t count
Definition: spi.h:375
SPI buffer structure.
Definition: spi.h:362
size_t len
Definition: spi.h:364
void * buf
Definition: spi.h:363
SPI controller configuration structure.
Definition: spi.h:244
uint16_t slave
Definition: spi.h:247
const struct spi_cs_control * cs
Definition: spi.h:249
uint32_t frequency
Definition: spi.h:245
uint16_t operation
Definition: spi.h:246
SPI Chip Select control structure.
Definition: spi.h:138
uint32_t delay
Definition: spi.h:140
gpio_pin_t gpio_pin
Definition: spi.h:141
gpio_dt_flags_t gpio_dt_flags
Definition: spi.h:142
const struct device * gpio_dev
Definition: spi.h:139
SPI driver API This is the mandatory API any SPI driver needs to expose.
Definition: spi.h:412
spi_api_io transceive
Definition: spi.h:413
spi_api_release release
Definition: spi.h:417
spi_api_io_async transceive_async
Definition: spi.h:415
Complete SPI DT information.
Definition: spi.h:306
const struct device * bus
Definition: spi.h:307
struct spi_config config
Definition: spi.h:308