1 /*
2  * Copyright (c) 2006-2020, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-02-26     Jackistang   first version
9  *
10  */
11 
12 #include "drv_i2c.h"
13 #include "drivers/dev_i2c.h"
14 #include <rtdevice.h>
15 #include <rtthread.h>
16 #include "board.h"
17 
18 #ifdef RT_USING_I2C
19 
20 #if !defined(BSP_USING_I2C0) && !defined(BSP_USING_I2C1)
21     #error "Please define at least one BSP_USING_I2Cx"
22     /* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable I2C */
23 #endif
24 
25 #define DBG_LEVEL   DBG_LOG
26 #include <rtdbg.h>
27 #define LOG_TAG                "drv.i2c"
28 
29 /* mxc config class */
30 struct mxc_i2c_config
31 {
32     const char *name;
33     mxc_i2c_regs_t *i2c_periph;
34     i2c_speed_t speed;
35     IRQn_Type irq_type;
36 };
37 
38 struct mxc_i2c
39 {
40     struct rt_i2c_bus_device bus;
41     struct mxc_i2c_config *config;
42 };
43 
44 static struct mxc_i2c_config i2c_config[] =
45 {
46 #ifdef BSP_USING_I2C0
47     {
48         .name       = "i2c0",
49         .i2c_periph = MXC_I2C_GET_I2C(0),
50         .irq_type   = MXC_I2C_GET_IRQ(0),
51         .speed      = I2C_STD_MODE,
52         /*I2C_STD_MODE: 100KHz, I2C_FAST_MODE: 400KHz, I2C_FASTPLUS_MODE: 1MHz, I2C_HS_MODE: 3.4MHz */
53     },
54 #endif
55 #ifdef BSP_USING_I2C1
56     {
57         .name       = "i2c1",
58         .i2c_periph = MXC_I2C_GET_I2C(1),
59         .irq_type   = MXC_I2C_GET_IRQ(1),
60         .speed      = I2C_STD_MODE,
61         /*I2C_STD_MODE: 100KHz, I2C_FAST_MODE: 400KHz, I2C_FASTPLUS_MODE: 1MHz, I2C_HS_MODE: 3.4MHz */
62     },
63 #endif
64 };
65 
66 static struct mxc_i2c i2c_obj[sizeof(i2c_config) / sizeof(i2c_config[0])] = {0};
67 
68 
mxc_i2c_mst_xfer(struct rt_i2c_bus_device * bus,struct rt_i2c_msg msgs[],rt_uint32_t num)69 static rt_ssize_t mxc_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
70                                   struct rt_i2c_msg msgs[],
71                                   rt_uint32_t num)
72 {
73     rt_uint32_t i, ret;
74     rt_uint16_t addr;
75     int error;
76     int restart = 0;
77     struct rt_i2c_msg *msg;
78     struct mxc_i2c *obj = (struct mxc_i2c *)bus;
79 
80     for (i = 0; i < num; i++)
81     {
82         msg = &msgs[i];
83 
84         if (msg->flags & RT_I2C_ADDR_10BIT || msg->flags & RT_I2C_NO_START || msg->flags & RT_I2C_IGNORE_NACK || msg->flags & RT_I2C_NO_READ_ACK)
85         {
86             LOG_E("Not support RT_I2C_ADDR_10BIT or RT_I2C_NO_START or RT_I2C_IGNORE_NACK or RT_I2C_NO_READ_ACK");
87             return 0;
88         }
89 
90         if (msg->flags & RT_I2C_NO_STOP)
91         {
92             restart = 1;
93         }
94 
95         if (msg->flags & RT_I2C_RD)
96         {
97             addr = msg->addr << 1;
98             if ((error = I2C_MasterRead(obj->config->i2c_periph, (uint8_t)addr, msg->buf, msg->len, restart)) != msg->len)
99             {
100                 LOG_E("Error writing %d", error);
101                 return 0;
102             }
103         }
104         else    /* RT_I2C_WR */
105         {
106             addr = msg->addr << 1;
107             if ((error = I2C_MasterWrite(obj->config->i2c_periph, (uint8_t)addr, msg->buf, msg->len, restart)) != msg->len)
108             {
109                 LOG_E("Error writing %d", error);
110                 return 0;
111             }
112         }
113     }
114     ret = i;
115 
116     return ret;
117 }
118 
119 static const struct rt_i2c_bus_device_ops mxc_i2c_ops =
120 {
121     mxc_i2c_mst_xfer,
122     RT_NULL,
123     RT_NULL,
124 };
125 
rt_hw_i2c_init(void)126 int rt_hw_i2c_init(void)
127 {
128     rt_size_t obj_num;
129     int index;
130     rt_err_t result = 0;
131 
132 #ifdef BSP_USING_I2C0
133     NVIC_EnableIRQ(I2C0_IRQn);
134 #endif
135 #ifdef BSP_USING_I2C1
136     NVIC_EnableIRQ(I2C1_IRQn);
137 #endif
138 
139     obj_num = sizeof(i2c_obj) / sizeof(struct mxc_i2c);
140     for (index = 0; index < obj_num; index++)
141     {
142         /* init i2c object */
143         i2c_obj[index].config = &i2c_config[index];
144         i2c_obj[index].bus.ops = &mxc_i2c_ops;
145 
146         /* init i2c device */
147         I2C_Shutdown(i2c_config[index].i2c_periph);
148         I2C_Init(i2c_config[index].i2c_periph, i2c_config[index].speed, RT_NULL);
149 
150         /* register i2c device */
151         result = rt_i2c_bus_device_register(&i2c_obj[index].bus,
152                                             i2c_obj[index].config->name
153                                            );
154         RT_ASSERT(result == RT_EOK);
155     }
156 
157     return 0;
158 }
159 INIT_DEVICE_EXPORT(rt_hw_i2c_init);
160 
161 #ifdef BSP_USING_I2C0
I2C0_IRQHandler(void)162 void I2C0_IRQHandler(void)
163 {
164     rt_interrupt_enter();
165     I2C_Handler(MXC_I2C0);
166     rt_interrupt_leave();
167 }
168 #endif
169 
170 #ifdef BSP_USING_I2C1
I2C1_IRQHandler(void)171 void I2C1_IRQHandler(void)
172 {
173     rt_interrupt_enter();
174     I2C_Handler(MXC_I2C1);
175     rt_interrupt_leave();
176 }
177 #endif
178 
179 #endif /* RT_USING_I2C */
180