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;
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);
#include "drivers/ssp.h"
#include "lib/stdio.h"
+#include "lib/errno.h"
#include "uSD.h"
#include "time.h"
/***************************************************************************** */
/* 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;
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 ? */
/***************************************************************************** */
/* 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;
}
/* 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;
-}