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