storage.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "storage.h"
  2. #include "Rtcx.h"
  3. #define INDEX_ADDR (0x00)
  4. #define INDEX_MAGIC (0xAA55AA55)
  5. typedef struct _recordindex
  6. {
  7. uint32_t magic;
  8. uint32_t start_addr;
  9. uint32_t next_addr;
  10. uint16_t num;
  11. uint16_t max_num;
  12. uint16_t crc;
  13. }record_index;
  14. static record_index g_recordindex;
  15. static uint8_t wait_time = 0;
  16. static uint8_t will_deletecount = 0;
  17. //uint16_t g_maxRecordNum = MAX_RECORD_NUM;
  18. //uint32_t g_start_addr = DEFAULT_START_ADDR;
  19. //uint16_t g_Record_Num = 0;
  20. //uint32_t g_next_addr = DEFAULT_START_ADDR;
  21. static void update_index(void);
  22. static uint8_t read_recordindex(void);
  23. void Storage_Init(void)
  24. {
  25. wait_time=0;
  26. will_deletecount = 0;
  27. W25Q64_Init();
  28. read_recordindex();
  29. printf("Storage_Init: num:%d \r\n", g_recordindex.num);
  30. }
  31. uint16_t Storage_GetItemNum(void)
  32. {
  33. return g_recordindex.num;
  34. }
  35. void Storage_AddItem(uint8_t itemtype, uint8_t eventtype)
  36. {
  37. DataItem item;
  38. uint8_t i=0;
  39. uint8_t* pItem = (uint8_t*)&item;
  40. item.magic = ITEM_MAGIC;
  41. item.itemtype = itemtype;
  42. item.eventtype = eventtype;
  43. //timestamp
  44. item.timestamp[0] = g_dateTime.year-2000;
  45. item.timestamp[1] = g_dateTime.month;
  46. item.timestamp[2] = g_dateTime.day;
  47. item.timestamp[3] = g_dateTime.hour;
  48. item.timestamp[4] = g_dateTime.minute;
  49. item.timestamp[5] = g_dateTime.second;
  50. //for(i=0; i<6; i++){
  51. // item.timestamp[i] = 0x00;
  52. //}
  53. item.crc = 0x00;
  54. printf("Storage_AddItem: itemtype:%d, eventtype:%d \r\n", itemtype, eventtype);
  55. if(0 == g_recordindex.next_addr%SECTOR_SIZE){ //新的 sector 需要先擦除
  56. printf("Storage_AddItem: erssector nextaddr:0x%x \r\n", g_recordindex.next_addr);
  57. W25Q64_ErsSector(g_recordindex.next_addr, 1);//擦除1个扇区4KB
  58. }
  59. //printf("Storage_AddItem: write start \r\n");
  60. W25Q64_WriteBytes(g_recordindex.next_addr, pItem, sizeof(DataItem));
  61. g_recordindex.num++;
  62. g_recordindex.next_addr += ITEM_STORAGE_SPACE;
  63. //printf("Storage_AddItem: write ok \r\n");
  64. if(g_recordindex.next_addr >= (g_recordindex.start_addr+MAX_STORAGE_SPACE)){
  65. g_recordindex.next_addr = g_recordindex.start_addr;
  66. }
  67. if(g_recordindex.num > MAX_RECORD_NUM){
  68. g_recordindex.num = MAX_RECORD_NUM;
  69. }
  70. update_index();
  71. }
  72. //读取一条记录,从最老的开始
  73. // return 0 ,成功 , 非0 失败
  74. uint8_t Storage_GetItemFromOldest(DataItem* pitem)
  75. {
  76. uint32_t item_addr = 0;
  77. if(0 == g_recordindex.num ){
  78. return 1;
  79. }
  80. if(g_recordindex.start_addr+g_recordindex.num*PAGE_SIZE <= g_recordindex.next_addr){
  81. item_addr = g_recordindex.next_addr - g_recordindex.num*ITEM_STORAGE_SPACE;
  82. }else{
  83. item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-g_recordindex.num*ITEM_STORAGE_SPACE;
  84. }
  85. W25Q64_ReadBytes(item_addr, (uint8_t *)pitem, sizeof(DataItem));
  86. g_recordindex.num--;
  87. return 0;
  88. }
  89. //读取第read_id条记录,从最老的开始
  90. // return 0 ,成功 , 非0 失败 , 读取并不删除
  91. uint8_t Storage_GetItemFromOldest2(uint8_t read_id, DataItem* pitem)
  92. {
  93. uint32_t item_addr = 0;
  94. if(read_id > g_recordindex.num-1 ){
  95. return 1;
  96. }
  97. if(g_recordindex.start_addr+g_recordindex.num*PAGE_SIZE <= g_recordindex.next_addr){
  98. item_addr = g_recordindex.next_addr - (g_recordindex.num-read_id)*ITEM_STORAGE_SPACE;
  99. }else{
  100. item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-(g_recordindex.num-read_id)*ITEM_STORAGE_SPACE;
  101. }
  102. W25Q64_ReadBytes(item_addr, (uint8_t *)pitem, sizeof(DataItem));
  103. //g_recordindex.num--;
  104. return 0;
  105. }
  106. void Storage_WaitDelete(uint8_t num)
  107. {
  108. wait_time = 5;
  109. if(num > g_recordindex.num){
  110. will_deletecount = g_recordindex.num;
  111. }else{
  112. will_deletecount = num;
  113. }
  114. }
  115. void Storage_DeleteConfirm(void)
  116. {
  117. g_recordindex.num -= will_deletecount;
  118. update_index();
  119. }
  120. //每秒执行一次
  121. void Storage_CountReduce(void)
  122. {
  123. if(wait_time > 0){
  124. wait_time--;
  125. if(0 == wait_time){
  126. will_deletecount=0;
  127. }
  128. }
  129. }
  130. uint8_t read_recordindex(void)
  131. {
  132. W25Q64_ReadBytes(INDEX_ADDR, (uint8_t *)&g_recordindex, sizeof(record_index));
  133. if(INDEX_MAGIC == g_recordindex.magic){
  134. }else{
  135. g_recordindex.magic = INDEX_MAGIC;
  136. g_recordindex.max_num = MAX_RECORD_NUM;
  137. g_recordindex.num = 0;
  138. g_recordindex.start_addr = DEFAULT_START_ADDR;
  139. g_recordindex.next_addr = DEFAULT_START_ADDR;
  140. }
  141. return 0;
  142. }
  143. void update_index(void)
  144. {
  145. /*擦除数据.*/
  146. //printf("update_index 111 \r\n");
  147. W25Q64_ErsSector(INDEX_ADDR, 1);//擦除1个扇区4KB
  148. //printf("update_index 222 \r\n");
  149. W25Q64_WriteBytes(INDEX_ADDR, (uint8_t *)&g_recordindex, sizeof(record_index));
  150. //printf("update_index 333 \r\n");
  151. }
  152. void Storage_Save(void)
  153. {
  154. update_index();
  155. }