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.

66 lines
1.5 KiB

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. using namespace iflytop;
  7. static zhdb_table_t table[MAX_TABLE_NUM];
  8. static uint32_t table_num = 0;
  9. static uint32_t table_add_off = 0;
  10. static zmutex lock("ZHDB");
  11. static void read_from_eeprom(uint32_t add, uint8_t* data, size_t size) { //
  12. for (size_t i = 0; i < size; i += 256) {
  13. AppHardware::ins()->eeprom.read(add + i, data + i, 256);
  14. }
  15. }
  16. static void write_to_eeprom(uint32_t add, uint8_t* data, size_t size) { //
  17. for (size_t i = 0; i < size; i += 256) {
  18. AppHardware::ins()->eeprom.write(add + i, data + i, 256);
  19. }
  20. }
  21. void ZHDB::init() { lock.init(); }
  22. zhdb_table_t* ZHDB::allocTable(const char* name, size_t size) {
  23. if (table_num >= MAX_TABLE_NUM) {
  24. return NULL;
  25. }
  26. if (size % 256 != 0) {
  27. size = (size / 256 + 1) * 256;
  28. }
  29. //
  30. zhdb_table_t* table = &table[table_num];
  31. uint32_t add = table_add_off;
  32. table->add = &cache[table_add_off];
  33. table->size = size;
  34. table->name = name;
  35. // read from eeprom
  36. read_from_eeprom(table_add_off, table->add, size);
  37. //
  38. table_add_off += size;
  39. table_num++;
  40. return table;
  41. }
  42. void ZHDB::storageData(uint8_t* data, size_t size) {
  43. zlock_guard guard(lock);
  44. //
  45. uint32_t off = data - cache;
  46. uint32_t align_off = (off / 256) * 256;
  47. uint32_t real_size = (size / 256 + 1) * 256;
  48. write_to_eeprom(align_off, &cache[align_off], real_size);
  49. }