Skip to content

Commit

Permalink
spiflash: Add stats
Browse files Browse the repository at this point in the history
This adds simple stats that can be use to evaluate
spiflash usage.

Signed-off-by: Jerzy Kasenberg <[email protected]>
  • Loading branch information
kasjer committed Jun 14, 2024
1 parent c5ca503 commit f46bb47
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
26 changes: 26 additions & 0 deletions hw/drivers/flash/spiflash/include/spiflash/spiflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 30 additions & 1 deletion hw/drivers/flash/spiflash/src/spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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),
dev->dev.bnode.odev.od_name);
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(),
Expand Down
5 changes: 5 additions & 0 deletions hw/drivers/flash/spiflash/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f46bb47

Please sign in to comment.