1 /*
2  *
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the License); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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, WITHOUT
13  * 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  * Change Logs:
18  * Date           Author        Notes
19  * 2019-01-24     wangyq        the first version
20  * 2019-11-01     wangyq        update libraries
21  * 2020-12-15     liuhy         update libraries
22  */
23 
24 #include <rthw.h>
25 #include <rtthread.h>
26 #include <rtdevice.h>
27 #include "board.h"
28 #include "drv_i2c.h"
29 
30 
31 #ifdef RT_USING_I2C
32 
33 #define TIMEOUT 0x0FFF
34 /* I2C struct definition */
35 #ifdef BSP_USING_I2C0
36     static i2c_handle_t _h_i2c0;
37 #endif
38 
39 #ifdef BSP_USING_I2C1
40     static i2c_handle_t _h_i2c1;
41 #endif
42 
_i2c_init(void)43 static void _i2c_init(void)
44 {
45     gpio_init_t gpio_instruct;
46 
47     /* Initialize I2C Pin */
48     gpio_instruct.mode = GPIO_MODE_OUTPUT;
49     gpio_instruct.odos = GPIO_OPEN_DRAIN;
50     gpio_instruct.pupd = GPIO_PUSH_UP;
51     gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
52     gpio_instruct.flt  = GPIO_FILTER_DISABLE;
53     gpio_instruct.type = GPIO_TYPE_CMOS;
54     gpio_instruct.func = GPIO_FUNC_5;
55 
56 #ifdef BSP_USING_I2C0
57 
58 #if  defined(ES_I2C0_SCL_GPIO_FUNC)&&defined(ES_I2C0_SCL_GPIO_PORT)&&defined(ES_I2C0_SCL_GPIO_PIN)
59     gpio_instruct.func = ES_I2C0_SCL_GPIO_FUNC;
60     ald_gpio_init(ES_I2C0_SCL_GPIO_PORT, ES_I2C0_SCL_GPIO_PIN, &gpio_instruct);
61 #endif
62 
63 #if  defined(ES_I2C0_SDA_GPIO_FUNC)&&defined(ES_I2C0_SDA_GPIO_PORT)&&defined(ES_I2C0_SDA_GPIO_PIN)
64     gpio_instruct.func = ES_I2C0_SDA_GPIO_FUNC;
65     ald_gpio_init(ES_I2C0_SDA_GPIO_PORT, ES_I2C0_SDA_GPIO_PIN, &gpio_instruct);
66 #endif
67 
68     /* Initialize I2C Function */
69     _h_i2c0.perh = I2C0;
70     _h_i2c0.init.duty         = I2C_DUTYCYCLE_2;
71     _h_i2c0.init.clk_speed    = ES_I2C0_CLK_SPEED;
72     _h_i2c0.init.own_addr1    = ES_I2C0_OWN_ADDR1;
73     _h_i2c0.init.addr_mode    = ES_I2C0_ADDR_MODE;
74     _h_i2c0.init.general_call = ES_I2C0_GENERAL_CALL;
75     _h_i2c0.init.no_stretch   = ES_I2C0_STRETCH;
76 
77     ald_i2c_reset(&_h_i2c0);
78     ald_i2c_init(&_h_i2c0);
79 
80 #endif
81 
82 #ifdef BSP_USING_I2C1
83 
84 #if  defined(ES_I2C1_SCL_GPIO_FUNC)&&defined(ES_I2C1_SCL_GPIO_PORT)&&defined(ES_I2C1_SCL_GPIO_PIN)
85     gpio_instruct.func = ES_I2C1_SCL_GPIO_FUNC;
86     ald_gpio_init(ES_I2C1_SCL_GPIO_PORT, ES_I2C1_SCL_GPIO_PIN, &gpio_instruct);
87 #endif
88 
89 #if  defined(ES_I2C1_SDA_GPIO_FUNC)&&defined(ES_I2C1_SDA_GPIO_PORT)&&defined(ES_I2C1_SDA_GPIO_PIN)
90     gpio_instruct.func = ES_I2C1_SDA_GPIO_FUNC;
91     ald_gpio_init(ES_I2C1_SDA_GPIO_PORT, ES_I2C1_SDA_GPIO_PIN, &gpio_instruct);
92 #endif
93 
94     /* Initialize i2c function */
95     _h_i2c1.perh = I2C1;
96     _h_i2c1.init.duty         = I2C_DUTYCYCLE_2;
97     _h_i2c1.init.clk_speed    = ES_I2C1_CLK_SPEED;
98     _h_i2c1.init.own_addr1    = ES_I2C1_OWN_ADDR1;
99     _h_i2c1.init.addr_mode    = ES_I2C1_ADDR_MODE;
100     _h_i2c1.init.general_call = ES_I2C1_GENERAL_CALL;
101     _h_i2c1.init.no_stretch   = ES_I2C1_STRETCH;
102 
103     ald_i2c_reset(&_h_i2c1);
104     ald_i2c_init(&_h_i2c1);
105 
106 #endif
107 }
108 
es32f0_master_xfer(struct rt_i2c_bus_device * bus,struct rt_i2c_msg msgs[],rt_uint32_t num)109 static rt_ssize_t es32f0_master_xfer(struct rt_i2c_bus_device *bus,
110                                     struct rt_i2c_msg msgs[],
111                                     rt_uint32_t num)
112 {
113     struct rt_i2c_msg *msg;
114     rt_uint32_t i;
115     rt_err_t ret = -RT_ERROR;
116 
117     for (i = 0; i < num; i++)
118     {
119         msg = &msgs[i];
120         if (msg->flags & RT_I2C_RD)
121         {
122             if (ald_i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
123             {
124                 LOG_E("i2c bus write failed,i2c bus stop!\n");
125                 goto out;
126             }
127         }
128         else
129         {
130             if (ald_i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
131             {
132                 LOG_E("i2c bus write failed,i2c bus stop!\n");
133                 goto out;
134             }
135         }
136     }
137 
138     ret = i;
139 
140 out:
141     LOG_E("send stop condition\n");
142 
143     return ret;
144 }
145 
146 const struct rt_i2c_bus_device_ops es32f0_i2c_ops =
147 {
148     es32f0_master_xfer,
149     RT_NULL,
150     RT_NULL,
151 };
152 
rt_hw_i2c_init(void)153 int rt_hw_i2c_init(void)
154 {
155     int result = RT_EOK;
156 
157     _i2c_init();
158 
159 #ifdef BSP_USING_I2C0
160     /* define i2c Instance */
161     static struct rt_i2c_bus_device _i2c_device0;
162     rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
163     _i2c_device0.ops = &es32f0_i2c_ops;
164     _i2c_device0.priv = &_h_i2c0;
165     result = rt_i2c_bus_device_register(&_i2c_device0, ES_DEVICE_NAME_I2C0);
166     if (result != RT_EOK)
167     {
168         return result;
169     }
170 #endif
171 
172 #ifdef BSP_USING_I2C1
173     /* define i2c Instance */
174     static struct rt_i2c_bus_device _i2c_device1;
175     rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
176 
177     _i2c_device1.ops = &es32f0_i2c_ops;
178     _i2c_device1.priv = &_h_i2c1;
179     rt_i2c_bus_device_register(&_i2c_device1, ES_DEVICE_NAME_I2C1);
180     if (result != RT_EOK)
181     {
182         return result;
183     }
184 #endif
185 
186     return RT_EOK;
187 }
188 INIT_DEVICE_EXPORT(rt_hw_i2c_init);
189 
190 #endif
191