This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 884ed2e08 spiflash: Add stats
884ed2e08 is described below

commit 884ed2e08660ce41f101b8be672fe3c69bc691a8
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Fri Jun 14 14:20:49 2024 +0200

    spiflash: Add stats
    
    This adds simple stats that can be use to evaluate
    spiflash usage.
    
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 .../flash/spiflash/include/spiflash/spiflash.h     | 26 ++++++++++++++++++
 hw/drivers/flash/spiflash/src/spiflash.c           | 31 +++++++++++++++++++++-
 hw/drivers/flash/spiflash/syscfg.yml               |  5 ++++
 3 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/hw/drivers/flash/spiflash/include/spiflash/spiflash.h 
b/hw/drivers/flash/spiflash/include/spiflash/spiflash.h
index f57c402fd..7e6298b48 100644
--- a/hw/drivers/flash/spiflash/include/spiflash/spiflash.h
+++ b/hw/drivers/flash/spiflash/include/spiflash/spiflash.h
@@ -29,6 +29,10 @@
 #include <bus/drivers/spi_common.h>
 #endif
 
+#if MYNEWT_VAL(SPIFLASH_STAT)
+#include "stats/stats.h"
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -51,6 +55,25 @@ struct spiflash_characteristics {
     struct spiflash_time_spec tbp1; /* Byte program time */
 };
 
+#if MYNEWT_VAL(SPIFLASH_STAT)
+STATS_SECT_START(spiflash_stats_section)
+    STATS_SECT_ENTRY(read_count)
+    STATS_SECT_ENTRY(write_count)
+    STATS_SECT_ENTRY(erase_count)
+    STATS_SECT_ENTRY(error_count)
+    STATS_SECT_ENTRY(read_bytes)
+    STATS_SECT_ENTRY(written_bytes)
+STATS_SECT_END
+
+#define SPIFLASH_STATS_INC STATS_INC
+#define SPIFLASH_STATS_INCN STATS_INCN
+
+#else
+
+#define SPIFLASH_STATS_INC(__sectvarname, __var)  do {} while (0)
+#define SPIFLASH_STATS_INCN(__sectvarname, __var, __n)  do {} while (0)
+#endif
+
 struct spiflash_dev {
     struct hal_flash hal;
 #if MYNEWT_VAL(BUS_DRIVER_PRESENT)
@@ -83,6 +106,9 @@ struct spiflash_dev {
     uint32_t cached_addr;
     uint8_t cache[MYNEWT_VAL(SPIFLASH_CACHE_SIZE)];
 #endif
+#if MYNEWT_VAL(SPIFLASH_STAT)
+    STATS_SECT_DECL(spiflash_stats_section) stats;
+#endif
 };
 
 extern struct spiflash_dev spiflash_dev;
diff --git a/hw/drivers/flash/spiflash/src/spiflash.c 
b/hw/drivers/flash/spiflash/src/spiflash.c
index 5ed115d24..f00d1c3a4 100644
--- a/hw/drivers/flash/spiflash/src/spiflash.c
+++ b/hw/drivers/flash/spiflash/src/spiflash.c
@@ -33,6 +33,17 @@
 
 #if MYNEWT_VAL(SPIFLASH)
 
+#if MYNEWT_VAL(SPIFLASH_STAT)
+STATS_NAME_START(spiflash_stats_section)
+    STATS_NAME(spiflash_stats_section, read_count)
+    STATS_NAME(spiflash_stats_section, write_count)
+    STATS_NAME(spiflash_stats_section, erase_count)
+    STATS_NAME(spiflash_stats_section, error_count)
+    STATS_NAME(spiflash_stats_section, read_bytes)
+    STATS_NAME(spiflash_stats_section, written_bytes)
+STATS_NAME_END(spiflash_stats_section)
+#endif
+
 #if MYNEWT_VAL(SPIFLASH_SPI_CS_PIN) < 0
 #error SPIFLASH_SPI_CS_PIN must be set to the correct value in bsp syscfg.yml
 #endif
@@ -1022,6 +1033,8 @@ hal_spiflash_read(const struct hal_flash *hal_flash_dev, 
uint32_t addr, void *bu
 
     spiflash_lock(dev);
 
+    SPIFLASH_STATS_INC(dev->stats, read_count);
+
     err = spiflash_wait_ready(dev, 100);
     if (!err) {
 #if MYNEWT_VAL(SPIFLASH_CACHE_SIZE)
@@ -1086,6 +1099,7 @@ hal_spiflash_read(const struct hal_flash *hal_flash_dev, 
uint32_t addr, void *bu
                        MYNEWT_VAL(SPIFLASH_CACHE_SIZE));
             }
 #endif
+            SPIFLASH_STATS_INCN(dev->stats, read_bytes, len);
         }
     }
 
@@ -1126,6 +1140,8 @@ hal_spiflash_write(const struct hal_flash *hal_flash_dev, 
uint32_t addr,
         pp_time_maximum = pp_time_typical;
     }
 
+    SPIFLASH_STATS_INC(dev->stats, write_count);
+
     while (len) {
         spiflash_write_enable(dev);
 
@@ -1160,9 +1176,12 @@ hal_spiflash_write(const struct hal_flash 
*hal_flash_dev, uint32_t addr,
         addr += to_write;
         u8buf += to_write;
         len -= to_write;
-
+        SPIFLASH_STATS_INCN(dev->stats, written_bytes, to_write);
     }
 err:
+    if (rc) {
+        SPIFLASH_STATS_INC(dev->stats, error_count);
+    }
     spiflash_unlock(dev);
 
     return rc;
@@ -1301,10 +1320,12 @@ spiflash_erase(struct spiflash_dev *dev, uint32_t 
address, uint32_t size)
     int rc = 0;
 
     if (address == 0 && size == dev->hal.hf_size) {
+        SPIFLASH_STATS_INC(dev->stats, erase_count);
         return spiflash_chip_erase(dev);
     }
     address &= ~0xFFFU;
     while (size) {
+        SPIFLASH_STATS_INC(dev->stats, erase_count);
 #if MYNEWT_VAL(SPIFLASH_BLOCK_ERASE_64BK)
         if ((address & 0xFFFFU) == 0 && (size >= 0x10000)) {
             /* 64 KB erase if possible */
@@ -1456,6 +1477,14 @@ hal_spiflash_init(const struct hal_flash *hal_flash_dev)
 
     dev = (struct spiflash_dev *)hal_flash_dev;
 
+#if MYNEWT_VAL(SPIFLASH_STAT)
+    rc = stats_init_and_reg(STATS_HDR(dev->stats),
+                            STATS_SIZE_INIT_PARMS(dev->stats, STATS_SIZE_32),
+                            STATS_NAME_INIT_PARMS(spiflash_stats_section),
+                            "spiflash");
+    assert(rc == 0);
+#endif
+
 #if MYNEWT_VAL(SPIFLASH_AUTO_POWER_DOWN)
     os_mutex_init(&dev->lock);
     os_callout_init(&dev->apd_tmo_co, os_eventq_dflt_get(),
diff --git a/hw/drivers/flash/spiflash/syscfg.yml 
b/hw/drivers/flash/spiflash/syscfg.yml
index 5d0b19003..8ab7671c8 100644
--- a/hw/drivers/flash/spiflash/syscfg.yml
+++ b/hw/drivers/flash/spiflash/syscfg.yml
@@ -35,6 +35,11 @@ syscfg.defs:
             second clock edge latches data).
         value: HAL_SPI_MODE3
 
+    SPIFLASH_STAT:
+        description: >
+            Enable statistics for driver.
+        value: 0
+
     SPIFLASH_SECTOR_COUNT:
         description: 'Number of sectors'
         value:  0

Reply via email to