LibDriver GT24CXX
Loading...
Searching...
No Matches
driver_gt24cxx.c
Go to the documentation of this file.
1
36
37#include "driver_gt24cxx.h"
38
42#define CHIP_NAME "Giantec Semiconductor GT24CXX"
43#define MANUFACTURER_NAME "Giantec Semiconductor"
44#define SUPPLY_VOLTAGE_MIN 1.7f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 3.0f
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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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("gt24cxx: 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 = (uint32_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 = (gt24cxx_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 = (gt24cxx_address_t)((handle->iic_addr & (~0xA0)) >> 1); /* get iic address */
238
239 return 0; /* success return 0 */
240}
241
256uint8_t gt24cxx_read(gt24cxx_handle_t *handle, uint32_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 ((address + len) > handle->id) /* check length */
267 {
268 handle->debug_print("gt24cxx: read out of range.\n"); /* read out of range */
269
270 return 4; /* return error */
271 }
272
273 if (handle->id > (uint32_t)GT24C16) /* choose id to set different address */
274 {
275 if (handle->iic_read_address16((uint8_t)(handle->iic_addr + ((address / 65536) << 1)),
276 address % 65536, buf,
277 len) != 0) /* read data */
278 {
279 handle->debug_print("gt24cxx: read failed.\n"); /* read failed */
280
281 return 1; /* return error */
282 }
283 }
284 else
285 {
286 if (handle->iic_read((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256,
287 buf, len) != 0) /* read data */
288 {
289 handle->debug_print("gt24cxx: read failed.\n"); /* read failed */
290
291 return 1; /* return error */
292 }
293 }
294
295 return 0; /* success return 0 */
296}
297
312uint8_t gt24cxx_write(gt24cxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
313{
314 uint16_t page_remain;
315 uint16_t page_div;
316 uint16_t page_delay_ms;
317
318 if (handle == NULL) /* check handle */
319 {
320 return 2; /* return error */
321 }
322 if (handle->inited != 1) /* check handle initialization */
323 {
324 return 3; /* return error */
325 }
326 if ((address + len) > handle->id) /* check length */
327 {
328 handle->debug_print("gt24cxx: write out of range.\n"); /* write out of range */
329
330 return 4; /* return error */
331 }
332
333 switch ((uint32_t)handle->id) /* choose id */
334 {
335 case GT24C02 : /* 02 */
336 {
337 page_div = 16; /* 16 bytes */
338 page_delay_ms = 6; /* 6 ms */
339
340 break; /* break */
341 }
342 case GT24C04 : /* 04 */
343 {
344 page_div = 16; /* 16 bytes */
345 page_delay_ms = 6; /* 6 ms */
346
347 break; /* break */
348 }
349 case GT24C08 : /* 08 */
350 {
351 page_div = 16; /* 16 bytes */
352 page_delay_ms = 6; /* 6 ms */
353
354 break; /* break */
355 }
356 case GT24C16 : /* 16 */
357 {
358 page_div = 16; /* 16 bytes */
359 page_delay_ms = 6; /* 6 ms */
360
361 break; /* break */
362 }
363 case GT24C32 : /* 32 */
364 {
365 page_div = 32; /* 32 bytes */
366 page_delay_ms = 6; /* 6 ms */
367
368 break; /* break */
369 }
370 case GT24C64 : /* 64 */
371 {
372 page_div = 32; /* 32 bytes */
373 page_delay_ms = 6; /* 6 ms */
374
375 break; /* break */
376 }
377 case GT24C128 : /* 128 */
378 {
379 page_div = 64; /* 64 bytes */
380 page_delay_ms = 6; /* 6 ms */
381
382 break; /* break */
383 }
384 case GT24C256 : /* 256 */
385 {
386 page_div = 64; /* 64 bytes */
387 page_delay_ms = 6; /* 6 ms */
388
389 break; /* break */
390 }
391 case GT24C512 : /* 512 */
392 {
393 page_div = 128; /* 128 bytes */
394 page_delay_ms = 6; /* 6 ms */
395
396 break; /* break */
397 }
398 case GT24C1024 : /* 1024 */
399 {
400 page_div = 256; /* 256 bytes */
401 page_delay_ms = 6; /* 6 ms */
402
403 break; /* break */
404 }
405 case GT24C2048 : /* 2048 */
406 {
407 page_div = 256; /* 256 bytes */
408 page_delay_ms = 6; /* 6 ms */
409
410 break; /* break */
411 }
412 default : /* default */
413 {
414 page_div = 16; /* 16 bytes */
415 page_delay_ms = 6; /* 6 ms */
416
417 break; /* break */
418 }
419 }
420
421 page_remain = (uint16_t)(page_div - address % page_div); /* set page remain */
422 if (len <= page_remain) /* check length */
423 {
424 page_remain = (uint16_t)len; /* set page remain */
425 }
426 if (handle->id > (uint32_t)GT24C16) /* check id */
427 {
428 while (1)
429 {
430 if (handle->iic_write_address16((uint8_t)(handle->iic_addr + ((address / 65536) << 1)),
431 address % 65536, buf,
432 page_remain) != 0) /* write page */
433 {
434 handle->debug_print("gt24cxx: write failed.\n"); /* write failed */
435
436 return 1; /* return error */
437 }
438 handle->delay_ms(page_delay_ms); /* wait ok */
439 if (page_remain == len) /* check break */
440 {
441 break; /* break */
442 }
443 else
444 {
445 address += page_remain; /* address increase */
446 buf += page_remain; /* buffer point increase */
447 len -= page_remain; /* length decrease */
448 if (len < page_div) /* check length */
449 {
450 page_remain = (uint16_t)len; /* set the rest length */
451 }
452 else
453 {
454 page_remain = page_div; /* set page */
455 }
456 }
457 }
458 }
459 else
460 {
461 while (1)
462 {
463 if (handle->iic_write((uint8_t)(handle->iic_addr + ((address / 256) << 1)), address % 256, buf,
464 page_remain) != 0) /* write page */
465 {
466 handle->debug_print("gt24cxx: write failed.\n"); /* write failed */
467
468 return 1; /* return error */
469 }
470 handle->delay_ms(page_delay_ms); /* wait ok */
471 if (page_remain == len) /* check break */
472 {
473 break; /* break */
474 }
475 else
476 {
477 address += page_remain; /* address increase */
478 buf += page_remain; /* buffer point increase */
479 len -= page_remain; /* length decrease */
480 if (len < page_div) /* check length */
481 {
482 page_remain = (uint16_t)len; /* set the rest length */
483 }
484 else
485 {
486 page_remain = page_div; /* set page */
487 }
488 }
489 }
490 }
491
492 return 0; /* success return 0 */
493}
494
504{
505 if (info == NULL) /* check handle */
506 {
507 return 2; /* return error */
508 }
509
510 memset(info, 0, sizeof(gt24cxx_info_t)); /* initialize gt24cxx info structure */
511 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
512 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
513 strncpy(info->interface, "IIC", 8); /* copy interface name */
514 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
515 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
516 info->max_current_ma = MAX_CURRENT; /* set maximum current */
517 info->temperature_max = TEMPERATURE_MAX; /* set maximum temperature */
518 info->temperature_min = TEMPERATURE_MIN; /* set minimal temperature */
519 info->driver_version = DRIVER_VERSION; /* set driver version */
520
521 return 0; /* success return 0 */
522}
#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 gt24cxx header file
uint8_t gt24cxx_init(gt24cxx_handle_t *handle)
initialize the chip
uint8_t gt24cxx_deinit(gt24cxx_handle_t *handle)
close the chip
uint8_t gt24cxx_info(gt24cxx_info_t *info)
get chip's information
uint8_t gt24cxx_set_addr_pin(gt24cxx_handle_t *handle, gt24cxx_address_t addr_pin)
set the chip address pin
uint8_t gt24cxx_set_type(gt24cxx_handle_t *handle, gt24cxx_t type)
set the chip type
uint8_t gt24cxx_write(gt24cxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
write bytes to the chip
uint8_t gt24cxx_read(gt24cxx_handle_t *handle, uint32_t address, uint8_t *buf, uint16_t len)
read bytes from the chip
uint8_t gt24cxx_get_type(gt24cxx_handle_t *handle, gt24cxx_t *type)
get the chip type
struct gt24cxx_handle_s gt24cxx_handle_t
gt24cxx handle structure definition
gt24cxx_t
gt24cxx type enumeration definition
uint8_t gt24cxx_get_addr_pin(gt24cxx_handle_t *handle, gt24cxx_address_t *addr_pin)
get the chip address pin
struct gt24cxx_info_s gt24cxx_info_t
gt24cxx information structure definition
gt24cxx_address_t
gt24cxx address enumeration definition
@ GT24C08
@ GT24C1024
@ GT24C04
@ GT24C16
@ GT24C02
@ GT24C256
@ GT24C32
@ GT24C512
@ GT24C2048
@ GT24C64
@ GT24C128
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)
uint32_t driver_version
char manufacturer_name[32]