1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18 /** @page I2C
19 *
20 * Introduction
21 * ===============
22 * i2c support master mode or slave mode.
23 *
24 * API Reference
25 * ===============
26 * Header File: i2c.h
27 */
28
29 #ifndef I2C_H
30 #define I2C_H
31 #include "gpio.h"
32
33 #include "analog.h"
34 #include "dma.h"
35 #include "reg_include/i2c_reg.h"
36
37 /**********************************************************************************************************************
38 * global constants *
39 *********************************************************************************************************************/
40
41 /**********************************************************************************************************************
42 * global macro *
43 *********************************************************************************************************************/
44
45 /**
46 * @brief select pin as SDA and SCL of i2c
47 */
48 typedef enum {
49 I2C_GPIO_SDA_B3 = GPIO_PB3,
50 I2C_GPIO_SDA_C2 = GPIO_PC2,
51 I2C_GPIO_SDA_E2 = GPIO_PE2,
52 I2C_GPIO_SDA_E3 = GPIO_PE3,
53 } i2c_sda_pin_e;
54
55 typedef enum {
56 I2C_GPIO_SCL_B2 = GPIO_PB2,
57 I2C_GPIO_SCL_C1 = GPIO_PC1,
58 I2C_GPIO_SCL_E0 = GPIO_PE0,
59 I2C_GPIO_SCL_E1 = GPIO_PE1,
60 } i2c_scl_pin_e;
61
62 typedef enum {
63 I2C_RX_BUF_MASK = BIT(2),
64 I2C_TX_BUF_MASK = BIT(3),
65 I2C_TX_DONE_MASK = BIT(4),
66 I2C_RX_DONE_MASK = BIT(5),
67 } i2c_irq_mask_e;
68
69 typedef enum {
70 I2C_RX_BUFF_CLR = BIT(6),
71 I2C_TX_BUFF_CLR = BIT(7),
72 } i2c_buff_clr_e;
73
74 typedef enum {
75 I2C_TXDONE_STATUS = BIT(0),
76 I2C_TX_BUF_STATUS = BIT(1),
77 I2C_RXDONE_STATUS = BIT(2),
78 I2C_RX_BUF_STATUS = BIT(3),
79 } i2c_irq_status_e;
80
81 typedef enum {
82 I2C_TX_DONE_CLR = BIT(4),
83 } i2c_irq_clr_e;
84
85 /**
86 * @brief The function of this API is to determine whether the bus is busy.
87 * @return 1:Indicates that the bus is busy. 0:Indicates that the bus is free
88 */
i2c_master_busy(void)89 static inline _Bool i2c_master_busy(void)
90 {
91 return reg_i2c_mst & FLD_I2C_MST_BUSY;
92 }
93
94 /**
95 * @brief The function of this API is to Get the number of bytes in tx_buffer.
96 * @return The actual number of bytes in tx_buffer.
97 */
i2c_get_tx_buf_cnt(void)98 static inline unsigned char i2c_get_tx_buf_cnt(void)
99 {
100 return (reg_i2c_buf_cnt & FLD_I2C_TX_BUFCNT) >> 4;
101 }
102
103 /**
104 * @brief The function of this API is to Get the number of bytes in rx_buffer.
105 * @return The actual number of bytes in rx_buffer.
106 */
i2c_get_rx_buf_cnt(void)107 static inline unsigned char i2c_get_rx_buf_cnt(void)
108 {
109 return (reg_i2c_buf_cnt & FLD_I2C_RX_BUFCNT);
110 }
111
112 /**
113 * @brief The function of this API is to set the number of bytes to triggered the receive interrupt.
114 * Its default value is 4. We recommend setting it to 1 or 4.
115 * @param[in] cnt - the interrupt trigger level.
116 * @return none
117 */
i2c_rx_irq_trig_cnt(unsigned char cnt)118 static inline void i2c_rx_irq_trig_cnt(unsigned char cnt)
119 {
120 reg_i2c_trig &= (~FLD_I2C_RX_IRQ_TRIG_LEV);
121 reg_i2c_trig |= cnt;
122 }
123
124 /**
125 * @brief The function of this interface is equivalent to
126 * that after the user finishes calling the write or read interface, the stop signal is not sent,
127 * and then the write or read command is executed again.
128 * The driver defaults that every write or read API will send a stop command at the end
129 * @param[in] en - Input parameters.Decide whether to disable the stop function after each write or read interface
130 * @return none
131 */
132 void i2c_master_send_stop(unsigned char en);
133
134 /**
135 * @brief This function selects a pin port for I2C interface.
136 * @param[in] sda_pin - the pin port selected as I2C sda pin port.
137 * @param[in] scl_pin - the pin port selected as I2C scl pin port.
138 * @return none
139 */
140 void i2c_set_pin(i2c_sda_pin_e sda_pin, i2c_scl_pin_e scl_pin);
141
142 /**
143 * @brief This function serves to enable i2c master function.
144 * @return none.
145 */
146 void i2c_master_init(void);
147
148 /**
149 * @brief This function serves to enable i2c RX/TX/MASK_RX/MASK_TX interrupt function.
150 * @param[in] mask - to select Interrupt type.
151 * @return none
152 */
i2c_set_irq_mask(i2c_irq_mask_e mask)153 static inline void i2c_set_irq_mask(i2c_irq_mask_e mask)
154 {
155 reg_i2c_sct0 |= mask;
156 }
157
158 /**
159 * @brief This function serves to disable i2c RX/TX/MASK_RX/MASK_TX interrupt function.
160 * @param[in] mask - to select Interrupt type.
161 * @return none
162 */
i2c_clr_irq_mask(i2c_irq_mask_e mask)163 static inline void i2c_clr_irq_mask(i2c_irq_mask_e mask)
164 {
165 reg_i2c_sct0 &= (~mask);
166 }
167
168 /**
169 * @brief This function serves to get i2c interrupt status.
170 * @return none
171 *
172 */
i2c_get_irq_status(i2c_irq_status_e status)173 static inline unsigned char i2c_get_irq_status(i2c_irq_status_e status)
174 {
175 return reg_i2c_irq_status & status;
176 }
177
178 /**
179 * @brief This function serves to clear i2c rx/tx fifo.
180 * @param[in] clr - to select Interrupt type.
181 * @return none
182 */
i2c_clr_fifo(i2c_buff_clr_e clr)183 static inline void i2c_clr_fifo(i2c_buff_clr_e clr)
184 {
185 reg_i2c_status = clr;
186 }
187
188 /**
189 * @brief This function serves to clear i2c irq status.
190 * @return none
191 */
i2c_clr_irq_status(i2c_irq_clr_e status)192 static inline void i2c_clr_irq_status(i2c_irq_clr_e status)
193 {
194 reg_i2c_irq_status = status;
195 }
196
197 /**
198 * @brief This function serves to enable slave mode.
199 * @param[in] id - the id of slave device.it contains write or read bit,the laster bit is write or read bit.
200 * ID|0x01 indicate read. ID&0xfe indicate write.
201 * @return none
202 */
203 void i2c_slave_init(unsigned char id);
204
205 /**
206 * @brief The function of this API is to ensure that the data can be successfully sent out.
207 * @param[in] id - to set the slave ID.for kite slave ID=0x5c,for eagle slave ID=0x5a.
208 * @param[in] data - The data to be sent, The first three bytes can be set as the RAM address of the slave.
209 * @param[in] len - This length is the total length, including both the length of the slave RAM address
210 * and the length of the data to be sent.
211 * @return 0 : the master receive NACK after sending out the id and then send stop.
212 * 1: the master sent the data successfully,(master does not detect NACK in data phase)
213 */
214 unsigned char i2c_master_write(unsigned char id, unsigned char *data, unsigned char len);
215
216 /**
217 * @brief This function serves to read a packet of data from the specified address of slave device
218 * @param[in] id - to set the slave ID.for kite slave ID=0x5c,for eagle slave ID=0x5a.
219 * @param[in] data - Store the read data
220 * @param[in] len - The total length of the data read back.
221 * @return 0 : the master receive NACK after sending out the id and then send stop.
222 * 1: the master receive the data successfully.
223 */
224 unsigned char i2c_master_read(unsigned char id, unsigned char *data, unsigned char len);
225
226 /**
227 * @brief This function serves to write data and restart read data.
228 * @param[in] id - to set the slave ID.for kite slave ID=0x5c,for eagle slave ID=0x5a.
229 * @param[in] wr_data - The data to be sent, The first three bytes can be set as the RAM address of the slave.
230 * @param[in] wr_len - This length is the total length, including both the length of the slave RAM address
231 * and the length of the data to be sent.
232 * @param[in] rd_data - Store the read data
233 * @param[in] rd_len - The total length of the data read back.
234 * @return 0 : the master receive NACK after sending out the id and then send stop.
235 * 1: the master receive the data successfully.
236 */
237 unsigned char i2c_master_write_read(unsigned char id, unsigned char *wr_data, unsigned char wr_len,
238 unsigned char *rd_data, unsigned char rd_len);
239
240 /**
241 * @brief The function of this API is just to write data to the i2c tx_fifo by DMA.
242 * @param[in] id - to set the slave ID.for kite slave ID=0x5c,for eagle slave ID=0x5a.
243 * @param[in] data - The data to be sent, The first three bytes represent the RAM address of the slave.
244 * @param[in] len - This length is the total length, including both the length of the slave RAM address
245 * and the length of the data to be sent.
246 * @return none.
247 */
248 void i2c_master_write_dma(unsigned char id, unsigned char *data, unsigned char len);
249
250 /**
251 * @brief This function serves to read a packet of data from the specified address of slave device.
252 * @param[in] id - to set the slave ID.for kite slave ID=0x5c,for eagle slave ID=0x5a.
253 * @param[in] data - Store the read data
254 * @param[in] len - The total length of the data read back.
255 * @return none.
256 */
257 void i2c_master_read_dma(unsigned char id, unsigned char *data, unsigned char len);
258
259 /**
260 * @brief This function serves to send a packet of data to master device.
261 * It will trigger after the master sends the read sequence.
262 * @param[in] data - the pointer of tx_buff.
263 * @param[in] len - The total length of the data .
264 * @return none.
265 */
266 void i2c_slave_set_tx_dma(unsigned char *data, unsigned char len);
267
268 /**
269 * @brief This function serves to receive a packet of data from master device,
270 * It will trigger after the master sends the write sequence.
271 * @param[in] data - the pointer of rx_buff.
272 * @param[in] len - The total length of the data.
273 * @return none.
274 */
275 void i2c_slave_set_rx_dma(unsigned char *data, unsigned char len);
276
277 /**
278 * @brief This function serves to receive data.
279 * @param[in] data - the data need read.
280 * @param[in] len - The total length of the data
281 * @return none
282 */
283 void i2c_slave_read(unsigned char *data, unsigned char len);
284
285 /**
286 * @brief This function serves to receive uart data by byte with not DMA method.
287 * @param[in] data - the data need send.
288 * @param[in] len - The total length of the data.
289 * @return none
290 */
291 void i2c_slave_write(unsigned char *data, unsigned char len);
292
293 /**
294 * @brief This function serves to set the i2c clock frequency. The i2c clock is consistent with the system clock.
295 * Currently, the default system clock is 48M, and the i2c clock is also 48M.
296 * @param[in] clock - the division factor of I2C clock,
297 * I2C frequency = System_clock / (4*DivClock).
298 * @return none
299 */
300 void i2c_set_master_clk(unsigned char clock);
301
302 /**
303 * @brief This function serves to set i2c tx_dam channel and config dma tx default.
304 * @param[in] chn: dma channel.
305 * @return none
306 */
307 void i2c_set_tx_dma_config(dma_chn_e chn);
308
309 /**
310 * @brief This function serves to set i2c rx_dam channel and config dma rx default.
311 * @param[in] chn: dma channel.
312 * @return none
313 */
314 void i2c_set_rx_dma_config(dma_chn_e chn);
315
316 #endif
317