1 /*
2  * Copyright (C) 2020-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef AOS_SPI_H
6 #define AOS_SPI_H
7 
8 #include <aos/device.h>
9 
10 #define AOS_SPI_MCFG_MODE_MASK  ((uint32_t)0x3 << 0)
11 #define AOS_SPI_MCFG_MODE_0     ((uint32_t)0x0 << 0)
12 #define AOS_SPI_MCFG_MODE_1     ((uint32_t)0x1 << 0)
13 #define AOS_SPI_MCFG_MODE_2     ((uint32_t)0x2 << 0)
14 #define AOS_SPI_MCFG_MODE_3     ((uint32_t)0x3 << 0)
15 #define AOS_SPI_MCFG_WIDTH_MASK ((uint32_t)0x3 << 2)
16 #define AOS_SPI_MCFG_WIDTH_1    ((uint32_t)0x0 << 2)
17 #define AOS_SPI_MCFG_WIDTH_2    ((uint32_t)0x1 << 2)
18 #define AOS_SPI_MCFG_WIDTH_4    ((uint32_t)0x2 << 2)
19 #define AOS_SPI_MCFG_WIDTH_8    ((uint32_t)0x3 << 2)
20 #define AOS_SPI_MCFG_MSB_FIRST  ((uint32_t)0x0 << 4)
21 #define AOS_SPI_MCFG_LSB_FIRST  ((uint32_t)0x1 << 4)
22 
23 typedef aos_dev_ref_t aos_spi_ref_t;
24 
25 typedef struct {
26     uint32_t cfg;
27     uint32_t cs;
28     uint32_t hz;
29     uint32_t pre_cs;
30     uint32_t post_cs;
31     uint32_t pre_clk;
32     uint32_t post_clk;
33     size_t count;
34     void *rx_buf;
35     const void *tx_buf;
36 } aos_spi_msg_t;
37 
38 #define AOS_SPI_MSG_INIT_VAL \
39     { \
40         .cfg            = 0, \
41         .cs             = 0, \
42         .hz             = 0, \
43         .pre_cs         = 0, \
44         .post_cs        = 0, \
45         .pre_clk        = 0, \
46         .post_clk       = 0, \
47         .count          = 0, \
48         .rx_buf         = NULL, \
49         .tx_buf         = NULL, \
50     }
51 
52 #define aos_spi_msg_init(x)     do { *(x) = (aos_spi_msg_t)AOS_SPI_MSG_INIT_VAL; } while (0)
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 /**
59  * @brief       Get a SPI device.
60  * @param[out]  ref     SPI ref to operate
61  * @param[in]   id      SPI device ID
62  * @return      0: on success; < 0: on failure
63  */
64 aos_status_t aos_spi_get(aos_spi_ref_t *ref, uint32_t id);
65 /**
66  * @brief       Release a SPI device.
67  * @param[in]   ref     SPI ref to operate
68  * @return      None
69  */
70 void aos_spi_put(aos_spi_ref_t *ref);
71 /**
72  * @brief       Synchronous SPI data transfer.
73  * @param[in]   ref             SPI ref to operate
74  * @param[in]   msgs            array of messages
75  * @param[in]   num_msgs        number of messages
76  * @return      0: on success; < 0: on failure
77  */
78 aos_status_t aos_spi_transfer(aos_spi_ref_t *ref, const aos_spi_msg_t *msgs, size_t num_msgs);
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 #endif /* AOS_SPI_H */
85