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 if (handle == NULL) /* check handle */
259 {
260 return 2; /* return error */
261 }
262 if (handle->inited != 1) /* check handle initialization */
263 {
264 return 3; /* return error */
265 }
266 if ((uint16_t)(address + len) > handle->id) /* check length */
267 {
268 handle->debug_print("fm24clxx: read out of range.\n"); /* read out of range */
269
270 return 4; /* return error */
271 }
272
273 if (handle->id > (uint16_t)FM24CL16B) /* choose id to set different address */
274 {
275 if (handle->iic_read_address16(handle->iic_addr, address, buf, len) != 0) /* read data */
276 {
277 handle->debug_print("fm24clxx: read failed.\n"); /* read failed */
278
279 return 1; /* return error */
280 }
281 }
282 else
283 {
284 if (handle->iic_read((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256,
285 buf, len) != 0) /* read page */
286 {
287 handle->debug_print("fm24clxx: read failed.\n"); /* read failed */
288
289 return 1; /* return error */
290 }
291 }
292
293 return 0; /* success return 0 */
294}
295
310uint8_t fm24clxx_write(fm24clxx_handle_t *handle, uint16_t address, uint8_t *buf, uint16_t len)
311{
312 if (handle == NULL) /* check handle */
313 {
314 return 2; /* return error */
315 }
316 if (handle->inited != 1) /* check handle initialization */
317 {
318 return 3; /* return error */
319 }
320 if ((uint16_t)(address + len) > handle->id) /* check length */
321 {
322 handle->debug_print("fm24clxx: write out of range.\n"); /* write out of range */
323
324 return 4; /* return error */
325 }
326
327 if (handle->id > (uint16_t)FM24CL16B) /* check id */
328 {
329 if (handle->iic_write_address16(handle->iic_addr, address, buf, len) != 0) /* write data */
330 {
331 handle->debug_print("fm24clxx: write failed.\n"); /* write failed */
332
333 return 1; /* return error */
334 }
335 }
336 else
337 {
338 if (handle->iic_write((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256,
339 buf, len) != 0) /* write page */
340 {
341 handle->debug_print("fm24clxx: write failed.\n"); /* write failed */
342
343 return 1; /* return error */
344 }
345 }
346
347 return 0; /* success return 0 */
348}
349
359{
360 if (info == NULL) /* check handle */
361 {
362 return 2; /* return error */
363 }
364
365 memset(info, 0, sizeof(fm24clxx_info_t)); /* initialize fm24clxx info structure */
366 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
367 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
368 strncpy(info->interface, "IIC", 8); /* copy interface name */
369 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
370 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
371 info->max_current_ma = MAX_CURRENT; /* set maximum current */
372 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
373 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
374 info->driver_version = DRIVER_VERSION; /* set driver version */
375
376 return 0; /* success return 0 */
377}
#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]