AnsweredAssumed Answered

A question for initializing a Drive of FATFs and SPI2 ?

Question asked by H.Rick on Jan 26, 2016
Latest reply on Apr 26, 2017 by Critch.Colin
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 ?

spi2%20test1.jpg
spi2%20test2.jpg

Outcomes