|
|
#include "zhdb.hpp"
#include <stdint.h>
#include "apphardware\apphardware.hpp"
#include "ucomponents\eeprom\m24m02_i2c_eeprom.hpp"
uint8_t cache[50 * 1024] __attribute__((section(".ccmram"))); // uint8_t cache[40 * 1024] ;
using namespace iflytop;
static zhdb_table_t tablecache[MAX_TABLE_NUM]; static uint32_t table_num = 0; static uint32_t table_add_off = 0; static zmutex lock("ZHDB");
static void read_from_eeprom(uint32_t add, uint8_t* data, size_t size) { //
for (size_t i = 0; i < size; i += 256) { AppHardware::ins()->eeprom.read(add + i, data + i, 256);
// static uint8_t buf[256];
// AppHardware::ins()->eeprom.read(add + i, buf, 256);
// memcpy(data + i, buf, 256);
} }
static void write_to_eeprom(uint32_t add, uint8_t* data, size_t size) { //
for (size_t i = 0; i < size; i += 256) { AppHardware::ins()->eeprom.write(add + i, data + i, 256); // static uint8_t buf[256];
// memcpy(buf, data + i, 256);
// AppHardware::ins()->eeprom.write(add + i, buf, 256);
} }
void ZHDB::init() { lock.init(); }
zhdb_table_t* ZHDB::allocTable(const char* name, size_t size) { if (table_num >= MAX_TABLE_NUM) { return NULL; }
if (size % 256 != 0) { size = (size / 256 + 1) * 256; }
//
zhdb_table_t* table = &tablecache[table_num];
table->add = &cache[table_add_off]; table->size = size; table->name = name;
// read from eeprom
read_from_eeprom(table_add_off, table->add, size);
//
table_add_off += size; table_num++;
ZLOGI("ZHDB", "alloc table %s add %p size %d", name, table->add, size);
return table; } void ZHDB::storageData(uint8_t* data, size_t size) { zlock_guard guard(lock);
//
uint32_t off = data - cache; uint32_t align_off = (off / 256) * 256; uint32_t real_size = (size / 256 + 1) * 256;
write_to_eeprom(align_off, &cache[align_off], real_size); }
|