1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv_spi.h
7  * @brief    header file for spi driver
8  * @version  V1.0
9  * @date     02. June 2017
10  * @model    spi
11  ******************************************************************************/
12 
13 #ifndef _CSI_SPI_H_
14 #define _CSI_SPI_H_
15 
16 
17 #include <stdint.h>
18 #include <drv/common.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 /// definition for spi handle.
24 typedef void *spi_handle_t;
25 
26 /****** SPI specific error codes *****/
27 typedef enum {
28     SPI_ERROR_MODE  = (DRV_ERROR_SPECIFIC + 1),     ///< Specified Mode not supported
29     SPI_ERROR_FRAME_FORMAT,                       ///< Specified Frame Format not supported
30     SPI_ERROR_DATA_BITS,                          ///< Specified number of Data bits not supported
31     SPI_ERROR_BIT_ORDER,                          ///< Specified Bit order not supported
32     SPI_ERROR_SS_MODE                             ///< Specified Slave Select Mode not supported
33 } spi_error_e;
34 
35 /*----- SPI Control Codes: Mode -----*/
36 typedef enum {
37     SPI_MODE_INACTIVE = 0,       ///< SPI Inactive
38     SPI_MODE_MASTER,             ///< SPI Master (Output on MOSI, Input on MISO); arg = Bus Speed in bps
39     SPI_MODE_SLAVE,              ///< SPI Slave  (Output on MISO, Input on MOSI)
40     SPI_MODE_MASTER_SIMPLEX,     ///< SPI Master (Output/Input on MOSI); arg = Bus Speed in bps
41     SPI_MODE_SLAVE_SIMPLEX       ///< SPI Slave  (Output/Input on MISO)
42 } spi_mode_e;
43 
44 /*----- SPI Control Codes: Mode Parameters: Frame Format -----*/
45 typedef enum {
46     SPI_FORMAT_CPOL0_CPHA0 = 0, ///< Clock Polarity 0, Clock Phase 0
47     SPI_FORMAT_CPOL0_CPHA1,     ///< Clock Polarity 0, Clock Phase 1
48     SPI_FORMAT_CPOL1_CPHA0,     ///< Clock Polarity 1, Clock Phase 0
49     SPI_FORMAT_CPOL1_CPHA1,     ///< Clock Polarity 1, Clock Phase 1
50 } spi_format_e;
51 
52 /*----- SPI Control Codes: Mode Parameters: Bit Order -----*/
53 typedef enum {
54     SPI_ORDER_MSB2LSB = 0,  ///< SPI Bit order from MSB to LSB
55     SPI_ORDER_LSB2MSB       ///< SPI Bit order from LSB to MSB
56 } spi_bit_order_e;
57 
58 /*----- SPI Control Codes: Mode Parameters: Data Width in bits -----*/
59 #define SPI_DATAWIDTH_MAX 32         /* 1 ~ 32 bit*/
60 
61 /*----- SPI Control Codes: Mode Parameters: Slave Select Mode -----*/
62 typedef enum {
63     /*options for SPI_MODE_MASTER/SPI_MODE_MASTER_SIMPLEX */
64     SPI_SS_MASTER_UNUSED = 0,        ///< SPI Slave Select when Master: Not used.SS line is not controlled by master, For example,SS line connected to a fixed low level
65     SPI_SS_MASTER_SW,               ///< SPI Slave Select when Master: Software controlled. SS line is configured by software
66     SPI_SS_MASTER_HW_OUTPUT,         ///< SPI Slave Select when Master: Hardware controlled Output.SS line is activated or deactivated automatically by hardware
67     SPI_SS_MASTER_HW_INPUT,          ///< SPI Slave Select when Master: Hardware monitored Input.Used in multi-master configuration where a master does not drive the Slave Select when driving the bus, but rather monitors it
68     /*options for SPI_MODE_SLAVE/SPI_MODE_SLAVE_SIMPLEX */
69     SPI_SS_SLAVE_HW,                 ///< SPI Slave Select when Slave: Hardware monitored.Hardware monitors the Slave Select line and accepts transfers only when the line is active
70     SPI_SS_SLAVE_SW                  ///< SPI Slave Select when Slave: Software controlled.Used only when the Slave Select line is not used. Software controls if the slave is responding or not(enables or disables transfers)
71 } spi_ss_mode_e;
72 
73 /****** SPI Slave Select Signal definitions *****/
74 typedef enum {
75     SPI_SS_INACTIVE = 0,        ///< SPI Slave Select Signal/line Inactive
76     SPI_SS_ACTIVE               ///< SPI Slave Select Signal/line Active
77 } spi_ss_stat_e;
78 
79 /**
80 \brief SPI Status
81 */
82 typedef struct {
83     uint32_t busy       : 1;              ///< Transmitter/Receiver busy flag
84     uint32_t data_lost  : 1;              ///< Data lost: Receive overflow / Transmit underflow (cleared on start of transfer operation)
85     uint32_t mode_fault : 1;              ///< Mode fault detected; optional (cleared on start of transfer operation)
86 } spi_status_t;
87 
88 /****** SPI Event *****/
89 typedef enum {
90     SPI_EVENT_TRANSFER_COMPLETE = 0,   ///< Data Transfer completed. Occurs after call to csi_spi_send, csi_spi_receive, or csi_spi_transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation
91     SPI_EVENT_TX_COMPLETE,              ///< Data Transfer completed. Occurs after call to csi_spi_send, csi_spi_receive, or csi_spi_transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation
92     SPI_EVENT_RX_COMPLETE,              ///< Data Transfer completed. Occurs after call to csi_spi_send, csi_spi_receive, or csi_spi_transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation
93     SPI_EVENT_DATA_LOST,               ///< Data lost: Receive overflow / Transmit underflow. Occurs in slave mode when data is requested/sent by master but send/receive/transfer operation has not been started and indicates that data is lost. Occurs also in master mode when driver cannot transfer data fast enough.
94     SPI_EVENT_MODE_FAULT               ///< Master Mode Fault (SS deactivated when Master).Occurs in master mode when Slave Select is deactivated and indicates Master Mode Fault. The driver is ready for the next transfer operation.
95 } spi_event_e;
96 
97 typedef void (*spi_event_cb_t)(int32_t idx, spi_event_e event);  ///< Pointer to \ref spi_event_cb_t : SPI Event call back.
98 
99 /**
100 \brief SPI Driver Capabilities.
101 */
102 typedef struct {
103     uint32_t simplex          : 1;        ///< supports Simplex Mode (Master and Slave)
104     uint32_t ti_ssi           : 1;        ///< supports TI Synchronous Serial Interface
105     uint32_t microwire        : 1;        ///< supports Microwire Interface
106     uint32_t event_mode_fault : 1;        ///< Signal Mode Fault event: \ref spi_event_e
107 } spi_capabilities_t;
108 
109 /**
110   \brief       Initialize SPI Interface. 1. Initializes the resources needed for the SPI interface 2.registers event callback function
111   \param[in]   idx spi index
112   \param[in]   cb_event  event callback function \ref spi_event_cb_t
113   \param[in]   cb_arg    argument for call back function
114   \return      return spi handle if success
115 */
116 spi_handle_t csi_spi_initialize(int32_t idx, spi_event_cb_t cb_event);
117 
118 /**
119   \brief       De-initialize SPI Interface. stops operation and releases the software resources used by the interface
120   \param[in]   handle spi handle to operate.
121   \return      error code
122 */
123 int32_t csi_spi_uninitialize(spi_handle_t handle);
124 
125 /**
126   \brief       Get driver capabilities.
127   \param[in]   idx spi index
128   \return      \ref spi_capabilities_t
129 */
130 spi_capabilities_t csi_spi_get_capabilities(int32_t idx);
131 
132 /**
133   \brief       config spi mode.
134   \param[in]   handle spi handle to operate.
135   \param[in]   baud      spi baud rate. if negative, then this attribute not changed
136   \param[in]   mode      \ref spi_mode_e . if negative, then this attribute not changed
137   \param[in]   format    \ref spi_format_e . if negative, then this attribute not changed
138   \param[in]   order     \ref spi_bit_order_e . if negative, then this attribute not changed
139   \param[in]   ss_mode   \ref spi_ss_mode_e . if negative, then this attribute not changed
140   \param[in]   bit_width spi data bitwidth: (1 ~ SPI_DATAWIDTH_MAX) . if negative, then this attribute not changed
141   \return      error code
142 */
143 int32_t csi_spi_config(spi_handle_t handle,
144                        int32_t          baud,
145                        spi_mode_e       mode,
146                        spi_format_e     format,
147                        spi_bit_order_e  order,
148                        spi_ss_mode_e    ss_mode,
149                        int32_t        bit_width);
150 
151 
152 /**
153   \brief         sending data to SPI transmitter,(received data is ignored).
154                if non-blocking mode, this function only starts the sending,
155                \ref spi_event_e is signaled when operation completes or error happens.
156                \ref csi_spi_get_status can get operation status.
157                if blocking mode, this function returns after operation completes or error happens.
158   \param[in]   handle spi handle to operate.
159   \param[in]   data  Pointer to buffer with data to send to SPI transmitter. data_type is : uint8_t for 1..8 data bits, uint16_t for 9..16 data bits,uint32_t for 17..32 data bits,
160   \param[in]   num   Number of data items to send.
161   \return      error code
162 */
163 int32_t csi_spi_send(spi_handle_t handle, const void *data, uint32_t num);
164 
165 /**
166   \brief       receiving data from SPI receiver. if non-blocking mode, this function only starts the receiving,
167                \ref spi_event_e is signaled when operation completes or error happens.
168                \ref csi_spi_get_status can get operation status.
169                if blocking mode, this function returns after operation completes or error happens.
170   \param[in]   handle spi handle to operate.
171   \param[out]  data  Pointer to buffer for data to receive from SPI receiver
172   \param[in]   num   Number of data items to receive
173   \return      error code
174 */
175 int32_t csi_spi_receive(spi_handle_t handle, void *data, uint32_t num);
176 
177 /**
178   \brief       sending/receiving data to/from SPI transmitter/receiver.
179                if non-blocking mode, this function only starts the transfer,
180                \ref spi_event_e is signaled when operation completes or error happens.
181                \ref csi_spi_get_status can get operation status.
182                if blocking mode, this function returns after operation completes or error happens.
183   \param[in]   handle spi handle to operate.
184   \param[in]   data_out  Pointer to buffer with data to send to SPI transmitter
185   \param[out]  data_in   Pointer to buffer for data to receive from SPI receiver
186   \param[in]   num_out      Number of data items to send
187   \param[in]   num_in       Number of data items to receive
188   \return      error code
189 */
190 int32_t csi_spi_transfer(spi_handle_t handle, const void *data_out, void *data_in, uint32_t num_out, uint32_t num_in);
191 
192 /**
193   \brief       abort spi transfer.
194   \param[in]   handle spi handle to operate.
195   \return      error code
196 */
197 int32_t csi_spi_abort_transfer(spi_handle_t handle);
198 
199 /**
200   \brief       Get SPI status.
201   \param[in]   handle spi handle to operate.
202   \return      SPI status \ref spi_status_t
203 */
204 spi_status_t csi_spi_get_status(spi_handle_t handle);
205 
206 /**
207   \brief       config the SPI mode.
208   \param[in]   handle   spi handle
209   \param[in]   mode     spi mode. \ref spi_mode_e
210   \return      error code
211 */
212 int32_t csi_spi_config_mode(spi_handle_t handle, spi_mode_e  mode);
213 
214 /**
215   \brief       config the SPI block mode.
216   \param[in]   handle   spi handle
217   \param[in]   flag 1 - enbale the block mode. 0 - disable the block mode
218   \return      error code
219 */
220 int32_t csi_spi_config_block_mode(spi_handle_t handle, int32_t flag);
221 
222 /**
223   \brief       Set the SPI baudrate.
224   \param[in]   handle   spi handle
225   \param[in]   baud     spi baud rate
226   \return      error code
227 */
228 int32_t csi_spi_config_baudrate(spi_handle_t handle, uint32_t baud);
229 
230 /**
231   \brief       config the SPI bit order.
232   \param[in]   handle   spi handle
233   \param[in]   order    spi bit order.\ref spi_bit_order_e
234   \return      error code
235 */
236 int32_t csi_spi_config_bit_order(spi_handle_t handle, spi_bit_order_e order);
237 
238 /**
239   \brief       Set the SPI datawidth.
240   \param[in]   handle     spi handle
241   \param[in]   datawidth  date frame size in bits
242   \return      error code
243 */
244 int32_t csi_spi_config_datawidth(spi_handle_t handle, uint32_t datawidth);
245 
246 /**
247   \brief       config the SPI format.
248   \param[in]   handle   spi handle
249   \param[in]   format   spi format. \ref spi_format_e
250   \return      error code
251 */
252 int32_t csi_spi_config_format(spi_handle_t handle, spi_format_e format);
253 
254 /**
255   \brief       config the SPI slave select mode.
256   \param[in]   handle   spi handle
257   \param[in]   ss_mode  spi slave select mode. \ref spi_ss_mode_e
258   \return      error code
259 */
260 int32_t csi_spi_config_ss_mode(spi_handle_t handle, spi_ss_mode_e ss_mode);
261 
262 /**
263   \brief       Get the number of the currently transferred.
264   \param[in]   handle  spi handle to operate.
265   \return      the number of the currently transferred data items
266 */
267 uint32_t csi_spi_get_data_count(spi_handle_t handle);
268 
269 /**
270   \brief       control spi power.
271   \param[in]   handle  spi handle to operate.
272   \param[in]   state   power state.\ref csi_power_stat_e.
273   \return      error code
274 */
275 int32_t csi_spi_power_control(spi_handle_t handle, csi_power_stat_e state);
276 
277 /**
278   \brief       Control the Slave Select signal (SS).
279   \param[in]   handle  spi handle to operate.
280   \param[in]   stat    SS state. \ref spi_ss_stat_e.
281   \return      error code
282 */
283 int32_t csi_spi_ss_control(spi_handle_t handle, spi_ss_stat_e stat);
284 
285 
286 #ifdef __cplusplus
287 }
288 #endif
289 
290 #endif /* _CSI_SPI_H_ */
291