Tool/software: TI-RTOS
I have a node set up that requires both 'pairing' to a network and calibration, and as a result has a couple of structs to hold this information and store it to NV memory in case the device is restarted. The NVS is organised through use of an additional typedef containing the two structs.
If I only calibrate the device it will recall this data correctly for the foreseeable future. However, if I pair the device and then perform a reset it will remember the pairing but clear any record it has of the calibration data. Following another restart it then not remember the pairing information any more.
I haven't got a clue what could be going on here or what I'm doing wrong. Here are some snippets of code that may help:
Setting up the structs to manage the data and NVS
typedef struct deviceParams{ uint8_t pairState; uint32_t receiverAddress; uint32_t doorId; // What else? } deviceParams; /* This struct is used to organised items in memory */ typedef struct nvs_data{ deviceParams nvsDeviceParams; calParams nvsCalData; } nvs_data; extern calParams calData; deviceParams deviceData;
calData is used from another file:
typedef struct calParams{ uint16_t margin; int16_t closedBx; int16_t closedBy; uint8_t saveState; } calParams; calParams calData;
The code I use to recall the data on startup:
int16_t status; /***** Check the pairing status and load in any relevant data *****/ status = NVS_read(nvsHandle, offsetof(nvs_data, nvsDeviceParams), &deviceData, sizeof(deviceData)); if(status != NVS_STATUS_SUCCESS){ System_abort("NVS read error"); } if(deviceData.pairState == 1){ // Node has been previously paired // Retrieve the receiver's address and store nodeRadioTask_setReceiverAddr(deviceData.receiverAddress); startupIndicatorLeds |= (1<<Board_PIN_LEDPAIR); }else{ deviceData.pairState = 0; startupIndicatorLeds |= (1<<Board_PIN_LEDMNTN); } /***** Check if there is existing calData *****/ // LED red = no cal on record, green = good status = NVS_read(nvsHandle, offsetof(nvs_data, nvsCalData), &calData, sizeof(calData)); if(status != NVS_STATUS_SUCCESS){ System_abort("NVS read error"); } if(calData.saveState != 0xCC){ // Set default calibration values if it's the very first run sensorCtrl_action(SENSOR_ACTION_CAL_INIT); // This sets the a couple of initial values if they've never been set before status = NVS_write(nvsHandle, offsetof(nvs_data, nvsCalData), &calData, sizeof(calData), NVS_WRITE_ERASE); if(status != NVS_STATUS_SUCCESS){ System_abort("NVS write error"); } } // Indicate if Cal has been done if(calData.closedBx == calData.closedBy){ startupIndicatorLeds |= (1<<Board_PIN_LEDPWRR); }else{ // Data should already be loaded into the struct from NVmem startupIndicatorLeds |= (1<<Board_PIN_LEDPWRG); }
The device 'pairs' when it received an ACK with the receiver's address:
if (events & NODE_EVENT_NEW_ACK_PACKET) { // Respond to the AckPacket - adjust timing, etc. // Was this ACK a result of an NJR packet? if(networkJoinRequestActive){ // Save our pairing status locally deviceData.pairState = 1; // Also store the Receiver's address in NV mem (update device data struct) deviceData.receiverAddress = latestAckPacket.address; NVS_write(nvsHandle, offsetof(nvs_data, nvsDeviceParams), &deviceData, sizeof(deviceData), NVS_WRITE_ERASE); if(maintenanceModeActive) UART_write(uart, "\n\rNode paired\n\r", 15); // Turn off pair LED //ledCtrl_action(Board_PIN_LEDPAIR, LED_ACTION_3SEC_TOGGLE); networkJoinRequestActive = 0; } }
When calibration is complete is stores the calibration data:
/* Conclude the calibration process */ if (events & NODE_EVENT_CALIBRATE_OFF) { ledCtrl_action(Board_PIN_LEDMNTN, LED_ACTION_OFF); // store data to NVmem NVS_write(nvsHandle, offsetof(nvs_data, nvsCalData), &calData, sizeof(calData), NVS_WRITE_ERASE); calModeActive = 0; }
Can anyone shed some light on what I might be doing wrong here?