summaryrefslogtreecommitdiff
path: root/bsp/imxrt1052-evk/drivers/drv_i2c.c
blob: 38a3c45482b0e6b50882348814a916912ded1d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * File      : drv_i2c.c
 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-08-08     Yang        the first version
 */
 
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "fsl_gpio.h"
#include "fsl_lpi2c.h"

//#define DRV_I2C_DEBUG

#ifdef RT_USING_I2C

#ifdef RT_USING_I2C_BITOPS

#define I2CBUS_NAME             "i2c0"

struct stm32_i2c_bit_data
{
    struct
    {
        GPIO_Type *base;
        uint32_t pin;
    } scl, sda;
};

static void gpio_udelay(rt_uint32_t us)
{
    volatile rt_int32_t i;
    for (; us > 0; us--)
    {
        i = 1000;
        while (i--);
    }
}

static void gpio_set_input(GPIO_Type* base, uint32_t pin)
{
    if (base->GDIR & (1 << pin))        //output mode
    {
        base->GDIR &= ~(1 << pin);
        gpio_udelay(5);
    }
}

static void gpio_set_output(GPIO_Type* base, uint32_t pin)
{
    if (!(base->GDIR & (1 << pin)))        //input mode
    {    
        base->GDIR |= (1 << pin);
        gpio_udelay(5);
    }
}

static void gpio_set_sda(void *data, rt_int32_t state)
{
    struct stm32_i2c_bit_data *bd = data;
    
    gpio_set_output(bd->sda.base, bd->sda.pin);
    
    GPIO_PinWrite(bd->sda.base, bd->sda.pin, !!state);
}

static void gpio_set_scl(void *data, rt_int32_t state)
{
    struct stm32_i2c_bit_data *bd = data;
    
    gpio_set_output(bd->scl.base, bd->scl.pin);
    
    GPIO_PinWrite(bd->scl.base, bd->scl.pin, !!state);
}

static rt_int32_t gpio_get_sda(void *data)
{
    struct stm32_i2c_bit_data *bd = data;
    
    gpio_set_input(bd->sda.base, bd->sda.pin);

    return GPIO_ReadPinInput(bd->sda.base, bd->sda.pin);
}

static rt_int32_t gpio_get_scl(void *data)
{
    struct stm32_i2c_bit_data *bd = data;
    
    gpio_set_input(bd->scl.base, bd->scl.pin);

    return GPIO_ReadPinInput(bd->scl.base, bd->scl.pin);
}


#else /* RT_USING_I2C_BITOPS */
    // todo : add hardware i2c
#endif /* RT_USING_I2C_BITOPS */

int rt_hw_i2c_init(void)
{
#ifdef RT_USING_I2C_BITOPS
    /* register I2C1: SCL/P0_20 SDA/P0_19 */
    {
        static struct rt_i2c_bus_device i2c_device;

        static const struct stm32_i2c_bit_data _i2c_bdata =
        {
            /* SCL */ {GPIO1, 16},
            /* SDA */ {GPIO1, 17},
        };

        static const struct rt_i2c_bit_ops _i2c_bit_ops =
        {
            (void*)&_i2c_bdata,
            gpio_set_sda,
            gpio_set_scl,
            gpio_get_sda,
            gpio_get_scl,

            gpio_udelay,

            50,
            1000
        };

        gpio_pin_config_t pin_config = {
            kGPIO_DigitalOutput, 0,
        };
        
        CLOCK_EnableClock(kCLOCK_Iomuxc);

        IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_00_GPIO1_IO16, 1U);
            IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_00_GPIO1_IO16,
            0xD8B0u);                                /* Slew Rate Field: Slow Slew Rate
                                                        Drive Strength Field: R0/6
                                                        Speed Field: medium(100MHz)
                                                        Open Drain Enable Field: Open Drain Enabled
                                                        Pull / Keep Enable Field: Pull/Keeper Enabled
                                                        Pull / Keep Select Field: Keeper
                                                        Pull Up / Down Config. Field: 22K Ohm Pull Up
                                                        Hyst. Enable Field: Hysteresis Disabled */
        IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_01_GPIO1_IO17, 1U);
            IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_01_GPIO1_IO17,
            0xD8B0u);                                /* Slew Rate Field: Slow Slew Rate
                                                        Drive Strength Field: R0/6
                                                        Speed Field: medium(100MHz)
                                                        Open Drain Enable Field: Open Drain Enabled
                                                        Pull / Keep Enable Field: Pull/Keeper Enabled
                                                        Pull / Keep Select Field: Keeper
                                                        Pull Up / Down Config. Field: 22K Ohm Pull Up
                                                        Hyst. Enable Field: Hysteresis Disabled */
        /* Enable touch panel controller */
        GPIO_PinInit(_i2c_bdata.sda.base, _i2c_bdata.sda.pin, &pin_config);
        GPIO_PinInit(_i2c_bdata.scl.base, _i2c_bdata.scl.pin, &pin_config);
        
        GPIO_PortSet(_i2c_bdata.sda.base, _i2c_bdata.sda.pin);
        GPIO_PortSet(_i2c_bdata.scl.base, _i2c_bdata.scl.pin);
        
        //RT_ASSERT(gpio_get_scl(&_i2c_bdata) != 0);
        //RT_ASSERT(gpio_get_sda(&_i2c_bdata) != 0);

        i2c_device.priv = (void *)&_i2c_bit_ops;
        rt_i2c_bit_add_bus(&i2c_device, I2CBUS_NAME);
    } /* register I2C */

#else /* RT_USING_I2C_BITOPS */
    // Todo : add hardware i2c

#endif /* RT_USING_I2C_BITOPS */

    return 0;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);

#if defined(RT_USING_FINSH) && defined(DRV_I2C_DEBUG)
#include <finsh.h>

static rt_device_t _i2c_find(const char *name)
{
    rt_device_t dev;

    dev = rt_device_find(name);
    if (!dev) 
    {
        rt_kprintf("search device failed: %s\n", name);
        return RT_NULL;
    }

    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
    {
        rt_kprintf("open device failed: %s\n", name);
        return RT_NULL;
    }

    rt_kprintf("open i2c bus: %s\n", name);
    
    return dev;
}

static void _search_i2c_device(rt_device_t dev, uint8_t cmd)
{
    int count = 0;
    struct rt_i2c_msg msgs[2];
    uint8_t buf = 0;

    msgs[0].flags = RT_I2C_WR;
    msgs[0].buf   = &cmd;
    msgs[0].len   = sizeof(cmd);

    msgs[1].flags = RT_I2C_RD;
    msgs[1].buf   = &buf;
    msgs[1].len   = 1;

    for (int i = 0; i <= 0x7f; i++)
    {
        int len;
        
        msgs[0].addr  = i;
        msgs[1].addr  = i;
        len = rt_i2c_transfer(dev, msgs, 2);
        if (len == 2)
        {
            count++;
            rt_kprintf("add:%02X transfer success, id: %02X\n", i, buf);
        } 
    }
    
    rt_kprintf("i2c device: %d\n", count);
}

static int i2c_test(const char *name, uint8_t cmd)
{
    rt_device_t dev = _i2c_find(name);
    if (dev == RT_NULL)
    {
        rt_kprintf("search i2c device faild\n");
        return -1;
    }
    
    _search_i2c_device(dev, cmd);
    
    rt_device_close(dev);
    
    return 0;
}
FINSH_FUNCTION_EXPORT(i2c_test, e.g: i2c_test("i2c0", 0xA3));
#endif

#endif /* RT_USING_I2C */