Software APIs
dif_spi_device.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors.
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_SPI_DEVICE_H_
6 #define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_SPI_DEVICE_H_
7 
8 /**
9  * @file
10  * @brief <a href="/hw/ip/spi_device/doc/">SPI Device</a> Device Interface
11  * Functions
12  */
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
20 
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif // __cplusplus
26 
27 /**
28  * A signal edge type: positive or negative.
29  */
30 typedef enum dif_spi_device_edge {
31  /**
32  * Represents a positive edge (i.e., from low to high).
33  */
35  /**
36  * Represents a negative edge (i.e., from high to low).
37  */
40 
41 /**
42  * A bit ordering within a byte.
43  */
45  /**
46  * Represents the most-significant-bit to least-significant-bit order.
47  */
49  /**
50  * Represents the least-significant-bit to most-significant-bit order.
51  */
54 
55 /**
56  * Runtime configuration for SPI.
57  *
58  * This struct describes runtime information for one-time configuration of the
59  * hardware.
60  */
61 typedef struct dif_spi_device_config {
62  dif_spi_device_edge_t clock_polarity;
63  dif_spi_device_edge_t data_phase;
64  dif_spi_device_bit_order_t tx_order;
65  dif_spi_device_bit_order_t rx_order;
66  uint8_t rx_fifo_timeout;
67  /**
68  * The length, in bytes, that should be reserved for the RX FIFO.
69  *
70  * `kDifSpiDeviceBufferLen / 2` is a good default for this value.
71  */
72  uint16_t rx_fifo_len;
73  /**
74  * The length, in bytes, that should be reserved for the TX FIFO.
75  *
76  * `kDifSpiDeviceBufferLen / 2` is a good default for this value.
77  */
78  uint16_t tx_fifo_len;
80 
81 /**
82  * The length of the SPI device FIFO buffer, in bytes.
83  *
84  * Useful for initializing FIFO lengths: for example, for equally-sized FIFOs,
85  * `rx_fifo_len` and `tx_fifo_len` would be set to `kDifSpiDeviceBufferLen / 2`.
86  */
87 extern const uint16_t kDifSpiDeviceBufferLen;
88 
89 /**
90  * Configures SPI with runtime information.
91  *
92  * This function should need to be called once for the lifetime of `handle`.
93  *
94  * @param spi A SPI handle.
95  * @param config Runtime configuration parameters.
96  * @return The result of the operation.
97  */
100  const dif_spi_device_config_t *config);
101 
102 /**
103  * Issues an "abort" to the given SPI device, causing all in-progress IO to
104  * halt.
105  *
106  * @param spi A SPI handle.
107  * @return The result of the operation.
108  */
111 
112 /**
113  * Sets up the "FIFO level" (that is, number of bytes present in a particular
114  * FIFO) at which the TxLevel and RxLevel IRQs will fire.
115  *
116  * For the reciept side, when the FIFO overflows `rx_level` (i.e., goes over
117  * the set level), an interrupt is fired. This can be used to detect that more
118  * data should be read from the RX FIFO. This is the
119  * `Spi Buffer -> Main Memory` case.
120  *
121  * Conversely, for the transmission side, when the FIFO underflows `tx_level`
122  * (i.e., goes below the set level), an interrupt is fired. This can be used
123  * to detect that there is free space to write more data to the TX FIFO.
124  * This is the `Main Memory -> Spi Buffer` case.
125  *
126  * @param spi A SPI handle.
127  * @param rx_level The new RX level, as described above.
128  * @param tx_level The new TX level, as described above.
129  * @return The result of the operation.
130  */
133  uint16_t rx_level,
134  uint16_t tx_level);
135 
136 /**
137  * Returns the number of bytes still pending receipt by software in the RX FIFO.
138  *
139  * @param spi A SPI handle.
140  * @param[out] bytes_pending The number of bytes pending
141  * @return The result of the operation.
142  */
145  const dif_spi_device_config_t *config,
146  size_t *bytes_pending);
147 
148 /**
149  * Returns the number of bytes still pending transmission by hardware in the TX
150  * FIFO.
151  *
152  * @param spi A SPI handle.
153  * @param[out] bytes_pending The number of bytes pending
154  * @return The result of the operation.
155  */
158  const dif_spi_device_config_t *config,
159  size_t *bytes_pending);
160 
161 /**
162  * Reads at most `buf_len` bytes from the RX FIFO; the number of bytes read
163  * will be written to `bytes_received`.
164  *
165  * @param spi A SPI device.
166  * @param[out] buf A pointer to valid memory.
167  * @param buf_len The length of the buffer `buf` points to.
168  * @param[out] bytes_received The number of bytes successfully read; may be
169  * null.
170  * @return The result of the operation.
171  */
174  const dif_spi_device_config_t *config,
175  void *buf, size_t buf_len,
176  size_t *bytes_received);
177 
178 /**
179  * Writes at most `buf_len` bytes to the TX FIFO; the number of bytes actually
180  * written will be written to `bytes_sent`.
181  *
182  * @param spi A SPI device.
183  * @param buf A pointer to bytes to be written.
184  * @param buf_len The length of the buffer `buf` points to.
185  * @param[out] bytes_sent The number of bytes successfully written; may be null.
186  * @return The result of the operation.
187  */
190  const dif_spi_device_config_t *config,
191  const void *buf, size_t buf_len,
192  size_t *bytes_sent);
193 
194 #ifdef __cplusplus
195 } // extern "C"
196 #endif // __cplusplus
197 
198 #endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_SPI_DEVICE_H_