EEPROM buffer mutex
Added a mutex locking system so that the EEPROM buffer cannot be used by multiple functions at the same time.
This commit is contained in:
committed by
Martin Jäger
parent
a5c1d46d63
commit
a9b0a278ad
+10
-3
@@ -20,12 +20,15 @@
|
||||
#include "helper.h"
|
||||
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
K_MUTEX_DEFINE(_lock);
|
||||
static uint8_t buf[512]; // Buffer used by store and restore functions
|
||||
#endif
|
||||
|
||||
#define EEPROM_HEADER_SIZE 8 // bytes
|
||||
|
||||
#define EEPROM_UPDATE_INTERVAL (6*60*60) // update every 6 hours
|
||||
|
||||
static uint8_t buf[512]; // Buffer used by store and restore functions
|
||||
|
||||
extern ThingSet ts;
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
@@ -91,7 +94,7 @@ void eeprom_restore_data()
|
||||
// buf_header[4], buf_header[5], buf_header[6], buf_header[7]);
|
||||
|
||||
if (version == DATA_NODES_VERSION && len <= sizeof(buf)) {
|
||||
|
||||
k_mutex_lock(&_lock, K_FOREVER);
|
||||
err = eeprom_read(eeprom_dev, EEPROM_HEADER_SIZE, buf, len);
|
||||
|
||||
//printf("Data (len=%d): ", len);
|
||||
@@ -105,6 +108,7 @@ void eeprom_restore_data()
|
||||
printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n",
|
||||
(unsigned int)crc, len);
|
||||
}
|
||||
k_mutex_unlock(&_lock);
|
||||
}
|
||||
else {
|
||||
printf("EEPROM: Empty or data layout version changed\n");
|
||||
@@ -117,6 +121,8 @@ void eeprom_store_data()
|
||||
|
||||
const struct device *eeprom_dev = device_get_binding("EEPROM_0");
|
||||
|
||||
k_mutex_lock(&_lock, K_FOREVER);
|
||||
|
||||
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);
|
||||
|
||||
@@ -143,6 +149,7 @@ void eeprom_store_data()
|
||||
printf("EEPROM: Write error.\n");
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&_lock);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user