1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author         Notes
8  * 2020-08-21     heyuanjie87    first version
9  * 2023-03-31     Vandoul        formatting code.
10  */
11 
12 #include <rtthread.h>
13 #include <rtdevice.h>
14 
15 #include "i2c.h"
16 #include "board.h"
17 #include "drivers/dev_i2c.h"
18 #include "gpiohs.h"
19 #include "utils.h"
20 #include "sleep.h"
21 #include "fpioa.h"
22 #ifdef RT_USING_I2C
23 
24 #ifndef BSP_I2C0_SCL_PIN
25 #define BSP_I2C0_SCL_PIN            0
26 #endif
27 #ifndef BSP_I2C0_SDA_PIN
28 #define BSP_I2C0_SDA_PIN            1
29 #endif
30 #ifndef BSP_I2C1_SCL_PIN
31 #define BSP_I2C1_SCL_PIN            30
32 #endif
33 #ifndef BSP_I2C1_SDA_PIN
34 #define BSP_I2C1_SDA_PIN            31
35 #endif
36 #ifndef BSP_I2C2_SCL_PIN
37 #define BSP_I2C2_SCL_PIN            4
38 #endif
39 #ifndef BSP_I2C2_SDA_PIN
40 #define BSP_I2C2_SDA_PIN            5
41 #endif
42 
ki2c_send(volatile i2c_t * i2c_adapter,rt_uint8_t * send_buf,rt_uint32_t send_buf_len)43 static rt_err_t ki2c_send(
44     volatile i2c_t *i2c_adapter,
45     rt_uint8_t *send_buf,
46     rt_uint32_t send_buf_len)
47 {
48     rt_uint32_t fifo_len, index;
49 
50     while (send_buf_len)
51     {
52         fifo_len = 8 - i2c_adapter->txflr;
53         fifo_len = send_buf_len < fifo_len ? send_buf_len : fifo_len;
54         for (index = 0; index < fifo_len; index++)
55             i2c_adapter->data_cmd = I2C_DATA_CMD_DATA(*send_buf++);
56         if (i2c_adapter->tx_abrt_source != 0)
57         {
58             while (i2c_adapter->status & I2C_STATUS_ACTIVITY);  //
59             i2c_adapter->clr_intr = i2c_adapter->clr_intr;      //
60             return -RT_ERROR;
61         }
62 
63         send_buf_len -= fifo_len;
64     }
65 
66     return RT_EOK;
67 }
68 
ki2c_recv(volatile i2c_t * i2c_adapter,rt_uint8_t * receive_buf,rt_uint32_t receive_buf_len)69 static rt_err_t ki2c_recv(
70     volatile i2c_t *i2c_adapter,
71     rt_uint8_t *receive_buf,
72     rt_uint32_t receive_buf_len)
73 {
74     rt_uint32_t fifo_len, index;
75     rt_uint32_t rx_len = receive_buf_len;
76 
77     while (receive_buf_len || rx_len)
78     {
79         fifo_len = i2c_adapter->rxflr;
80         fifo_len = rx_len < fifo_len ? rx_len : fifo_len;
81         for (index = 0; index < fifo_len; index++)
82             *receive_buf++ = (rt_uint8_t)i2c_adapter->data_cmd;
83         rx_len -= fifo_len;
84         fifo_len = 8 - i2c_adapter->txflr;
85         fifo_len = receive_buf_len < fifo_len ? receive_buf_len : fifo_len;
86         for (index = 0; index < fifo_len; index++)
87             i2c_adapter->data_cmd = I2C_DATA_CMD_CMD;
88         if (i2c_adapter->tx_abrt_source != 0)
89             return -RT_ERROR;
90         receive_buf_len -= fifo_len;
91     }
92 
93     return RT_EOK;
94 }
95 
ki2c_setaddr(volatile i2c_t * i2c_adapter,rt_uint16_t addr,int width)96 static void ki2c_setaddr(
97     volatile i2c_t *i2c_adapter,
98     rt_uint16_t addr,
99     int width)
100 {
101     i2c_adapter->tar = I2C_TAR_ADDRESS(addr) & I2C_TAR_ADDRESS_MASK;
102 
103     if(width == 10)
104     {
105         i2c_adapter->tar |= I2C_TAR_10BITADDR_MASTER;
106     }
107     else
108     {
109         i2c_adapter->tar &= ~I2C_TAR_10BITADDR_MASTER;
110     }
111 
112 }
113 
ki2c_waittx(volatile i2c_t * i2c_adapter,int timeout_ms)114 static int ki2c_waittx(volatile i2c_t *i2c_adapter, int timeout_ms)
115 {
116     rt_tick_t start;
117 
118     start = rt_tick_get();
119     while ((i2c_adapter->status & I2C_STATUS_ACTIVITY) || !(i2c_adapter->status & I2C_STATUS_TFE))
120     {
121         if (rt_tick_from_millisecond(rt_tick_get() - start) > timeout_ms)
122             break;
123     }
124 
125     if (i2c_adapter->tx_abrt_source != 0)
126         return -RT_ERROR;
127 
128     return RT_EOK;
129 }
130 
ki2c_clearerr(volatile i2c_t * i2c_adapter)131 static void ki2c_clearerr(volatile i2c_t *i2c_adapter)
132 {
133     i2c_adapter->clr_tx_abrt = i2c_adapter->clr_tx_abrt;
134 }
135 
_i2c_mst_xfer(struct rt_i2c_bus_device * bus,struct rt_i2c_msg msgs[],rt_uint32_t num)136 static rt_ssize_t _i2c_mst_xfer(struct rt_i2c_bus_device *bus,
137                                struct rt_i2c_msg msgs[],
138                                rt_uint32_t num)
139 {
140     rt_ssize_t i;
141     i2c_t *kbus = (i2c_t *)bus->priv;
142     rt_err_t status;
143     int waittx = 0;
144 
145     RT_ASSERT(bus != RT_NULL);
146     if(msgs[0].flags & RT_I2C_ADDR_10BIT)
147     {
148         ki2c_setaddr(kbus, msgs[0].addr, 10);
149     }
150     else
151     {
152         ki2c_setaddr(kbus, msgs[0].addr, 7);
153     }
154 
155 
156     ki2c_clearerr(kbus);
157 
158     for (i = 0; i < num; i++)
159     {
160         waittx = 0;
161 
162         if (msgs[i].flags & RT_I2C_RD)
163         {
164             status = ki2c_recv(kbus, msgs[i].buf, msgs[i].len);
165         }
166         else
167         {
168             status = ki2c_send(kbus, msgs[i].buf, msgs[i].len);
169             waittx = 1;
170         }
171 
172         if (status != RT_EOK)
173         {
174             goto _out;
175         }
176     }
177 
178     if (waittx)
179     {
180         status = ki2c_waittx(kbus, 2000);
181         if (status != RT_EOK)
182         {
183             goto _out;
184         }
185     }
186 
187     return i;
188 _out:
189     return status;
190 }
191 
192 static const struct rt_i2c_bus_device_ops i2c_ops =
193 {
194     .master_xfer = _i2c_mst_xfer,
195     .slave_xfer = RT_NULL,
196     .i2c_bus_control = RT_NULL,
197 };
198 
199 #ifdef RT_USING_I2C_BITOPS
200 
201 typedef struct pin_info_s {
202     uint32_t    scl;
203     uint32_t    sda;
204 } pin_info_t;
205 
set_sda(void * data,rt_int32_t state)206 static void set_sda(void *data, rt_int32_t state)
207 {
208     pin_info_t  *pin = (pin_info_t *)data;
209     /* state = 1: disable output. state = 0: enable output.*/
210     set_gpio_bit(gpiohs->output_en.u32, pin->sda, !state);
211 }
212 
set_scl(void * data,rt_int32_t state)213 static void set_scl(void *data, rt_int32_t state)
214 {
215     pin_info_t  *pin = (pin_info_t *)data;
216     /* state = 1: disable output. state = 0: enable output.*/
217     set_gpio_bit(gpiohs->output_en.u32, pin->scl, !state);
218 }
219 
get_sda(void * data)220 static rt_int32_t get_sda(void *data)
221 {
222     pin_info_t  *pin = (pin_info_t *)data;
223     /* disable output.*/
224     set_gpio_bit(gpiohs->output_en.u32, pin->sda, 0);
225 
226     return get_gpio_bit(gpiohs->input_val.u32, pin->sda);
227 }
228 
get_scl(void * data)229 static rt_int32_t get_scl(void *data)
230 {
231     pin_info_t  *pin = (pin_info_t *)data;
232     /* disable output.*/
233     set_gpio_bit(gpiohs->output_en.u32, pin->scl, 0);
234 
235     return get_gpio_bit(gpiohs->input_val.u32, pin->scl);
236 }
237 
udelay(rt_uint32_t us)238 static void udelay(rt_uint32_t us)
239 {
240     usleep((uint64_t)us);
241 }
242 
243 static struct rt_i2c_bit_ops bit_ops_0 =
244 {
245     RT_NULL,
246     set_sda,
247     set_scl,
248     get_sda,
249     get_scl,
250     udelay,
251     5,
252     5
253 };
254 
255 static struct rt_i2c_bit_ops bit_ops_1 =
256 {
257     RT_NULL,
258     set_sda,
259     set_scl,
260     get_sda,
261     get_scl,
262     udelay,
263     5,
264     5
265 };
266 
267 static struct rt_i2c_bit_ops bit_ops_2 =
268 {
269     RT_NULL,
270     set_sda,
271     set_scl,
272     get_sda,
273     get_scl,
274     udelay,
275     5,
276     5
277 };
278 
279 extern int get_pin_channel(rt_base_t pin_index);
280 #endif
281 
rt_hw_i2c_init(void)282 int rt_hw_i2c_init(void)
283 {
284     struct  rt_i2c_bus_device *busdev;
285 
286 #ifdef BSP_USING_I2C0
287     static  struct rt_i2c_bus_device i2c_dev0;
288     busdev = &i2c_dev0;
289 
290     #ifdef RT_USING_I2C_BITOPS
291     fpioa_set_function(BSP_I2C0_SCL_PIN, FUNC_RESV0);
292     fpioa_set_function(BSP_I2C0_SDA_PIN, FUNC_RESV0);
293 
294     rt_pin_write(BSP_I2C0_SCL_PIN, PIN_LOW);
295     rt_pin_write(BSP_I2C0_SDA_PIN, PIN_LOW);
296     rt_pin_mode(BSP_I2C0_SCL_PIN, PIN_MODE_INPUT_PULLUP);
297     rt_pin_mode(BSP_I2C0_SDA_PIN, PIN_MODE_INPUT_PULLUP);
298 
299     static  pin_info_t  pin0;
300     pin0.scl = get_pin_channel(BSP_I2C0_SCL_PIN);
301     pin0.sda = get_pin_channel(BSP_I2C0_SDA_PIN);
302     bit_ops_0.data = (void *)&pin0;
303 
304     busdev->priv = (void *)&bit_ops_0;
305     rt_i2c_bit_add_bus(busdev, "i2c0");
306     #else
307 
308     busdev->ops = &i2c_ops;
309     busdev->priv = (void *)I2C0_BASE_ADDR;
310 
311     i2c_init(I2C_DEVICE_0, 0, 7, 100000);
312     rt_i2c_bus_device_register(busdev, "i2c0");
313     #endif
314 #endif
315 
316 #ifdef BSP_USING_I2C1
317     static  struct rt_i2c_bus_device i2c_dev1;
318     busdev = &i2c_dev1;
319 
320     #ifdef RT_USING_I2C_BITOPS
321     fpioa_set_function(BSP_I2C1_SCL_PIN, FUNC_RESV0);
322     fpioa_set_function(BSP_I2C1_SDA_PIN, FUNC_RESV0);
323 
324     rt_pin_write(BSP_I2C1_SCL_PIN, PIN_LOW);
325     rt_pin_write(BSP_I2C1_SDA_PIN, PIN_LOW);
326     rt_pin_mode(BSP_I2C1_SCL_PIN, PIN_MODE_INPUT_PULLUP);
327     rt_pin_mode(BSP_I2C1_SDA_PIN, PIN_MODE_INPUT_PULLUP);
328 
329     static  pin_info_t  pin1;
330     pin1.scl = get_pin_channel(BSP_I2C1_SCL_PIN);
331     pin1.sda = get_pin_channel(BSP_I2C1_SDA_PIN);
332     bit_ops_1.data = (void *)&pin1;
333 
334     busdev->priv = (void *)&bit_ops_1;
335     rt_i2c_bit_add_bus(busdev, "i2c1");
336     #else
337 
338     busdev->ops = &i2c_ops;
339     busdev->priv = (void *)I2C1_BASE_ADDR;
340 
341     i2c_init(I2C_DEVICE_1, 0, 7, 100000);
342     rt_i2c_bus_device_register(busdev, "i2c1");
343     #endif
344 #endif
345 
346 #ifdef BSP_USING_I2C2
347     static  struct rt_i2c_bus_device i2c_dev2;
348     busdev = &i2c_dev2;
349 
350     #ifdef RT_USING_I2C_BITOPS
351     fpioa_set_function(BSP_I2C2_SCL_PIN, FUNC_RESV0);
352     fpioa_set_function(BSP_I2C2_SDA_PIN, FUNC_RESV0);
353 
354     rt_pin_write(BSP_I2C2_SCL_PIN, PIN_LOW);
355     rt_pin_write(BSP_I2C2_SDA_PIN, PIN_LOW);
356     rt_pin_mode(BSP_I2C2_SCL_PIN, PIN_MODE_INPUT_PULLUP);
357     rt_pin_mode(BSP_I2C2_SDA_PIN, PIN_MODE_INPUT_PULLUP);
358 
359     static  pin_info_t  pin2;
360     pin2.scl = get_pin_channel(BSP_I2C2_SCL_PIN);
361     pin2.sda = get_pin_channel(BSP_I2C2_SDA_PIN);
362     bit_ops_2.data = (void *)&pin2;
363 
364     busdev->priv = (void *)&bit_ops_2;
365     rt_i2c_bit_add_bus(busdev, "i2c2");
366     #else
367 
368     busdev->ops = &i2c_ops;
369     busdev->priv = (void *)I2C2_BASE_ADDR;
370 
371     i2c_init(I2C_DEVICE_2, 0, 7, 100000);
372     rt_i2c_bus_device_register(busdev, "i2c2");
373     #endif
374 #endif
375     return 0;
376 }
377 INIT_BOARD_EXPORT(rt_hw_i2c_init);
378 #endif
379