hal_minifds.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. #if MINIFDS_PRINTF
  50. for (int k = 0; k < 0xffff; k++)
  51. ;
  52. #endif
  53. }
  54. DEBUG_LOG_MINIFDS("%2x ", data[i]);
  55. }
  56. DEBUG_LOG_MINIFDS("\r\n");
  57. }
  58. char minfds_Scan(minfds_t* p_minifds)
  59. {
  60. uint32_t hendaddress = p_minifds->start_addr;
  61. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  62. p_minifds->duty_capacity = 0;
  63. p_minifds->free_capacity = p_minifds->end_addr - p_minifds->start_addr;
  64. p_minifds->record_count = 0;
  65. DEBUG_LOG_MINIFDS(" minfds_Scan start !!!!!\r\n");
  66. while (1)
  67. {
  68. if ((*(uint32_t*)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  69. {
  70. for (uint32_t addr = hendaddress; addr < p_minifds->end_addr; addr++)
  71. {
  72. if (*(uint8_t*)addr != 0xff)
  73. {
  74. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE 0XFFFF 0x%x !!!!!\r\n", addr);
  75. return MINIFDS_ERR_HARDWARE;
  76. }
  77. }
  78. DEBUG_LOG_MINIFDS(" minfds_Scan end !!!!!\r\n");
  79. return MINIFDS_SUCCESS;
  80. }
  81. else
  82. {
  83. recordhend = (minfds_record_t*)hendaddress;
  84. if (checkcec(hendaddress))
  85. {
  86. 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)));
  87. if (recordhend->record_key == 0)
  88. {
  89. p_minifds->duty_capacity += ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  90. }
  91. else
  92. {
  93. p_minifds->record_count++;
  94. p_minifds->free_capacity -= ALIGN_WORD(recordhend->length + sizeof(minfds_record_t));
  95. }
  96. }
  97. else
  98. {
  99. DEBUG_LOG_MINIFDS("Record Key %3d : CRCERROR !!!!!\r\n", recordhend->record_key);
  100. }
  101. // print_record((uint8_t*)(hendaddress+sizeof(minfds_record_t)),recordhend->length);
  102. hendaddress = jump(hendaddress);
  103. if (hendaddress > p_minifds->end_addr)
  104. {
  105. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE : 0x%x\r\n", hendaddress);
  106. return MINIFDS_ERR_HARDWARE;
  107. }
  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. #if MINIFDS_PRINTF
  225. for (int k = 0; k < 0xffff; k++);
  226. #endif
  227. }
  228. if (rev != FLASH_OP_SUCCESS)
  229. {
  230. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_API_FAIL 0x%x\r\n", (uint32_t)p_minifds->duty_capacity);
  231. return MINIFDS_ERR_API_FAIL;
  232. }
  233. printctb(p_minifds);
  234. DEBUG_LOG_MINIFDS("----------------------------gc e-------------------------- \r\n");
  235. return 0;
  236. }
  237. char minfds_record_add(minfds_t* p_minifds, key_t key, uint8_t* data, length_t length)
  238. {
  239. uint32_t sizewords;
  240. uint32_t rev;
  241. uint32_t hendaddress = p_minifds->start_addr;
  242. DEBUG_LOG_MINIFDS("minfds_record_add : \r\n");
  243. printctb(p_minifds);
  244. DEBUG_LOG_MINIFDS(" Key = %d length = %d , 4klen:%d\r\n", key, length, ALIGN_WORD(length + sizeof(minfds_record_t)));
  245. if (length > MAX_RECORD_LENGTH)
  246. {
  247. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH length=%d\r\n", length);
  248. return MINIFDS_ERR_REPEAT_LENGTH;
  249. }
  250. if (key == 0)
  251. {
  252. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  253. return MINIFDS_ERR_REPEAT_KEY;
  254. }
  255. if (p_minifds->free_capacity < ALIGN_WORD(length + sizeof(minfds_record_t)))
  256. {
  257. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FREE_CAPACITY\r\n");
  258. return MINIFDS_ERR_NOT_FREE_CAPACITY;
  259. }
  260. if (p_minifds->free_capacity - p_minifds->duty_capacity < ALIGN_WORD(length + sizeof(minfds_record_t)))
  261. {
  262. DEBUG_LOG_MINIFDS(" minfds_record_add minfds_gc(p_minifds); %d,%d \r\n", p_minifds->duty_capacity, p_minifds->free_capacity);
  263. rev = minfds_gc(p_minifds);
  264. if (rev != 0)
  265. {
  266. DEBUG_LOG_MINIFDS(" minfds_record_add minfds_gc ERROR ; %d,%d \r\n", rev, p_minifds->free_capacity);
  267. return rev;
  268. }
  269. }
  270. while (1)
  271. {
  272. if ((*(uint32_t*)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  273. {
  274. minfds_record_t hend;
  275. hend.record_key = key;
  276. hend.length = length;
  277. hend.crc8 = 0;
  278. for (int i = 0; i < length; i++)
  279. {
  280. hend.crc8 += data[i];
  281. }
  282. sizewords = length;
  283. if (sizewords % 4 != 0)
  284. {
  285. sizewords = sizewords / 4 + 1;
  286. }
  287. else
  288. {
  289. sizewords = sizewords / 4;
  290. }
  291. 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);
  292. p_minifds->free_capacity -= ALIGN_WORD(length + sizeof(minfds_record_t));
  293. for (int a = 0; a < ALIGN_WORD(hend.length + sizeof(minfds_record_t)); a++)
  294. {
  295. if (*(uint8_t*)(hendaddress + a) != 0xff)
  296. {
  297. DEBUG_LOG_MINIFDS("flash data :\r\n");
  298. print_record((uint8_t*)(hendaddress + sizeof(minfds_record_t)), sizewords * 4);
  299. DEBUG_LOG_MINIFDS("Ram data :\r\n");
  300. print_record(data, sizewords * 4);
  301. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE NOT 0xFF\r\n");
  302. return MINIFDS_ERR_HARDWARE;
  303. }
  304. }
  305. rev = p_minifds->api.flash_write((uint32_t*)hendaddress, (uint32_t*)&hend, sizeof(minfds_record_t) / 4);
  306. if (rev != FLASH_OP_SUCCESS)
  307. {
  308. printctb(p_minifds);
  309. 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);
  310. return MINIFDS_ERR_API_FAIL;
  311. }
  312. if (sizewords != 0)
  313. {
  314. rev = p_minifds->api.flash_write((uint32_t*)(hendaddress + sizeof(minfds_record_t)), (uint32_t*)data, sizewords);
  315. }
  316. if (rev == FLASH_OP_SUCCESS)
  317. {
  318. p_minifds->record_count++;
  319. if (checkcec(hendaddress) != 1)
  320. {
  321. DEBUG_LOG_MINIFDS("flash data :\r\n");
  322. print_record((uint8_t*)(hendaddress + sizeof(minfds_record_t)), sizewords * 4);
  323. DEBUG_LOG_MINIFDS("Ram data :\r\n");
  324. print_record(data, sizewords * 4);
  325. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE CRC\r\n");
  326. return MINIFDS_ERR_HARDWARE;
  327. }
  328. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS\r\n");
  329. return MINIFDS_SUCCESS;
  330. }
  331. else
  332. {
  333. 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);
  334. return MINIFDS_ERR_API_FAIL;
  335. }
  336. }
  337. else
  338. {
  339. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  340. if (recordhend->record_key == key)
  341. {
  342. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY\r\n");
  343. return MINIFDS_ERR_REPEAT_KEY;
  344. }
  345. if (!checkcec(hendaddress))
  346. {
  347. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE CRC add 0x%x\r\n", hendaddress);
  348. return MINIFDS_ERR_HARDWARE;
  349. }
  350. hendaddress = jump(hendaddress);
  351. if (hendaddress > p_minifds->end_addr)
  352. {
  353. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 426 0x%x\r\n", hendaddress);
  354. return MINIFDS_ERR_HARDWARE;
  355. }
  356. }
  357. }
  358. }
  359. char minfds_record_find(minfds_t* p_minifds, key_t key, uint8_t* data, length_t* length)
  360. {
  361. uint32_t hendaddress = p_minifds->start_addr;
  362. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  363. DEBUG_LOG_MINIFDS("minfds_record_find : \r\n");
  364. DEBUG_LOG_MINIFDS(" Key = %d \r\n", key);
  365. if (key == 0)
  366. {
  367. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  368. return MINIFDS_ERR_REPEAT_KEY;
  369. }
  370. if (*length == 0)
  371. {
  372. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH *length=0\r\n");
  373. return MINIFDS_ERR_REPEAT_LENGTH;
  374. }
  375. while (1)
  376. {
  377. if ((*(uint32_t*)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  378. {
  379. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  380. return MINIFDS_ERR_NOT_FIND_RECORD;
  381. }
  382. else if (recordhend->record_key == key)
  383. {
  384. if (*length < recordhend->length)
  385. {
  386. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH *length=%d\r\n", *length);
  387. return MINIFDS_ERR_REPEAT_LENGTH;
  388. }
  389. memcpy(data, (uint8_t*)(hendaddress + sizeof(minfds_record_t)), recordhend->length);
  390. *length = recordhend->length;
  391. DEBUG_LOG_MINIFDS(" length = %d \r\n", recordhend->length);
  392. return MINIFDS_SUCCESS;
  393. }
  394. else
  395. {
  396. hendaddress = jump(hendaddress);
  397. if (hendaddress > p_minifds->end_addr)
  398. {
  399. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  400. return MINIFDS_ERR_HARDWARE;
  401. }
  402. recordhend = (minfds_record_t*)hendaddress;
  403. }
  404. }
  405. }
  406. char get_record_hendaddr(minfds_t* p_minifds, key_t key, uint32_t* addr)
  407. {
  408. uint32_t hendaddress = p_minifds->start_addr;
  409. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  410. DEBUG_LOG_MINIFDS("minfds_record_get_length : \r\n");
  411. DEBUG_LOG_MINIFDS(" Key = %d \r\n", key);
  412. if (key == 0)
  413. {
  414. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  415. return MINIFDS_ERR_REPEAT_KEY;
  416. }
  417. while (1)
  418. {
  419. if ((*(uint32_t*)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  420. {
  421. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  422. return MINIFDS_ERR_NOT_FIND_RECORD;
  423. }
  424. else if (recordhend->record_key == key)
  425. {
  426. *addr = (uint32_t)recordhend;
  427. DEBUG_LOG_MINIFDS(" addr = %x \r\n", *addr);
  428. return MINIFDS_SUCCESS;
  429. }
  430. else
  431. {
  432. hendaddress = jump(hendaddress);
  433. if (hendaddress > p_minifds->end_addr)
  434. {
  435. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  436. return MINIFDS_ERR_HARDWARE;
  437. }
  438. recordhend = (minfds_record_t*)hendaddress;
  439. }
  440. }
  441. }
  442. char minfds_record_get_length(minfds_t* p_minifds, key_t key, length_t* length)
  443. {
  444. char rev = 0;
  445. uint32_t addr_hend = 0;
  446. minfds_record_t* recordhend = NULL;
  447. rev = get_record_hendaddr(p_minifds, key, &addr_hend);
  448. if (rev != MINIFDS_SUCCESS)return rev;
  449. recordhend = (minfds_record_t*)addr_hend;
  450. *length = recordhend->length;
  451. return MINIFDS_SUCCESS;
  452. }
  453. char minfds_record_find_mem(minfds_t* p_minifds, uint8_t* mem, int mem_len, key_t* key)
  454. {
  455. uint32_t hendaddress = p_minifds->start_addr;
  456. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  457. DEBUG_LOG_MINIFDS("minfds_record_find_mem : \r\n");
  458. DEBUG_LOG_MINIFDS(" mem_len = %d \r\n", mem_len);
  459. DEBUG_LOG_MINIFDS(" mem_len254 = %d \r\n", 100);
  460. DEBUG_LOG_MINIFDS(" [%s] \r\n", mem);
  461. if ((mem_len == 0) || (mem == NULL))
  462. {
  463. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  464. return MINIFDS_ERR_REPEAT_KEY;
  465. }
  466. while (1)
  467. {
  468. if ((*(uint32_t*)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  469. {
  470. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  471. return MINIFDS_ERR_NOT_FIND_RECORD;
  472. }
  473. else if (recordhend->record_key != 0)
  474. {
  475. if (recordhend->length >= mem_len)
  476. {
  477. for (int i = 0;i < recordhend->length - mem_len;i++)
  478. {
  479. if (memcmp(mem, (uint8_t*)(hendaddress + i + sizeof(minfds_record_t)), mem_len) == 0)
  480. {
  481. *key = recordhend->record_key;
  482. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS -----\r\n");
  483. return MINIFDS_SUCCESS;
  484. }
  485. }
  486. }
  487. hendaddress = jump(hendaddress);
  488. if (hendaddress > p_minifds->end_addr)
  489. {
  490. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  491. return MINIFDS_ERR_HARDWARE;
  492. }
  493. recordhend = (minfds_record_t*)hendaddress;
  494. }
  495. else
  496. {
  497. hendaddress = jump(hendaddress);
  498. if (hendaddress > p_minifds->end_addr)
  499. {
  500. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  501. return MINIFDS_ERR_HARDWARE;
  502. }
  503. recordhend = (minfds_record_t*)hendaddress;
  504. }
  505. }
  506. }
  507. char minfds_record_update(minfds_t* p_minifds, key_t key, uint8_t* data, length_t length)
  508. {
  509. uint32_t hendaddr = 0;
  510. char rev = 0;
  511. if (key == 0)
  512. {
  513. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  514. return MINIFDS_ERR_REPEAT_KEY;
  515. }
  516. DEBUG_LOG_MINIFDS("minfds_record_update key=%d\r\n", key);
  517. rev = get_record_hendaddr(p_minifds, key, &hendaddr);
  518. if (rev == MINIFDS_SUCCESS)
  519. {
  520. minfds_record_t* recordhend = (minfds_record_t*)hendaddr;
  521. if (recordhend->length == length)
  522. {
  523. if (memcmp((uint8_t*)(hendaddr + sizeof(minfds_record_t)), data, length) == 0)
  524. {
  525. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS NOT NEED UPDATE\r\n");
  526. return MINIFDS_SUCCESS;
  527. }
  528. }
  529. }
  530. rev = minfds_record_delete(p_minifds, key);
  531. if (rev != 0)
  532. {
  533. return rev;
  534. }
  535. return minfds_record_add(p_minifds, key, data, length);
  536. }
  537. char minfds_record_delete(minfds_t* p_minifds, key_t key)
  538. {
  539. uint32_t hendaddress = p_minifds->start_addr;
  540. flash_OPER_Result rev;
  541. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  542. DEBUG_LOG_MINIFDS("minfds_record_delete : key %d ", key);
  543. if (key == 0)
  544. {
  545. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY key=0\r\n");
  546. return MINIFDS_ERR_REPEAT_KEY;
  547. }
  548. while (1)
  549. {
  550. if ((*(uint32_t*)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  551. {
  552. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD record_count %d\r\n", p_minifds->record_count);
  553. return MINIFDS_ERR_NOT_FIND_RECORD;
  554. }
  555. if (recordhend->record_key == key)
  556. {
  557. minfds_record_t temp;
  558. memcpy(&temp, recordhend, sizeof(minfds_record_t));
  559. temp.record_key = 0x00;
  560. p_minifds->duty_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  561. p_minifds->free_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  562. rev = p_minifds->api.flash_write((uint32_t*)hendaddress, (uint32_t*)&temp, sizeof(minfds_record_t) / 4);
  563. if (rev == FLASH_OP_SUCCESS)
  564. {
  565. if (memcmp(&temp, (uint32_t*)hendaddress, sizeof(minfds_record_t)) != 0)
  566. {
  567. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL1\r\n");
  568. return MINIFDS_ERR_API_FAIL;
  569. }
  570. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS\r\n");
  571. p_minifds->record_count--;
  572. return MINIFDS_SUCCESS;
  573. }
  574. else
  575. {
  576. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL2\r\n");
  577. return MINIFDS_ERR_API_FAIL;
  578. }
  579. }
  580. else
  581. {
  582. hendaddress = jump(hendaddress);
  583. if (hendaddress > p_minifds->end_addr)
  584. {
  585. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 0x%x\r\n", hendaddress);
  586. return MINIFDS_ERR_HARDWARE;
  587. }
  588. recordhend = (minfds_record_t*)hendaddress;
  589. }
  590. }
  591. }
  592. char minfds_record_delete_all(minfds_t* p_minifds)
  593. {
  594. uint32_t hendaddress = p_minifds->start_addr;
  595. uint32_t rev;
  596. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  597. DEBUG_LOG_MINIFDS("minfds_record_delete_all : ");
  598. while (1)
  599. {
  600. if ((*(uint32_t*)hendaddress == 0xffffffff) || (uint32_t)hendaddress == p_minifds->end_addr)
  601. {
  602. return MINIFDS_SUCCESS;
  603. }
  604. if (recordhend->record_key != 0)
  605. {
  606. minfds_record_t temp;
  607. memcpy(&temp, recordhend, sizeof(minfds_record_t));
  608. temp.record_key = 0x00;
  609. p_minifds->duty_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  610. p_minifds->free_capacity += ALIGN_WORD(temp.length + sizeof(minfds_record_t));
  611. DEBUG_LOG_MINIFDS(" delete record_key : %d\r\n", recordhend->record_key);
  612. rev = p_minifds->api.flash_write((uint32_t*)hendaddress, (uint32_t*)&temp, sizeof(minfds_record_t) / 4);
  613. if (rev == FLASH_OP_SUCCESS)
  614. {
  615. p_minifds->record_count--;
  616. }
  617. else
  618. {
  619. DEBUG_LOG_MINIFDS("MINIFDS_ERR_API_FAIL\r\n");
  620. return MINIFDS_ERR_API_FAIL;
  621. }
  622. }
  623. else
  624. {
  625. hendaddress = jump(hendaddress);
  626. if (hendaddress > p_minifds->end_addr)
  627. {
  628. DEBUG_LOG_MINIFDS(" MINIFDS_ERR_HARDWARE 0x%x\r\n", hendaddress);
  629. return MINIFDS_ERR_HARDWARE;
  630. }
  631. recordhend = (minfds_record_t*)hendaddress;
  632. }
  633. }
  634. }
  635. char minfds_format(minfds_t* p_minifds)
  636. {
  637. for (int i = 0; i < (p_minifds->end_addr - p_minifds->start_addr) / 4096; i++)
  638. {
  639. p_minifds->api.flash_page_erase(p_minifds->start_addr + i * 4096);
  640. }
  641. p_minifds->duty_capacity = 0;
  642. p_minifds->record_count = 0;
  643. p_minifds->free_capacity = p_minifds->end_addr - p_minifds->start_addr;
  644. return MINIFDS_SUCCESS;
  645. }
  646. char minfds_init(minfds_t* p_minifds)
  647. {
  648. DEBUG_LOG_MINIFDS("----------------------------------\r\n");
  649. DEBUG_LOG_MINIFDS(" Minfds Start_addr : 0x%x\r\n", p_minifds->start_addr);
  650. DEBUG_LOG_MINIFDS(" Minfds End_addr : 0x%x \r\n", p_minifds->end_addr);
  651. minfds_Scan(p_minifds);
  652. printctb(p_minifds);
  653. DEBUG_LOG_MINIFDS("----------------------------------\r\n");
  654. return 0;
  655. }
  656. char record_index(minfds_t* p_minifds, minfds_record_t* record, uint8_t* data, int index)
  657. {
  658. uint32_t hendaddress = p_minifds->start_addr;
  659. int i = 0;
  660. minfds_record_t* recordhend = (minfds_record_t*)hendaddress;
  661. DEBUG_LOG_MINIFDS("record_index : \r\n");
  662. DEBUG_LOG_MINIFDS(" index = %d \r\n", index);
  663. if ((index >= p_minifds->record_count) || (record == NULL) || (data == NULL))
  664. {
  665. // DEBUG_LOG_MINIFDS("MINIFDS_ERR_PARAMETER \r\n");
  666. return MINIFDS_ERR_PARAMETER;
  667. }
  668. while (1)
  669. {
  670. if ((*(uint32_t*)hendaddress == 0xffffffff) || (hendaddress == p_minifds->end_addr))
  671. {
  672. DEBUG_LOG_MINIFDS("MINIFDS_ERR_NOT_FIND_RECORD \r\n");
  673. return MINIFDS_ERR_NOT_FIND_RECORD;
  674. }
  675. else if (recordhend->record_key != 0)
  676. {
  677. if (i == index)
  678. {
  679. data = (uint8_t*)(hendaddress + sizeof(minfds_record_t));
  680. record = recordhend;
  681. DEBUG_LOG_MINIFDS("MINIFDS_SUCCESS \r\n");
  682. return MINIFDS_SUCCESS;
  683. }
  684. i++;
  685. }
  686. else
  687. {
  688. hendaddress = jump(hendaddress);
  689. if (hendaddress > p_minifds->end_addr)
  690. {
  691. DEBUG_LOG_MINIFDS("MINIFDS_ERR_HARDWARE \r\n");
  692. return MINIFDS_ERR_HARDWARE;
  693. }
  694. recordhend = (minfds_record_t*)hendaddress;
  695. }
  696. }
  697. }
  698. char pm_add(minfds_t* p_minifds, const char* key, int key_len, unsigned char* data, length_t length)
  699. {
  700. unsigned char buff[MAX_RECORD_LENGTH];
  701. key_t TEMP_KEY = 1;
  702. char rev = 0;
  703. if (key_len + length + 1 >= MAX_RECORD_LENGTH)
  704. {
  705. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH \r\n");
  706. return MINIFDS_ERR_REPEAT_LENGTH;
  707. }
  708. memcpy(buff, key, key_len);
  709. buff[key_len] = ':';
  710. rev = minfds_record_find_mem(p_minifds, buff, key_len + 1, &TEMP_KEY);
  711. if (rev == MINIFDS_SUCCESS)
  712. {
  713. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_KEY \r\n");
  714. return MINIFDS_ERR_REPEAT_KEY;
  715. }
  716. memcpy(&buff[key_len + 1], data, length);
  717. while (1)
  718. {
  719. rev = minfds_record_add(p_minifds, TEMP_KEY, buff, key_len + length + 1);
  720. if (rev != MINIFDS_ERR_REPEAT_KEY)
  721. {
  722. return rev;
  723. }
  724. TEMP_KEY++;
  725. if (TEMP_KEY == 0)
  726. {
  727. return MINIFDS_ERR_REPEAT_KEY;
  728. }
  729. }
  730. }
  731. char pm_find(minfds_t* p_minifds, const char* key, int key_len, uint8_t* data, length_t* length)
  732. {
  733. key_t TEMP_KEY = 1;
  734. char rev = 0;
  735. length_t temp_len = MAX_RECORD_LENGTH;
  736. uint8_t buff[MAX_RECORD_LENGTH];
  737. memcpy(buff, key, key_len);
  738. buff[key_len] = ':';
  739. rev = minfds_record_find_mem(p_minifds, buff, key_len + 1, &TEMP_KEY);
  740. if (rev != MINIFDS_SUCCESS)return rev;
  741. rev = minfds_record_find(p_minifds, TEMP_KEY, buff, &temp_len);
  742. if (rev != MINIFDS_SUCCESS)return rev;
  743. memcpy(data, &buff[key_len + 1], temp_len - key_len - 1);
  744. *length = temp_len - key_len - 1;
  745. return MINIFDS_SUCCESS;
  746. }
  747. char pm_get_length(minfds_t* p_minifds, const char* key, int key_len, length_t* length)
  748. {
  749. key_t TEMP_KEY = 1;
  750. char rev = 0;
  751. length_t temp_len = 0;
  752. uint8_t buff[MAX_RECORD_LENGTH];
  753. memcpy(buff, key, key_len);
  754. buff[key_len] = ':';
  755. rev = minfds_record_find_mem(p_minifds, buff, key_len + 1, &TEMP_KEY);
  756. if (rev != MINIFDS_SUCCESS)return rev;
  757. rev = minfds_record_get_length(p_minifds, TEMP_KEY, &temp_len);
  758. *length = temp_len - key_len - 1;
  759. return rev;
  760. }
  761. char pm_delete(minfds_t* p_minifds, const char* key, int key_len)
  762. {
  763. key_t TEMP_KEY = 1;
  764. char rev = 0;
  765. uint8_t buff[MAX_RECORD_LENGTH];
  766. memcpy(buff, key, key_len);
  767. buff[key_len] = ':';
  768. rev = minfds_record_find_mem(p_minifds, buff, key_len + 1, &TEMP_KEY);
  769. if (rev != MINIFDS_SUCCESS)return rev;
  770. rev = minfds_record_delete(p_minifds, TEMP_KEY);
  771. if (rev != MINIFDS_SUCCESS)return rev;
  772. return MINIFDS_SUCCESS;
  773. }
  774. char pm_update(minfds_t* p_minifds, const char* key, int key_len, uint8_t* data, length_t length)
  775. {
  776. key_t TEMP_KEY = 1;
  777. char rev = 0;
  778. uint8_t buff[MAX_RECORD_LENGTH];
  779. printctb(p_minifds);
  780. if (key_len + length + 1 >= MAX_RECORD_LENGTH)
  781. {
  782. DEBUG_LOG_MINIFDS("MINIFDS_ERR_REPEAT_LENGTH \r\n");
  783. return MINIFDS_ERR_REPEAT_LENGTH;
  784. }
  785. memcpy(buff, key, key_len);
  786. buff[key_len] = ':';
  787. memcpy(&buff[key_len + 1], data, length);
  788. rev = minfds_record_find_mem(p_minifds, buff, key_len + 1, &TEMP_KEY);
  789. if (rev != MINIFDS_SUCCESS)return rev;
  790. return minfds_record_update(p_minifds, TEMP_KEY, buff, key_len + length + 1);
  791. }
  792. //-----------------------------------------------------------------------------------------------------
  793. /*
  794. extern minfds_t minifds;
  795. void pma_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  796. {
  797. char keu[]="key1";
  798. uint8_t datat[]="testdata";
  799. pm_add(&minifds,keu,sizeof("key1"),datat,sizeof("testdata"));
  800. minfds_Scan(&minifds);
  801. }
  802. CLI_CMD_REGISTER(pma, "Cli cmd history", pma_pcs);
  803. void pmf_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  804. {
  805. char keu[]="key1";
  806. uint8_t datat[50]="11111111111";
  807. uint8_t length=0;
  808. pm_find(&minifds,keu,sizeof("key1"),datat,&length);
  809. DEBUG_LOG_MINIFDS("%s \r\n",datat);
  810. }
  811. CLI_CMD_REGISTER(pmf, "Cli cmd history", pmf_pcs);
  812. void pmd_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  813. {
  814. char keu[]="key1";
  815. uint8_t datat[50]="11111111111";
  816. pm_delete(&minifds,keu,sizeof("key1"));
  817. DEBUG_LOG_MINIFDS("%s \r\n",datat);
  818. }
  819. CLI_CMD_REGISTER(pmd, "Cli cmd history", pmd_pcs);
  820. void pmu_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  821. {
  822. char keu[]="key1";
  823. uint8_t datat[50]="11111111111";
  824. pm_update(&minifds,keu,sizeof("key1"),datat,10);
  825. DEBUG_LOG_MINIFDS("%s \r\n",datat);
  826. }
  827. CLI_CMD_REGISTER(pmu, "Cli cmd history", pmu_pcs);
  828. */