Imported upstream version 1.20
[fw/openalt] / i2c / eeprom.c
1 #include "FreeRTOS.h"
2
3 #include <string.h>
4
5 #include "i2c.h"
6 #include "eeprom.h"
7
8 //
9 //
10 //
11 static U32 rwAddress;
12 static U8 deviceAddress = EEPROM_ADDRESS;
13
14 //
15 //
16 //
17 void eepromInit (void)
18 {
19 }
20
21 int eepromSetAddress (U32 address)
22 {
23   int r = 0;
24
25   if (address >= EEPROM_SIZE)
26     r = -1;
27
28   address %= EEPROM_SIZE;
29
30   if ((rwAddress = address) >= 65536)
31     deviceAddress |= 0x02;
32   else
33     deviceAddress &= ~0x02;
34
35   return r;
36 }
37
38 static int eepromSetAddressEx (U32 address, U8 *buffer)
39 {
40   int r;
41
42   if ((r = eepromSetAddress (address)))
43     return r;
44
45   buffer [0] = rwAddress >> 8;
46   buffer [1] = rwAddress;
47
48   return 0;
49 }
50
51 U32 eepromGetAddress (void)
52 {
53   return rwAddress;
54 }
55
56 //
57 //
58 //
59 int eepromRead (U8 *buffer, U32 bufferLength)
60 {
61   int r;
62
63   buffer [0] = rwAddress >> 8;
64   buffer [1] = rwAddress;
65
66   if (!(r = i2cWriteReadBufferPoll (deviceAddress, buffer, sizeof (U16), bufferLength)))
67     eepromSetAddress (eepromGetAddress () + bufferLength);
68
69   return r;
70 }
71
72 int eepromReadAddress (U32 address, U8 *buffer, U32 bufferLength)
73 {
74   int r;
75
76   if ((r = eepromSetAddress (address)))
77     return r;
78
79   return eepromRead (buffer, bufferLength);
80 }
81
82 //
83 //
84 //
85 int eepromWrite (U8 *buffer, U32 bufferLength)
86 {
87   int r;
88
89   buffer [0] = rwAddress >> 8;
90   buffer [1] = rwAddress;
91
92   if (!(r = i2cWriteBufferPoll (deviceAddress, buffer, bufferLength + 2)))
93     eepromSetAddress (eepromGetAddress () + bufferLength);
94
95   return r;
96 }
97
98 int eepromWriteAddress (U32 address, U8 *buffer, U32 bufferLength)
99 {
100   int r;
101
102   if ((r = eepromSetAddress (address)))
103     return r;
104
105   return eepromWrite (buffer, bufferLength);
106 }
107
108 //
109 //
110 //
111 static int eepromFillAddressCommon (U32 address, U8 *buffer, U32 bufferLength)
112 {
113   int r;
114
115   if (!(r = eepromSetAddressEx (address, buffer)))
116     r = i2cWriteBufferPoll (deviceAddress, buffer, bufferLength + 2);
117
118   return r;
119 }
120
121 int eepromFillAddress (U32 address, U32 bufferLength, U8 fillValue)
122 {
123   int r;
124   U32 l;
125   U32 i;
126   U8 buffer [EEPROM_PAGESIZE + 2];
127
128   memset (buffer, fillValue, sizeof (buffer));
129
130   l = (EEPROM_PAGESIZE - ((address + EEPROM_PAGESIZE) % EEPROM_PAGESIZE)) % EEPROM_PAGESIZE;
131   l = MIN (l, bufferLength);
132
133   if (l && (r = eepromFillAddressCommon (address, buffer, l)))
134     return r;
135
136   address += l;
137   bufferLength -= l;
138   l = bufferLength - (bufferLength % EEPROM_PAGESIZE);
139
140   for (i = 0; i < l; i += EEPROM_PAGESIZE, address += (sizeof (buffer) - 2), bufferLength -= (sizeof (buffer) - 2))
141     if ((r = eepromFillAddressCommon (address, buffer, sizeof (buffer) - 2)))
142       return r;
143
144   if (bufferLength && (r = eepromFillAddressCommon (address, buffer, bufferLength)))
145     return r;
146
147   return 0;
148 }
149