2006-08-05 16:02:34 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Miika Pekkarinen
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-02-28 13:20:36 +00:00
|
|
|
|
2006-08-05 16:02:34 +00:00
|
|
|
#include "logf.h"
|
|
|
|
#include "string.h"
|
2006-11-26 14:40:47 +00:00
|
|
|
#include "inttypes.h"
|
2006-08-05 16:02:34 +00:00
|
|
|
|
2007-02-28 13:20:36 +00:00
|
|
|
#include "sw_i2c.h"
|
2006-08-05 16:02:34 +00:00
|
|
|
|
2007-02-28 13:20:36 +00:00
|
|
|
#include "eeprom_24cxx.h"
|
2006-08-05 16:02:34 +00:00
|
|
|
|
2006-11-26 14:50:15 +00:00
|
|
|
/* Use cache to speedup writing to the chip. */
|
|
|
|
static char data_cache[EEPROM_SIZE];
|
|
|
|
static uint8_t cached_bitfield[EEPROM_SIZE/8];
|
|
|
|
|
|
|
|
#define IS_CACHED(addr) (cached_bitfield[addr/8] & (1 << (addr % 8)))
|
2007-02-28 13:20:36 +00:00
|
|
|
#define SET_CACHED(addr) (cached_bitfield[addr/8] |= 1 << (addr % 8))
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
void eeprom_24cxx_init(void)
|
|
|
|
{
|
|
|
|
sw_i2c_init();
|
2006-11-26 14:40:47 +00:00
|
|
|
memset(cached_bitfield, 0, sizeof cached_bitfield);
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_read_byte(unsigned int address, char *c)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char byte;
|
2006-08-11 19:02:23 +00:00
|
|
|
int count = 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (address >= EEPROM_SIZE)
|
|
|
|
{
|
|
|
|
logf("EEPROM address: %d", address);
|
2006-08-07 22:30:12 +00:00
|
|
|
return -9;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-11-26 14:40:47 +00:00
|
|
|
/* Check from cache. */
|
|
|
|
if (IS_CACHED(address))
|
|
|
|
{
|
|
|
|
logf("EEPROM RCached: %d", address);
|
|
|
|
*c = data_cache[address];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-05 16:02:34 +00:00
|
|
|
*c = 0;
|
2006-08-07 22:30:12 +00:00
|
|
|
do
|
|
|
|
{
|
2007-02-28 13:20:36 +00:00
|
|
|
ret = sw_i2c_read(EEPROM_ADDR, address, &byte, 1);
|
2006-08-11 19:02:23 +00:00
|
|
|
} while (ret < 0 && count++ < 200);
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2006-08-11 19:02:23 +00:00
|
|
|
logf("EEPROM RFail: %d/%d/%d", ret, address, count);
|
2006-08-07 22:30:12 +00:00
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 19:02:23 +00:00
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
/* keep between {} as logf is whitespace in normal builds */
|
|
|
|
logf("EEPROM rOK: %d retries", count);
|
|
|
|
}
|
2006-11-26 14:40:47 +00:00
|
|
|
|
|
|
|
/* Cache the byte. */
|
|
|
|
data_cache[address] = byte;
|
|
|
|
SET_CACHED(address);
|
|
|
|
|
2006-08-05 16:02:34 +00:00
|
|
|
*c = byte;
|
2006-08-07 22:30:12 +00:00
|
|
|
return 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_write_byte(unsigned int address, char c)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2006-08-11 19:02:23 +00:00
|
|
|
int count = 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (address >= EEPROM_SIZE)
|
|
|
|
{
|
|
|
|
logf("EEPROM address: %d", address);
|
2006-08-07 22:30:12 +00:00
|
|
|
return -9;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-11-26 14:40:47 +00:00
|
|
|
/* Check from cache. */
|
|
|
|
if (IS_CACHED(address) && data_cache[address] == c)
|
|
|
|
{
|
|
|
|
logf("EEPROM WCached: %d", address);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
do
|
|
|
|
{
|
2007-02-28 13:20:36 +00:00
|
|
|
ret = sw_i2c_write(EEPROM_ADDR, address, &c, 1);
|
2006-08-11 19:02:23 +00:00
|
|
|
} while (ret < 0 && count++ < 200) ;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
logf("EEPROM WFail: %d/%d", ret, address);
|
2006-08-07 22:30:12 +00:00
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 19:02:23 +00:00
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
/* keep between {} as logf is whitespace in normal builds */
|
|
|
|
logf("EEPROM wOK: %d retries", count);
|
|
|
|
}
|
|
|
|
|
2006-11-26 14:40:47 +00:00
|
|
|
SET_CACHED(address);
|
|
|
|
data_cache[address] = c;
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
return 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_read(unsigned char address, void *dest, int length)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
char *buf = (char *)dest;
|
2006-08-07 22:30:12 +00:00
|
|
|
int ret = 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
2006-08-07 22:30:12 +00:00
|
|
|
ret = eeprom_24cxx_read_byte(address+i, &buf[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_write(unsigned char address, const void *src, int length)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
const char *buf = (const char *)src;
|
2006-08-07 22:30:12 +00:00
|
|
|
int i;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
2006-11-26 14:40:47 +00:00
|
|
|
for (i = 0; i < length; i++)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
2006-11-26 14:40:47 +00:00
|
|
|
if (eeprom_24cxx_write_byte(address+i, buf[i]) < 0)
|
|
|
|
return -1;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-11-26 14:40:47 +00:00
|
|
|
return 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|