You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
8 months ago
9 months ago
9 months ago
9 months ago
  1. #include "zhdb.hpp"
  2. #include <stdint.h>
  3. #include "apphardware\apphardware.hpp"
  4. #include "ucomponents\eeprom\m24m02_i2c_eeprom.hpp"
  5. uint8_t cache[50 * 1024] __attribute__((section(".ccmram")));
  6. // uint8_t cache[40 * 1024] ;
  7. using namespace iflytop;
  8. static zhdb_table_t tablecache[MAX_TABLE_NUM];
  9. static uint32_t table_num = 0;
  10. static uint32_t table_add_off = 0;
  11. static zmutex lock("ZHDB");
  12. static void read_from_eeprom(uint32_t add, uint8_t* data, size_t size) { //
  13. for (size_t i = 0; i < size; i += 256) {
  14. AppHardware::ins()->eeprom.read(add + i, data + i, 256);
  15. // static uint8_t buf[256];
  16. // AppHardware::ins()->eeprom.read(add + i, buf, 256);
  17. // memcpy(data + i, buf, 256);
  18. }
  19. }
  20. static void write_to_eeprom(uint32_t add, uint8_t* data, size_t size) { //
  21. for (size_t i = 0; i < size; i += 256) {
  22. AppHardware::ins()->eeprom.write(add + i, data + i, 256);
  23. // static uint8_t buf[256];
  24. // memcpy(buf, data + i, 256);
  25. // AppHardware::ins()->eeprom.write(add + i, buf, 256);
  26. }
  27. }
  28. void ZHDB::init() { lock.init(); }
  29. zhdb_table_t* ZHDB::allocTable(const char* name, size_t size) {
  30. if (table_num >= MAX_TABLE_NUM) {
  31. return NULL;
  32. }
  33. if (size % 256 != 0) {
  34. size = (size / 256 + 1) * 256;
  35. }
  36. //
  37. zhdb_table_t* table = &tablecache[table_num];
  38. table->add = &cache[table_add_off];
  39. table->size = size;
  40. table->name = name;
  41. // read from eeprom
  42. read_from_eeprom(table_add_off, table->add, size);
  43. //
  44. table_add_off += size;
  45. table_num++;
  46. ZLOGI("ZHDB", "alloc table %s add %p size %d", name, table->add, size);
  47. return table;
  48. }
  49. void ZHDB::storageData(uint8_t* data, size_t size) {
  50. zlock_guard guard(lock);
  51. //
  52. uint32_t off = data - cache;
  53. uint32_t align_off = (off / 256) * 256;
  54. uint32_t real_size = (size / 256 + 1) * 256;
  55. write_to_eeprom(align_off, &cache[align_off], real_size);
  56. }