cancel
Showing results for 
Search instead for 
Did you mean: 

A question for initializing a Drive of FATFs and SPI2 ?

antonius
Senior
Posted on January 26, 2016 at 21:35

Guys,

I have A question for initializing a Drive of FATFs and SPI2 ? I tested this only function without connecting to any other function at main.c... and it's doing the task properly, I can see at Logic analyzer, How can I connect it to FATFs properly ?

DSTATUS USER_initialize(BYTE pdrv)
{
HAL_UART_Transmit(&huart1, ''USER INITIALIZE START! \r\n'', 100, 1000); //debug message 
Stat = STA_NOINIT;
DSTATUS stat = RES_OK;
BYTE n, cmd, ty, ocr[4];
//; 
if (pdrv) return STA_NOINIT; /* Supports only single drive */
power_off(); /* Turn off the socket power to reset the card */
if (stat & STA_NODISK) return stat; /* No card in the socket */;
power_on(); /* Turn on the socket power */
FCLK_SLOW();
for (n = 10; n; n--) xchg_spi(0xFF); /* 80 dummy clocks */
ty = 0;
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
Timer1 = 100; /* Initialization timeout of 1000 msec */
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get trailing return value of R7 resp */
if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
while (Timer1 && send_cmd(ACMD41, 1UL << 30)); /* Wait for leaving idle state (ACMD41 with HCS bit) */
if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */
}
}
} else { /* SDv1 or MMCv3 */
if (send_cmd(ACMD41, 0) <= 1) {
ty = CT_SD1; cmd = ACMD41; /* SDv1 */
} else {
ty = CT_MMC; cmd = CMD1; /* MMCv3 */
}
while (Timer1 && send_cmd(cmd, 0)); /* Wait for leaving idle state */
if (!Timer1 || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
ty = 0;
}
}
CardType = ty;
deselect();
if (ty) { /* Initialization succeded */
stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
FCLK_FAST();
HAL_UART_Transmit(&huart1, ''USER INITIALIZE SUCCEDED! \r\n'', 100, 1000);
} else { /* Initialization failed */
power_off();
}
HAL_UART_Transmit(&huart1, ''USER INITIALIZE FINISHED! \n \n'', 100, 1000);
return stat;
/* USER CODE HERE */
//HAL_UART_Transmit(&huart1, ''DISK INITIALIZE user_diskio.c BEGIN! \r\n'', 100, 1000);
//Stat &= ~STA_NOINIT;
//return Stat;
}

Logic analyzer screen shot below, any ideas ? 0690X00000602bIQAQ.jpg 0690X00000602RTQAY.jpg
1 REPLY 1
colin
Associate II
Posted on April 26, 2017 at 14:55

Just to save you some time. If you are doing this on a NOR SPI flash device it can work (erase times will be slow). If you are doing on a NAND SPI flash you will wear out the flash. Try looking at emFile by Segger if you want your NAND flash to have any life. Remember even if it's use is mostly read only you will get Read Disturb on NAND flash which means you need to Copy erase the sectors/pages every 90 days or so.