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