hal_minifds.c 30 KB

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