Line data Source code
1 1 : /*
2 : * Copyright 2020 Google LLC
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_INCLUDE_DRIVERS_SPI_SPI_EMUL_H_
8 : #define ZEPHYR_INCLUDE_DRIVERS_SPI_SPI_EMUL_H_
9 :
10 : #include <zephyr/device.h>
11 : #include <zephyr/drivers/emul.h>
12 : #include <zephyr/drivers/spi.h>
13 : #include <zephyr/sys/slist.h>
14 : #include <zephyr/types.h>
15 :
16 : /**
17 : * @file
18 : *
19 : * @brief Public APIs for the SPI emulation drivers.
20 : */
21 :
22 : /**
23 : * @brief SPI Emulation Interface
24 : * @defgroup spi_emul_interface SPI Emulation Interface
25 : * @ingroup io_emulators
26 : * @ingroup spi_interface
27 : * @{
28 : */
29 :
30 : #ifdef __cplusplus
31 : extern "C" {
32 : #endif
33 :
34 : struct spi_msg;
35 : struct spi_emul_api;
36 :
37 : /** Node in a linked list of emulators for SPI devices */
38 1 : struct spi_emul {
39 0 : sys_snode_t node;
40 :
41 : /** Target emulator - REQUIRED for all bus emulators */
42 1 : const struct emul *target;
43 :
44 : /* API provided for this device */
45 0 : const struct spi_emul_api *api;
46 :
47 : /**
48 : * A mock API that if not NULL will take precedence over the actual API. If set, a return
49 : * value of -ENOSYS will revert back to the default api.
50 : */
51 1 : struct spi_emul_api *mock_api;
52 :
53 : /* SPI chip-select of the emulated device */
54 0 : uint16_t chipsel;
55 : };
56 :
57 : /**
58 : * Passes SPI messages to the emulator. The emulator updates the data with what
59 : * was read back.
60 : *
61 : * @param target The device Emulator instance
62 : * @param config Pointer to a valid spi_config structure instance.
63 : * Pointer-comparison may be used to detect changes from
64 : * previous operations.
65 : * @param tx_bufs Buffer array where data to be sent originates from,
66 : * or NULL if none.
67 : * @param rx_bufs Buffer array where data to be read will be written to,
68 : * or NULL if none.
69 : *
70 : * @retval 0 If successful.
71 : * @retval -EIO General input / output error.
72 : */
73 1 : typedef int (*spi_emul_io_t)(const struct emul *target, const struct spi_config *config,
74 : const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs);
75 :
76 : /**
77 : * Register an emulated device on the controller
78 : *
79 : * @param dev Device that will use the emulator
80 : * @param emul SPI emulator to use
81 : * @return 0 indicating success (always)
82 : */
83 1 : int spi_emul_register(const struct device *dev, struct spi_emul *emul);
84 :
85 : /** Definition of the emulator API */
86 1 : struct spi_emul_api {
87 0 : spi_emul_io_t io;
88 : };
89 :
90 : /**
91 : * Back door to allow an emulator to retrieve the host configuration.
92 : *
93 : * @param dev SPI device associated with the emulator
94 : * @return Bit-packed 32-bit value containing the device's runtime configuration
95 : */
96 1 : uint32_t spi_emul_get_config(const struct device *dev);
97 :
98 : #ifdef __cplusplus
99 : }
100 : #endif
101 :
102 : /**
103 : * @}
104 : */
105 :
106 : #endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_SPI_EMUL_H_ */
|