Added checks to detect and work with faulty SEEPROM.

This commit is contained in:
skarg
2012-12-22 05:41:38 +00:00
parent 4c0599f104
commit d9877756bf
2 changed files with 25 additions and 6 deletions
+11 -4
View File
@@ -61,17 +61,24 @@ static bool seeprom_version_test(
uint16_t version = 0;
uint16_t id = 0;
bool status = false;
int rv;
seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2);
rv = seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
if (rv > 0) {
rv = seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2);
}
if ((id == SEEPROM_ID) && (version == SEEPROM_VERSION)) {
if ((rv > 0) && (id == SEEPROM_ID) && (version == SEEPROM_VERSION)) {
status = true;
} else {
} else if (rv > 0) {
version = SEEPROM_VERSION;
id = SEEPROM_ID;
seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2);
} else {
while (1) {
/* SEEPROM is faulty! */
}
}
return status;
+14 -2
View File
@@ -108,6 +108,7 @@ int seeprom_bytes_read(
uint8_t sla, twcr, n = 0;
int rv = 0;
uint8_t twst; /* status - only valid while TWINT is set. */
uint16_t timeout = 0xFFFF;
#if SEEPROM_WORD_ADDRESS_16BIT
/* 16bit address devices need only TWI Device Address */
@@ -125,7 +126,12 @@ int seeprom_bytes_read(
/* send start condition */
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
/* wait for transmission */
while ((TWCR & _BV(TWINT)) == 0);
while ((TWCR & _BV(TWINT)) == 0) {
timeout--;
if (timeout == 0) {
return -1;
}
}
twst = TWSR & TW_STATUS_MASK;
switch (twst) {
case TW_REP_START:
@@ -309,6 +315,7 @@ static int seeprom_bytes_write_page(
uint16_t endaddr;
uint8_t twst; /* status - only valid while TWINT is set. */
uint16_t page_end_addr;
uint16_t timeout = 0xFFFF;
/* limit the length to end of the EEPROM page */
page_end_addr = eeaddr | (SEEPROM_PAGE_SIZE - 1);
@@ -342,7 +349,12 @@ static int seeprom_bytes_write_page(
/* send start condition */
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
/* wait for transmission */
while ((TWCR & _BV(TWINT)) == 0);
while ((TWCR & _BV(TWINT)) == 0) {
timeout--;
if (timeout == 0) {
return -1;
}
}
twst = TWSR & TW_STATUS_MASK;
switch (twst) {
case TW_REP_START: