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 * @{ 27 */ 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 struct spi_msg; 34 struct spi_emul_api; 35 36 /** Node in a linked list of emulators for SPI devices */ 37 struct spi_emul { 38 sys_snode_t node; 39 40 /** Target emulator - REQUIRED for all bus emulators */ 41 const struct emul *target; 42 43 /* API provided for this device */ 44 const struct spi_emul_api *api; 45 46 /** 47 * A mock API that if not NULL will take precedence over the actual API. If set, a return 48 * value of -ENOSYS will revert back to the default api. 49 */ 50 struct spi_emul_api *mock_api; 51 52 /* SPI chip-select of the emulated device */ 53 uint16_t chipsel; 54 }; 55 56 /** 57 * Passes SPI messages to the emulator. The emulator updates the data with what 58 * was read back. 59 * 60 * @param target The device Emulator instance 61 * @param config Pointer to a valid spi_config structure instance. 62 * Pointer-comparison may be used to detect changes from 63 * previous operations. 64 * @param tx_bufs Buffer array where data to be sent originates from, 65 * or NULL if none. 66 * @param rx_bufs Buffer array where data to be read will be written to, 67 * or NULL if none. 68 * 69 * @retval 0 If successful. 70 * @retval -EIO General input / output error. 71 */ 72 typedef int (*spi_emul_io_t)(const struct emul *target, const struct spi_config *config, 73 const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs); 74 75 /** 76 * Register an emulated device on the controller 77 * 78 * @param dev Device that will use the emulator 79 * @param emul SPI emulator to use 80 * @return 0 indicating success (always) 81 */ 82 int spi_emul_register(const struct device *dev, struct spi_emul *emul); 83 84 /** Definition of the emulator API */ 85 struct spi_emul_api { 86 spi_emul_io_t io; 87 }; 88 89 /** 90 * Back door to allow an emulator to retrieve the host configuration. 91 * 92 * @param dev SPI device associated with the emulator 93 * @return Bit-packed 32-bit value containing the device's runtime configuration 94 */ 95 uint32_t spi_emul_get_config(const struct device *dev); 96 97 #ifdef __cplusplus 98 } 99 #endif 100 101 /** 102 * @} 103 */ 104 105 #endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_SPI_EMUL_H_ */ 106