From 6e290fbace1c178c2f727050e74cc59160529b43 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 31 Aug 2023 14:32:56 +0200 Subject: [PATCH 1/2] drivers/mtd_spi_nor: fix init when only ztimer_msec is used --- drivers/mtd_spi_nor/mtd_spi_nor.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/mtd_spi_nor/mtd_spi_nor.c b/drivers/mtd_spi_nor/mtd_spi_nor.c index 03d35661cf..34bde1843c 100644 --- a/drivers/mtd_spi_nor/mtd_spi_nor.c +++ b/drivers/mtd_spi_nor/mtd_spi_nor.c @@ -27,12 +27,14 @@ #include "byteorder.h" #include "kernel_defines.h" +#include "macros/math.h" #include "macros/utils.h" #include "mtd.h" #include "mtd_spi_nor.h" +#include "time_units.h" #include "thread.h" -#if IS_USED(MODULE_ZTIMER_USEC) +#if IS_USED(MODULE_ZTIMER) #include "ztimer.h" #elif IS_USED(MODULE_XTIMER) #include "xtimer.h" @@ -451,6 +453,9 @@ static int mtd_spi_nor_power(mtd_dev_t *mtd, enum mtd_power_state power) do { #if IS_USED(MODULE_ZTIMER_USEC) ztimer_sleep(ZTIMER_USEC, dev->params->wait_chip_wake_up); +#elif IS_USED(MODULE_ZTIMER_MSEC) + ztimer_sleep(ZTIMER_MSEC, + DIV_ROUND_UP(dev->params->wait_chip_wake_up, US_PER_MS)); #elif IS_USED(MODULE_XTIMER) xtimer_usleep(dev->params->wait_chip_wake_up); #endif From 4f2f412c39e24704b4a96e1637ca45b257460658 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 31 Aug 2023 15:22:33 +0200 Subject: [PATCH 2/2] drivers/mtd_spi_nor: improve fallback when no timer is used --- drivers/mtd_spi_nor/mtd_spi_nor.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/mtd_spi_nor/mtd_spi_nor.c b/drivers/mtd_spi_nor/mtd_spi_nor.c index 34bde1843c..76a0f553f7 100644 --- a/drivers/mtd_spi_nor/mtd_spi_nor.c +++ b/drivers/mtd_spi_nor/mtd_spi_nor.c @@ -446,9 +446,13 @@ static int mtd_spi_nor_power(mtd_dev_t *mtd, enum mtd_power_state power) switch (power) { case MTD_POWER_UP: mtd_spi_cmd(dev, dev->params->opcode->wake); - /* No sense in trying multiple times if no xtimer to wait between - reads */ - uint8_t retries = 0; + + /* fall back to polling if no timer is used */ + unsigned retries = MTD_POWER_UP_WAIT_FOR_ID; + if (!IS_USED(MODULE_ZTIMER) && !IS_USED(MODULE_XTIMER)) { + retries *= dev->params->wait_chip_wake_up * 1000; + } + int res = 0; do { #if IS_USED(MODULE_ZTIMER_USEC) @@ -460,8 +464,7 @@ static int mtd_spi_nor_power(mtd_dev_t *mtd, enum mtd_power_state power) xtimer_usleep(dev->params->wait_chip_wake_up); #endif res = mtd_spi_read_jedec_id(dev, &dev->jedec_id); - retries++; - } while (res < 0 && retries < MTD_POWER_UP_WAIT_FOR_ID); + } while (res < 0 && --retries); if (res < 0) { mtd_spi_release(dev); return -EIO;