AnsweredAssumed Answered

FATFS_LinkDriver(&USER_Driver, USER_Path)==0) question ?

Question asked by H.Rick on Jan 26, 2016
Latest reply on Feb 2, 2016 by H.Rick
Guys,


Can I use SPI2 for SDcard with STM32CubeMX ?

I can see from :
#define HAL_SD_MODULE_ENABLED  

but there's no SPI implementation for it, it's for SDIO, and STM32F107 doesn't have SDIO...

I tried to run

 if(FATFS_LinkDriver(&USER_Driver, USER_Path)==0)
 {
      HAL_UART_Transmit(&huart1, "FATFS_LinkDriver OK! \r\n", 55, 1000); // Debug
  if (f_mount(&FatFs, (TCHAR const*)USER_Path, 0)==FR_OK)    
     {
         HAL_UART_Transmit(&huart1, "F_MOUNT \r\n", 55, 1000); // Debug
         if (f_open(&Fil, "newfile.txt", FA_WRITE |  FA_CREATE_ALWAYS) == FR_OK)
            {
                HAL_UART_Transmit(&huart1, "F_OPEN test_write..\r\n", 15, 1000); // As a pointer, with a length
                

                if (f_write(&Fil, "Test write line1 !\r\n", 17, &bw) == FR_OK)    /* Write data to the file */
                 {
                        HAL_UART_Transmit(&huart1, "F_WRITE 1 \r\n", 55, 1000); // Debug
                        //f_write(&Fil, "Next line test\r\n", 16, &bw);    /* Write data to the file */
                        //HAL_UART_Transmit(&huart1, "F_WRITE 2 \n\r", 55, 1000); // Debug
                        
                        //HAL_UART_Transmit(&huart1, "FWRITE \n\r", 15, 1000); // As a pointer, with a length
                         HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 0 );
                        HAL_Delay(5000);
                        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 1 );
                 }
                f_close(&Fil);                                /* Close the file */
                
                HAL_UART_Transmit(&huart1, "FCLOSE \r\n", 15, 1000); // As a pointer, with a length
            }
   }
    }   

I can pass
FATFS_LinkDriver(&USER_Driver, USER_Path)==0)
but I can't continue to f_mount ....

What's missing here ?
I can not see my debug message in disk_initialize but it can give FR_OK ??

user_diskio.h
extern Diskio_drvTypeDef  USER_Driver; <== I got error "unknown type Diskio_drvTypeDef" on this line, but I can compile it, what should I fix ?


user_diskio.c
Diskio_drvTypeDef  USER_Driver =
{
  USER_initialize,
  USER_status,
  USER_read,
#if  _USE_WRITE
  USER_write,
#endif  /* _USE_WRITE == 1 */  
#if  _USE_IOCTL == 1
  USER_ioctl,
#endif /* _USE_IOCTL == 1 */
};



/**
  * @brief  Initializes a Drive
  * @param  None
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_initialize(void)
 
{
  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];
  BYTE pdrv; 
 
    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;
}
fatfs.c
void MX_FATFS_Init(void)
{
  /*## FatFS: Link the USER driver ###########################*/
  retUSER = FATFS_LinkDriver(&USER_Driver, USER_Path);
    if (retUSER == 0)
        {
          HAL_UART_Transmit(&huart1, "DISK connected fatfs.c Line 48!", 55, 1000); // Debug
             
        }  
ff_gen_drv.c

/**
  * @brief  Links a compatible diskio driver and increments the number of active
  *         linked drivers.         
  * @note   The number of linked drivers (volumes) is up to 10 due to FatFs limits
  * @param  drv: pointer to the disk IO Driver structure
  * @param  path: pointer to the logical drive path
  * @retval Returns 0 in case of success, otherwise 1.
  */
uint8_t FATFS_LinkDriver(Diskio_drvTypeDef *drv, char *path)
{
  uint8_t ret = 1;
  uint8_t DiskNum = 0;
   
  if(disk.nbr <= _VOLUMES)
  {
    disk.is_initialized[disk.nbr] = 0;
    disk.drv[disk.nbr] = drv; 
    DiskNum = disk.nbr++;
    path[0] = DiskNum + '0';
    path[1] = ':';
    path[2] = '/';
    path[3] = 0;
    ret = 0;
  }
   
  return ret;
}

Thanks

Outcomes