1 /**************************************************************************//**
2 *
3 * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Change Logs:
8 * Date            Author           Notes
9 * 2020-6-28       YCHuang12        First version
10 *
11 ******************************************************************************/
12 
13 #include <rtconfig.h>
14 
15 #if (defined(BSP_USING_SOFT_I2C) && defined(BSP_USING_GPIO) && defined(RT_USING_I2C_BITOPS) && defined(RT_USING_I2C) && defined(RT_USING_PIN))
16 
17 #include <rtthread.h>
18 #include <rthw.h>
19 #include <rtdevice.h>
20 #include "NuMicro.h"
21 
22 /* Private define ---------------------------------------------------------------*/
23 #define LOG_TAG          "drv.softi2c"
24 #define DBG_ENABLE
25 #define DBG_SECTION_NAME LOG_TAG
26 #define DBG_LEVEL        DBG_INFO
27 #include <rtdbg.h>
28 
29 #ifdef BSP_USING_SOFT_I2C0
30 #define NU_SOFT_I2C0_BUS_CONFIG                          \
31     {                                                    \
32         .scl = BSP_SOFT_I2C0_SCL_PIN,                    \
33         .sda = BSP_SOFT_I2C0_SDA_PIN,                    \
34         .bus_name = "softi2c0",                         \
35     }
36 #endif
37 
38 #ifdef BSP_USING_SOFT_I2C1
39 #define NU_SOFT_I2C1_BUS_CONFIG                          \
40     {                                                    \
41         .scl = BSP_SOFT_I2C1_SCL_PIN,                    \
42         .sda = BSP_SOFT_I2C1_SDA_PIN,                    \
43         .bus_name = "softi2c1",                         \
44     }
45 #endif
46 
47 #if (!defined(BSP_USING_SOFT_I2C0) && !defined(BSP_USING_SOFT_I2C1))
48     #error "Please define at least one BSP_USING_SOFT_I2Cx"
49     /* this driver can be disabled at menuconfig ? RT-Thread Components ? Device Drivers */
50 #endif
51 
52 /* Private typedef --------------------------------------------------------------*/
53 /* soft i2c config class */
54 struct nu_soft_i2c_config
55 {
56     rt_uint8_t scl;
57     rt_uint8_t sda;
58     const char *bus_name;
59 };
60 /* soft i2c driver class */
61 struct nu_soft_i2c
62 {
63     struct rt_i2c_bit_ops ops;
64     struct rt_i2c_bus_device soft_i2c_bus;
65 };
66 
67 /* Private functions ------------------------------------------------------------*/
68 static void nu_soft_i2c_set_sda(void *data, rt_int32_t state);
69 static void nu_soft_i2c_set_scl(void *data, rt_int32_t state);
70 static rt_int32_t nu_soft_i2c_get_sda(void *data);
71 static rt_int32_t nu_soft_i2c_get_scl(void *data);
72 
73 /* Private variables ------------------------------------------------------------*/
74 static const struct nu_soft_i2c_config nu_soft_i2c_cfg[] =
75 {
76 #ifdef BSP_USING_SOFT_I2C0
77     NU_SOFT_I2C0_BUS_CONFIG,
78 #endif
79 #ifdef BSP_USING_SOFT_I2C1
80     NU_SOFT_I2C1_BUS_CONFIG,
81 #endif
82 };
83 
84 static struct nu_soft_i2c nu_soft_i2c_obj[sizeof(nu_soft_i2c_cfg) / sizeof(nu_soft_i2c_cfg[0])];
85 
86 static const struct rt_i2c_bit_ops nu_soft_i2c_bit_ops =
87 {
88     .data     = RT_NULL,
89     .set_sda  = nu_soft_i2c_set_sda,
90     .set_scl  = nu_soft_i2c_set_scl,
91     .get_sda  = nu_soft_i2c_get_sda,
92     .get_scl  = nu_soft_i2c_get_scl,
93     .udelay   = rt_hw_us_delay,
94     .delay_us = 1,
95     .timeout  = 100
96 };
97 
98 /* Functions define ------------------------------------------------------------*/
99 
100 /**
101  * This function initializes the soft i2c pin.
102  *
103  * @param soft i2c config class.
104  */
nu_soft_i2c_gpio_init(const struct nu_soft_i2c_config * cfg)105 static void nu_soft_i2c_gpio_init(const struct nu_soft_i2c_config *cfg)
106 {
107     rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
108     rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
109 
110     rt_pin_write(cfg->scl, PIN_HIGH);
111     rt_pin_write(cfg->sda, PIN_HIGH);
112 }
113 
114 /**
115  * if i2c is locked, this function will unlock it
116  *
117  * @param soft i2c config class
118  *
119  * @return RT_EOK indicates successful unlock.
120  */
nu_soft_i2c_bus_unlock(const struct nu_soft_i2c_config * cfg)121 static rt_err_t nu_soft_i2c_bus_unlock(const struct nu_soft_i2c_config *cfg)
122 {
123     rt_int32_t i = 0;
124 
125     if (PIN_LOW == rt_pin_read(cfg->sda))
126     {
127         while (i++ < 9)
128         {
129             rt_pin_write(cfg->scl, PIN_HIGH);
130             rt_hw_us_delay(100);
131             rt_pin_write(cfg->scl, PIN_LOW);
132             rt_hw_us_delay(100);
133         }
134     }
135     if (PIN_LOW == rt_pin_read(cfg->sda))
136     {
137         return -RT_ERROR;
138     }
139 
140     return RT_EOK;
141 }
142 
143 /**
144  * This function sets the sda pin.
145  *
146  * @param soft i2c config class.
147  * @param The sda pin state.
148  */
nu_soft_i2c_set_sda(void * data,rt_int32_t state)149 static void nu_soft_i2c_set_sda(void *data, rt_int32_t state)
150 {
151     struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
152 
153     rt_pin_write(cfg->sda, state ? PIN_HIGH : PIN_LOW);
154 }
155 
156 /**
157  * This function sets the scl pin.
158  *
159  * @param soft i2c config class.
160  * @param The scl pin state.
161  */
nu_soft_i2c_set_scl(void * data,rt_int32_t state)162 static void nu_soft_i2c_set_scl(void *data, rt_int32_t state)
163 {
164     struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
165 
166     rt_pin_write(cfg->scl, state ? PIN_HIGH : PIN_LOW);
167 }
168 
169 /**
170  * This function gets the sda pin state.
171  *
172  * @param The sda pin state.
173  */
nu_soft_i2c_get_sda(void * data)174 static rt_int32_t nu_soft_i2c_get_sda(void *data)
175 {
176     struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
177     return rt_pin_read(cfg->sda);
178 }
179 
180 /**
181  * This function gets the scl pin state.
182  *
183  * @param The scl pin state.
184  */
nu_soft_i2c_get_scl(void * data)185 static rt_int32_t nu_soft_i2c_get_scl(void *data)
186 {
187     struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
188     return rt_pin_read(cfg->scl);
189 }
190 
191 /* Soft I2C initialization function */
rt_soft_i2c_init(void)192 int rt_soft_i2c_init(void)
193 {
194     rt_size_t obj_num = sizeof(nu_soft_i2c_obj) / sizeof(struct nu_soft_i2c);
195     rt_err_t result;
196 
197     for (int i = 0; i < obj_num; i++)
198     {
199         nu_soft_i2c_obj[i].ops = nu_soft_i2c_bit_ops;
200         nu_soft_i2c_obj[i].ops.data = (void *)&nu_soft_i2c_cfg[i];
201         nu_soft_i2c_obj[i].soft_i2c_bus.priv = &nu_soft_i2c_obj[i].ops;
202         nu_soft_i2c_gpio_init(&nu_soft_i2c_cfg[i]);
203         result = rt_i2c_bit_add_bus(&nu_soft_i2c_obj[i].soft_i2c_bus, nu_soft_i2c_cfg[i].bus_name);
204         RT_ASSERT(result == RT_EOK);
205         nu_soft_i2c_bus_unlock(&nu_soft_i2c_cfg[i]);
206 
207         LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
208               nu_soft_i2c_cfg[i].bus_name,
209               nu_soft_i2c_cfg[i].scl,
210               nu_soft_i2c_cfg[i].sda);
211     }
212 
213     return 0;
214 }
215 INIT_DEVICE_EXPORT(rt_soft_i2c_init);
216 
217 #endif //#if (defined(BSP_USING_SOFT_I2C) && defined(BSP_USING_GPIO) && defined(RT_USING_I2C_BITOPS) && defined(RT_USING_I2C) && defined(RT_USING_PIN))
218