blob: 8b8c6a02eabdb1a990e3a46063dc2588e9dbb57d [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <linux/types.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <stdbool.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include <sys/types.h>
9#include <sys/ioctl.h>
10#include <errno.h>
11#include <assert.h>
12#include <string.h>
13#include <linux/i2c.h>
14#include <linux/i2c-dev.h>
15
16#include "mbtk_log.h"
17#include "ql/ql_i2c.h"
18
19int Ql_I2C_Init(char *dev_name)
20{
21 int fd = open(dev_name, O_RDWR);
22 if(fd <= 0) {
23 LOGE("open (%s) fail:errno - %d", dev_name, errno);
24 return -1;
25 }
26 return fd;
27}
28
29
30int Ql_I2C_Read(int fd, unsigned short slaveAddr, unsigned char ofstAddr, unsigned char* ptrBuff, unsigned short length)
31{
32 uint8_t outbuf[1];
33 struct i2c_rdwr_ioctl_data packets;
34 struct i2c_msg messages[2];
35
36 outbuf[0] = ofstAddr;
37 messages[0].addr = slaveAddr;
38 messages[0].flags = 0;
39 messages[0].len = sizeof(outbuf);
40 messages[0].buf = outbuf;
41
42 /* The data will get returned in this structure */
43 messages[1].addr = slaveAddr;
44 messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
45 messages[1].len = length;
46 messages[1].buf = ptrBuff;
47
48 /* Send the request to the kernel and get the result back */
49 packets.msgs = messages;
50 packets.nmsgs = 2;
51 if(ioctl(fd, I2C_RDWR, &packets) < 0)
52 {
53 LOGE("Error: Unable to send data");
54 return -1;
55 }
56
57 return 0;
58}
59
60
61int Ql_I2C_Write(int fd, unsigned short slaveAddr, unsigned char ofstAddr, unsigned char* ptrData, unsigned short length)
62{
63 uint8_t *outbuf = NULL;
64 struct i2c_rdwr_ioctl_data packets;
65 struct i2c_msg messages[1];
66
67 outbuf = malloc(length + 1);
68 if (!outbuf) {
69 printf("Error: No memory for buffer\n");
70 return -1;
71 }
72
73 outbuf[0] = ofstAddr;
74 memcpy(outbuf + 1, ptrData, length);
75
76 messages[0].addr = slaveAddr;
77 messages[0].flags = 0;
78 messages[0].len = length + 1;
79 messages[0].buf = outbuf;
80
81 /* Transfer the i2c packets to the kernel and verify it worked */
82 packets.msgs = messages;
83 packets.nmsgs = 1;
84 if(ioctl(fd, I2C_RDWR, &packets) < 0)
85 {
86 printf("Error: Unable to send data");
87 free(outbuf);
88 return -1;
89 }
90
91 free(outbuf);
92
93 return 0;
94}
95
96
97int Ql_I2C_Deinit(int fd)
98{
99 if (fd <= 0)
100 return -1;
101
102 close(fd);
103 return 0;
104}
105
106#if 0
107int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)
108{
109 int file, len;
110
111 len = snprintf(filename, size, "/dev/i2c/%d", i2cbus);
112 if (len >= (int)size) {
113 fprintf(stderr, "%s: path truncated\n", filename);
114 return -EOVERFLOW;
115 }
116 file = open(filename, O_RDWR);
117
118 if (file < 0 && (errno == ENOENT || errno == ENOTDIR)) {
119 len = snprintf(filename, size, "/dev/i2c-%d", i2cbus);
120 if (len >= (int)size) {
121 fprintf(stderr, "%s: path truncated\n", filename);
122 return -EOVERFLOW;
123 }
124 file = open(filename, O_RDWR);
125 }
126 ...
127 return file;
128}
129
130static int i2c_read_bytes(int fd, uint8_t slave_addr, uint8_t reg_addr, uint8_t *values, uint8_t len)
131{
132 uint8_t outbuf[1];
133 struct i2c_rdwr_ioctl_data packets;
134 struct i2c_msg messages[2];
135
136 outbuf[0] = reg_addr;
137 messages[0].addr = slave_addr;
138 messages[0].flags = 0;
139 messages[0].len = sizeof(outbuf);
140 messages[0].buf = outbuf;
141
142 /* The data will get returned in this structure */
143 messages[1].addr = slave_addr;
144 messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
145 messages[1].len = len;
146 messages[1].buf = values;
147
148 /* Send the request to the kernel and get the result back */
149 packets.msgs = messages;
150 packets.nmsgs = 2;
151 if(ioctl(fd, I2C_RDWR, &packets) < 0)
152 {
153 printf("Error: Unable to send data");
154 return -1;
155 }
156
157 return 0;
158}
159
160
161static int i2c_write_bytes(int fd, uint8_t slave_addr, uint8_t reg_addr, uint8_t *values, uint8_t len)
162{
163 uint8_t *outbuf = NULL;
164 struct i2c_rdwr_ioctl_data packets;
165 struct i2c_msg messages[1];
166
167 outbuf = malloc(len + 1);
168 if (!outbuf) {
169 printf("Error: No memory for buffer\n");
170 return -1;
171 }
172
173 outbuf[0] = reg_addr;
174 memcpy(outbuf + 1, values, len);
175
176 messages[0].addr = slave_addr;
177 messages[0].flags = 0;
178 messages[0].len = len + 1;
179 messages[0].buf = outbuf;
180
181 /* Transfer the i2c packets to the kernel and verify it worked */
182 packets.msgs = messages;
183 packets.nmsgs = 1;
184 if(ioctl(fd, I2C_RDWR, &packets) < 0)
185 {
186 printf("Error: Unable to send data");
187 free(outbuf);
188 return -1;
189 }
190
191 free(outbuf);
192
193 return 0;
194}
195
196
197int set_slave_addr(int file, int address, int force)
198{
199 /* With force, let the user read from/write to the registers
200 even when a driver is also running */
201 if (ioctl(file, force ? I2C_SLAVE_FORCE : I2C_SLAVE, address) < 0) {
202 fprintf(stderr,
203 "Error: Could not set address to 0x%02x: %s\n",
204 address, strerror(errno));
205 return -errno;
206 }
207
208 return 0;
209}
210
211
212int main(void)
213{
214 int fd = -1;
215 char send_data[64] = {0};
216 send_data[0] = 0x55;
217 send_data[1] = 0x00;
218 send_data[2] = 0x84;
219 fd= open_i2c_dev(0, "i2c_test", 5, 0);
220 i2c_write_bytes(fd, 0x12, 0x10,send_data, 3)
221}
222#endif