12 static U8 deviceAddress = EEPROM_ADDRESS;
17 void eepromInit (void)
21 int eepromSetAddress (U32 address)
25 if (address >= EEPROM_SIZE)
28 address %= EEPROM_SIZE;
30 if ((rwAddress = address) >= 65536)
32 deviceAddress |= 0x08;
34 deviceAddress &= ~0x08;
36 deviceAddress |= 0x02;
38 deviceAddress &= ~0x02;
44 static int eepromSetAddressEx (U32 address, U8 *buffer)
48 if ((r = eepromSetAddress (address)))
51 buffer [0] = rwAddress >> 8;
52 buffer [1] = rwAddress;
57 U32 eepromGetAddress (void)
65 int eepromRead (U8 *buffer, U32 bufferLength)
69 buffer [0] = rwAddress >> 8;
70 buffer [1] = rwAddress;
72 if (!(r = i2cWriteReadBufferPoll (deviceAddress, buffer, sizeof (U16), bufferLength)))
73 eepromSetAddress (eepromGetAddress () + bufferLength);
78 int eepromReadAddress (U32 address, U8 *buffer, U32 bufferLength)
82 if ((r = eepromSetAddress (address)))
85 return eepromRead (buffer, bufferLength);
91 int eepromWrite (U8 *buffer, U32 bufferLength)
95 buffer [0] = rwAddress >> 8;
96 buffer [1] = rwAddress;
98 if (!(r = i2cWriteBufferPoll (deviceAddress, buffer, bufferLength + 2)))
99 eepromSetAddress (eepromGetAddress () + bufferLength);
104 int eepromWriteAddress (U32 address, U8 *buffer, U32 bufferLength)
108 if ((r = eepromSetAddress (address)))
111 return eepromWrite (buffer, bufferLength);
117 static int eepromFillAddressCommon (U32 address, U8 *buffer, U32 bufferLength)
121 if (!(r = eepromSetAddressEx (address, buffer)))
122 r = i2cWriteBufferPoll (deviceAddress, buffer, bufferLength + 2);
127 int eepromFillAddress (U32 address, U32 bufferLength, U8 fillValue)
132 U8 buffer [EEPROM_PAGESIZE + 2];
134 memset (buffer, fillValue, sizeof (buffer));
136 l = (EEPROM_PAGESIZE - ((address + EEPROM_PAGESIZE) % EEPROM_PAGESIZE)) % EEPROM_PAGESIZE;
137 l = MIN (l, bufferLength);
139 if (l && (r = eepromFillAddressCommon (address, buffer, l)))
144 l = bufferLength - (bufferLength % EEPROM_PAGESIZE);
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)))
150 if (bufferLength && (r = eepromFillAddressCommon (address, buffer, bufferLength)))