Skip to content

Commit

Permalink
feat: added rtc time tracking as nvram entry
Browse files Browse the repository at this point in the history
  • Loading branch information
charliekush committed Jan 13, 2025
1 parent cea3451 commit 9df5318
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 66 deletions.
19 changes: 18 additions & 1 deletion src/cellular/dataUpload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ STATES_e DataUpload::can_upload(void)

STATES_e DataUpload::run(void)
{
uint16_t reupload_delay;

pSystemDesc->pNvram->get(NVRAM::REUPLOAD_WAIT_TIME, reupload_delay);
if (reupload_delay == 0)
{
pSystemDesc->pNvram->put(NVRAM::REUPLOAD_WAIT_TIME, 10);
}
else if (reupload_delay <= 900)
{
reupload_delay *= 2;
}
else
{
reupload_delay = 1800;
}
pSystemDesc->pNvram->put(NVRAM::REUPLOAD_WAIT_TIME, reupload_delay);

uint8_t binary_packet_buffer[SF_PACKET_SIZE];
char ascii_record_buffer[SF_RECORD_SIZE + 1];
char publishName[DU_PUBLISH_ID_NAME_LEN + 1];
Expand Down Expand Up @@ -127,6 +144,7 @@ STATES_e DataUpload::run(void)
FLOG_AddError(FLOG_UPL_PUB_FAIL, 0);
return STATE_DEEP_SLEEP;
case sf::cloud::SUCCESS:
pSystemDesc->pNvram->put(NVRAM::REUPLOAD_WAIT_TIME, 0);
break;
}

Expand All @@ -146,7 +164,6 @@ STATES_e DataUpload::run(void)
}
}
return next_state;

}

void DataUpload::exit(void)
Expand Down
2 changes: 2 additions & 0 deletions src/sys/NVRAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ void NVRAM::displayNVRAM(void)
SF_OSAL_printf("No Upload Flag: 0x%02hx" __NL__, u8);
this->get(CLOUD_CONNECT_COUNTER, u16);
SF_OSAL_printf("Cloud Connect Counter: 0x%04hx" __NL__, u16);
this->get(REUPLOAD_WAIT_TIME, u16);
SF_OSAL_printf("Upload reattempt time: s0x%04hx" __NL__, u16);
}
129 changes: 64 additions & 65 deletions src/sys/NVRAM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,74 @@
class NVRAM
{
public:
typedef enum DATA_ID_
{
NVRAM_VALID,
BOOT_BEHAVIOR,
TMP116_CAL_VALUE,
TMP116_CAL_CURRENT_ATTEMPT,
TMP116_CAL_ATTEMPTS_TOTAL,
TMP116_CAL_DATA_COLLECTION_PERIOD_SEC,
TMP116_CAL_CYCLE_PERIOD_SEC,
UPLOAD_REATTEMPTS,
NO_UPLOAD_FLAG,
CLOUD_CONNECT_COUNTER,
NUM_DATA_IDs
}DATA_ID_e;

typedef struct nvramTableEntry_
{
DATA_ID_e id;

uint32_t addr;
uint32_t len;
}nvramTableEntry_t;

const nvramTableEntry_t LAYOUT[NUM_DATA_IDs] =
{
{NVRAM_VALID, 0x0000, sizeof(uint8_t)},
{BOOT_BEHAVIOR, 0x0001, sizeof(uint8_t)},
{TMP116_CAL_VALUE, 0x0002, sizeof(uint16_t)},
{TMP116_CAL_CURRENT_ATTEMPT, 0x0004, sizeof(uint8_t)},
{TMP116_CAL_ATTEMPTS_TOTAL, 0x0005, sizeof(uint8_t)},
{TMP116_CAL_DATA_COLLECTION_PERIOD_SEC, 0x0008, sizeof(uint32_t)},
{TMP116_CAL_CYCLE_PERIOD_SEC, 0x000C, sizeof(uint32_t)},
{UPLOAD_REATTEMPTS, 0x0014, sizeof(uint8_t)},
{NO_UPLOAD_FLAG, 0x0015, sizeof(uint8_t)},
{CLOUD_CONNECT_COUNTER, 0x0016, sizeof(uint16_t)},

};
static NVRAM& getInstance(void);


/**
* @brief Get data from NVRAM memory
*
* @tparam T Type
* @param dataID ID of data to get
* @param pData Data
* @return int Sucsess
*/
template <typename T> int get(DATA_ID_e dataID, T& pData)
{
uint32_t addr;
const NVRAM::nvramTableEntry_t* tableEntry;

tableEntry = this->getTableEntry(dataID);
if(!tableEntry)
typedef enum DATA_ID_
{
return 0;
}

if(sizeof(pData) != tableEntry->len)
NVRAM_VALID,
BOOT_BEHAVIOR,
TMP116_CAL_VALUE,
TMP116_CAL_CURRENT_ATTEMPT,
TMP116_CAL_ATTEMPTS_TOTAL,
TMP116_CAL_DATA_COLLECTION_PERIOD_SEC,
TMP116_CAL_CYCLE_PERIOD_SEC,
UPLOAD_REATTEMPTS,
NO_UPLOAD_FLAG,
CLOUD_CONNECT_COUNTER,
REUPLOAD_WAIT_TIME,
NUM_DATA_IDs

} DATA_ID_e;

typedef struct nvramTableEntry_
{
return 0;
DATA_ID_e id;

uint32_t addr;
uint32_t len;
} nvramTableEntry_t;

const nvramTableEntry_t LAYOUT[NUM_DATA_IDs] = {
{NVRAM_VALID, 0x0000, sizeof(uint8_t)},
{BOOT_BEHAVIOR, 0x0001, sizeof(uint8_t)},
{TMP116_CAL_VALUE, 0x0002, sizeof(uint16_t)},
{TMP116_CAL_CURRENT_ATTEMPT, 0x0004, sizeof(uint8_t)},
{TMP116_CAL_ATTEMPTS_TOTAL, 0x0005, sizeof(uint8_t)},
{TMP116_CAL_DATA_COLLECTION_PERIOD_SEC, 0x0008, sizeof(uint32_t)},
{TMP116_CAL_CYCLE_PERIOD_SEC, 0x000C, sizeof(uint32_t)},
{UPLOAD_REATTEMPTS, 0x0014, sizeof(uint8_t)},
{NO_UPLOAD_FLAG, 0x0015, sizeof(uint8_t)},
{CLOUD_CONNECT_COUNTER, 0x0016, sizeof(uint16_t)},
{REUPLOAD_WAIT_TIME, 0x0018, sizeof(uint16_t)}};
static NVRAM &getInstance(void);

/**
* @brief Get data from NVRAM memory
*
* @tparam T Type
* @param dataID ID of data to get
* @param pData Data
* @return int Sucsess
*/
template <typename T> int get(DATA_ID_e dataID, T &pData)
{
uint32_t addr;
const NVRAM::nvramTableEntry_t *tableEntry;

tableEntry = this->getTableEntry(dataID);
if (!tableEntry)
{
return 0;
}

if (sizeof(pData) != tableEntry->len)
{
return 0;
}

addr = tableEntry->addr;
EEPROM.get(addr, pData);
return 1;
}

addr = tableEntry->addr;
EEPROM.get(addr, pData);
return 1;
}

/**
* @brief Put data into memory
*
Expand Down

0 comments on commit 9df5318

Please sign in to comment.