LibDriver FM24CLXX
Loading...
Searching...
No Matches
driver_fm24clxx.c
Go to the documentation of this file.
1
36
37#include "driver_fm24clxx.h"
38
42#define CHIP_NAME "Cypress FM24CLXX"
43#define MANUFACTURER_NAME "Cypress"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 3.65f
46#define MAX_CURRENT 0.1f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
62{
63 if (handle == NULL) /* check handle */
64 {
65 return 2; /* return error */
66 }
67 if (handle->debug_print == NULL) /* check debug_print */
68 {
69 return 3; /* return error */
70 }
71 if (handle->iic_init == NULL) /* check iic_init */
72 {
73 handle->debug_print("fm24clxx: iic_init is null.\n"); /* iic_init is null */
74
75 return 3; /* return error */
76 }
77 if (handle->iic_deinit == NULL) /* check iic_deinit */
78 {
79 handle->debug_print("fm24clxx: iic_deinit is null.\n"); /* iic_deinit is null */
80
81 return 3; /* return error */
82 }
83 if (handle->iic_read == NULL) /* check iic_read */
84 {
85 handle->debug_print("fm24clxx: iic_read is null.\n"); /* iic_read is null */
86
87 return 3; /* return error */
88 }
89 if (handle->iic_write == NULL) /* check iic_write */
90 {
91 handle->debug_print("fm24clxx: iic_write is null.\n"); /* iic_write is null */
92
93 return 3; /* return error */
94 }
95 if (handle->iic_read_address16 == NULL) /* check iic_read_address16 */
96 {
97 handle->debug_print("fm24clxx: iic_read_address16 is null.\n"); /* iic_read_address16 is null */
98
99 return 3; /* return error */
100 }
101 if (handle->iic_write_address16 == NULL) /* check iic_write_address16 */
102 {
103 handle->debug_print("fm24clxx: iic_write_address16 is null.\n"); /* iic_write_address16 is null */
104
105 return 3; /* return error */
106 }
107 if (handle->delay_ms == NULL) /* check delay_ms */
108 {
109 handle->debug_print("fm24clxx: delay_ms is null.\n"); /* delay_ms is null */
110
111 return 3; /* return error */
112 }
113
114 if (handle->iic_init() != 0) /* iic init */
115 {
116 handle->debug_print("fm24clxx: iic init failed.\n"); /* iic init failed */
117
118 return 1; /* return error */
119 }
120 handle->inited = 1; /* flag finish initialization */
121
122 return 0; /* success return 0 */
123}
124
136{
137 if (handle == NULL) /* check handle */
138 {
139 return 2; /* return error */
140 }
141 if (handle->inited != 1) /* check handle initialization */
142 {
143 return 3; /* return error */
144 }
145
146 if (handle->iic_deinit() != 0) /* iic deinit */
147 {
148 handle->debug_print("fm24clxx: iic deinit failed.\n"); /* iic deinit failed */
149
150 return 1; /* return error */
151 }
152 handle->inited = 0; /* flag close */
153
154 return 0; /* success return 0 */
155}
156
167{
168 if (handle == NULL) /* check handle */
169 {
170 return 2; /* return error */
171 }
172
173 handle->id = (uint16_t)type; /* set id */
174
175 return 0; /* success return 0 */
176}
177
188{
189 if (handle == NULL) /* check handle */
190 {
191 return 2; /* return error */
192 }
193
194 *type = (fm24clxx_t)(handle->id); /* get id */
195
196 return 0; /* success return 0 */
197}
198
209{
210 if (handle == NULL) /* check handle */
211 {
212 return 2; /* return error */
213 }
214
215 handle->iic_addr = 0xA0; /* set iic addr */
216 handle->iic_addr |= addr_pin << 1; /* set iic address */
217
218 return 0; /* success return 0 */
219}
220
231{
232 if (handle == NULL) /* check handle */
233 {
234 return 2; /* return error */
235 }
236
237 *addr_pin = (fm24clxx_address_t)((handle->iic_addr & (~0xA0)) >> 1); /* get iic address */
238
239 return 0; /* success return 0 */
240}
241
256uint8_t fm24clxx_read(fm24clxx_handle_t *handle, uint16_t address, uint8_t *buf, uint16_t len)
257{
258 uint8_t page_remain;
259
260 if (handle == NULL) /* check handle */
261 {
262 return 2; /* return error */
263 }
264 if (handle->inited != 1) /* check handle initialization */
265 {
266 return 3; /* return error */
267 }
268
269 if ((uint16_t)(address + len) > handle->id) /* check length */
270 {
271 handle->debug_print("fm24clxx: read out of range.\n"); /* read out of range */
272
273 return 4; /* return error */
274 }
275 page_remain = (uint8_t)(8 - address % 8); /* get page remain */
276 if (len <= page_remain) /* page remain */
277 {
278 page_remain = (uint8_t)len; /* set page remain */
279 }
280 if (handle->id > (uint16_t)FM24CL16B) /* choose id to set different address */
281 {
282 while (1)
283 {
284 if (handle->iic_read_address16(handle->iic_addr, address, buf, page_remain) != 0) /* read data */
285 {
286 handle->debug_print("fm24clxx: read failed.\n"); /* read failed */
287
288 return 1; /* return error */
289 }
290 if (page_remain == len) /* check break */
291 {
292 break; /* break loop */
293 }
294 else
295 {
296 address += page_remain; /* address increase */
297 buf += page_remain; /* buffer point increase */
298 len -= page_remain; /* length decrease */
299 if (len < 8) /* check length */
300 {
301 page_remain = (uint8_t)len; /* set the reset length */
302 }
303 else
304 {
305 page_remain = 8; /* set page */
306 }
307 }
308 }
309 }
310 else
311 {
312 while (1)
313 {
314 if (handle->iic_read((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256, buf,
315 page_remain) != 0) /* read page */
316 {
317 handle->debug_print("fm24clxx: read failed.\n"); /* read failed */
318
319 return 1; /* return error */
320 }
321 if (page_remain == len) /* check break */
322 {
323 break; /* break loop */
324 }
325 else
326 {
327 address += page_remain; /* address increase */
328 buf += page_remain; /* buffer point increase */
329 len -= page_remain; /* length decrease */
330 if (len < 8) /* check length */
331 {
332 page_remain = (uint8_t)len; /* set the reset length */
333 }
334 else
335 {
336 page_remain = 8; /* set page */
337 }
338 }
339 }
340 }
341
342 return 0; /* success return 0 */
343}
344
359uint8_t fm24clxx_write(fm24clxx_handle_t *handle, uint16_t address, uint8_t *buf, uint16_t len)
360{
361 uint8_t page_remain;
362
363 if (handle == NULL) /* check handle */
364 {
365 return 2; /* return error */
366 }
367 if (handle->inited != 1) /* check handle initialization */
368 {
369 return 3; /* return error */
370 }
371
372 if ((uint16_t)(address + len) > handle->id) /* check length */
373 {
374 handle->debug_print("fm24clxx: write out of range.\n"); /* write out of range */
375
376 return 1; /* return error */
377 }
378 page_remain = (uint8_t)(8 - address % 8); /* set page remain */
379 if (len <= page_remain) /* check length */
380 {
381 page_remain = (uint8_t)len; /* set page remain */
382 }
383 if (handle->id > (uint16_t)FM24CL16B) /* check id */
384 {
385 while (1)
386 {
387 if (handle->iic_write_address16(handle->iic_addr, address, buf, page_remain) != 0) /* write data */
388 {
389 handle->debug_print("fm24clxx: write failed.\n"); /* write failed */
390
391 return 1; /* return error */
392 }
393 if (page_remain == len) /* check break */
394 {
395 break; /* break */
396 }
397 else
398 {
399 address += page_remain; /* address increase */
400 buf += page_remain; /* buffer point increase */
401 len -= page_remain; /* length decrease */
402 if (len < 8) /* check length */
403 {
404 page_remain = (uint8_t)len; /* set the reset length */
405 }
406 else
407 {
408 page_remain = 8; /* set page */
409 }
410 }
411 }
412 }
413 else
414 {
415 while (1)
416 {
417 if (handle->iic_write((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256, buf,
418 page_remain) != 0) /* write page */
419 {
420 handle->debug_print("fm24clxx: write failed.\n"); /* write failed */
421
422 return 1; /* return error */
423 }
424 if (page_remain == len) /* check break */
425 {
426 break; /* break */
427 }
428 else
429 {
430 address += page_remain; /* address increase */
431 buf += page_remain; /* buffer point increase */
432 len -= page_remain; /* length decrease */
433 if (len < 8) /* check length */
434 {
435 page_remain = (uint8_t)len; /* set the rest length */
436 }
437 else
438 {
439 page_remain = 8; /* set page */
440 }
441 }
442 }
443 }
444
445 return 0; /* success return 0 */
446}
447
448
458{
459 if (info == NULL) /* check handle */
460 {
461 return 2; /* return error */
462 }
463
464 memset(info, 0, sizeof(fm24clxx_info_t)); /* initialize fm24clxx info structure */
465 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
466 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
467 strncpy(info->interface, "IIC", 8); /* copy interface name */
468 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
469 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
470 info->max_current_ma = MAX_CURRENT; /* set maximum current */
471 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
472 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
473 info->driver_version = DRIVER_VERSION; /* set driver version */
474
475 return 0; /* success return 0 */
476}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver fm24clxx header file
uint8_t fm24clxx_set_addr_pin(fm24clxx_handle_t *handle, fm24clxx_address_t addr_pin)
set the chip address pin
uint8_t fm24clxx_init(fm24clxx_handle_t *handle)
initialize the chip
struct fm24clxx_handle_s fm24clxx_handle_t
fm24clxx handle structure definition
uint8_t fm24clxx_read(fm24clxx_handle_t *handle, uint16_t address, uint8_t *buf, uint16_t len)
read bytes from the chip
fm24clxx_address_t
fm24clxx address enumeration definition
fm24clxx_t
fm24clxx type enumeration definition
struct fm24clxx_info_s fm24clxx_info_t
fm24clxx information structure definition
uint8_t fm24clxx_set_type(fm24clxx_handle_t *handle, fm24clxx_t type)
set the chip type
uint8_t fm24clxx_write(fm24clxx_handle_t *handle, uint16_t address, uint8_t *buf, uint16_t len)
write bytes to the chip
uint8_t fm24clxx_info(fm24clxx_info_t *info)
get chip's information
uint8_t fm24clxx_get_addr_pin(fm24clxx_handle_t *handle, fm24clxx_address_t *addr_pin)
get the chip address pin
uint8_t fm24clxx_deinit(fm24clxx_handle_t *handle)
close the chip
uint8_t fm24clxx_get_type(fm24clxx_handle_t *handle, fm24clxx_t *type)
get the chip type
@ FM24CL16B
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
char manufacturer_name[32]