drv_i2c.c 3.7 KB
Newer Older
W
wangyq2018 已提交
1 2 3 4 5 6 7 8
/*
 * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2019-01-24     wangyq        the first version
9
 * 2019-11-01     wangyq        update libraries
W
wangyq2018 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "drv_i2c.h"
#include <ald_i2c.h>
#include <ald_gpio.h>

#ifdef RT_USING_I2C

#define TIMEOUT 0x0FFF
23 24
/* I2C struct definition */
static i2c_handle_t _h_i2c0, _h_i2c1;
W
wangyq2018 已提交
25 26 27

static void _i2c_init(void)
{
28
    gpio_init_t gpio_instruct;
W
wangyq2018 已提交
29

30
    /* Initialize I2C Pin */
W
wangyq2018 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    gpio_instruct.mode = GPIO_MODE_OUTPUT;
    gpio_instruct.odos = GPIO_PUSH_PULL;
    gpio_instruct.pupd = GPIO_PUSH_UP;
    gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
    gpio_instruct.flt  = GPIO_FILTER_DISABLE;
    gpio_instruct.type = GPIO_TYPE_CMOS;
    gpio_instruct.func = GPIO_FUNC_5;

#ifdef BSP_USING_I2C0
    /* Initialize I2C Function */
    _h_i2c0.perh = I2C0;
    _h_i2c0.init.clk_speed    = 100000;
    _h_i2c0.init.duty         = I2C_DUTYCYCLE_2;
    _h_i2c0.init.own_addr1    = 0x0A;
    _h_i2c0.init.addr_mode    = I2C_ADDR_7BIT;
    _h_i2c0.init.general_call = I2C_GENERALCALL_DISABLE;
    _h_i2c0.init.no_stretch   = I2C_NOSTRETCH_ENABLE;

49 50
    ald_i2c_reset(&_h_i2c0);
    ald_i2c_init(&_h_i2c0);
W
wangyq2018 已提交
51
    /* I2C0_SCL->PB8,  I2C0_SDA->PB9 */
52
    ald_gpio_init(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, &gpio_instruct);
53
#endif
W
wangyq2018 已提交
54 55 56 57 58 59 60 61 62 63 64

#ifdef BSP_USING_I2C1
    /* Initialize i2c function */
    _h_i2c1.perh = I2C1;
    _h_i2c1.init.clk_speed    = 100000;
    _h_i2c1.init.duty         = I2C_DUTYCYCLE_2;
    _h_i2c1.init.own_addr1    = 0xA0;
    _h_i2c1.init.addr_mode    = I2C_ADDR_7BIT;
    _h_i2c1.init.general_call = I2C_GENERALCALL_DISABLE;
    _h_i2c1.init.no_stretch   = I2C_NOSTRETCH_ENABLE;

65 66
    ald_i2c_reset(&_h_i2c1);
    ald_i2c_init(&_h_i2c1);
W
wangyq2018 已提交
67
    /* I2C1_SCL->PB10, I2C1_SDA->PB11 */
68
    ald_gpio_init(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, &gpio_instruct);
69
#endif
W
wangyq2018 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}

static rt_size_t es32f0_master_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;

    for (i = 0; i < num; i++)
    {
        msg = &msgs[i];
        if (msg->flags & RT_I2C_RD)
        {
85
            if (ald_i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
W
wangyq2018 已提交
86 87 88 89 90 91 92
            {
                i2c_dbg("i2c bus write failed,i2c bus stop!\n");
                goto out;
            }
        }
        else
        {
93
            if (ald_i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
W
wangyq2018 已提交
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
            {
                i2c_dbg("i2c bus write failed,i2c bus stop!\n");
                goto out;
            }
        }
    }

    ret = i;

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

    return ret;
}

const struct rt_i2c_bus_device_ops es32f0_i2c_ops =
{
    es32f0_master_xfer,
    RT_NULL,
    RT_NULL,
};

int rt_hw_i2c_init(void)
{
    _i2c_init();

#ifdef BSP_USING_I2C0
121 122
    /* define i2c Instance */
    static struct rt_i2c_bus_device _i2c_device0;
W
wangyq2018 已提交
123 124 125 126 127 128 129
    rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
    _i2c_device0.ops = &es32f0_i2c_ops;
    _i2c_device0.priv = &_h_i2c0;
    rt_i2c_bus_device_register(&_i2c_device0, "i2c0");
#endif

#ifdef BSP_USING_I2C1
130 131
    /* define i2c Instance */
    static struct rt_i2c_bus_device _i2c_device1;
W
wangyq2018 已提交
132 133 134 135 136 137 138 139 140
    rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
    _i2c_device1.ops = &es32f0_i2c_ops;
    _i2c_device1.priv = &_h_i2c1;
    rt_i2c_bus_device_register(&_i2c_device1, "i2c1");
#endif

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);
141

W
wangyq2018 已提交
142
#endif