File: i2c_eeprom.c

package info (click to toggle)
imsprog 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,828 kB
  • sloc: cpp: 6,525; ansic: 5,899; xml: 552; sh: 231; makefile: 5
file content (139 lines) | stat: -rw-r--r-- 3,349 bytes parent folder | download
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
/*
 * Copyright (C) 2021 McMCC <mcmcc@mail.ru>
 * i2c_eeprom.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "ch341a_spi.h"
#include "ch341a_i2c.h"
#include "timer.h"

extern unsigned int bsize;
struct EEPROM eeprom_info;
char eepromname[12];
int eepromsize = 0;

/*int i2c_eeprom_read(unsigned char *buf, unsigned long from, unsigned long len)
{
	unsigned char *pbuf, ebuf[MAX_EEPROM_SIZE];

	if (len == 0)
		return -1;

	timer_start();
	memset(ebuf, 0, sizeof(ebuf));
	pbuf = ebuf;

	if (ch341readEEPROM(pbuf, eepromsize, &eeprom_info) < 0) {
		printf("Couldnt read [%d] bytes from [%s] EEPROM address 0x%08lu\n", (int)len, eepromname, from);
		return -1;
	}

	memcpy(buf, pbuf + from, len);

	printf("Read [%d] bytes from [%s] EEPROM address 0x%08lu\n", (int)len, eepromname, from);
	timer_end();

	return (int)len;
}
*/
/*int i2c_eeprom_erase(unsigned long offs, unsigned long len)
{
	unsigned char *pbuf, ebuf[MAX_EEPROM_SIZE];

	if (len == 0)
		return -1;

	timer_start();
	memset(ebuf, 0xff, sizeof(ebuf));
	pbuf = ebuf;

	if (offs || len < eepromsize) {
		if (ch341readEEPROM(pbuf, eepromsize, &eeprom_info) < 0) {
			printf("Couldnt read [%d] bytes from [%s] EEPROM\n", eepromsize, eepromname);
			return -1;
		}
		memset(pbuf + offs, 0xff, len);
	}

	if(ch341writeEEPROM(pbuf, eepromsize, &eeprom_info) < 0) {
		printf("Failed to erase [%d] bytes of [%s] EEPROM address 0x%08lu\n", (int)len, eepromname, offs);
		return -1;
	}

	printf("Erased [%d] bytes of [%s] EEPROM address 0x%08lu\n", (int)len, eepromname, offs);
	timer_end();

	return 0;
}
*/
/*int i2c_eeprom_write(unsigned char *buf, unsigned long to, unsigned long len)
{
	unsigned char *pbuf, ebuf[MAX_EEPROM_SIZE];

	if (len == 0)
		return -1;

	timer_start();
	memset(ebuf, 0xff, sizeof(ebuf));
	pbuf = ebuf;

	if (to || len < eepromsize) {
		if (ch341readEEPROM(pbuf, eepromsize, &eeprom_info) < 0) {
			printf("Couldnt read [%d] bytes from [%s] EEPROM\n", (int)len, eepromname);
			return -1;
		}
	}
	memcpy(pbuf + to, buf, len);

	if(ch341writeEEPROM(pbuf, eepromsize, &eeprom_info) < 0) {
		printf("Failed to write [%d] bytes of [%s] EEPROM address 0x%08lu\n", (int)len, eepromname, to);
		return -1;
	}

	printf("Wrote [%d] bytes to [%s] EEPROM address 0x%08lu\n", (int)len, eepromname, to);
	timer_end();

	return (int)len;
}

long i2c_init(void)
{
	if (config_stream(CH341_I2C_STANDARD_SPEED) < 0)
		return -1;

	if (eepromsize <= 0) {
		printf("I2C EEPROM Not Detected!\n");
		return -1;
	}

	bsize = 1;

	printf("I2C EEPROM chip: %s, Size: %d bytes\n", eepromname, eepromsize);

	return (long)eepromsize;
}

void support_i2c_eeprom_list(void)
{
	int i;

	printf("I2C EEPROM Support List:\n");
	for ( i = 0; i < (sizeof(eepromlist)/sizeof(struct EEPROM)); i++)
	{
		if (!eepromlist[i].size)
			break;
		printf("%03d. %s\n", i + 1, eepromlist[i].name);
	}
}
*/
/* End of [i2c_eeprom.c] package */