From e37029ed7eb71e0207bd4847257e51d345073b7f Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 10 Nov 2022 12:34:28 +0100 Subject: [PATCH] Update/Fix uSD support Now need to test effective data logging. --- v10/config.c | 4 +--- v10/uSD.c | 66 ++++++++++++++++++++-------------------------------- v10/uSD.h | 2 -- 3 files changed, 26 insertions(+), 46 deletions(-) diff --git a/v10/config.c b/v10/config.c index 6495330..702ea0d 100644 --- a/v10/config.c +++ b/v10/config.c @@ -103,12 +103,11 @@ const struct pio overtemperature_pin = LPC_GPIO_0_28; const struct pio ac_ctrl = LPC_GPIO_0_6; const struct pio fan_ctrl = LPC_GPIO_0_5; /* Chip selects */ -const struct pio uSD_cs = LPC_GPIO_1_1; /* uSD card */ +const struct pio uSD_cs = LPC_GPIO_1_1; /* uSD card - used in uSD.c !! */ const struct pio th_cs = LPC_GPIO_0_7; /* Thermocouple */ const struct pio uext_cs = LPC_GPIO_0_18; /* UEXT module (optionnal) */ - /***************************************************************************** */ /* Basic system init and configuration */ static volatile int got_wdt_int = 0; @@ -290,7 +289,6 @@ int external_components_config(uint32_t uart) scialys_time_init_check(uart); /* uSD card */ - scialys_uSD_config((struct pio *)&uSD_cs, SSP_BUS_0); scialys_uSD_detect(uart); scialys_uSD_logs_init(uart); diff --git a/v10/uSD.c b/v10/uSD.c index bf40f97..f1ec9a7 100644 --- a/v10/uSD.c +++ b/v10/uSD.c @@ -27,6 +27,7 @@ #include "drivers/ssp.h" #include "lib/stdio.h" +#include "lib/errno.h" #include "uSD.h" #include "time.h" @@ -36,8 +37,10 @@ /***************************************************************************** */ /* SD/MMC Card */ struct sdmmc_card micro_sd = { + .ssp_bus_num = SSP_BUS_0, .card_type = MMC_CARDTYPE_UNKNOWN, .block_size = 64, + .chip_select = LPC_GPIO_1_1, }; static uint8_t got_uSD = 0; @@ -69,10 +72,11 @@ int scialys_uSD_write(uint32_t block_num) ret = sdmmc_write_block(µ_sd, block_num, mmc_data); /* Check return value */ if (ret == -EBUSY) { + /* FIXME : do not loop, use flag to retry wait later */ uint8_t cnt = 10; do { - ret = sdmmc_wait_for_ready(mmc, 0xFF); - while ((ret != 0xFF) && (cnt-- > 0)); + ret = sdmmc_wait_write_end(µ_sd); + } while ((ret != 0) && (cnt-- > 0)); } else if (ret == -ENODEV) { /* uSD stalled ??? */ /* FIXME : Try reset ? */ @@ -191,54 +195,37 @@ int scialys_uSD_append_data(struct sd_data_blob* data) /***************************************************************************** */ /* micro SD card init */ +#define DEBUG 1 - +#define NB_SD_INIT_LOOPS 3 +#define NB_SD_INIT_WAIT_RETRIES 10 int scialys_uSD_detect(int uart) { - int i = 0, loop = 0, ret = 0; -#ifdef DEBUG - int step = 0; -#endif + int ret = 0; + uint8_t step = 0, loop = 0; + uint8_t retries = NB_SD_INIT_WAIT_RETRIES; - /* Issue a first CMD0 to get SD card to SPI mode */ + /* Get card to SPI mode */ ret = sdmmc_reset(µ_sd); /* Let some time for uSD card to power up */ do { - i = 0; - ret = sdmmc_init(µ_sd); - if (ret == 0) { + ret = sdmmc_init_single(µ_sd, &step, &retries); #ifdef DEBUG - step = 1; + uprintf(uart, "uSD init (%d): step:%d, retries: %d, ret: %d, type: %d, bs: %d\n", + loop, step, retries, ret, micro_sd.card_type, micro_sd.block_size); #endif - do { - i++; - msleep(10); - ret = sdmmc_init_wait_card_ready(µ_sd); - } while ((ret != 0 ) && (i < 10)); - if (ret <= 1) { -#ifdef DEBUG - step = 2; -#endif - ret = sdmmc_init_end(µ_sd); - } - } - if (micro_sd.card_type == MMC_CARDTYPE_UNKNOWN) { - uprintf(uart, "Unknown card type :(\n"); - } -#ifdef DEBUG - uprintf(uart, "uSD init(%d - %d): step:%d, ret: %d, type: %d, bs: %d\n", - loop, i, step, ret, micro_sd.card_type, micro_sd.block_size); -#endif - if (loop++ > 10) { - break; + if (ret != 0) { + msleep(20); } - msleep(50); - } while (ret != 0); + loop++; + } while ((ret != 0) && (loop < NB_SD_INIT_LOOPS)); /* Got uSD ? */ - if (loop >= 10) { + if (loop >= NB_SD_INIT_LOOPS) { +#ifdef DEBUG uprintf(uart, "uSD init failed, no uSD card present.\n"); +#endif got_uSD = 0; return -1; } @@ -248,14 +235,11 @@ int scialys_uSD_detect(int uart) /* got_uSD MUST be set to 1 from here on ! */ ret = scialys_uSD_read(0); /* Read 1 block at start of card */ /* FIXME : check that the card magic is present */ +#ifdef DEBUG uprintf(uart, "uSD read (ret : %d) : %s\n", ret, mmc_data); +#endif return 0; } -void scialys_uSD_config(struct pio* uSD_cs, uint32_t ssp_bus_num) -{ - memcpy(&(micro_sd.chip_select), uSD_cs, sizeof(struct pio)); - micro_sd.ssp_bus_num = ssp_bus_num; -} diff --git a/v10/uSD.h b/v10/uSD.h index 2bf065d..bb8fca3 100644 --- a/v10/uSD.h +++ b/v10/uSD.h @@ -86,8 +86,6 @@ int scialys_uSD_logs_init(int uart); int scialys_uSD_detect(int uart); -void scialys_uSD_config(struct pio* uSD_cs, uint32_t ssp_bus_num); - #endif /* USD_H */ -- 2.43.0