2017-02-22 12:18 AM
Posted on February 22, 2017 at 09:18
Hey all,
I want to write to SD card, I'm Using cube-mxstm32Nucleo-L476RG MCU ,and 4-pin SD card,
I attach my cube project, the main program has the following code:
declaring and init :
FATFS SDFatFs;
FIL MyFile;
extern char SD_Path[4]; /* SD logical drive path */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
FRESULT res;
uint32_t byteswriten,byteread;
uint8_t wtext[]='Hello from STM32!!\0';
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_DFSDM1_Init();
MX_SDMMC1_SD_Init();
MX_FATFS_Init();
MX_TIM6_Init();
MX_SDMMC1_SD_Init();
I am trying to write to sd card by the following code:
if(res=f_mount(&SDFatFs,(TCHAR const*)SD_Path,0)!=FR_OK)
{
Error_Handler();
}
else
{
if(res=f_open(&MyFile,'0:/mywrite.txt',FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK)
{
Error_Handler();
}
else
{
res=f_write(&MyFile,wtext,sizeof(wtext),(void*)&byteswriten);
if((byteswriten==0) || (res!=FR_OK))
{
Error_Handler();
}
else
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}
f_close(&MyFile);
}
}
I get FR_NO_FILESYSTEM if the SD is connected and if the SD is not connected, I get FR_NOT_READY,
Thanks for any help.
Note: this post was migrated and contained many threaded conversations, some content may be missing.
2018-05-09 04:31 AM
hello
I am working on sd card fatfs interfacing using stm32f103ze controller,i have developed a firmware for interfacing sd card through spi,i am trying to interface sd card using sdio + fatfs,I am looking for that code all over,if u have sdio+fatfs interfacing code,do u mind sharing that code with me.
thank you
2018-05-09 07:04 AM
There should be a workable SDIO+FATFS example for the STM3210E-EVAL board under the HAL/Cube software trees.
2018-05-09 07:39 AM
i have been searching from few days i cant get any code,if u have any codes can u plz forward it .i am using stm32f103ze controller with keil 4.21 compiler
2018-05-09 08:08 AM
You fail at searching skills.
STM32Cube_FW_F1_V1.6.1\Projects\STM3210E_EVAL\Applications\FatFs\FatFs_uSD
STM32Cube_FW_F1_V1.6.1\Drivers\BSP\STM3210E_EVAL\stm3210e_eval_sd.c
You might have to upgrade to Keil v5.2x for the HAL/CUBE Examples
STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\SDIO\uSDCard
STM32F10x_StdPeriph_Lib_V3.5.0\Utilities\STM32_EVAL\Common\stm32_eval_sdio_sd.c
I can build Keil 4.2x projects, but would charge hourly.
2018-05-10 01:05 AM
thank you...
2018-05-10 03:19 PM
Hi,
This may be to do with the polarity of your card detect. I have always found I have to invert it to get it to work correctly.
I fixed it in
fatfs_platform.c
by changing BSP_PlatformIsDetected()from: if
(
HAL_GPIO_ReadPin
(
SD_PORT
,
SD_PIN
) ==
GPIO_PIN_RESET
)
to: if
(
HAL_GPIO_ReadPin
(
SD_PORT
,
SD_PIN
) !=
GPIO_PIN_RESET
)
Not This has worked for me on a F1 and an F4 so hopefully works for you. It seems a bit silly that here isn't any option to define the polarity for this.
2018-05-11 02:20 AM
,
,
hi,
,I HAVE GOT SAME PROBLEM,ERROR IS , ,FR_NO_FILESYSTEM,I AM USING 8 GB SD CARD,INTERFACING USING SPI,I AM NEW TO FAT FS,CAN U HELP ME OUT HERE..
FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */
,
const char **path, /* Pointer to pointer to the path name (drive number) */,
FATFS **rfs, /* Pointer to pointer to the found file system object */,
BYTE chk_wp /* !=0: Check media write protection for write access */,
),
{,
FRESULT res,,
BYTE vol, fmt, *tbl,,
DSTATUS stat,,
DWORD bsect, fsize, tsect, mclst,,
const char *p = *path,,
FATFS *fs,,
/* Get logical drive number from the path name */,
vol = p[0] - '0', /* Is there a drive number? */,
if (vol <,= 9 &,&, p[1] == ':') {,
p += 2, /* Found a drive number, get and strip it */,
*path = p, /* Return pointer to the path name */,
} else {,
vol = 0, /* No drive number is given, use drive number 0 as default */,
}/* Check if the logical drive number is valid or not */
,
if (vol >,= _DRIVES) return FR_INVALID_DRIVE, /* Is the drive number valid? */,
*rfs = fs = FatFs[vol], /* Returen pointer to the corresponding file system object */,
if (!fs) return FR_NOT_ENABLED, /* Is the file system object registered? */ENTER_FF(fs), /* Lock file system */
if (fs->,fs_type) { /* If the logical drive has been mounted */
,
stat = disk_status(fs->,drive),,
if (!(stat &, STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */,
♯ if !_FS_READONLY,
if (chk_wp &,&, (stat &, STA_PROTECT)) /* Check write protection if needed */,
return FR_WRITE_PROTECTED,,
♯ endif,
return FR_OK, /* The file system object is valid */,
},
}/* The logical drive must be re-mounted. Following code attempts to mount the volume */
fs->,fs_type = 0, /* Clear the file system object */
,
fs->,drive = LD2PD(vol), /* Bind the logical drive and a physical drive */,
stat = disk_initialize(fs->,drive), /* Initialize low level disk I/O layer */,
if (stat &, STA_NOINIT) /* Check if the drive is ready */,
return FR_NOT_READY,,
♯ if _MAX_SS != 512 /* Get disk sector size if needed */,
if (disk_ioctl(fs->,drive, GET_SECTOR_SIZE, &,SS(fs)) != RES_OK || SS(fs) >, _MAX_SS),
return FR_NO_FILESYSTEM,,
♯ endif,
♯ if !_FS_READONLY,
if (chk_wp &,&, (stat &, STA_PROTECT)) /* Check write protection if needed */,
return FR_WRITE_PROTECTED,,
♯ endif,
/* Search FAT partition on the drive */,
fmt = check_fs(fs, bsect = 0), /* Check sector 0 as an SFD format */,
if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */,
/* Check a partition listed in top of the partition table */,
tbl = &,fs->,win[MBR_Table + LD2PT(vol) * 16], /* Partition table */,
if (tbl[4]) { /* Is the partition existing? */,
bsect = LD_DWORD(&,tbl[8]), /* Partition offset in LBA */,
fmt = check_fs(fs, bsect), /* Check the partition */,
},
},
if (fmt == 3) return FR_DISK_ERR,,
if (fmt || LD_WORD(fs->,win+BPB_BytsPerSec) != SS(fs)) /* No valid FAT patition is found *////////THIS LINE ERROR,
return FR_NO_FILESYSTEM,/* Initialize the file system object */
,
fsize = LD_WORD(fs->,win+BPB_FATSz16), /* Number of sectors per FAT */,
if (!fsize) fsize = LD_DWORD(fs->,win+BPB_FATSz32),,
fs->,sects_fat = fsize,,
fs->,n_fats = fs->,win[BPB_NumFATs], /* Number of FAT copies */,
fsize *= fs->,n_fats, /* (Number of sectors in FAT area) */,
fs->,fatbase = bsect + LD_WORD(fs->,win+BPB_RsvdSecCnt), /* FAT start sector (lba) */,
fs->,csize = fs->,win[BPB_SecPerClus], /* Number of sectors per cluster */,
fs->,n_rootdir = LD_WORD(fs->,win+BPB_RootEntCnt), /* Nmuber of root directory entries */,
tsect = LD_WORD(fs->,win+BPB_TotSec16), /* Number of sectors on the file system */,
if (!tsect) tsect = LD_DWORD(fs->,win+BPB_TotSec32),,
fs->,max_clust = mclst = (tsect /* Last cluster ♯ + 1 */,
- LD_WORD(fs->,win+BPB_RsvdSecCnt) - fsize - fs->,n_rootdir / (SS(fs)/32),
) / fs->,csize + 2,fmt = FS_FAT12, /* Determine the FAT sub type */
,
if (mclst >,= 0xFF7) fmt = FS_FAT16, /* Number of clusters >,= 0xFF5 */,
if (mclst >,= 0xFFF7) fmt = FS_FAT32, /* Number of clusters >,= 0xFFF5 */if (fmt == FS_FAT32)
,
fs->,dirbase = LD_DWORD(fs->,win+BPB_RootClus), /* Root directory start cluster */,
else,
fs->,dirbase = fs->,fatbase + fsize, /* Root directory start sector (lba) */,
fs->,database = fs->,fatbase + fsize + fs->,n_rootdir / (SS(fs)/32), /* Data start sector (lba) */♯ if !_FS_READONLY
,
/* Initialize allocation information */,
fs->,free_clust = 0xFFFFFFFF,,
fs->,wflag = 0,,
/* Get fsinfo if needed */,
if (fmt == FS_FAT32) {,
fs->,fsi_sector = bsect + LD_WORD(fs->,win+BPB_FSInfo),,
fs->,fsi_flag = 0,,
if (disk_read(fs->,drive, fs->,win, fs->,fsi_sector, 1) == RES_OK &,&,,
LD_WORD(fs->,win+BS_55AA) == 0xAA55 &,&,,
LD_DWORD(fs->,win+FSI_LeadSig) == 0x41615252 &,&,,
LD_DWORD(fs->,win+FSI_StrucSig) == 0x61417272) {,
fs->,last_clust = LD_DWORD(fs->,win+FSI_Nxt_Free),,
fs->,free_clust = LD_DWORD(fs->,win+FSI_Free_Count),,
},
},
♯ endif,
fs->,winsect = 0,,
fs->,fs_type = fmt, /* FAT syb-type */,
fs->,id = ++Fsid, /* File system mount ID */,
res = FR_OK,return res,
,
}2018-05-11 03:29 AM
Fails here because you're reading junk data from the media. Fix the DISKIO layer so the read sector routines work properly. ie test and validate operation.
2018-05-16 04:51 AM
HELLO CLIVE ONE
MY CODE IS WORKING FOR 2GB MEMORY CARD ,I CAN CREATE FILE AND WRITE TO IT, USING SPI-SD INTERFACE,BUT IF I USE 8GB MEMORY CARD ITS GIVING FR_NO_FILE_SYSTEM,CAN U HELP ME OUT HERE .
THANK YOU