changing circuitry to disable RTC, update initialization to match
[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 #ifdef FC1025
32     deviceAddress |= 0x08;
33   else
34     deviceAddress &= ~0x08;
35 #else
36     deviceAddress |= 0x02;
37   else
38     deviceAddress &= ~0x02;
39 #endif
40
41   return r;
42 }
43
44 static int eepromSetAddressEx (U32 address, U8 *buffer)
45 {
46   int r;
47
48   if ((r = eepromSetAddress (address)))
49     return r;
50
51   buffer [0] = rwAddress >> 8;
52   buffer [1] = rwAddress;
53
54   return 0;
55 }
56
57 U32 eepromGetAddress (void)
58 {
59   return rwAddress;
60 }
61
62 //
63 //
64 //
65 int eepromRead (U8 *buffer, U32 bufferLength)
66 {
67   int r;
68
69   buffer [0] = rwAddress >> 8;
70   buffer [1] = rwAddress;
71
72   if (!(r = i2cWriteReadBufferPoll (deviceAddress, buffer, sizeof (U16), bufferLength)))
73     eepromSetAddress (eepromGetAddress () + bufferLength);
74
75   return r;
76 }
77
78 int eepromReadAddress (U32 address, U8 *buffer, U32 bufferLength)
79 {
80   int r;
81
82   if ((r = eepromSetAddress (address)))
83     return r;
84
85   return eepromRead (buffer, bufferLength);
86 }
87
88 //
89 //
90 //
91 int eepromWrite (U8 *buffer, U32 bufferLength)
92 {
93   int r;
94
95   buffer [0] = rwAddress >> 8;
96   buffer [1] = rwAddress;
97
98   if (!(r = i2cWriteBufferPoll (deviceAddress, buffer, bufferLength + 2)))
99     eepromSetAddress (eepromGetAddress () + bufferLength);
100
101   return r;
102 }
103
104 int eepromWriteAddress (U32 address, U8 *buffer, U32 bufferLength)
105 {
106   int r;
107
108   if ((r = eepromSetAddress (address)))
109     return r;
110
111   return eepromWrite (buffer, bufferLength);
112 }
113
114 //
115 //
116 //
117 static int eepromFillAddressCommon (U32 address, U8 *buffer, U32 bufferLength)
118 {
119   int r;
120
121   if (!(r = eepromSetAddressEx (address, buffer)))
122     r = i2cWriteBufferPoll (deviceAddress, buffer, bufferLength + 2);
123
124   return r;
125 }
126
127 int eepromFillAddress (U32 address, U32 bufferLength, U8 fillValue)
128 {
129   int r;
130   U32 l;
131   U32 i;
132   U8 buffer [EEPROM_PAGESIZE + 2];
133
134   memset (buffer, fillValue, sizeof (buffer));
135
136   l = (EEPROM_PAGESIZE - ((address + EEPROM_PAGESIZE) % EEPROM_PAGESIZE)) % EEPROM_PAGESIZE;
137   l = MIN (l, bufferLength);
138
139   if (l && (r = eepromFillAddressCommon (address, buffer, l)))
140     return r;
141
142   address += l;
143   bufferLength -= l;
144   l = bufferLength - (bufferLength % EEPROM_PAGESIZE);
145
146   for (i = 0; i < l; i += EEPROM_PAGESIZE, address += (sizeof (buffer) - 2), bufferLength -= (sizeof (buffer) - 2))
147     if ((r = eepromFillAddressCommon (address, buffer, sizeof (buffer) - 2)))
148       return r;
149
150   if (bufferLength && (r = eepromFillAddressCommon (address, buffer, bufferLength)))
151     return r;
152
153   return 0;
154 }
155