1 #ifndef _BFLB_I2C_H
2 #define _BFLB_I2C_H
3 
4 #include "bflb_core.h"
5 
6 /** @addtogroup LHAL
7   * @{
8   */
9 
10 /** @addtogroup I2C
11   * @{
12   */
13 
14 /* Bit definitions for the flags field in struct bflb_i2c_msg_s
15  *
16  * START/STOP Rules:
17  *
18  * 1. The lower half I2C driver will always issue the START condition at the
19  *    beginning of a message unless I2C_M_NOSTART flag is set in the
20  *    message.
21  *
22  * 2. The lower half I2C driver will always issue the STOP condition at the
23  *    end of the messages unless:
24  *
25  *    a. The I2C_M_NOSTOP flag is set in the message, OR
26  *    b. The following message has the I2C_M_NOSTART flag set (meaning
27  *       that following message is simply a continuation of the transfer).
28  *
29  * A proper I2C repeated start would then have I2C_M_NOSTOP set on msg[n]
30  * and I2C_M_NOSTART *not* set on msg[n+1].  See the following table:
31  *
32  *   msg[n].flags  msg[n+1].flags Behavior
33  *   ------------ --------------- -----------------------------------------
34  *   0            0                Two normal, separate messages with STOP
35  *                                 on msg[n] then START on msg[n+1]
36  *   0*           I2C_M_NOSTART    Continuation of the same transfer (must
37  *                                 be the same direction).  See NOTE below.
38  *   NO_STOP      0                No STOP on msg[n]; repeated START on
39  *                                 msg[n+1].
40  *
41  * * NOTE: NO_STOP is implied in this case and may or not be explicitly
42  *   included in the msg[n] flags
43  */
44 
45 #define I2C_M_READ    0x0001 /* Read data, from slave to master */
46 #define I2C_M_TEN     0x0002 /* Ten bit address */
47 #define I2C_M_DMA     0x0004 /* Enable dma mode */
48 #define I2C_M_NOSTOP  0x0040 /* Message should not end with a STOP */
49 #define I2C_M_NOSTART 0x0080 /* Message should not begin with a START */
50 
51 /** @defgroup I2C_INTSTS i2c interrupt status definition
52   * @{
53   */
54 #define I2C_INTSTS_END     (1 << 0) /* Transfer end interrupt */
55 #define I2C_INTSTS_TX_FIFO (1 << 1) /* TX FIFO ready interrupt */
56 #define I2C_INTSTS_RX_FIFO (1 << 2) /* RX FIFO ready interrupt */
57 #define I2C_INTSTS_NACK    (1 << 3) /* NACK interrupt */
58 #define I2C_INTSTS_ARB     (1 << 4) /* Arbitration lost interrupt */
59 #define I2C_INTSTS_FER     (1 << 5) /* TX/RX FIFO error interrupt */
60 /**
61  * @}
62  */
63 
64 /** @defgroup I2C_INTCLR i2c interrupt clear definition
65   * @{
66   */
67 #define I2C_INTCLR_END  (1 << 0) /* Transfer end interrupt */
68 #define I2C_INTCLR_NACK (1 << 3) /* NACK interrupt */
69 #define I2C_INTCLR_ARB  (1 << 4) /* Arbitration lost interrupt */
70 /**
71  * @}
72  */
73 
74 /** @defgroup I2C_INTEN i2c interrupt enable definition
75   * @{
76   */
77 #define I2C_INTEN_END     (1 << 0) /* Transfer end interrupt */
78 #define I2C_INTEN_TX_FIFO (1 << 1) /* TX FIFO ready interrupt */
79 #define I2C_INTEN_RX_FIFO (1 << 2) /* RX FIFO ready interrupt */
80 #define I2C_INTEN_NACK    (1 << 3) /* NACK interrupt */
81 #define I2C_INTEN_ARB     (1 << 4) /* Arbitration lost interrupt */
82 #define I2C_INTEN_FER     (1 << 5) /* TX/RX FIFO error interrupt */
83 /**
84  * @}
85  */
86 
87 /**
88  * @brief I2C message structure
89  *
90  * @param addr          Slave address (7- or 10-bit)
91  * @param flags         See I2C_M_* definitions
92  * @param buffer        Buffer to be transferred
93  * @param length        Length of the buffer in bytes, should be less than 256.
94  */
95 struct bflb_i2c_msg_s {
96     uint16_t addr;
97     uint16_t flags;
98     uint8_t *buffer;
99     uint16_t length;
100 };
101 
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
105 
106 /**
107  * @brief Initialize i2c.
108  *
109  * @param [in] dev device handle
110  * @param [in] frequency i2c frequency, range from 305Hz to 400KHz
111  */
112 void bflb_i2c_init(struct bflb_device_s *dev, uint32_t frequency);
113 
114 /**
115  * @brief Deinitialize i2c.
116  *
117  * @param [in] dev device handle
118  */
119 void bflb_i2c_deinit(struct bflb_device_s *dev);
120 
121 /**
122  * @brief Enable i2c tx dma.
123  *
124  * @param [in] dev device handle
125  * @param [in] enable true means enable, otherwise disable.
126  */
127 void bflb_i2c_link_txdma(struct bflb_device_s *dev, bool enable);
128 
129 /**
130  * @brief Enable i2c rx dma.
131  *
132  * @param [in] dev device handle
133  * @param [in] enable true means enable, otherwise disable.
134  */
135 void bflb_i2c_link_rxdma(struct bflb_device_s *dev, bool enable);
136 
137 /**
138  * @brief Start transferring i2c message.
139  *
140  * @param [in] dev device handle
141  * @param [in] msgs pointer to i2c message
142  * @param [in] count message count
143  * @return A negated errno value on failure.
144  */
145 int bflb_i2c_transfer(struct bflb_device_s *dev, struct bflb_i2c_msg_s *msgs, int count);
146 
147 /**
148  * @brief Enable or disable i2c interrupt.
149  *
150  * @param [in] dev device handle
151  * @param [in] int_type interrupt type ,use @ref I2C_INTEN
152  * @param [in] mask true means disable, false means enable
153  */
154 void bflb_i2c_int_mask(struct bflb_device_s *dev, uint32_t int_type, bool mask);
155 
156 /**
157  * @brief Clear i2c interrupt status.
158  *
159  * @param [in] dev device handle
160  * @param [in] int_clear clear value, use @ref I2C_INTCLR
161  */
162 void bflb_i2c_int_clear(struct bflb_device_s *dev, uint32_t int_clear);
163 
164 /**
165  * @brief Get i2c interrupt status.
166  *
167  * @param [in] dev device handle
168  * @return interrupt status value, use @ref I2C_INTSTS
169  */
170 uint32_t bflb_i2c_get_intstatus(struct bflb_device_s *dev);
171 
172 /**
173  * @brief Control i2c feature.
174  *
175  * @param [in] dev device handle
176  * @param [in] cmd feature command
177  * @param [in] arg user data
178  * @return A negated errno value on failure.
179  */
180 int bflb_i2c_feature_control(struct bflb_device_s *dev, int cmd, size_t arg);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 /**
187   * @}
188   */
189 
190 /**
191   * @}
192   */
193 
194 #endif
195