求大神指导。我的SD卡写文件能够写的进去,但是在电脑上打开时什么都没有,求帮忙

2019-07-21 04:30发布

如题,我的F4的板子,文件系统版本是R0.07e。已经调了4,5天了,快崩溃了,就大家帮我看看程序到底有什么问题,万分感谢!!!
/*********************************************************************************************************
*
* File               : main.c
* Hardware Environment: 
* Build Environment   : RealView MDK-ARM  Version: 4.20
* Version             : V1.0
* By                 : 
*
*                                  (c) Copyright 2005-2011, WaveShare
*                                       http://www.waveshare.net
*                                          All Rights Reserved
*
*********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "sdcard.h"
#include "usart.h"
#include "string.h"
#include "diskio.h"
#include "ff.h"

/* Private functions ---------------------------------------------------------*/
FATFS fs;            // Work area (file system object) for logical drive
FIL fsrc;      // file objects
FRESULT res;         // FatFs function common result code
UINT bw;         // File R/W count
XCHAR fileName[20]="test1.txt";

/*******************************************************************************
* Function Name : NVIC_Config
* Description   : Configures SDIO IRQ channel.
* Input         : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
 
void WRUTE_File(void)

FRESULT res;
DIR dirs;
char path[16]={""};  
char buffer[512]; //??????????????×é
int i;
for(i=0;i<512;i++)
{
buffer='k';
}

printf(" ??????????:%d ",disk_initialize(0));
printf("????????:%d ",f_mount(0, &fs));
if (f_opendir(&dirs, path) == FR_OK)
{
if(f_open(&fsrc, fileName, FA_READ|FA_WRITE|FA_OPEN_ALWAYS)) 
    {
printf("can not open file ");
    }
else
{
printf("here A ");

if(f_lseek(&fsrc,fsrc.fsize))//f_size(&file)
{
printf("can not seek the file pointer ");
}
else
{
printf("filesize=%d ",fsrc.fsize);

res=f_write(&fsrc, buffer, 512, &bw);

if(res==FR_OK)//????&file????filename???????×???·??3000??????????×?????
{
printf(" write ok ");
printf("bw=%d ",bw);
}
else
{printf("can not write file ");}
}

}
else{printf("cao not open dir ");}
    f_close(&fsrc);      // ??±???????±?????f_open????????????
    f_mount(0, 0);  //????????????
printf("here C ");

}

/*******************************************************************************
* Function Name : main
* Description   : Main program.
* Input         : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{   

  SystemInit();/* Clock Config */
  
  NVIC_Configuration();/* Interrupt Config */

  USART_Configuration();  

//printf("buffer=%s ",buffer);
  WRITE_File();
  
  /* Infinite loop */
  while (1);
}

这些ABC是我用来调试用的,没有什么意义。我在SD卡里看test1.txt还是0KB,不知道是什么原因
麻烦大家帮我看看,这的是绝望了。

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
19条回答
黑白边缘
2019-07-22 18:56
额,LZ有其他参考代码吗?发一点代码先

[mw_shl_code=c,true]int main(void) { FRESULT res; uint32_t byteswritten;   volatile int num = 0; HAL_Init(); SystemClock_Config(); res = FATFS_LinkDriver(&SDRAMDISK_Driver, RAMpath); if(res != FR_OK) Error_Handler(); res = FATFS_LinkDriver(&SD_Driver, SDpath); if(res != FR_OK) Error_Handler(); res = f_mount(&RAMFatFs, (TCHAR const*)RAMpath, 0); if(res != FR_OK) Error_Handler(); res = f_mount(&SDFatFs, (TCHAR const*)SDpath, 0); if(res != FR_OK) Error_Handler(); /* res = f_mkfs((TCHAR const*)SDpath, 0, 0); if(res != FR_OK) Error_Handler(); */ res = f_open(&SDFile, "1:STM000.c", FA_OPEN_ALWAYS | FA_WRITE); if(res != FR_OK)     Error_Handler(); write_buf(middle);       //往middle里面填满4096字节数据 while(1) {     num++; res = f_write(&SDFile, middle, 4096, (void *)&byteswritten); f_sync(&SDFile); } f_close(&SDFile); FATFS_UnLinkDriver(RAMpath); FATFS_UnLinkDriver(SDpath); /* Infinite loop */ while (1) { } } void write_buf(char middle[4096]) { char *p = "aaaaaaaabbbbbbbbeeeeeeeeffffffff"; int count = 0; while(count < 128) { count++; strncat(middle,p,128); } } [/mw_shl_code]
大致调用就这样了,里面有RAM,不过没用到
另外我的SD卡簇大小格式化成了4096

一周热门 更多>