123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #include "storage.h"
- #include "Ds1302.h"
- #define INDEX_ADDR (0x00)
- #define INDEX_MAGIC (0xAA55AA55)
- typedef struct _recordindex
- {
- uint32_t magic;
- uint32_t start_addr;
- uint32_t next_addr;
- uint16_t num;
- uint16_t max_num;
- uint16_t crc;
- }record_index;
- static record_index g_recordindex;
- static uint8_t wait_time = 0;
- static uint8_t will_deletecount = 0;
- //uint16_t g_maxRecordNum = MAX_RECORD_NUM;
- //uint32_t g_start_addr = DEFAULT_START_ADDR;
- //uint16_t g_Record_Num = 0;
- //uint32_t g_next_addr = DEFAULT_START_ADDR;
- static void update_index(void);
- static uint8_t read_recordindex(void);
- void Storage_Init(void)
- {
- wait_time=0;
- will_deletecount = 0;
-
- W25Q64_Init();
-
- read_recordindex();
-
- printf("Storage_Init: num:%d \r\n", g_recordindex.num);
- }
- uint16_t Storage_GetItemNum(void)
- {
- return g_recordindex.num;
- }
- void Storage_AddItem(uint8_t itemtype, uint8_t eventtype)
- {
- DataItem item;
- uint8_t i=0;
- uint8_t* pItem = (uint8_t*)&item;
- item.magic = ITEM_MAGIC;
- item.itemtype = itemtype;
- item.eventtype = eventtype;
-
- //timestamp
- item.timestamp[0] = g_dateTime.year;
- item.timestamp[1] = g_dateTime.month;
- item.timestamp[2] = g_dateTime.day;
- item.timestamp[3] = g_dateTime.hour;
- item.timestamp[4] = g_dateTime.minute;
- item.timestamp[5] = g_dateTime.second;
-
- //for(i=0; i<6; i++){
- // item.timestamp[i] = 0x00;
- //}
- item.crc = 0x00;
-
- printf("Storage_AddItem: itemtype:%d, eventtype:%d \r\n", itemtype, eventtype);
-
- if(0 == g_recordindex.next_addr%SECTOR_SIZE){ //新的 sector 需要先擦除
- printf("Storage_AddItem: erssector nextaddr:0x%x \r\n", g_recordindex.next_addr);
- W25Q64_ErsSector(g_recordindex.next_addr, 1);//擦除1个扇区4KB
- }
-
- //printf("Storage_AddItem: write start \r\n");
-
- W25Q64_WriteBytes(g_recordindex.next_addr, pItem, sizeof(DataItem));
- g_recordindex.num++;
- g_recordindex.next_addr += PAGE_SIZE;
-
- //printf("Storage_AddItem: write ok \r\n");
-
- if(g_recordindex.next_addr >= (g_recordindex.start_addr+MAX_STORAGE_SPACE)){
- g_recordindex.next_addr = g_recordindex.start_addr;
- }
-
- if(g_recordindex.num > MAX_RECORD_NUM){
- g_recordindex.num = MAX_RECORD_NUM;
- }
-
- update_index();
-
- }
- //读取一条记录,从最老的开始
- // return 0 ,成功 , 非0 失败
- uint8_t Storage_GetItemFromOldest(DataItem* pitem)
- {
- uint32_t item_addr = 0;
- if(0 == g_recordindex.num ){
- return 1;
- }
-
- if(g_recordindex.start_addr+g_recordindex.num*PAGE_SIZE <= g_recordindex.next_addr){
- item_addr = g_recordindex.next_addr - g_recordindex.num*PAGE_SIZE;
- }else{
- item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-g_recordindex.num*PAGE_SIZE;
- }
-
-
- W25Q64_ReadBytes(item_addr, (uint8_t *)pitem, sizeof(DataItem));
-
- g_recordindex.num--;
-
- return 0;
- }
- //读取第read_id条记录,从最老的开始
- // return 0 ,成功 , 非0 失败 , 读取并不删除
- uint8_t Storage_GetItemFromOldest2(uint8_t read_id, DataItem* pitem)
- {
- uint32_t item_addr = 0;
- if(read_id > g_recordindex.num-1 ){
- return 1;
- }
-
- if(g_recordindex.start_addr+g_recordindex.num*PAGE_SIZE <= g_recordindex.next_addr){
- item_addr = g_recordindex.next_addr - (g_recordindex.num-read_id)*PAGE_SIZE;
- }else{
- item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-(g_recordindex.num-read_id)*PAGE_SIZE;
- }
-
-
- W25Q64_ReadBytes(item_addr, (uint8_t *)pitem, sizeof(DataItem));
-
- //g_recordindex.num--;
-
- return 0;
- }
- void Storage_WaitDelete(uint8_t num)
- {
- wait_time = 5;
- if(num > g_recordindex.num){
- will_deletecount = g_recordindex.num;
- }else{
- will_deletecount = num;
- }
-
- }
- void Storage_DeleteConfirm(void)
- {
- g_recordindex.num -= will_deletecount;
- update_index();
- }
- //每秒执行一次
- void Storage_CountReduce(void)
- {
-
- if(wait_time > 0){
- wait_time--;
- if(0 == wait_time){
- will_deletecount=0;
- }
-
- }
- }
- uint8_t read_recordindex(void)
- {
-
- W25Q64_ReadBytes(INDEX_ADDR, (uint8_t *)&g_recordindex, sizeof(record_index));
-
- if(INDEX_MAGIC == g_recordindex.magic){
-
-
- }else{
-
- g_recordindex.magic = INDEX_MAGIC;
- g_recordindex.max_num = MAX_RECORD_NUM;
- g_recordindex.num = 0;
- g_recordindex.start_addr = DEFAULT_START_ADDR;
- g_recordindex.next_addr = DEFAULT_START_ADDR;
- }
-
- return 0;
- }
- void update_index(void)
- {
- /*擦除数据.*/
- //printf("update_index 111 \r\n");
- W25Q64_ErsSector(INDEX_ADDR, 1);//擦除1个扇区4KB
-
- //printf("update_index 222 \r\n");
- W25Q64_WriteBytes(INDEX_ADDR, (uint8_t *)&g_recordindex, sizeof(record_index));
-
- printf("update_index 333 \r\n");
- }
- void Storage_Save(void)
- {
- update_index();
- }
|