Sfoglia il codice sorgente

v0.0.6 Release.

v0.0.6 Release:
add TFDB_VALUE_AFTER_ERASE_SIZE option
smartmx 3 anni fa
parent
commit
4c881dcf5f
5 ha cambiato i file con 106 aggiunte e 15 eliminazioni
  1. 49 1
      README.md
  2. 2 1
      tfdb_port.c
  3. 13 3
      tfdb_port.h
  4. 39 8
      tinyflashdb.c
  5. 3 2
      tinyflashdb.h

+ 49 - 1
README.md

@@ -157,9 +157,14 @@ TFDB_Err_Code tfdb_port_write(tfdb_addr_t addr, const uint8_t *buf, size_t size)
 
 #define TFDB_DEBUG                          printf
 
-/* The data value in flash after erased, most are 0xff, some flash maybe different. */
+/* The data value in flash after erased, most are 0xff, some flash maybe different.
+ * if it's over 1 byte, please be care of little endian or big endian. */
 #define TFDB_VALUE_AFTER_ERASE              0xff
 
+/* The size of TFDB_VALUE_AFTER_ERASE, only support 1 / 2 / 4.
+ * This value must not bigger than TFDB_WRITE_UNIT_BYTES. */
+#define TFDB_VALUE_AFTER_ERASE_SIZE         1
+
 /* the flash write granularity, unit: byte
  * only support 1(stm32f4)/ 2(CH559)/ 4(stm32f1)/ 8(stm32L4) */
 #define TFDB_WRITE_UNIT_BYTES               8 /* @note you must define it for a value */
@@ -171,6 +176,49 @@ TFDB_Err_Code tfdb_port_write(tfdb_addr_t addr, const uint8_t *buf, size_t size)
 typedef uint32_t    tfdb_addr_t;
 ```
 
+## TFDB资源占用
+
+在去除DEBUG打印信息后,资源占用如下:
+
+### Cortex M4平台
+
+keil -o2编译优化选项
+
+```c
+      Code (inc. data)   RO Data    RW Data    ZI Data      Debug   Object Name
+
+       154          0          0          0          0       2621   tfdb_port.o
+       682          0          0          0          0       4595   tinyflashdb.o
+```
+
+### RISC-V平台
+
+gcc -os编译优化选项
+
+```c
+ .text.tfdb_port_read
+                0x00000000000039b4       0x1a ./Drivers/TFDB/tfdb_port.o
+                0x00000000000039b4                tfdb_port_read
+ .text.tfdb_port_erase
+                0x00000000000039ce       0x46 ./Drivers/TFDB/tfdb_port.o
+                0x00000000000039ce                tfdb_port_erase
+ .text.tfdb_port_write
+                0x0000000000003a14       0x5c ./Drivers/TFDB/tfdb_port.o
+                0x0000000000003a14                tfdb_port_write
+ .text.tfdb_check
+                0x0000000000003a70       0x56 ./Drivers/TFDB/tinyflashdb.o
+                0x0000000000003a70                tfdb_check
+ .text.tfdb_init
+                0x0000000000003ac6       0x56 ./Drivers/TFDB/tinyflashdb.o
+                0x0000000000003ac6                tfdb_init
+ .text.tfdb_set
+                0x0000000000003b1c      0x186 ./Drivers/TFDB/tinyflashdb.o
+                0x0000000000003b1c                tfdb_set
+ .text.tfdb_get
+                0x0000000000003ca2      0x11c ./Drivers/TFDB/tinyflashdb.o
+                0x0000000000003ca2                tfdb_get
+```
+
 ## Demo
 
 裸机移植例程,RT-Thread可以参考使用:  

+ 2 - 1
tfdb_port.c

@@ -20,13 +20,14 @@
  * SOFTWARE.
  *
  * This file is part of the Tiny Flash DataBase Library.
- * 
+ *
  * Change Logs:
  * Date           Author       Notes
  * 2022-02-03     smartmx      the first version
  * 2022-02-08     smartmx      fix bugs
  * 2022-02-12     smartmx      fix bugs, add support for 2 byte write flash
  * 2022-03-15     smartmx      fix bugs, add support for stm32l4 flash
+ * 2022-08-02     smartmx      add TFDB_VALUE_AFTER_ERASE_SIZE option
  *
  */
 #include "tfdb_port.h"

+ 13 - 3
tfdb_port.h

@@ -20,13 +20,14 @@
  * SOFTWARE.
  *
  * This file is part of the Tiny Flash DataBase Library.
- * 
+ *
  * Change Logs:
  * Date           Author       Notes
  * 2022-02-03     smartmx      the first version
  * 2022-02-08     smartmx      fix bugs
  * 2022-02-12     smartmx      fix bugs, add support for 2 byte write flash
  * 2022-03-15     smartmx      fix bugs, add support for stm32l4 flash
+ * 2022-08-02     smartmx      add TFDB_VALUE_AFTER_ERASE_SIZE option
  *
  */
 #ifndef _TFDB_PORT_H_
@@ -63,12 +64,21 @@ typedef enum
 
 #define TFDB_DEBUG                          printf
 
-/* The data value in flash after erased, most are 0xff, some flash maybe different. */
+/* The data value in flash after erased, most are 0xff, some flash maybe different.
+ * if it's over 1 byte, please be care of little endian or big endian. */
 #define TFDB_VALUE_AFTER_ERASE              0xff
 
+/* The size of value in flash after erased, only support 1/2/4.
+ * This value must not bigger than TFDB_WRITE_UNIT_BYTES. */
+#define TFDB_VALUE_AFTER_ERASE_SIZE         1
+
 /* the flash write granularity, unit: byte
  * only support 1(stm32f4)/ 2(CH559)/ 4(stm32f1)/ 8(stm32L4) */
-#define TFDB_WRITE_UNIT_BYTES               8 /* @note you must define it for a value */
+#define TFDB_WRITE_UNIT_BYTES               4 /* @note you must define it for a value */
+
+#if TFDB_VALUE_AFTER_ERASE_SIZE > TFDB_WRITE_UNIT_BYTES
+    #error "TFDB_VALUE_AFTER_ERASE_SIZE must not bigger than TFDB_WRITE_UNIT_BYTES."
+#endif
 
 /* @note the max retry times when flash is error ,set 0 will disable retry count */
 #define TFDB_WRITE_MAX_RETRY                32

+ 39 - 8
tinyflashdb.c

@@ -20,13 +20,14 @@
  * SOFTWARE.
  *
  * This file is part of the Tiny Flash DataBase Library.
- * 
+ *
  * Change Logs:
  * Date           Author       Notes
  * 2022-02-03     smartmx      the first version
  * 2022-02-08     smartmx      fix bugs
  * 2022-02-12     smartmx      fix bugs, add support for 2 byte write flash
  * 2022-03-15     smartmx      fix bugs, add support for stm32l4 flash
+ * 2022-08-02     smartmx      add TFDB_VALUE_AFTER_ERASE_SIZE option
  *
  */
 #include "tinyflashdb.h"
@@ -187,10 +188,25 @@ start:
                     TFDB_DEBUG("    read err\n");
                     goto end;
                 }
-                if ((rw_buffer[aligned_value_size - 1] == TFDB_VALUE_AFTER_ERASE))
+                if ((rw_buffer[aligned_value_size - 1] == (TFDB_VALUE_AFTER_ERASE & 0x000000ff)))
                 {
-                    /* find value addr success */
-                    break;
+#if (TFDB_VALUE_AFTER_ERASE_SIZE == 2)||(TFDB_VALUE_AFTER_ERASE_SIZE == 4)
+                    if ((rw_buffer[aligned_value_size - 2] == ((TFDB_VALUE_AFTER_ERASE>>8) & 0x000000ff)))
+                    {
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 2 */
+#if TFDB_VALUE_AFTER_ERASE_SIZE == 4
+                        if ((rw_buffer[aligned_value_size - 3] == ((TFDB_VALUE_AFTER_ERASE>>16) & 0x000000ff)) &&
+                                (rw_buffer[aligned_value_size - 4] == ((TFDB_VALUE_AFTER_ERASE>>24) & 0x000000ff)))
+                        {
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 4 */
+                            /* find value addr success */
+                            break;
+#if TFDB_VALUE_AFTER_ERASE_SIZE == 4
+                        }
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 4 */
+#if (TFDB_VALUE_AFTER_ERASE_SIZE == 2)||(TFDB_VALUE_AFTER_ERASE_SIZE == 4)
+                    }
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 2 */
                 }
                 else
                 {
@@ -366,10 +382,25 @@ start:
                     TFDB_DEBUG("    read err\n");
                     goto end;
                 }
-                if ((rw_buffer[aligned_value_size - 1] == TFDB_VALUE_AFTER_ERASE))
+                if ((rw_buffer[aligned_value_size - 1] == (TFDB_VALUE_AFTER_ERASE & 0x000000ff)))
                 {
-                    /* find value addr success */
-                    break;
+#if (TFDB_VALUE_AFTER_ERASE_SIZE == 2)||(TFDB_VALUE_AFTER_ERASE_SIZE == 4)
+                    if ((rw_buffer[aligned_value_size - 2] == ((TFDB_VALUE_AFTER_ERASE>>8) & 0x000000ff)))
+                    {
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 2 */
+#if TFDB_VALUE_AFTER_ERASE_SIZE == 4
+                        if ((rw_buffer[aligned_value_size - 3] == ((TFDB_VALUE_AFTER_ERASE>>16) & 0x000000ff)) &&
+                                (rw_buffer[aligned_value_size - 4] == ((TFDB_VALUE_AFTER_ERASE>>24) & 0x000000ff)))
+                        {
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 4 */
+                            /* find value addr success */
+                            break;
+#if TFDB_VALUE_AFTER_ERASE_SIZE == 4
+                        }
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 4 */
+#if (TFDB_VALUE_AFTER_ERASE_SIZE == 2)||(TFDB_VALUE_AFTER_ERASE_SIZE == 4)
+                    }
+#endif  /* TFDB_VALUE_AFTER_ERASE_SIZE == 2 */
                 }
                 else
                 {
@@ -380,7 +411,7 @@ start:
             find_addr = find_addr - aligned_value_size;
             if ((find_addr) <= (index->flash_size + index->flash_addr - (2 * aligned_value_size)))
             {
-                /* the flash block is not fill. And if it's fill the data in rw_buffer is what we need. */
+                /* the flash block is not fill. And if it's fill, the data in rw_buffer is what we need. */
                 result = tfdb_port_read(find_addr, rw_buffer, aligned_value_size);
                 if (result != TFDB_NO_ERR)
                 {

+ 3 - 2
tinyflashdb.h

@@ -20,13 +20,14 @@
  * SOFTWARE.
  *
  * This file is part of the Tiny Flash DataBase Library.
- * 
+ *
  * Change Logs:
  * Date           Author       Notes
  * 2022-02-03     smartmx      the first version
  * 2022-02-08     smartmx      fix bugs
  * 2022-02-12     smartmx      fix bugs, add support for 2 byte write flash
  * 2022-03-15     smartmx      fix bugs, add support for stm32l4 flash
+ * 2022-08-02     smartmx      add TFDB_VALUE_AFTER_ERASE_SIZE option
  *
  */
 #ifndef _TINY_FLASH_DB_H_
@@ -34,7 +35,7 @@
 
 #include "tfdb_port.h"
 
-#define TFDB_VERSION    "0.0.5"
+#define TFDB_VERSION    "0.0.6"
 
 typedef struct _tfdb_index_struct
 {