tinyflashdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2022, smartmx - smartmx@qq.com
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * This file is part of the Tiny Flash DataBase Library.
  23. *
  24. */
  25. #include "tinyflashdb.h"
  26. /**
  27. * check header in flash.
  28. *
  29. * @param index the data manage index.
  30. * @param rw_buffer buffer to store prepared read data or write data.
  31. *
  32. * @return TFDB_Err_Code
  33. */
  34. TFDB_Err_Code tfdb_check(const tfdb_index_t *index, uint8_t *rw_buffer)
  35. {
  36. TFDB_Err_Code result;
  37. TFDB_DEBUG("tfdb_check >\n");
  38. #if (TFDB_WRITE_UNIT_BYTES==8)
  39. /* flash_size / value_len / end_byte */
  40. result = tfdb_port_read(index->flash_addr, rw_buffer, 8);
  41. #else
  42. /* flash_size / value_len / end_byte */
  43. result = tfdb_port_read(index->flash_addr, rw_buffer, 4);
  44. #endif
  45. if (result != TFDB_NO_ERR)
  46. {
  47. //read err
  48. TFDB_DEBUG(" read err\n");
  49. goto end;
  50. }
  51. result = TFDB_HDR_ERR;
  52. /* compare flash_size */
  53. if ((rw_buffer[0] == ((index->flash_size >> 8) & 0xff)) && (rw_buffer[1] == ((index->flash_size) & 0xff)))
  54. {
  55. /* compare value_length and end_byte */
  56. if ((rw_buffer[2] == index->value_length) && (rw_buffer[3] == index->end_byte))
  57. {
  58. /* check hdr success */
  59. result = TFDB_NO_ERR;
  60. goto end;
  61. }
  62. }
  63. end:
  64. TFDB_DEBUG("tfdb_check:%d\n", result);
  65. return result;
  66. }
  67. /**
  68. * erase the flash block and init header in flash.
  69. *
  70. * @param index the data manage index.
  71. * @param rw_buffer buffer to store prepared read data or write data.
  72. *
  73. * @return TFDB_Err_Code
  74. */
  75. TFDB_Err_Code tfdb_init(const tfdb_index_t *index, uint8_t *rw_buffer)
  76. {
  77. TFDB_Err_Code result = TFDB_NO_ERR;
  78. TFDB_DEBUG("tfdb_init >\n");
  79. result = tfdb_port_erase(index->flash_addr, index->flash_size);
  80. if (result != TFDB_NO_ERR)
  81. {
  82. //erase err
  83. TFDB_DEBUG(" erase err\n");
  84. goto end;
  85. }
  86. rw_buffer[0] = ((index->flash_size >> 8) & 0xff);
  87. rw_buffer[1] = ((index->flash_size) & 0xff);
  88. rw_buffer[2] = index->value_length;
  89. rw_buffer[3] = index->end_byte;
  90. #if (TFDB_WRITE_UNIT_BYTES==8)
  91. rw_buffer[4] = index->end_byte;
  92. rw_buffer[5] = index->end_byte;
  93. rw_buffer[6] = index->end_byte;
  94. rw_buffer[7] = index->end_byte;
  95. /* flash_size / value_len / end_byte */
  96. result = tfdb_port_write(index->flash_addr, rw_buffer, 8);
  97. #else
  98. /* flash_size / value_len / end_byte */
  99. result = tfdb_port_write(index->flash_addr, rw_buffer, 4);
  100. #endif
  101. if (result != TFDB_NO_ERR)
  102. {
  103. //write err
  104. TFDB_DEBUG(" write err\n");
  105. goto end;
  106. }
  107. result = tfdb_check(index, rw_buffer);
  108. if (result != TFDB_NO_ERR)
  109. {
  110. TFDB_DEBUG(" flash ERR\n");
  111. result = TFDB_FLASH_ERR;
  112. goto end;
  113. }
  114. end:
  115. TFDB_DEBUG("tfdb_init:%d\n", result);
  116. return result;
  117. }
  118. /**
  119. * set data in flash and save the addr to addr_cache.
  120. *
  121. * @param index the data manage index.
  122. * @param rw_buffer buffer to store prepared read data or write data.
  123. * @param addr_cache the pointer to addr which is user offered, which will save read addr.
  124. * @param value_from the pointer to buffer which is user offered that need to save.
  125. *
  126. * @return TFDB_Err_Code
  127. */
  128. TFDB_Err_Code tfdb_set(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t *addr_cache, void *value_from)
  129. {
  130. TFDB_Err_Code result;
  131. tfdb_addr_t find_addr;
  132. uint8_t aligned_value_size;
  133. uint8_t sum_verify_byte;
  134. uint8_t i;
  135. #if TFDB_WRITE_MAX_RETRY
  136. uint32_t max_retry = 0;
  137. #endif
  138. TFDB_DEBUG("tfdb_set >\n");
  139. aligned_value_size = index->value_length + 2;/* data + verify + end_byte */
  140. #if (TFDB_WRITE_UNIT_BYTES==2)
  141. /* aligned with TFDB_WRITE_UNIT_BYTES */
  142. aligned_value_size = ((aligned_value_size + 1) & 0xfe);
  143. #elif (TFDB_WRITE_UNIT_BYTES==4)
  144. /* aligned with TFDB_WRITE_UNIT_BYTES */
  145. aligned_value_size = ((aligned_value_size + 3) & 0xfc);
  146. #elif (TFDB_WRITE_UNIT_BYTES==8)
  147. /* aligned with TFDB_WRITE_UNIT_BYTES */
  148. aligned_value_size = ((aligned_value_size + 7) & 0xf8);
  149. #endif
  150. TFDB_DEBUG("aigned size:%d\n", aligned_value_size);
  151. if (addr_cache == NULL)
  152. {
  153. start:
  154. /* addr_cache is not init. so check header first. */
  155. result = tfdb_check(index, rw_buffer);
  156. if (result == TFDB_NO_ERR)
  157. {
  158. /* the header is right. so start to find data location address in flash. */
  159. #if (TFDB_WRITE_UNIT_BYTES==8)
  160. find_addr = index->flash_addr + 8;
  161. #else
  162. find_addr = index->flash_addr + 4;
  163. #endif
  164. while ((find_addr) <= (index->flash_size + index->flash_addr - aligned_value_size))
  165. {
  166. /* start to find value */
  167. result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
  168. if (result != TFDB_NO_ERR)
  169. {
  170. TFDB_DEBUG(" read err\n");
  171. goto end;
  172. }
  173. if ((rw_buffer[aligned_value_size - 1] == TFDB_VALUE_AFTER_ERASE))
  174. {
  175. /* find value addr success */
  176. break;
  177. }
  178. else
  179. {
  180. /* some flash bits maybe bad, can't write. */
  181. find_addr += aligned_value_size;
  182. }
  183. }
  184. /* the flash block is fill */
  185. if ((find_addr) > (index->flash_size + index->flash_addr - aligned_value_size))
  186. {
  187. goto init;
  188. }
  189. /* find the addr success */
  190. TFDB_DEBUG(" find success\n");
  191. set:
  192. /* calculate sum verify */
  193. sum_verify_byte = 0;
  194. for (i = 0; i < index->value_length; i++)
  195. {
  196. sum_verify_byte = ((sum_verify_byte + ((uint8_t *)(value_from))[i]) & 0xff);
  197. }
  198. write:
  199. #if TFDB_WRITE_MAX_RETRY
  200. max_retry++;
  201. if (max_retry > TFDB_WRITE_MAX_RETRY)
  202. {
  203. result = TFDB_FLASH_ERR;
  204. goto end;
  205. }
  206. #endif
  207. tfdb_memcpy(rw_buffer, value_from, index->value_length);
  208. rw_buffer[index->value_length] = sum_verify_byte;
  209. for (i = index->value_length + 1; i < aligned_value_size; i++)
  210. {
  211. /* fill aligned data with end_byte */
  212. rw_buffer[i] = index->end_byte;
  213. }
  214. result = tfdb_port_write(find_addr, rw_buffer, aligned_value_size);
  215. if (result != TFDB_NO_ERR)
  216. {
  217. TFDB_DEBUG(" write err\n");
  218. goto end;
  219. }
  220. result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
  221. if (result != TFDB_NO_ERR)
  222. {
  223. TFDB_DEBUG(" read err\n");
  224. goto end;
  225. }
  226. if ((tfdb_memcmp(rw_buffer, value_from, index->value_length) != TFDB_MEMCMP_SAME) \
  227. || (rw_buffer[index->value_length] != sum_verify_byte)\
  228. || (rw_buffer[aligned_value_size - 1] != index->end_byte))
  229. {
  230. /* write verify failed, maybe the flash is error, try next address. */
  231. TFDB_DEBUG(" Write verify failed, try next address.\n");
  232. find_addr += aligned_value_size;
  233. if ((index->flash_size + index->flash_addr - find_addr) >= (aligned_value_size))
  234. {
  235. goto write;
  236. }
  237. else
  238. {
  239. /* the flash is fill */
  240. TFDB_DEBUG(" the flash is fill\n");
  241. goto init;
  242. }
  243. }
  244. else
  245. {
  246. /* write data to flash success */
  247. /* save addr to addr_cache */
  248. if (addr_cache != NULL)
  249. {
  250. *addr_cache = find_addr;
  251. }
  252. }
  253. }
  254. else if (result == TFDB_HDR_ERR)
  255. {
  256. TFDB_DEBUG(" header err\n");
  257. init:
  258. result = tfdb_init(index, rw_buffer);
  259. if (result == TFDB_NO_ERR)
  260. {
  261. #if (TFDB_WRITE_UNIT_BYTES==8)
  262. find_addr = index->flash_addr + 8;
  263. #else
  264. find_addr = index->flash_addr + 4;
  265. #endif
  266. goto set;
  267. }
  268. goto end;
  269. }
  270. }
  271. else if (*addr_cache == 0)
  272. {
  273. /* addr_cache is not set */
  274. goto start;
  275. }
  276. else
  277. {
  278. /* addr_cache is set */
  279. TFDB_DEBUG(" addr_cache is set\n");
  280. find_addr = *addr_cache + aligned_value_size;
  281. if (find_addr > (index->flash_addr + index->flash_size - aligned_value_size))
  282. {
  283. /* the flash is fill */
  284. TFDB_DEBUG(" the flash is fill\n");
  285. goto init;
  286. }
  287. else
  288. {
  289. goto set;
  290. }
  291. }
  292. end:
  293. TFDB_DEBUG("tfdb_set:%d\n", result);
  294. return result;
  295. }
  296. /**
  297. * get the data in flash and save the addr of data to addr_cache.
  298. *
  299. * @param index the data manage index.
  300. * @param rw_buffer buffer to store prepared read data or write data.
  301. * @param addr_cache the pointer to addr which is user offered.
  302. * @param value_to the pointer to buffer which is user offered to save data.
  303. *
  304. * @return TFDB_Err_Code
  305. */
  306. TFDB_Err_Code tfdb_get(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t *addr_cache, void *value_to)
  307. {
  308. TFDB_Err_Code result;
  309. tfdb_addr_t find_addr;
  310. uint8_t aligned_value_size;
  311. uint8_t sum_verify_byte;
  312. uint8_t i;
  313. TFDB_DEBUG("tfdb_get >\n");
  314. aligned_value_size = index->value_length + 2;/* data + verify + end_byte */
  315. #if (TFDB_WRITE_UNIT_BYTES==2)
  316. /* aligned with TFDB_WRITE_UNIT_BYTES */
  317. aligned_value_size = ((aligned_value_size + 1) & 0xfe);
  318. #elif (TFDB_WRITE_UNIT_BYTES==4)
  319. /* aligned with TFDB_WRITE_UNIT_BYTES */
  320. aligned_value_size = ((aligned_value_size + 3) & 0xfc);
  321. #elif (TFDB_WRITE_UNIT_BYTES==8)
  322. /* aligned with TFDB_WRITE_UNIT_BYTES */
  323. aligned_value_size = ((aligned_value_size + 7) & 0xf8);
  324. #endif
  325. TFDB_DEBUG("aigned size:%d\n", aligned_value_size);
  326. if (addr_cache == NULL)
  327. {
  328. start:
  329. /* addr_cache is not init. so check header first. */
  330. result = tfdb_check(index, rw_buffer);
  331. if (result == TFDB_NO_ERR)
  332. {
  333. /* the header is right. so start to find data location address in flash. */
  334. #if (TFDB_WRITE_UNIT_BYTES==8)
  335. find_addr = index->flash_addr + 8;
  336. #else
  337. find_addr = index->flash_addr + 4;
  338. #endif
  339. while ((find_addr) <= (index->flash_size + index->flash_addr - aligned_value_size))
  340. {
  341. /* start to find value */
  342. result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
  343. if (result != TFDB_NO_ERR)
  344. {
  345. TFDB_DEBUG(" read err\n");
  346. goto end;
  347. }
  348. if ((rw_buffer[aligned_value_size - 1] == TFDB_VALUE_AFTER_ERASE))
  349. {
  350. /* find value addr success */
  351. break;
  352. }
  353. else
  354. {
  355. /* some flash bits maybe bad, can't write. */
  356. find_addr += aligned_value_size;
  357. }
  358. }
  359. find_addr = find_addr - aligned_value_size;
  360. if ((find_addr) <= (index->flash_size + index->flash_addr - (2 * aligned_value_size)))
  361. {
  362. /* the flash block is not fill. And if it's fill, the data in rw_buffer is what we need. */
  363. result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
  364. if (result != TFDB_NO_ERR)
  365. {
  366. TFDB_DEBUG(" read err\n");
  367. goto end;
  368. }
  369. }
  370. verify:
  371. sum_verify_byte = 0;
  372. /* calculate sum verify */
  373. for (i = 0; i < index->value_length; i++)
  374. {
  375. sum_verify_byte = ((sum_verify_byte + rw_buffer[i]) & 0xff);
  376. }
  377. if ((sum_verify_byte != rw_buffer[index->value_length])\
  378. || (rw_buffer[aligned_value_size - 1] != index->end_byte))
  379. {
  380. /* not right data, maybe the flash is broken. */
  381. TFDB_DEBUG("verify err:%02x,%02x,end:%02x\n", sum_verify_byte, rw_buffer[index->value_length], rw_buffer[aligned_value_size - 1]);
  382. find_addr = find_addr - aligned_value_size;
  383. #if (TFDB_WRITE_UNIT_BYTES==8)
  384. if (find_addr >= (index->flash_addr + 8))
  385. #else
  386. if (find_addr >= (index->flash_addr + 4))
  387. #endif
  388. {
  389. result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
  390. if (result != TFDB_NO_ERR)
  391. {
  392. TFDB_DEBUG(" read err\n");
  393. goto end;
  394. }
  395. goto verify;
  396. }
  397. else
  398. {
  399. TFDB_DEBUG(" flash err\n");
  400. result = TFDB_FLASH_ERR;
  401. goto end;
  402. }
  403. }
  404. else
  405. {
  406. TFDB_DEBUG(" find success\n");
  407. result = TFDB_NO_ERR;
  408. tfdb_memcpy(value_to, rw_buffer, index->value_length);
  409. if (addr_cache != NULL)
  410. {
  411. *addr_cache = find_addr;
  412. }
  413. }
  414. }
  415. else
  416. {
  417. TFDB_DEBUG(" header err\n");
  418. result = TFDB_HDR_ERR;
  419. goto end;
  420. }
  421. }
  422. else if (*addr_cache == 0)
  423. {
  424. /* addr_cache is not set */
  425. goto start;
  426. }
  427. else
  428. {
  429. find_addr = *addr_cache;
  430. result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
  431. if (result != TFDB_NO_ERR)
  432. {
  433. TFDB_DEBUG(" read err\n");
  434. goto end;
  435. }
  436. goto verify;
  437. }
  438. end:
  439. TFDB_DEBUG("tfdb_get:%d\n", result);
  440. return result;
  441. }