hal_minifds.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. #include "hal_minifds.h"
  2. #include "cli.h"
  3. #define MINIFDS_PRINTF 0
  4. #if MINIFDS_PRINTF
  5. #include "SEGGER_RTT.h"
  6. #include "cli.h"
  7. #define DEBUG_LOG_MINIFDS DEBUG_LOG
  8. #else
  9. #define DEBUG_LOG_MINIFDS(...) ;
  10. #endif
  11. #define ALIGN_WORD(x) ((x) % 4 == 0 ? (x) : (((x) / 4 + 1) * 4)) //例如 33->36
  12. #define PARAMETER_CHECK(x)
  13. static char checkcec(uint32_t addr)
  14. {
  15. uint8_t crc = 0;
  16. minfds_record_t *recordhend = (minfds_record_t *)addr;
  17. for (int i = 0; i < recordhend->length; i++)
  18. {
  19. crc += ((uint8_t *)(addr + sizeof(minfds_record_t)))[i];
  20. }
  21. if (crc == recordhend->crc8)
  22. {
  23. return 1;
  24. }
  25. else
  26. {
  27. return 0;
  28. }
  29. }
  30. static uint32_t jump(uint32_t addr)
  31. {
  32. minfds_record_t *recordhend = (minfds_record_t *)addr;
  33. return addr + ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  34. }
  35. void printctb(minfds_t *p_minifds)
  36. {
  37. DEBUG_LOG_MINIFDS(" free_capacity %6d bytes\r\n", p_minifds->free_capacity);
  38. DEBUG_LOG_MINIFDS(" duty_capacity %6d bytes\r\n", p_minifds->duty_capacity);
  39. DEBUG_LOG_MINIFDS(" FFFFFFFF %6d bytes\r\n", p_minifds->free_capacity - p_minifds->duty_capacity);
  40. DEBUG_LOG_MINIFDS(" record_count %3d\r\n", p_minifds->record_count);
  41. }
  42. void print_record(uint8_t *data, length_t length)
  43. {
  44. for (int i = 0; i < length; i++)
  45. {
  46. if (i % 16 == 0)
  47. {
  48. DEBUG_LOG_MINIFDS("\r\n %4d ", i);
  49. for (int k = 0; k < 0xffff; k++)
  50. ;
  51. }
  52. DEBUG_LOG_MINIFDS("%2x ", data[i]);
  53. }
  54. DEBUG_LOG_MINIFDS("\r\n");
  55. }
  56. char minfds_Scan(minfds_t *p_minifds)
  57. {
  58. uint32_t hendaddress = p_minifds->start_addr;
  59. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  60. p_minifds->duty_capacity = 0;
  61. p_minifds->free_capacity = p_minifds->end_addr - p_minifds->start_addr;
  62. p_minifds->record_count = 0;
  63. DEBUG_LOG_MINIFDS(" minfds_Scan start !!!!!\r\n");
  64. while (1)
  65. {
  66. if ((*(uint32_t *)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  67. {
  68. for (uint32_t addr = hendaddress; addr < p_minifds->end_addr; addr++)
  69. {
  70. if (*(uint8_t *)addr != 0xff)
  71. {
  72. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE 0XFFFF 0x%x !!!!!\r\n", addr);
  73. return MINIFDS_ERR_HARDWARE;
  74. }
  75. }
  76. DEBUG_LOG_MINIFDS(" minfds_Scan end !!!!!\r\n");
  77. return MINIFDS_SUCCESS;
  78. }
  79. else
  80. {
  81. recordhend = (minfds_record_t *)hendaddress;
  82. if (checkcec(hendaddress))
  83. {
  84. DEBUG_LOG_MINIFDS("Record addr %x Key %3d length %d %d CRC OK\r\n", (uint32_t)recordhend, recordhend->record_key, recordhend->length, ALIGN_WORD(recordhend->length + sizeof(minfds_record_t)));
  85. if (recordhend->record_key == 0)
  86. {
  87. p_minifds->duty_capacity += ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  88. }
  89. else
  90. {
  91. p_minifds->record_count++;
  92. p_minifds->free_capacity -= ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  93. }
  94. }
  95. else
  96. {
  97. DEBUG_LOG_MINIFDS("Record Key %3d : CRCERROR !!!!!\r\n", recordhend->record_key);
  98. }
  99. // print_record((uint8_t*)(hendaddress+sizeof(minfds_record_t)),recordhend->length);
  100. hendaddress = jump(hendaddress);
  101. if (hendaddress > p_minifds->end_addr)
  102. {
  103. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE : 0x%x\r\n", hendaddress);
  104. return MINIFDS_ERR_HARDWARE;
  105. }
  106. }
  107. }
  108. }
  109. unsigned short minfds_record_count_get(minfds_t *p_minifds)
  110. {
  111. return p_minifds->record_count;
  112. }
  113. char minfds_gc(minfds_t *p_minifds)
  114. {
  115. #define WRITE_TINES 8
  116. uint8_t gcbuffer[4096];
  117. uint32_t duty_recodecount = 0;
  118. uint32_t recodecount = 0;
  119. uint32_t gcbuffer_index = 0;
  120. uint32_t erase_page_addr = p_minifds->start_addr; //接下来要擦除的扇区地址
  121. uint32_t write_page_addr = p_minifds->start_addr; //接下来要的写地址
  122. flash_OPER_Result rev = FLASH_OP_SUCCESS;
  123. unsigned short length;
  124. minfds_record_t *next_recordhend;
  125. minfds_record_t *recordhend = (minfds_record_t *)p_minifds->start_addr;
  126. DEBUG_LOG_MINIFDS("----------------------------gc s-------------------------- \r\n");
  127. printctb(p_minifds);
  128. p_minifds->free_capacity = p_minifds->end_addr - p_minifds->start_addr;
  129. while (1)
  130. {
  131. if ((*(uint32_t *)recordhend == 0xffffffff) || (uint32_t)recordhend == p_minifds->end_addr)
  132. {
  133. if (duty_recodecount == 0)
  134. {
  135. break;
  136. }
  137. p_minifds->api.flash_page_erase(erase_page_addr);
  138. DEBUG_LOG_MINIFDS("flash_page_erase(1) %x %x\r\n", erase_page_addr, write_page_addr);
  139. //---------write---------
  140. DEBUG_LOG_MINIFDS("write2 >= %x len =%d\r\n",write_page_addr,gcbuffer_index);
  141. for (int i = 0; i < gcbuffer_index / (4096 / WRITE_TINES); i++)
  142. {
  143. rev = p_minifds->api.flash_write(
  144. (uint32_t *)(write_page_addr + i * (4096 / WRITE_TINES)),
  145. (uint32_t *)(gcbuffer + i * (4096 / WRITE_TINES)),
  146. 4096 / 4 / WRITE_TINES);
  147. }
  148. if (gcbuffer_index % (4096 / WRITE_TINES) != 0)
  149. {
  150. rev = p_minifds->api.flash_write(
  151. (uint32_t *)(write_page_addr + (gcbuffer_index / (4096 / WRITE_TINES)) * (4096 / WRITE_TINES)),
  152. (uint32_t *)(gcbuffer + (gcbuffer_index / (4096 / WRITE_TINES)) * (4096 / WRITE_TINES)),
  153. (gcbuffer_index % (4096 / WRITE_TINES)) % 4 == 0 ? (gcbuffer_index % (4096 / WRITE_TINES)) / 4 : (gcbuffer_index % (4096 / WRITE_TINES)) / 4 + 1);
  154. }
  155. break;
  156. }
  157. else if (recordhend->record_key == 0)
  158. {
  159. duty_recodecount++;
  160. DEBUG_LOG_MINIFDS(" %3d page %2d :key duty, length %5d addr: %x\r\n", duty_recodecount, (erase_page_addr - p_minifds->start_addr) / 4096, recordhend->length,(uint32_t )recordhend);
  161. p_minifds->duty_capacity -= ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  162. next_recordhend = (minfds_record_t *)jump((uint32_t)recordhend);
  163. recordhend = next_recordhend;
  164. if ((uint32_t)recordhend > p_minifds->end_addr)
  165. {
  166. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 0x%x\r\n", (uint32_t)recordhend);
  167. return MINIFDS_ERR_HARDWARE;
  168. }
  169. while ((uint32_t)recordhend >= erase_page_addr + 4096)
  170. {
  171. p_minifds->api.flash_page_erase(erase_page_addr);
  172. erase_page_addr += 4096;
  173. DEBUG_LOG_MINIFDS("flash_page_erase(2) %x %x\r\n", erase_page_addr - 4096, write_page_addr);
  174. }
  175. }
  176. else
  177. {
  178. recodecount++;
  179. p_minifds->free_capacity -= ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  180. DEBUG_LOG_MINIFDS(" %3d page %2d :key %3d , length %5d addr: %x\r\n", recodecount, (erase_page_addr - p_minifds->start_addr) / 4096, recordhend->record_key, recordhend->length,(uint32_t )recordhend);
  181. next_recordhend = (minfds_record_t *)jump((uint32_t)recordhend);
  182. if ((uint32_t)next_recordhend > p_minifds->end_addr)
  183. {
  184. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 118 0x%x\r\n", (uint32_t)next_recordhend);
  185. return MINIFDS_ERR_HARDWARE;
  186. }
  187. length = (uint32_t)next_recordhend - (uint32_t)recordhend;
  188. for (uint32_t i = 0; i < length; i++)
  189. {
  190. gcbuffer[gcbuffer_index] = ((uint8_t *)recordhend)[i];
  191. gcbuffer_index++;
  192. if (gcbuffer_index >= 4096)
  193. {
  194. gcbuffer_index = 0;
  195. DEBUG_LOG_MINIFDS("gcbuffer_index >= 4096\r\n");
  196. while (erase_page_addr <= write_page_addr)
  197. {
  198. p_minifds->api.flash_page_erase(erase_page_addr);
  199. erase_page_addr += 4096;
  200. DEBUG_LOG_MINIFDS("flash_page_erase(3) %x %x\r\n", erase_page_addr - 4096, write_page_addr);
  201. }
  202. //---------write---------
  203. DEBUG_LOG_MINIFDS("write >= %x len =%d\r\n",write_page_addr,4096);
  204. for (int j = 0; j < WRITE_TINES; j++)
  205. {
  206. rev = p_minifds->api.flash_write(
  207. (uint32_t *)(write_page_addr + j * (4096 / WRITE_TINES)),
  208. (uint32_t *)(gcbuffer + j * (4096 / WRITE_TINES)),
  209. 4096 / 4 / WRITE_TINES);
  210. }
  211. write_page_addr += 4096;
  212. }
  213. }
  214. while ((uint32_t)next_recordhend >= erase_page_addr + 4096)
  215. {
  216. p_minifds->api.flash_page_erase(erase_page_addr);
  217. erase_page_addr += 4096;
  218. DEBUG_LOG_MINIFDS("flash_page_erase(4) %x %x\r\n", erase_page_addr - 4096, write_page_addr);
  219. }
  220. recordhend = next_recordhend;
  221. }
  222. for (int k = 0; k < 0xffff; k++);
  223. }
  224. if (rev != FLASH_OP_SUCCESS)
  225. {
  226. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_API_FAIL 0x%x\r\n", (uint32_t)p_minifds->duty_capacity);
  227. return MINIFDS_ERR_API_FAIL;
  228. }
  229. printctb(p_minifds);
  230. DEBUG_LOG_MINIFDS("----------------------------gc e-------------------------- \r\n");
  231. return 0;
  232. }
  233. char minfds_record_add(minfds_t *p_minifds, key_t key, uint8_t *data, length_t length)
  234. {
  235. uint32_t sizewords;
  236. uint32_t rev;
  237. uint32_t hendaddress = p_minifds->start_addr;
  238. DEBUG_LOG_MINIFDS("minfds_record_add : \r\n");
  239. printctb(p_minifds);
  240. DEBUG_LOG_MINIFDS(" Key = %d length = %d , 4klen:%d\r\n", key, length, ALIGN_WORD(length + sizeof(minfds_record_t)));
  241. if(length>MAX_RECORD_LENGTH)
  242. {
  243. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH length=%d\r\n",length);
  244. return MINIFDS_ERR_REPEAT_LENGTH;
  245. }
  246. if (key == 0)
  247. {
  248. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  249. return MINIFDS_ERR_REPEAT_KEY;
  250. }
  251. if (p_minifds->free_capacity < ALIGN_WORD(length + sizeof(minfds_record_t)))
  252. {
  253. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FREE_CAPACITY\r\n");
  254. return MINIFDS_ERR_NOT_FREE_CAPACITY;
  255. }
  256. if (p_minifds->free_capacity - p_minifds->duty_capacity < ALIGN_WORD(length + sizeof(minfds_record_t)))
  257. {
  258. DEBUG_LOG_MINIFDS(" minfds_record_add minfds_gc(p_minifds); %d,%d \r\n", p_minifds->duty_capacity, p_minifds->free_capacity);
  259. rev = minfds_gc(p_minifds);
  260. if (rev != 0)
  261. {
  262. DEBUG_LOG_MINIFDS(" minfds_record_add minfds_gc ERROR ; %d,%d \r\n", rev, p_minifds->free_capacity);
  263. return rev;
  264. }
  265. }
  266. while (1)
  267. {
  268. if ((*(uint32_t *)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  269. {
  270. minfds_record_t hend;
  271. hend.record_key = key;
  272. hend.length = length;
  273. hend.crc8 = 0;
  274. for (int i = 0; i < length; i++)
  275. {
  276. hend.crc8 += data[i];
  277. }
  278. sizewords = length;
  279. if (sizewords % 4 != 0)
  280. {
  281. sizewords = sizewords / 4 + 1;
  282. }
  283. else
  284. {
  285. sizewords = sizewords / 4;
  286. }
  287. DEBUG_LOG_MINIFDS(" writaddr 0x%x ,nextaddr 0x%x %d\r\n", hendaddress, hendaddress + ALIGN_WORD(hend.length + sizeof(minfds_record_t)), p_minifds->end_addr - hendaddress);
  288. p_minifds->free_capacity -= ALIGN_WORD(length + sizeof(minfds_record_t));
  289. for (int a = 0; a < ALIGN_WORD(hend.length + sizeof(minfds_record_t)); a++)
  290. {
  291. if (*(uint8_t *)(hendaddress + a) != 0xff)
  292. {
  293. DEBUG_LOG_MINIFDS("flash data :\r\n");
  294. print_record((uint8_t *)(hendaddress + sizeof(minfds_record_t)), sizewords * 4);
  295. DEBUG_LOG_MINIFDS("Ram data :\r\n");
  296. print_record(data, sizewords * 4);
  297. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE NOT 0xFF\r\n");
  298. return MINIFDS_ERR_HARDWARE;
  299. }
  300. }
  301. rev = p_minifds->api.flash_write((uint32_t *)hendaddress, (uint32_t *)&hend, sizeof(minfds_record_t) / 4);
  302. if (rev != FLASH_OP_SUCCESS)
  303. {
  304. printctb(p_minifds);
  305. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL flash_write 1 %d 0x%x 0x%x 0x%x\r\n", rev, (uint32_t *)hendaddress, (uint32_t *)&hend, sizeof(minfds_record_t) / 4);
  306. return MINIFDS_ERR_API_FAIL;
  307. }
  308. if (sizewords != 0)
  309. {
  310. rev = p_minifds->api.flash_write((uint32_t *)(hendaddress + sizeof(minfds_record_t)), (uint32_t *)data, sizewords);
  311. }
  312. if (rev == FLASH_OP_SUCCESS)
  313. {
  314. p_minifds->record_count++;
  315. if (checkcec(hendaddress) != 1)
  316. {
  317. DEBUG_LOG_MINIFDS("flash data :\r\n");
  318. print_record((uint8_t *)(hendaddress + sizeof(minfds_record_t)), sizewords * 4);
  319. DEBUG_LOG_MINIFDS("Ram data :\r\n");
  320. print_record(data, sizewords * 4);
  321. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE CRC\r\n");
  322. return MINIFDS_ERR_HARDWARE;
  323. }
  324. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS\r\n");
  325. return MINIFDS_SUCCESS;
  326. }
  327. else
  328. {
  329. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL flash_write 2 %d 0x%x 0x%x 0x%x\r\n", rev, (uint32_t *)(hendaddress + sizeof(minfds_record_t)), (uint32_t *)data, sizewords);
  330. return MINIFDS_ERR_API_FAIL;
  331. }
  332. }
  333. else
  334. {
  335. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  336. if (recordhend->record_key == key)
  337. {
  338. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY\r\n");
  339. return MINIFDS_ERR_REPEAT_KEY;
  340. }
  341. if (!checkcec(hendaddress))
  342. {
  343. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE CRC add 0x%x\r\n", hendaddress);
  344. return MINIFDS_ERR_HARDWARE;
  345. }
  346. hendaddress = jump(hendaddress);
  347. if (hendaddress > p_minifds->end_addr)
  348. {
  349. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 426 0x%x\r\n", hendaddress);
  350. return MINIFDS_ERR_HARDWARE;
  351. }
  352. }
  353. }
  354. }
  355. char minfds_record_find(minfds_t *p_minifds, key_t key, uint8_t *data, length_t *length)
  356. {
  357. uint32_t hendaddress = p_minifds->start_addr;
  358. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  359. DEBUG_LOG_MINIFDS("minfds_record_find : \r\n");
  360. DEBUG_LOG_MINIFDS(" Key = %d \r\n", key);
  361. if (key == 0)
  362. {
  363. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  364. return MINIFDS_ERR_REPEAT_KEY;
  365. }
  366. if(*length==0)
  367. {
  368. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH *length=0\r\n");
  369. return MINIFDS_ERR_REPEAT_LENGTH;
  370. }
  371. while (1)
  372. {
  373. if ((*(uint32_t *)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  374. {
  375. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  376. return MINIFDS_ERR_NOT_FIND_RECORD;
  377. }
  378. else if (recordhend->record_key == key)
  379. {
  380. if(*length < recordhend->length)
  381. {
  382. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH *length=%d\r\n",*length);
  383. return MINIFDS_ERR_REPEAT_LENGTH;
  384. }
  385. memcpy(data, (uint8_t *)(hendaddress + sizeof(minfds_record_t)), recordhend->length);
  386. *length = recordhend->length;
  387. DEBUG_LOG_MINIFDS(" length = %d \r\n", recordhend->length);
  388. return MINIFDS_SUCCESS;
  389. }
  390. else
  391. {
  392. hendaddress = jump(hendaddress);
  393. if (hendaddress > p_minifds->end_addr)
  394. {
  395. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  396. return MINIFDS_ERR_HARDWARE;
  397. }
  398. recordhend = (minfds_record_t *)hendaddress;
  399. }
  400. }
  401. }
  402. char get_record_hendaddr(minfds_t *p_minifds, key_t key,uint32_t *addr)
  403. {
  404. uint32_t hendaddress = p_minifds->start_addr;
  405. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  406. DEBUG_LOG_MINIFDS("minfds_record_get_length : \r\n");
  407. DEBUG_LOG_MINIFDS(" Key = %d \r\n", key);
  408. if (key == 0)
  409. {
  410. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  411. return MINIFDS_ERR_REPEAT_KEY;
  412. }
  413. while (1)
  414. {
  415. if ((*(uint32_t *)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  416. {
  417. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  418. return MINIFDS_ERR_NOT_FIND_RECORD;
  419. }
  420. else if (recordhend->record_key == key)
  421. {
  422. *addr = (uint32_t)recordhend;
  423. DEBUG_LOG_MINIFDS(" addr = %x \r\n", *addr);
  424. return MINIFDS_SUCCESS;
  425. }
  426. else
  427. {
  428. hendaddress = jump(hendaddress);
  429. if (hendaddress > p_minifds->end_addr)
  430. {
  431. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  432. return MINIFDS_ERR_HARDWARE;
  433. }
  434. recordhend = (minfds_record_t *)hendaddress;
  435. }
  436. }
  437. }
  438. char minfds_record_get_length(minfds_t *p_minifds, key_t key, length_t *length)
  439. {
  440. char rev = 0 ;
  441. uint32_t addr_hend=0;
  442. minfds_record_t *recordhend =NULL;
  443. rev=get_record_hendaddr(p_minifds,key,&addr_hend);
  444. if(rev!=MINIFDS_SUCCESS)return rev;
  445. recordhend = (minfds_record_t *)addr_hend;
  446. *length = recordhend->length;
  447. return MINIFDS_SUCCESS;
  448. }
  449. char minfds_record_find_mem(minfds_t *p_minifds, uint8_t *mem, int mem_len,key_t * key)
  450. {
  451. uint32_t hendaddress = p_minifds->start_addr;
  452. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  453. DEBUG_LOG_MINIFDS("minfds_record_find_mem : \r\n");
  454. DEBUG_LOG_MINIFDS(" mem_len = %d \r\n", mem_len);
  455. DEBUG_LOG_MINIFDS(" mem_len254 = %d \r\n", 100);
  456. DEBUG_LOG_MINIFDS(" [%s] \r\n", mem);
  457. if ((mem_len == 0)||(mem==NULL))
  458. {
  459. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  460. return MINIFDS_ERR_REPEAT_KEY;
  461. }
  462. while (1)
  463. {
  464. if ((*(uint32_t *)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  465. {
  466. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  467. return MINIFDS_ERR_NOT_FIND_RECORD;
  468. }
  469. else if (recordhend->record_key != 0 )
  470. {
  471. if(recordhend->length >= mem_len)
  472. {
  473. for(int i=0;i<recordhend->length-mem_len;i++)
  474. {
  475. if(memcmp(mem,(uint8_t *)(hendaddress+i+sizeof(minfds_record_t)),mem_len)==0)
  476. {
  477. *key=recordhend->record_key;
  478. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS -----\r\n");
  479. return MINIFDS_SUCCESS;
  480. }
  481. }
  482. }
  483. hendaddress = jump(hendaddress);
  484. if (hendaddress > p_minifds->end_addr)
  485. {
  486. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  487. return MINIFDS_ERR_HARDWARE;
  488. }
  489. recordhend = (minfds_record_t *)hendaddress;
  490. }
  491. else
  492. {
  493. hendaddress = jump(hendaddress);
  494. if (hendaddress > p_minifds->end_addr)
  495. {
  496. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  497. return MINIFDS_ERR_HARDWARE;
  498. }
  499. recordhend = (minfds_record_t *)hendaddress;
  500. }
  501. }
  502. }
  503. char minfds_record_update(minfds_t *p_minifds, key_t key, uint8_t *data, length_t length)
  504. {
  505. uint32_t hendaddr=0;
  506. char rev = 0;
  507. if (key == 0)
  508. {
  509. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  510. return MINIFDS_ERR_REPEAT_KEY;
  511. }
  512. DEBUG_LOG_MINIFDS("minfds_record_update key=%d\r\n",key);
  513. rev = get_record_hendaddr(p_minifds, key,&hendaddr);
  514. if (rev == MINIFDS_SUCCESS)
  515. {
  516. minfds_record_t *recordhend=(minfds_record_t*)hendaddr;
  517. if(recordhend->length==length)
  518. {
  519. if(memcmp((uint8_t*)(hendaddr+sizeof(minfds_record_t)),data,length)==0)
  520. {
  521. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS NOT NEED UPDATE\r\n");
  522. return MINIFDS_SUCCESS;
  523. }
  524. }
  525. }
  526. rev = minfds_record_delete(p_minifds, key);
  527. if (rev != 0)
  528. {
  529. return rev;
  530. }
  531. return minfds_record_add(p_minifds, key, data, length);
  532. }
  533. char minfds_record_delete(minfds_t *p_minifds, key_t key)
  534. {
  535. uint32_t hendaddress = p_minifds->start_addr;
  536. flash_OPER_Result rev;
  537. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  538. DEBUG_LOG_MINIFDS("minfds_record_delete : key %d ", key);
  539. if (key == 0)
  540. {
  541. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  542. return MINIFDS_ERR_REPEAT_KEY;
  543. }
  544. while (1)
  545. {
  546. if ((*(uint32_t *)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  547. {
  548. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD record_count %d\r\n", p_minifds->record_count);
  549. return MINIFDS_ERR_NOT_FIND_RECORD;
  550. }
  551. if (recordhend->record_key == key)
  552. {
  553. minfds_record_t temp;
  554. memcpy(&temp, recordhend, sizeof(minfds_record_t));
  555. temp.record_key = 0x00;
  556. p_minifds->duty_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  557. p_minifds->free_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  558. rev = p_minifds->api.flash_write((uint32_t *)hendaddress, (uint32_t *)&temp, sizeof(minfds_record_t) / 4);
  559. if (rev == FLASH_OP_SUCCESS)
  560. {
  561. if (memcmp(&temp, (uint32_t *)hendaddress, sizeof(minfds_record_t)) != 0)
  562. {
  563. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL1\r\n");
  564. return MINIFDS_ERR_API_FAIL;
  565. }
  566. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS\r\n");
  567. p_minifds->record_count--;
  568. return MINIFDS_SUCCESS;
  569. }
  570. else
  571. {
  572. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL2\r\n");
  573. return MINIFDS_ERR_API_FAIL;
  574. }
  575. }
  576. else
  577. {
  578. hendaddress = jump(hendaddress);
  579. if (hendaddress > p_minifds->end_addr)
  580. {
  581. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 0x%x\r\n", hendaddress);
  582. return MINIFDS_ERR_HARDWARE;
  583. }
  584. recordhend = (minfds_record_t *)hendaddress;
  585. }
  586. }
  587. }
  588. char minfds_record_delete_all(minfds_t *p_minifds)
  589. {
  590. uint32_t hendaddress = p_minifds->start_addr;
  591. uint32_t rev;
  592. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  593. DEBUG_LOG_MINIFDS("minfds_record_delete_all : ");
  594. while (1)
  595. {
  596. if ((*(uint32_t *)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  597. {
  598. return MINIFDS_SUCCESS;
  599. }
  600. if (recordhend->record_key != 0)
  601. {
  602. minfds_record_t temp;
  603. memcpy(&temp, recordhend, sizeof(minfds_record_t));
  604. temp.record_key = 0x00;
  605. p_minifds->duty_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  606. p_minifds->free_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  607. DEBUG_LOG_MINIFDS(" delete record_key : %d\r\n", recordhend->record_key);
  608. rev = p_minifds->api.flash_write((uint32_t *)hendaddress, (uint32_t *)&temp, sizeof(minfds_record_t) / 4);
  609. if (rev == FLASH_OP_SUCCESS)
  610. {
  611. p_minifds->record_count--;
  612. }
  613. else
  614. {
  615. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL\r\n");
  616. return MINIFDS_ERR_API_FAIL;
  617. }
  618. }
  619. else
  620. {
  621. hendaddress = jump(hendaddress);
  622. if (hendaddress > p_minifds->end_addr)
  623. {
  624. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 0x%x\r\n", hendaddress);
  625. return MINIFDS_ERR_HARDWARE;
  626. }
  627. recordhend = (minfds_record_t *)hendaddress;
  628. }
  629. }
  630. }
  631. char minfds_format(minfds_t *p_minifds)
  632. {
  633. for (int i = 0; i < (p_minifds->end_addr - p_minifds->start_addr) / 4096; i++)
  634. {
  635. p_minifds->api.flash_page_erase(p_minifds->start_addr + i * 4096);
  636. }
  637. p_minifds->duty_capacity = 0;
  638. p_minifds->record_count = 0;
  639. p_minifds->free_capacity = p_minifds->end_addr - p_minifds->start_addr;
  640. return MINIFDS_SUCCESS;
  641. }
  642. char minfds_init(minfds_t *p_minifds)
  643. {
  644. DEBUG_LOG_MINIFDS("----------------------------------\r\n");
  645. DEBUG_LOG_MINIFDS(" Minfds Start_addr : 0x%x\r\n", p_minifds->start_addr);
  646. DEBUG_LOG_MINIFDS(" Minfds End_addr : 0x%x \r\n", p_minifds->end_addr);
  647. minfds_Scan(p_minifds);
  648. printctb(p_minifds);
  649. DEBUG_LOG_MINIFDS("----------------------------------\r\n");
  650. return 0;
  651. }
  652. char record_index(minfds_t *p_minifds,minfds_record_t *record,uint8_t * data,int index)
  653. {
  654. uint32_t hendaddress = p_minifds->start_addr;
  655. int i=0;
  656. minfds_record_t *recordhend = (minfds_record_t *)hendaddress;
  657. DEBUG_LOG_MINIFDS("record_index : \r\n");
  658. DEBUG_LOG_MINIFDS(" index = %d \r\n", index);
  659. if ((index >= p_minifds->record_count)||(record==NULL)||(data==NULL))
  660. {
  661. // DEBUG_LOG_MINIFDS("MINIFDS_ERR_PARAMETER \r\n");
  662. return MINIFDS_ERR_PARAMETER;
  663. }
  664. while (1)
  665. {
  666. if ((*(uint32_t *)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  667. {
  668. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  669. return MINIFDS_ERR_NOT_FIND_RECORD;
  670. }
  671. else if (recordhend->record_key !=0)
  672. {
  673. if(i==index)
  674. {
  675. data=(uint8_t *)(hendaddress+sizeof(minfds_record_t));
  676. record=recordhend;
  677. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS \r\n");
  678. return MINIFDS_SUCCESS;
  679. }
  680. i++;
  681. }
  682. else
  683. {
  684. hendaddress = jump(hendaddress);
  685. if (hendaddress > p_minifds->end_addr)
  686. {
  687. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  688. return MINIFDS_ERR_HARDWARE;
  689. }
  690. recordhend = (minfds_record_t *)hendaddress;
  691. }
  692. }
  693. }
  694. char pm_add(minfds_t* p_minifds, const char* key, int key_len, unsigned char* data, length_t length)
  695. {
  696. unsigned char buff[MAX_RECORD_LENGTH];
  697. key_t TEMP_KEY = 1;
  698. char rev = 0;
  699. if (key_len + length + 1 >= MAX_RECORD_LENGTH)
  700. {
  701. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH \r\n");
  702. return MINIFDS_ERR_REPEAT_LENGTH;
  703. }
  704. memcpy(buff, key, key_len);
  705. buff[key_len] = ':';
  706. rev = minfds_record_find_mem(p_minifds, buff, key_len + 1, &TEMP_KEY);
  707. if (rev == MINIFDS_SUCCESS)
  708. {
  709. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY \r\n");
  710. return MINIFDS_ERR_REPEAT_KEY;
  711. }
  712. memcpy(&buff[key_len + 1], data, length);
  713. while (1)
  714. {
  715. rev = minfds_record_add(p_minifds, TEMP_KEY, buff, key_len + length + 1);
  716. if (rev != MINIFDS_ERR_REPEAT_KEY)
  717. {
  718. return rev;
  719. }
  720. TEMP_KEY++;
  721. if (TEMP_KEY == 0)
  722. {
  723. return MINIFDS_ERR_REPEAT_KEY;
  724. }
  725. }
  726. }
  727. char pm_find(minfds_t *p_minifds, const char * key,int key_len, uint8_t *data, length_t *length)
  728. {
  729. key_t TEMP_KEY=1;
  730. char rev=0;
  731. length_t temp_len=MAX_RECORD_LENGTH;
  732. uint8_t buff[MAX_RECORD_LENGTH];
  733. memcpy(buff,key,key_len);
  734. buff[key_len]=':';
  735. rev=minfds_record_find_mem(p_minifds,buff,key_len+1,&TEMP_KEY);
  736. if(rev!=MINIFDS_SUCCESS)return rev;
  737. rev=minfds_record_find(p_minifds, TEMP_KEY, buff, &temp_len);
  738. if(rev!=MINIFDS_SUCCESS)return rev;
  739. memcpy(data,&buff[key_len+1],temp_len-key_len-1);
  740. *length=temp_len-key_len-1;
  741. return MINIFDS_SUCCESS;
  742. }
  743. char pm_get_length(minfds_t *p_minifds,const char * key,int key_len, length_t *length)
  744. {
  745. key_t TEMP_KEY=1;
  746. char rev=0;
  747. length_t temp_len=0;
  748. uint8_t buff[MAX_RECORD_LENGTH];
  749. memcpy(buff,key,key_len);
  750. buff[key_len]=':';
  751. rev=minfds_record_find_mem(p_minifds,buff,key_len+1,&TEMP_KEY);
  752. if(rev!=MINIFDS_SUCCESS)return rev;
  753. rev=minfds_record_get_length(p_minifds, TEMP_KEY, &temp_len);
  754. *length=temp_len-key_len-1;
  755. return rev;
  756. }
  757. char pm_delete(minfds_t *p_minifds, const char * key,int key_len)
  758. {
  759. key_t TEMP_KEY=1;
  760. char rev=0;
  761. uint8_t buff[MAX_RECORD_LENGTH];
  762. memcpy(buff,key,key_len);
  763. buff[key_len]=':';
  764. rev=minfds_record_find_mem(p_minifds,buff,key_len+1,&TEMP_KEY);
  765. if(rev!=MINIFDS_SUCCESS)return rev;
  766. rev=minfds_record_delete(p_minifds, TEMP_KEY);
  767. if(rev!=MINIFDS_SUCCESS)return rev;
  768. return MINIFDS_SUCCESS;
  769. }
  770. char pm_update(minfds_t *p_minifds, const char * key,int key_len, uint8_t *data, length_t length)
  771. {
  772. key_t TEMP_KEY=1;
  773. char rev=0;
  774. uint8_t buff[MAX_RECORD_LENGTH];
  775. printctb(p_minifds);
  776. if(key_len+length+1>=MAX_RECORD_LENGTH)
  777. {
  778. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH \r\n");
  779. return MINIFDS_ERR_REPEAT_LENGTH;
  780. }
  781. memcpy(buff,key,key_len);
  782. buff[key_len]=':';
  783. memcpy(&buff[key_len+1],data,length);
  784. rev=minfds_record_find_mem(p_minifds,buff,key_len+1,&TEMP_KEY);
  785. if(rev!=MINIFDS_SUCCESS)return rev;
  786. return minfds_record_update(p_minifds, TEMP_KEY,buff,key_len+length+1);
  787. }
  788. //-----------------------------------------------------------------------------------------------------
  789. /*
  790. extern minfds_t minifds;
  791. void pma_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  792. {
  793. char keu[]="key1";
  794. uint8_t datat[]="testdata";
  795. pm_add(&minifds,keu,sizeof("key1"),datat,sizeof("testdata"));
  796. minfds_Scan(&minifds);
  797. }
  798. CLI_CMD_REGISTER(pma, "Cli cmd history", pma_pcs);
  799. void pmf_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  800. {
  801. char keu[]="key1";
  802. uint8_t datat[50]="11111111111";
  803. uint8_t length=0;
  804. pm_find(&minifds,keu,sizeof("key1"),datat,&length);
  805. DEBUG_LOG_MINIFDS("%s \r\n",datat);
  806. }
  807. CLI_CMD_REGISTER(pmf, "Cli cmd history", pmf_pcs);
  808. void pmd_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  809. {
  810. char keu[]="key1";
  811. uint8_t datat[50]="11111111111";
  812. pm_delete(&minifds,keu,sizeof("key1"));
  813. DEBUG_LOG_MINIFDS("%s \r\n",datat);
  814. }
  815. CLI_CMD_REGISTER(pmd, "Cli cmd history", pmd_pcs);
  816. void pmu_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  817. {
  818. char keu[]="key1";
  819. uint8_t datat[50]="11111111111";
  820. pm_update(&minifds,keu,sizeof("key1"),datat,10);
  821. DEBUG_LOG_MINIFDS("%s \r\n",datat);
  822. }
  823. CLI_CMD_REGISTER(pmu, "Cli cmd history", pmu_pcs);
  824. */