Demo project details

Function code description

The demo project mainly demonstrates the process of FlashDB from initialization to running the example.

main.c

In the demo project, the main function in main.c is the entry function. This function is divided into two sections, which initialize a KVDB and TSDB objects respectively, and then execute the corresponding example functions. The general content is as follows:

  1. #ifdef FDB_USING_KVDB
  2. { /* KVDB Sample */
  3. struct fdb_default_kv default_kv;
  4. default_kv.kvs = default_kv_table;
  5. default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
  6. /* set the lock and unlock function if you want */
  7. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
  8. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
  9. /* Key-Value database initialization
  10. *
  11. * &kvdb: database object
  12. * "env": database name
  13. * "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
  14. * Please change to YOUR partition name.
  15. * &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully.
  16. * NULL: The user data if you need, now is empty.
  17. */
  18. result = fdb_kvdb_init(&kvdb, "env", "fdb_kvdb1", &default_kv, NULL);
  19. if (result != FDB_NO_ERR) {
  20. return -1;
  21. }
  22. /* run basic KV samples */
  23. kvdb_basic_sample(&kvdb);
  24. /* run string KV samples */
  25. kvdb_type_string_sample(&kvdb);
  26. /* run blob KV samples */
  27. kvdb_type_blob_sample(&kvdb);
  28. }
  29. #endif /* FDB_USING_KVDB */
  30. #ifdef FDB_USING_TSDB
  31. { /* TSDB Sample */
  32. /* set the lock and unlock function if you want */
  33. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, lock);
  34. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
  35. /* Time series database initialization
  36. *
  37. * &tsdb: database object
  38. * "log": database name
  39. * "fdb_tsdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
  40. * Please change to YOUR partition name.
  41. * get_time: The get current timestamp function.
  42. * 128: maximum length of each log
  43. * NULL: The user data if you need, now is empty.
  44. */
  45. result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", get_time, 128, NULL);
  46. /* read last saved time for simulated timestamp */
  47. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
  48. if (result != FDB_NO_ERR) {
  49. return -1;
  50. }
  51. /* run TSDB sample */
  52. tsdb_sample(&tsdb);
  53. }
  54. #endif /* FDB_USING_TSDB */

Set lock and unlock

Before initializing KVDB and TSDB, it is usually necessary to set the lock callback and unlock callback through the control function:

  • KVDB:
    • fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
    • fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
  • TSDB:
    • fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, lock);
    • fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);

For bare metal platforms, the lock and unlock callbacks are usually set to close interrupt and open interrupt functions. The RTOS platform generally uses mutex mutex lock or binary semaphore take and release actions as locking and unlocking methods.

timestamp simulation

For TSDB, the timestamp in the normal project should be obtained through RTC or network clock, but here to enhance the versatility of the demonstration project, use fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts); to get the last used timestamp of TSDB, Deposit in counts. Every time you use get_time to get the current time, it will add one to counts to simulate the action of moving forward in time and avoid repetition.

Therefore, the time stamp simulated by this method does not have the meaning of real-time time, just to make the time stamp inserted in each record not repeated.

Example

This article mainly explains initialization related code functions. For more detailed explanations of example functions, please read the corresponding detailed explanations of the examples.

sample function description detailed
kvdb_basic_sample(&kvdb) KVDB basic example click to view
kvdb_type_string_sample(&kvdb) KV example of string type click to view
kvdb_type_blob_sample(&kvdb) Blob type KV sample click to view
tsdb_sample(&tsdb) TSDB basic example click to view

First run log

The log will be explained in sections below.

FAL initialization

Flash device information and partition table information will be printed when FAL is initialized.

  1. [D/FAL] (fal_flash_init:65) Flash device | stm32_onchip | addr: 0x08000000 | len: 0x00040000 | blk_size: 0x00000800 |initialized finish.
  2. [I/FAL] ==================== FAL partition table ====================
  3. [I/FAL] | name | flash_dev | offset | length |
  4. [I/FAL] -------------------------------------------------------------
  5. [I/FAL] | fdb_tsdb1 | stm32_onchip | 0x0001a000 | 0x00002000 |
  6. [I/FAL] | fdb_kvdb1 | stm32_onchip | 0x0001c000 | 0x00004000 |
  7. [I/FAL] =============================================================
  8. [I/FAL] Flash Abstraction Layer (V0.5.0) initialize success.

KVDB initialization

Each time KVDB is initialized, it will check whether the sector header information is correct (some attribute information is stored in the sector header). If it is not correct, the sector will be formatted automatically.

When the Flash is used for the first time, it usually needs to be formatted, so the log at the first initialization will contain formatted information. After the format is successful, there is no need to format it again for each subsequent initialization.

  1. [FlashDB][kv][env] (src/fdb_kvdb.c:1599) KVDB in partition fdb_kvdb1, size is 16384 bytes.
  2. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00000000).
  3. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00000800).
  4. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00001000).
  5. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00001800).
  6. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00002000).
  7. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00002800).
  8. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00003000).
  9. [FlashDB][kv][env] Sector header info is incorrect. Auto format this sector (0x00003800).
  10. [FlashDB][kv][env] All sector header is incorrect. Set it to default.
  11. [FlashDB] FlashDB V1.0.0 beta is initialize success.
  12. [FlashDB] You can get the latest version on https://github.com/armink/FlashDB .

TSDB initialization

Similar to KVDB, when TSDB is initialized for the first time, it will automatically perform formatting.

  1. [FlashDB][tsl][log] Sector (0x00000000) header info is incorrect.
  2. [FlashDB][tsl][log] All sector format finished.
  3. [FlashDB][tsl][log] (src/fdb_tsdb.c:759) TSDB (log) oldest sectors is 0x00000000, current using sector is 0x00000000.

Run the example

For detailed log explanation, please read Sample Document

  1. [FlashDB][sample][kvdb][basic] ==================== kvdb_basic_sample ====================
  2. [FlashDB][sample][kvdb][basic] get the 'boot_count' value is 0
  3. [FlashDB][sample][kvdb][basic] set the 'boot_count' value to 1
  4. [FlashDB][sample][kvdb][basic] ===========================================================
  5. [FlashDB][sample][kvdb][string] ==================== kvdb_type_string_sample ====================
  6. [FlashDB][sample][kvdb][string] create the 'temp' string KV, value is: 36C
  7. [FlashDB][sample][kvdb][string] get the 'temp' value is: 36C
  8. [FlashDB][sample][kvdb][string] set 'temp' value to 38C
  9. [FlashDB][sample][kvdb][string] delete the 'temp' finish
  10. [FlashDB][sample][kvdb][string] ===========================================================
  11. [FlashDB][sample][kvdb][blob] ==================== kvdb_type_blob_sample ====================
  12. [FlashDB][sample][kvdb][blob] create the 'temp' blob KV, value is: 36
  13. [FlashDB][sample][kvdb][blob] get the 'temp' value is: 36
  14. [FlashDB][sample][kvdb][blob] set 'temp' value to 38
  15. [FlashDB][sample][kvdb][blob] delete the 'temp' finish
  16. [FlashDB][sample][kvdb][blob] ===========================================================
  17. Omit TSDB initialization log...
  18. [FlashDB][sample][tsdb] ==================== tsdb_sample ====================
  19. [FlashDB][sample][tsdb] append the new status.temp (36) and status.humi (85)
  20. [FlashDB][sample][tsdb] append the new status.temp (38) and status.humi (90)
  21. [FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 1, temp: 36, humi: 85
  22. [FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 2, temp: 38, humi: 90
  23. [FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 1, temp: 36, humi: 85
  24. [FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 2, temp: 38, humi: 90
  25. [FlashDB][sample][tsdb] query count is: 2
  26. [FlashDB][sample][tsdb] set the TSL (time 1) status from 2 to 3
  27. [FlashDB][sample][tsdb] set the TSL (time 2) status from 2 to 3
  28. [FlashDB][sample][tsdb] ===========================================================

Secondary run log

Here we mainly look at the initialization logs of KVDB and TSDB. Compared with the first initialization, it can be found that the initialization logs at the second startup will be relatively small, and the formatting process is mainly missing.

  1. [D/FAL] (fal_flash_init:65) Flash device | stm32_onchip | addr: 0x08000000 | len: 0x00040000 | blk_size: 0x00000800 |initialized finish.
  2. [I/FAL] ==================== FAL partition table ====================
  3. [I/FAL] | name | flash_dev | offset | length |
  4. [I/FAL] -------------------------------------------------------------
  5. [I/FAL] | fdb_tsdb1 | stm32_onchip | 0x0001a000 | 0x00002000 |
  6. [I/FAL] | fdb_kvdb1 | stm32_onchip | 0x0001c000 | 0x00004000 |
  7. [I/FAL] =============================================================
  8. [I/FAL] Flash Abstraction Layer (V0.5.0) initialize success.
  9. [FlashDB][kv][env] (D:/Program/STM32/FlashDB/src/fdb_kvdb.c:1599) KVDB in partition fdb_kvdb1, size is 16384 bytes.
  10. [FlashDB] FlashDB V1.0.0 beta is initialize success.
  11. [FlashDB] You can get the latest version on https://github.com/armink/FlashDB .
  12. Omit the sample run log...
  13. [FlashDB][tsl][log] (D:/Program/STM32/FlashDB/src/fdb_tsdb.c:759) TSDB (log) oldest sectors is 0x00000000, current using sector is 0x00000000.
  14. Omit the sample run log...