diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c index b24737dd..a74b9df8 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c @@ -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; diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/seeprom.c b/bacnet-stack/ports/bdk-atxx4-mstp/seeprom.c index 1b1ffaab..d5cab89a 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/seeprom.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/seeprom.c @@ -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: