forked from XiaojieFan/at24cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat24cxx.c
295 lines (259 loc) · 6.95 KB
/
at24cxx.c
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
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-04-13 XiaojieFan the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <string.h>
#define DBG_ENABLE
#define DBG_SECTION_NAME "AHT10"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>
#include "at24cxx.h"
//分支
#ifdef PKG_USING_AT24CXX
#define AT24CXX_ADDR 0x50 //A0 A1 A2 connect GND
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs;
msgs.addr = AT24CXX_ADDR;
msgs.flags = RT_I2C_RD;
msgs.buf = buf;
msgs.len = len;
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
uint8_t at24cxx_read_one_byte(struct rt_i2c_bus_device *bus, uint8_t readAddr)
{
rt_uint8_t buf[2];
rt_uint8_t temp;
buf[0] = readAddr;
if (rt_i2c_master_send(bus, AT24CXX_ADDR, 0, buf, 1) == 0)
{
return -RT_ERROR;
}
read_regs(bus, 1, &temp);
return temp;
}
rt_err_t at24cxx_write_one_byte(struct rt_i2c_bus_device *bus, uint8_t writeAddr, uint8_t dataToWrite)
{
rt_uint8_t buf[2];
buf[0] = writeAddr; //cmd
buf[1] = dataToWrite;
//buf[2] = data[1];
if (rt_i2c_master_send(bus, AT24CXX_ADDR, 0, buf, 2) == 2)
return RT_EOK;
else
return -RT_ERROR;
}
rt_err_t at24cxx_check(at24cxx_device_t dev)
{
uint8_t temp;
rt_err_t result;
RT_ASSERT(dev);
temp = at24cxx_read_one_byte(dev->i2c, 255);
if (temp == 0x55) return RT_EOK;
else
{
at24cxx_write_one_byte(dev->i2c, 255, 0x55);
temp = at24cxx_read_one_byte(dev->i2c, 255);
if (temp == 0x55) return RT_EOK;
}
return RT_ERROR;
}
/**
* This function read the specific numbers of data to the specific position
*
* @param bus the name of at24cxx device
* @param ReadAddr the start position to read
* @param pBuffer the read data store position
* @param NumToRead
* @return RT_EOK write ok.
*/
rt_err_t at24cxx_read(at24cxx_device_t dev, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead)
{
rt_err_t result;
RT_ASSERT(dev);
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
while (NumToRead)
{
*pBuffer++ = at24cxx_read_one_byte(dev->i2c, ReadAddr++);
NumToRead--;
}
}
else
{
LOG_E("The at24cxx could not respond at this time. Please try again");
}
rt_mutex_release(dev->lock);
return RT_EOK;
}
/**
* This function write the specific numbers of data to the specific position
*
* @param bus the name of at24cxx device
* @param WriteAddr the start position to write
* @param pBuffer the data need to write
* @param NumToWrite
* @return RT_EOK write ok.at24cxx_device_t dev
*/
rt_err_t at24cxx_write(at24cxx_device_t dev, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite)
{
uint8_t i = 0;
rt_err_t result;
RT_ASSERT(dev);
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
while (1) //NumToWrite--
{
if (at24cxx_write_one_byte(dev->i2c, WriteAddr, pBuffer[i]) != RT_EOK)
{
rt_thread_mdelay(1);
}
else
{
WriteAddr++;
i++;
}
if (WriteAddr == NumToWrite)
{
break;
}
}
}
else
{
LOG_E("The at24cxx could not respond at this time. Please try again");
}
rt_mutex_release(dev->lock);
return RT_EOK;
}
/**
* This function initializes at24cxx registered device driver
*
* @param dev the name of at24cxx device
*
* @return the at24cxx device.
*/
at24cxx_device_t at24cxx_init(const char *i2c_bus_name)
{
at24cxx_device_t dev;
RT_ASSERT(i2c_bus_name);
dev = rt_calloc(1, sizeof(struct at24cxx_device));
if (dev == RT_NULL)
{
LOG_E("Can't allocate memory for at24cxx device on '%s' ", i2c_bus_name);
return RT_NULL;
}
dev->i2c = rt_i2c_bus_device_find(i2c_bus_name);
if (dev->i2c == RT_NULL)
{
LOG_E("Can't find at24cxx device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
dev->lock = rt_mutex_create("mutex_at24cxx", RT_IPC_FLAG_FIFO);
if (dev->lock == RT_NULL)
{
LOG_E("Can't create mutex for at24cxx device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
return dev;
}
/**
* This function releases memory and deletes mutex lock
*
* @param dev the pointer of device driver structure
*/
void at24cxx_deinit(at24cxx_device_t dev)
{
RT_ASSERT(dev);
rt_mutex_delete(dev->lock);
rt_free(dev);
}
uint8_t TEST_BUFFER[] = "WELCOM TO RTT";
#define SIZE sizeof(TEST_BUFFER)
void at24cxx(int argc, char *argv[])
{
static at24cxx_device_t dev = RT_NULL;
if (argc > 1)
{
if (!strcmp(argv[1], "probe"))
{
if (argc > 2)
{
/* initialize the sensor when first probe */
if (!dev || strcmp(dev->i2c->parent.parent.name, argv[2]))
{
/* deinit the old device */
if (dev)
{
at24cxx_deinit(dev);
}
dev = at24cxx_init(argv[2]);
}
}
else
{
rt_kprintf("at24cxx probe <dev_name> - probe sensor by given name\n");
}
}
else if (!strcmp(argv[1], "read"))
{
if (dev)
{
uint8_t testbuffer[50];
/* read the eeprom data */
at24cxx_read(dev, 0, testbuffer, SIZE);
rt_kprintf("read at24cxx : %s\n", testbuffer);
}
else
{
rt_kprintf("Please using 'at24cxx probe <dev_name>' first\n");
}
}
else if (!strcmp(argv[1], "write"))
{
at24cxx_write(dev, 0, TEST_BUFFER, SIZE);
rt_kprintf("write ok\n");
}
else if (!strcmp(argv[1], "check"))
{
if (at24cxx_check(dev) == 1)
{
rt_kprintf("check faild \n");
}
}
else
{
rt_kprintf("Unknown command. Please enter 'at24cxx0' for help\n");
}
}
else
{
rt_kprintf("Usage:\n");
rt_kprintf("at24cxx probe <dev_name> - probe eeprom by given name\n");
rt_kprintf("at24cxx check - check eeprom at24cxx \n");
rt_kprintf("at24cxx read - read eeprom at24cxx data\n");
rt_kprintf("at24cxx write - write eeprom at24cxx data\n");
}
}
MSH_CMD_EXPORT(at24cxx, at24cxx eeprom function);
#endif