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