Update to ThingSet library v0.3

This commit is contained in:
Martin Jäger
2020-05-06 12:53:26 +02:00
parent 7da96abbe4
commit efc0bc8fdc
14 changed files with 622 additions and 689 deletions
+7 -23
View File
@@ -17,11 +17,9 @@
#include "mcu.h"
#include "board.h"
#include "thingset.h"
#include "data_nodes.h"
#include "helper.h"
// versioning of EEPROM layout (2 bytes)
// change the version number each time the data object array below is changed!
#define EEPROM_VERSION 3
#define EEPROM_HEADER_SIZE 8 // bytes
@@ -29,19 +27,6 @@
extern ThingSet ts;
// stores object-ids of values to be stored in EEPROM
const uint16_t eeprom_data_objects[] = {
0x01, // timestamp
0x18, // DeviceID
0x08, 0x09, 0x0A, 0x0B, // input / output wh
0x0C, 0x0D, 0x0E, // num full charge / deep-discharge / usable Ah
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3F, // battery settings
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, // resistances and min/max temperatures
0x40, 0x41, 0x42, 0x43, 0x46, 0x47, // load settings
0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, // V, I, T max
0xA6 // day count
};
#ifndef UNIT_TEST
uint32_t _calc_crc(const uint8_t *buf, size_t len)
@@ -105,7 +90,7 @@ void eeprom_restore_data()
// buf_header[0], buf_header[1], buf_header[2], buf_header[3],
// buf_header[4], buf_header[5], buf_header[6], buf_header[7]);
if (version == EEPROM_VERSION && len <= sizeof(buf_req)) {
if (version == DATA_NODES_VERSION && len <= sizeof(buf_req)) {
err = eeprom_read(eeprom_dev, EEPROM_HEADER_SIZE, buf_req, len);
@@ -113,8 +98,8 @@ void eeprom_restore_data()
//for (int i = 0; i < len; i++) printf("%.2x ", buf_req[i]);
if (_calc_crc(buf_req, len) == crc) {
int status = ts.init_cbor(buf_req, sizeof(buf_req)); // first byte is ignored
printf("EEPROM: Data objects read and updated, ThingSet result: %d\n", status);
int status = ts.bin_sub(buf_req, sizeof(buf_req), TS_WRITE_MASK, PUB_NVM);
printf("EEPROM: Data objects read and updated, ThingSet result: %x\n", status);
}
else {
printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n",
@@ -132,12 +117,11 @@ void eeprom_store_data()
struct device *eeprom_dev = device_get_binding("EEPROM_0");
int len = ts.pub_msg_cbor(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE,
eeprom_data_objects, sizeof(eeprom_data_objects)/sizeof(uint16_t));
int len = ts.bin_pub(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, PUB_NVM);
uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len);
// store EEPROM_VERSION, number of bytes and CRC
*((uint16_t*)&buf[0]) = (uint16_t)EEPROM_VERSION;
*((uint16_t*)&buf[0]) = (uint16_t)DATA_NODES_VERSION;
*((uint16_t*)&buf[2]) = (uint16_t)(len); // length of data
*((uint32_t*)&buf[4]) = crc;
@@ -148,7 +132,7 @@ void eeprom_store_data()
// buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
if (len == 0) {
printf("EEPROM: Data could not be stored, ThingSet error: %d\n", len);
printf("EEPROM: Data could not be stored. ThingSet error (len = %d)\n", len);
}
else {
int err = eeprom_write(eeprom_dev, 0, buf, len + EEPROM_HEADER_SIZE);