summaryrefslogtreecommitdiff
path: root/bsp/nxp/mcx/mcxe/libraries/drivers/drv_i2c.c
blob: 9faf1e6b8a7721c877e56f446f9621951d648b1f (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
/*
 * Copyright (c) 2006-2024 RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-08-21     hywing       The first version
 */

#include <rtdevice.h>
#include "fsl_lpi2c.h"

#ifdef RT_USING_I2C

#define DBG_TAG "drv.i2c"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

enum
{
#ifdef BSP_USING_I2C0
    I2C0_INDEX,
#endif
#ifdef BSP_USING_I2C1
    I2C1_INDEX,
#endif
};


struct mcx_i2c_bus
{
    struct rt_i2c_bus_device i2c_bus;
    LPI2C_Type              *i2c_base;
    uint32_t                 baud;
    clock_ip_name_t          clock_ip_name;
    clock_ip_src_t           clock_ip_src;
    char                    *name;
};


static struct mcx_i2c_bus i2c_buses[] =
    {
#ifdef BSP_USING_I2C0
        {
                             .i2c_base      = LPI2C0,
                             .baud          = 100000U,
                             .clock_ip_name = kCLOCK_Lpi2c0,
                             .clock_ip_src  = kCLOCK_IpSrcSysOscAsync,
                             .name          = "i2c0",
                             },
#endif
#ifdef BSP_USING_I2C1
        {
                             .i2c_base      = LPI2C1,
                             .baud          = 100000U,
                             .clock_ip_name = kCLOCK_Lpi2c1,
                             .clock_ip_src  = kCLOCK_IpSrcSysOscAsync,
                             .name          = "i2c1",
                             },
#endif
};

static rt_ssize_t lpc_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
{
    struct rt_i2c_msg      *msg;
    lpi2c_master_transfer_t xfer = {0};
    rt_uint32_t             i;
    rt_ssize_t              ret = 0;

    struct mcx_i2c_bus *priv = (struct mcx_i2c_bus *)bus;

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

        if (msg->flags & RT_I2C_RD)
        {
            xfer.slaveAddress   = msg->addr;
            xfer.direction      = kLPI2C_Read;
            xfer.subaddress     = 0;
            xfer.subaddressSize = 0;
            xfer.data           = msg->buf;
            xfer.dataSize       = msg->len;

            xfer.flags = kLPI2C_TransferDefaultFlag;

            if (i != 0)
            {
                xfer.flags |= kLPI2C_TransferRepeatedStartFlag;
            }

            if (i != num - 1)
            {
                xfer.flags |= kLPI2C_TransferNoStopFlag;
            }

            if (LPI2C_MasterTransferBlocking(priv->i2c_base, &xfer) != kStatus_Success)
            {
                LOG_D("i2c bus read failed!\n");
                return i;
            }
        }
        else
        {
            xfer.slaveAddress   = msg->addr;
            xfer.direction      = kLPI2C_Write;
            xfer.subaddress     = 0;
            xfer.subaddressSize = 0;
            xfer.data           = msg->buf;
            xfer.dataSize       = msg->len;

            xfer.flags = kLPI2C_TransferDefaultFlag;

            if (i != 0)
            {
                xfer.flags |= kLPI2C_TransferRepeatedStartFlag;
            }

            if (i != num - 1)
            {
                xfer.flags |= kLPI2C_TransferNoStopFlag;
            }

            if (LPI2C_MasterTransferBlocking(priv->i2c_base, &xfer) != kStatus_Success)
            {
                LOG_D("i2c bus write failed!\n");
                return i;
            }
        }
    }
    ret = i;

    return ret;
}

static const struct rt_i2c_bus_device_ops i2c_ops = {
    .master_xfer     = lpc_i2c_xfer,
    .slave_xfer      = RT_NULL,
    .i2c_bus_control = RT_NULL,
};

int rt_hw_i2c_init(void)
{
    int                   i;
    lpi2c_master_config_t masterConfig;

    for (i = 0; i < ARRAY_SIZE(i2c_buses); i++)
    {
        struct mcx_i2c_bus *priv = &i2c_buses[i];

        CLOCK_SetIpSrc(priv->clock_ip_name, priv->clock_ip_src);

        LPI2C_MasterGetDefaultConfig(&masterConfig);
        masterConfig.baudRate_Hz = priv->baud;

        LPI2C_MasterInit(priv->i2c_base, &masterConfig, CLOCK_GetIpFreq(priv->clock_ip_name));

        priv->i2c_bus.ops = &i2c_ops;

        rt_i2c_bus_device_register(&priv->i2c_bus, priv->name);
    }

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);

#endif /* RT_USING_I2C */