Added checks to detect and work with faulty SEEPROM.
This commit is contained in:
@@ -61,17 +61,24 @@ static bool seeprom_version_test(
|
|||||||
uint16_t version = 0;
|
uint16_t version = 0;
|
||||||
uint16_t id = 0;
|
uint16_t id = 0;
|
||||||
bool status = false;
|
bool status = false;
|
||||||
|
int rv;
|
||||||
|
|
||||||
seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
|
rv = seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
|
||||||
seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 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;
|
status = true;
|
||||||
} else {
|
} else if (rv > 0) {
|
||||||
version = SEEPROM_VERSION;
|
version = SEEPROM_VERSION;
|
||||||
id = SEEPROM_ID;
|
id = SEEPROM_ID;
|
||||||
seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
|
seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2);
|
||||||
seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2);
|
seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2);
|
||||||
|
} else {
|
||||||
|
while (1) {
|
||||||
|
/* SEEPROM is faulty! */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ int seeprom_bytes_read(
|
|||||||
uint8_t sla, twcr, n = 0;
|
uint8_t sla, twcr, n = 0;
|
||||||
int rv = 0;
|
int rv = 0;
|
||||||
uint8_t twst; /* status - only valid while TWINT is set. */
|
uint8_t twst; /* status - only valid while TWINT is set. */
|
||||||
|
uint16_t timeout = 0xFFFF;
|
||||||
|
|
||||||
#if SEEPROM_WORD_ADDRESS_16BIT
|
#if SEEPROM_WORD_ADDRESS_16BIT
|
||||||
/* 16bit address devices need only TWI Device Address */
|
/* 16bit address devices need only TWI Device Address */
|
||||||
@@ -125,7 +126,12 @@ int seeprom_bytes_read(
|
|||||||
/* send start condition */
|
/* send start condition */
|
||||||
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
|
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
|
||||||
/* wait for transmission */
|
/* wait for transmission */
|
||||||
while ((TWCR & _BV(TWINT)) == 0);
|
while ((TWCR & _BV(TWINT)) == 0) {
|
||||||
|
timeout--;
|
||||||
|
if (timeout == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
twst = TWSR & TW_STATUS_MASK;
|
twst = TWSR & TW_STATUS_MASK;
|
||||||
switch (twst) {
|
switch (twst) {
|
||||||
case TW_REP_START:
|
case TW_REP_START:
|
||||||
@@ -309,6 +315,7 @@ static int seeprom_bytes_write_page(
|
|||||||
uint16_t endaddr;
|
uint16_t endaddr;
|
||||||
uint8_t twst; /* status - only valid while TWINT is set. */
|
uint8_t twst; /* status - only valid while TWINT is set. */
|
||||||
uint16_t page_end_addr;
|
uint16_t page_end_addr;
|
||||||
|
uint16_t timeout = 0xFFFF;
|
||||||
|
|
||||||
/* limit the length to end of the EEPROM page */
|
/* limit the length to end of the EEPROM page */
|
||||||
page_end_addr = eeaddr | (SEEPROM_PAGE_SIZE - 1);
|
page_end_addr = eeaddr | (SEEPROM_PAGE_SIZE - 1);
|
||||||
@@ -342,7 +349,12 @@ static int seeprom_bytes_write_page(
|
|||||||
/* send start condition */
|
/* send start condition */
|
||||||
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
|
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
|
||||||
/* wait for transmission */
|
/* wait for transmission */
|
||||||
while ((TWCR & _BV(TWINT)) == 0);
|
while ((TWCR & _BV(TWINT)) == 0) {
|
||||||
|
timeout--;
|
||||||
|
if (timeout == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
twst = TWSR & TW_STATUS_MASK;
|
twst = TWSR & TW_STATUS_MASK;
|
||||||
switch (twst) {
|
switch (twst) {
|
||||||
case TW_REP_START:
|
case TW_REP_START:
|
||||||
|
|||||||
Reference in New Issue
Block a user