1 /**
2  * @file spi.h
3  * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
4  */
5 
6 #ifndef HAL_SPI_H
7 #define HAL_SPI_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /** @addtogroup hal_spi SPI
14  *  spi hal API.
15  *
16  *  @{
17  */
18 
19 #include <stdint.h>
20 
21 /* Define the wait forever timeout macro */
22 #define HAL_WAIT_FOREVER 0xFFFFFFFFU
23 
24 #define HAL_SPI_MODE_MASTER 1 /**< spi communication is master mode */
25 #define HAL_SPI_MODE_SLAVE  2 /**< spi communication is slave mode */
26 
27 #define DEFAULT_SPI_SERAIL_LEN 280
28 
29 typedef enum
30 {
31     SPI_ROLE_SLAVE,
32     SPI_ROLE_MASTER,
33 } spi_role_e;
34 
35 typedef enum
36 {
37     SPI_FIRSTBIT_MSB,
38     SPI_FIRSTBIT_LSB,
39 } spi_firstbit_e;
40 
41 typedef enum
42 {
43     SPI_WORK_MODE_0,                  // CPOL = 0; CPHA = 0
44     SPI_WORK_MODE_2,                  // CPOL = 1; CPHA = 0
45     SPI_WORK_MODE_1,                  // CPOL = 0; CPHA = 1
46     SPI_WORK_MODE_3,                  // CPOL = 1; CPHA = 1
47 } spi_work_mode_e;
48 
49 typedef enum
50 {
51     SPI_TRANSFER_DMA,
52     SPI_TRANSFER_NORMAL,
53 } spi_transfer_mode_e;
54 
55 /* size of single spi frame data */
56 typedef enum
57 {
58     SPI_DATA_SIZE_4BIT = 4,
59     SPI_DATA_SIZE_5BIT,
60     SPI_DATA_SIZE_6BIT,
61     SPI_DATA_SIZE_7BIT,
62     SPI_DATA_SIZE_8BIT,
63     SPI_DATA_SIZE_9BIT,
64     SPI_DATA_SIZE_10BIT,
65     SPI_DATA_SIZE_11BIT,
66     SPI_DATA_SIZE_12BIT,
67     SPI_DATA_SIZE_13BIT,
68     SPI_DATA_SIZE_14BIT,
69     SPI_DATA_SIZE_15BIT,
70     SPI_DATA_SIZE_16BIT,
71 } spi_data_size_e;
72 
73 /* cs signal to active for transfer */
74 typedef enum
75 {
76     SPI_CS_DIS,
77     SPI_CS_EN,
78 } spi_cs_e;
79 
80 /* Define spi config args */
81 typedef struct
82 {
83     spi_role_e      role; /* spi communication mode */
84     spi_firstbit_e  firstbit;
85     spi_work_mode_e mode;
86     spi_transfer_mode_e t_mode;
87     uint32_t        freq; /* communication frequency Hz */
88     uint16_t        serial_len; /* serial frame length, necessary for SPI running as Slave */
89 	spi_data_size_e data_size;
90 	spi_cs_e        cs;
91 } spi_config_t;
92 
93 /* Define spi dev handle */
94 typedef struct {
95     uint8_t       port;   /**< spi port */
96     spi_config_t  config; /**< spi config */
97     void         *priv;   /**< priv data */
98 } spi_dev_t;
99 
100 typedef struct{
101     spi_work_mode_e work_mode;
102 }spi_attribute_t;
103 
104 /**
105  * Initialises the SPI interface for a given SPI device
106  *
107  * @param[in]  spi  the spi device
108  *
109  * @return  0 : on success,  otherwise is error
110  */
111 int32_t hal_spi_init(spi_dev_t *spi);
112 
113 /**
114  * Spi send
115  *
116  * @param[in]  spi      the spi device
117  * @param[in]  data     spi send data
118  * @param[in]  size     spi send data size
119  * @param[in]  timeout  timeout in milisecond, set this value to HAL_WAIT_FOREVER
120  *                      if you want to wait forever
121  *
122  * @return  0 : on success,  otherwise is error
123  */
124 int32_t hal_spi_send(spi_dev_t *spi, const uint8_t *data, uint16_t size, uint32_t timeout);
125 
126 /**
127  * spi_recv
128  *
129  * @param[in]   spi      the spi device
130  * @param[out]  data     spi recv data
131  * @param[in]   size     spi recv data size
132  * @param[in]   timeout  timeout in milisecond, set this value to HAL_WAIT_FOREVER
133  *                       if you want to wait forever
134  *
135  * @return  0 : on success,  otherwise is error
136  */
137 int32_t hal_spi_recv(spi_dev_t *spi, uint8_t *data, uint16_t size, uint32_t timeout);
138 
139 
140 /**
141  * spi send data and recv
142  *
143  * @param[in]  spi      the spi device
144  * @param[in]  tx_data  spi send data
145  * @param[out] rx_data  spi recv data
146  * @param[in]  size     spi data to be sent and recived
147  * @param[in]  timeout  timeout in milisecond, set this value to HAL_WAIT_FOREVER
148  *                      if you want to wait forever
149  *
150  * @return  0, on success,  otherwise is error
151  */
152 int32_t hal_spi_send_recv(spi_dev_t *spi, uint8_t *tx_data, uint8_t *rx_data,
153                           uint16_t size, uint32_t timeout);
154 
155 /**
156  * spi send data and then recv data
157  *
158  * @param[in]  spi      the spi device
159  * @param[in]  tx_data  the data to be sent
160  * @param[in]  tx_size  data size to be sent
161  * @param[out] rx_data  spi recv data
162  * @param[in]  rx_size  data size to be recived
163  * @param[in]  timeout  timeout in milisecond, set this value to HAL_WAIT_FOREVER
164  *                      if you want to wait forever
165  *
166  * @return  0, on success,  otherwise is error
167 */
168 int32_t hal_spi_send_and_recv(spi_dev_t *spi, uint8_t *tx_data, uint16_t tx_size, uint8_t *rx_data,
169                               uint16_t rx_size, uint32_t timeout);
170 
171 /**
172  * spi send data and then send data
173  * @param[in]  spi       the spi device
174  * @param[in]  tx1_data  the first data to be sent
175  * @param[in]  tx1_size  the first data size to be sent
176  * @param[out] tx2_data  the second data to be sent
177  * @param[in]  tx2_size  the second data size to be sent
178  * @param[in]  timeout   timeout in milisecond, set this value to HAL_WAIT_FOREVER
179  *                       if you want to wait forever
180  *
181  * @return  0, on success,  otherwise is error
182  */
183  int32_t hal_spi_send_and_send(spi_dev_t *spi, uint8_t *tx1_data, uint16_t tx1_size, uint8_t *tx2_data,
184                                uint16_t tx2_size, uint32_t timeout);
185 
186 /**
187  * De-initialises a SPI interface
188  *
189  *
190  * @param[in]  spi  the SPI device to be de-initialised
191  *
192  * @return  0 : on success,  otherwise is error
193  */
194 int32_t hal_spi_finalize(spi_dev_t *spi);
195 
196 /** @} */
197 void _hal_spi_test(void);
198 #ifdef __cplusplus
199 }
200 #endif
201 
202 #endif /* HAL_SPI_H */