summaryrefslogtreecommitdiff
path: root/bsp/n32/libraries/n32_drivers/drv_i2c.c
blob: 6f22c63a934a7cdf3d2b94b461429db3dc55326c (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-10-19     Nations      first version
 */

#include "drv_i2c.h"

#ifdef RT_USING_I2C

#define DBG_TAG               "drv.I2C"
#ifdef RT_I2C_DEBUG
#define DBG_LVL               DBG_LOG
#else
#define DBG_LVL               DBG_INFO
#endif
#include <rtdbg.h>


#ifdef RT_USING_I2C_BITOPS
static const struct n32_soft_i2c_config soft_i2c_config[] =
{
#ifdef BSP_USING_I2C1
    I2C1_BUS_CONFIG,
#endif

#ifdef BSP_USING_I2C2
    I2C2_BUS_CONFIG,
#endif

#ifdef BSP_USING_I2C3
    I2C3_BUS_CONFIG,
#endif

#ifdef BSP_USING_I2C4
    I2C4_BUS_CONFIG,
#endif
};

static struct n32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];

/**
*\*\name    n32_i2c_gpio_init
*\*\fun     Initializes the i2c pin.
*\*\param   i2c dirver class
*\*\return  none
**/
static void n32_i2c_gpio_init(struct n32_i2c *i2c)
{
    struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)i2c->ops.data;

    rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
    rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);

    rt_pin_write(cfg->scl, PIN_HIGH);
    rt_pin_write(cfg->sda, PIN_HIGH);
}

static void n32_i2c_pin_init(void)
{
    rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);

    for(rt_size_t i = 0; i < obj_num; i++)
    {
        n32_i2c_gpio_init(&i2c_obj[i]);
    }
}

/**
*\*\name    n32_set_sda
*\*\fun     sets the sda pin.
*\*\param   data config class
*\*\param   state sda pin state
*\*\return  none
**/
static void n32_set_sda(void *data, rt_int32_t state)
{
    struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
    if (state)
    {
        rt_pin_write(cfg->sda, PIN_HIGH);
    }
    else
    {
        rt_pin_write(cfg->sda, PIN_LOW);
    }
}

/**
*\*\name    n32_set_scl
*\*\fun     sets the scl pin.
*\*\param   data config class
*\*\param   state scl pin state
*\*\return  none
**/
static void n32_set_scl(void *data, rt_int32_t state)
{
    struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
    if (state)
    {
        rt_pin_write(cfg->scl, PIN_HIGH);
    }
    else
    {
        rt_pin_write(cfg->scl, PIN_LOW);
    }
}

/**
*\*\name    n32_get_sda
*\*\fun     gets the sda pin state.
*\*\param   data config class
*\*\return  sda pin state
**/
static rt_int32_t n32_get_sda(void *data)
{
    struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
    return rt_pin_read(cfg->sda);
}

/**
*\*\name    n32_get_scl
*\*\fun     gets the scl pin state.
*\*\param   data config class
*\*\return  scl pin state
**/
static rt_int32_t n32_get_scl(void *data)
{
    struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
    return rt_pin_read(cfg->scl);
}


/**
*\*\name    n32_udelay
*\*\fun     The time delay function.
*\*\param   us
*\*\return  none
**/
static void n32_udelay(rt_uint32_t us)
{
    rt_uint32_t ticks;
    rt_uint32_t told, tnow, tcnt = 0;
    rt_uint32_t reload = SysTick->LOAD;

    ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
    told = SysTick->VAL;

    while (1)
    {
        tnow = SysTick->VAL;
        if (tnow != told)
        {
            if (tnow < told)
            {
                tcnt += told - tnow;
            }
            else
            {
                tcnt += reload - tnow + told;
            }
            told = tnow;

            if (tcnt >= ticks)
            {
                break;
            }
        }
    }
}

static const struct rt_i2c_bit_ops n32_bit_ops_default =
{
    .data     = RT_NULL,
    .pin_init = n32_i2c_pin_init,
    .set_sda  = n32_set_sda,
    .set_scl  = n32_set_scl,
    .get_sda  = n32_get_sda,
    .get_scl  = n32_get_scl,
    .udelay   = n32_udelay,
    .delay_us = 1,
    .timeout  = 100,
    .i2c_pin_init_flag = RT_FALSE
};


/**
*\*\name    n32_i2c_bus_unlock
*\*\fun     If i2c is locked, this function will unlock it.
*\*\param   cfg
*\*\return  RT_EOK indicates successful unlock
**/
static rt_err_t n32_i2c_bus_unlock(const struct n32_soft_i2c_config *cfg)
{
    rt_int32_t i = 0;

    if (PIN_LOW == rt_pin_read(cfg->sda))
    {
        while (i++ < 9)
        {
            rt_pin_write(cfg->scl, PIN_HIGH);
            n32_udelay(100);
            rt_pin_write(cfg->scl, PIN_LOW);
            n32_udelay(100);
        }
    }

    if (PIN_LOW == rt_pin_read(cfg->sda))
    {
        return -RT_ERROR;
    }

    return RT_EOK;
}
#endif /* RT_USING_I2C_BITOPS */

#ifdef RT_USING_HARDWARE_I2C

#define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))

static uint32_t I2CTimeout = I2CT_LONG_TIMEOUT;

static int rt_i2c_read(rt_uint32_t i2c_periph, rt_uint16_t slave_address, rt_uint8_t* p_buffer, rt_uint16_t data_byte)
{
    I2CTimeout = I2CT_LONG_TIMEOUT;
    /* wait until I2C bus is idle */
    while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_BUSY))
    {
        if ((I2CTimeout--) == 0)
            return 9;
    };

    I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);

    /** Send START condition */
    I2C_GenerateStart((I2C_Module*)i2c_periph, ENABLE);

    I2CTimeout = I2CT_LONG_TIMEOUT;
    /* wait until SBSEND bit is set */
    while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_MODE_FLAG)) // EV5
    {
        if ((I2CTimeout--) == 0)
            return 10;
    };

    /* send slave address to I2C bus */
    I2C_SendAddr7bit((I2C_Module*)i2c_periph, slave_address, I2C_DIRECTION_RECV);

    I2CTimeout = I2CT_LONG_TIMEOUT;
    while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_RXMODE_FLAG)) // EV6
    {
        if ((I2CTimeout--) == 0)
            return 6;
    };

    /* while there is data to be read */
    while (data_byte)
    {
        /* wait until the RBNE bit is set and clear it */
        if (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_RXDATNE))
        {
            /* read a byte*/
            *p_buffer = I2C_RecvData((I2C_Module*)i2c_periph);

            /* point to the next location where the byte read will be saved */
            p_buffer++;

            /* decrement the read bytes counter */
            data_byte--;
            if (1 == data_byte)
            {
                /* disable acknowledge */
                I2C_ConfigAck((I2C_Module*)i2c_periph, DISABLE);
                /* send a stop condition to I2C bus */
                I2C_GenerateStop((I2C_Module*)i2c_periph, ENABLE);
            }
        }
    }

    /* wait until the stop condition is finished */
    while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_STOPF))
    {
        if ((I2CTimeout--) == 0)
            return 7;
    };

    /* enable acknowledge */
    I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);

    I2C_ConfigNackLocation((I2C_Module*)i2c_periph,I2C_NACK_POS_CURRENT);

    return 0;
}

static int rt_i2c_write(rt_uint32_t i2c_periph, uint16_t slave_address, uint8_t* p_buffer, uint16_t data_byte)
{
    uint8_t* sendBufferPtr = p_buffer;
    I2CTimeout             = I2CT_LONG_TIMEOUT;
    while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_BUSY))
    {
        if ((I2CTimeout--) == 0)
            return 4;
    };

    I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
    I2C_GenerateStart((I2C_Module*)i2c_periph, ENABLE);
    I2CTimeout = I2CT_LONG_TIMEOUT;
    while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_MODE_FLAG)) // EV5
    {
        if ((I2CTimeout--) == 0)
            return 5;
    };

    I2C_SendAddr7bit((I2C_Module*)i2c_periph, slave_address, I2C_DIRECTION_SEND);
    I2CTimeout = I2CT_LONG_TIMEOUT;
    while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_TXMODE_FLAG)) // EV6
    {
        if ((I2CTimeout--) == 0)
            return 6;
    };

    /* send data */
    while (data_byte-- > 0)
    {
        I2C_SendData((I2C_Module*)i2c_periph, *sendBufferPtr++);
        I2CTimeout = I2CT_LONG_TIMEOUT;
        while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_DATA_SENDING)) // EV8
        {
            if ((I2CTimeout--) == 0)
                return 7;
        };
    };

    I2CTimeout = I2CT_LONG_TIMEOUT;
    while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_DATA_SENDED)) // EV8-2
    {
        if ((I2CTimeout--) == 0)
            return 8;
    };
    I2C_GenerateStop((I2C_Module*)i2c_periph, ENABLE);
    return 0;
}



static rt_ssize_t rt_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
{
    struct rt_i2c_msg *msg;
    rt_uint32_t i;
    rt_err_t ret = -RT_ERROR;

    struct rt_i2c_bus *rt_i2c = (struct rt_i2c_bus *)bus;

    for (i = 0; i < num; i++)
    {
        msg = &msgs[i];

        if (msg->flags & RT_I2C_RD)
        {
            if (rt_i2c_read(rt_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
            {
                LOG_E("i2c bus write failed,i2c bus stop!");
                goto out;
            }
        }
        else
        {
            if (rt_i2c_write(rt_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
            {
                LOG_E("i2c bus write failed,i2c bus stop!");
                goto out;
            }
        }
    }

    ret = i;
    return ret;

out:
    LOG_E("send stop condition\n");

    return ret;
}

static const struct rt_i2c_bus_device_ops i2c_ops =
{
    rt_i2c_xfer,
    RT_NULL,
    RT_NULL
};

#endif /* RT_USING_HARDWARE_I2C */

int rt_hw_i2c_init(void)
{
#ifdef RT_USING_I2C_BITOPS

    rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
    rt_err_t result;

    for(rt_size_t i = 0; i < obj_num; i++)
    {
        i2c_obj[i].ops           = n32_bit_ops_default;
        i2c_obj[i].ops.data      = (void*)&soft_i2c_config[i];
        i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;

        result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);

        RT_ASSERT(result == RT_EOK);
        n32_i2c_bus_unlock(&soft_i2c_config[i]);

        rt_kprintf("software simulation %s init done, pin scl: %d, pin sda %d",
                   soft_i2c_config[i].bus_name,
                   soft_i2c_config[i].scl,
                   soft_i2c_config[i].sda);
    }
#endif /* RT_USING_I2C_BITOPS */

#ifdef RT_USING_HARDWARE_I2C

    GPIO_InitType GPIO_InitStructure;
    I2C_InitType  I2C_InitStructure;
#ifdef BSP_USING_I2C1
#define I2C1_SPEED  400000

    static struct rt_i2c_bus i2c_bus1;

#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* connect PB8 to I2C1_SCL, PB9 to I2C1_SDA */
    GPIO_InitStructure.Pin        = GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_OD;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);

    GPIO_ConfigPinRemap(GPIO_RMP_I2C1, ENABLE);
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
    /* Enable I2C clock */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* Confige I2C1_SCL(PB8) and  I2C1_SDA(PB9) */
    GPIO_InitStructure.Pin            = GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Pull      = GPIO_Pull_Up;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_I2C1;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
#endif

    I2C_DeInit(I2C1);
    I2C_InitStructure.BusMode     = I2C_BUSMODE_I2C;
    I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
    I2C_InitStructure.OwnAddr1    = 0xff;
    I2C_InitStructure.AckEnable   = I2C_ACKEN;
    I2C_InitStructure.AddrMode    = I2C_ADDR_MODE_7BIT;
    I2C_InitStructure.ClkSpeed    = I2C1_SPEED; // 400000 400K

    I2C_Init(I2C1, &I2C_InitStructure);

    rt_memset((void *)&i2c_bus1, 0, sizeof(struct rt_i2c_bus));
    i2c_bus1.parent.ops = &i2c_ops;
    i2c_bus1.i2c_periph = (rt_uint32_t)I2C1;
    rt_i2c_bus_device_register(&i2c_bus1.parent, "i2c1");
#endif

#ifdef BSP_USING_I2C2
#define I2C2_SPEED  100000

    static struct rt_i2c_bus i2c_bus2;

#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C2, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* connect PB10 to I2C2_SCL, PB11 to I2C2_SDA */
    GPIO_InitStructure.Pin        = GPIO_PIN_10 | GPIO_PIN_11;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_OD;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
    /* Enable I2C clock */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C2, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* Confige I2C1_SCL(PB10) and  I2C1_SDA(PB11) */
    GPIO_InitStructure.Pin            = GPIO_PIN_10 | GPIO_PIN_11;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Pull      = GPIO_Pull_Up;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_I2C2;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
#endif

    I2C_DeInit(I2C2);
    I2C_InitStructure.BusMode     = I2C_BUSMODE_I2C;
    I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
    I2C_InitStructure.OwnAddr1    = 0xff;
    I2C_InitStructure.AckEnable   = I2C_ACKEN;
    I2C_InitStructure.AddrMode    = I2C_ADDR_MODE_7BIT;
    I2C_InitStructure.ClkSpeed    = I2C2_SPEED; // 100000 100K

    I2C_Init(I2C2, &I2C_InitStructure);

    rt_memset((void *)&i2c_bus2, 0, sizeof(struct rt_i2c_bus));
    i2c_bus2.parent.ops = &i2c_ops;
    i2c_bus2.i2c_periph = (rt_uint32_t)I2C2;
    rt_i2c_bus_device_register(&i2c_bus2.parent, "i2c2");
#endif

#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
#ifdef BSP_USING_I2C3
#define I2C3_SPEED  100000

    static struct rt_i2c_bus i2c_bus3;

    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_I2C3, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* connect PC0 to I2C3_SCL, PC1 to I2C3_SDA */
    GPIO_InitStructure.Pin        = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_OD;
    GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);

    I2C_DeInit(I2C3);
    I2C_InitStructure.BusMode     = I2C_BUSMODE_I2C;
    I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
    I2C_InitStructure.OwnAddr1    = 0xff;
    I2C_InitStructure.AckEnable   = I2C_ACKEN;
    I2C_InitStructure.AddrMode    = I2C_ADDR_MODE_7BIT;
    I2C_InitStructure.ClkSpeed    = I2C3_SPEED; // 100000 100K

    I2C_Init(I2C3, &I2C_InitStructure);

    rt_memset((void *)&i2c_bus3, 0, sizeof(struct rt_i2c_bus));
    i2c_bus3.parent.ops = &i2c_ops;
    i2c_bus3.i2c_periph = (rt_uint32_t)I2C3;
    rt_i2c_bus_device_register(&i2c_bus3.parent, "i2c3");
#endif

#ifdef BSP_USING_I2C4
#define I2C4_SPEED  100000

    static struct rt_i2c_bus i2c_bus4;

    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_I2C4, ENABLE);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* connect PC6 to I2C4_SCL, PC7 to I2C4_SDA */
    GPIO_InitStructure.Pin        = GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_OD;
    GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);

    I2C_DeInit(I2C4);
    I2C_InitStructure.BusMode     = I2C_BUSMODE_I2C;
    I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
    I2C_InitStructure.OwnAddr1    = 0xff;
    I2C_InitStructure.AckEnable   = I2C_ACKEN;
    I2C_InitStructure.AddrMode    = I2C_ADDR_MODE_7BIT;
    I2C_InitStructure.ClkSpeed    = I2C4_SPEED; // 100000 100K

    I2C_Init(I2C4, &I2C_InitStructure);

    rt_memset((void *)&i2c_bus4, 0, sizeof(struct rt_i2c_bus));
    i2c_bus4.parent.ops = &i2c_ops;
    i2c_bus4.i2c_periph = (rt_uint32_t)I2C4;
    rt_i2c_bus_device_register(&i2c_bus4.parent, "i2c4");
#endif
#endif
#endif /* RT_USING_HARDWARE_I2C */

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);

#endif
/* end of i2c driver */