1 /*
2 * Copyright (c) 2006-2024 RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2024-09-03 Yilin Sun Initial version
9 */
10
11 #include <rtdevice.h>
12
13 #include "fsl_i2c.h"
14
15 #ifdef RT_USING_I2C
16
17 #define BSP_DEFAULT_I2C_FREQ (100000)
18
19 typedef struct
20 {
21 struct rt_i2c_bus_device parent;
22 I2C_Type *instance;
23 uint32_t input_frequency;
24 } mcx_i2c_obj_t;
25
26 typedef struct
27 {
28 I2C_Type *instance;
29 uint8_t id;
30 } mcx_i2c_instance_t;
31
32 static const mcx_i2c_instance_t mcx_i2c_instances[] =
33 {
34 #ifdef BSP_USING_I2C0
35 {I2C0, 0},
36 #endif
37
38 #ifdef BSP_USING_I2C1
39 {I2C1, 1},
40 #endif
41 };
42
43 static mcx_i2c_obj_t mcx_i2c_list[ARRAY_SIZE(mcx_i2c_instances)];
44
mcx_i2c_master_xfer(struct rt_i2c_bus_device * bus,struct rt_i2c_msg msgs[],rt_uint32_t num)45 static int mcx_i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
46 {
47 struct rt_i2c_msg *msg;
48 i2c_master_transfer_t xfer = {0};
49 rt_uint32_t i;
50 rt_ssize_t ret = 0;
51
52 mcx_i2c_obj_t *i2c = (mcx_i2c_obj_t *)bus;
53
54 for (i = 0; i < num; i++)
55 {
56 msg = &msgs[i];
57
58 if (msg->flags & RT_I2C_RD)
59 {
60 xfer.slaveAddress = msg->addr;
61 xfer.direction = kI2C_Read;
62 xfer.subaddress = 0;
63 xfer.subaddressSize = 0;
64 xfer.data = msg->buf;
65 xfer.dataSize = msg->len;
66
67 xfer.flags = kI2C_TransferDefaultFlag;
68
69 if (i != 0)
70 {
71 xfer.flags |= kI2C_TransferRepeatedStartFlag;
72 }
73
74 if (i != num - 1)
75 {
76 xfer.flags |= kI2C_TransferNoStopFlag;
77 }
78
79
80 if (I2C_MasterTransferBlocking(i2c->instance, &xfer) != kStatus_Success)
81 {
82 return i;
83 }
84 }
85 else
86 {
87 xfer.slaveAddress = msg->addr;
88 xfer.direction = kI2C_Write;
89 xfer.subaddress = 0;
90 xfer.subaddressSize = 0;
91 xfer.data = msg->buf;
92 xfer.dataSize = msg->len;
93
94 xfer.flags = kI2C_TransferDefaultFlag;
95
96 if (i != 0)
97 {
98 xfer.flags |= kI2C_TransferRepeatedStartFlag;
99 }
100
101 if (i != num - 1)
102 {
103 xfer.flags |= kI2C_TransferNoStopFlag;
104 }
105
106 if (I2C_MasterTransferBlocking(i2c->instance, &xfer) != kStatus_Success)
107 {
108 return i;
109 }
110 }
111 }
112
113 ret = num;
114
115 return ret;
116 }
117
118 static const struct rt_i2c_bus_device_ops mcx_i2c_ops =
119 {
120 mcx_i2c_master_xfer,
121 RT_NULL,
122 RT_NULL,
123 };
124
rt_hw_i2c_init(void)125 static int rt_hw_i2c_init(void)
126 {
127 i2c_master_config_t master_cfg;
128 char name_buf[16];
129
130 for (size_t i = 0; i < ARRAY_SIZE(mcx_i2c_instances); i++)
131 {
132 mcx_i2c_list[i].input_frequency = CLOCK_GetCoreSysClkFreq();
133 mcx_i2c_list[i].instance = mcx_i2c_instances[i].instance;
134 mcx_i2c_list[i].parent.ops = &mcx_i2c_ops;
135
136 I2C_MasterGetDefaultConfig(&master_cfg);
137
138 master_cfg.baudRate_Bps = BSP_DEFAULT_I2C_FREQ;
139
140 I2C_MasterInit(mcx_i2c_list[i].instance, &master_cfg, mcx_i2c_list[i].input_frequency);
141
142 rt_snprintf(name_buf, 16, "i2c%d", mcx_i2c_instances[i].id);
143
144 rt_i2c_bus_device_register(&mcx_i2c_list[i].parent, name_buf);
145 }
146
147 return RT_EOK;
148 }
149
150 INIT_DEVICE_EXPORT(rt_hw_i2c_init);
151
152 #endif
153