Hi I have a problem to write an dread data from SDcard with stm32f107VCt processor. I want to Write/read data with Chan Library. But I cant use it for my 8GB SDCard. That is my code:
#include "diskio.h" #include "ff.h" #include "stdio.h" #include "bsp.h" #include "ff_gen_drv.h" #include "drivers/sd_diskio.h"
//printf init struct __FILE { int handle;} ;
FILE __stdout; FILE __stdin; FILE __stderr;
int fputc(int ch, FILE *f) {
while(!USART_GetFlagStatus(COM2,USART_FLAG_TXE));
USART_SendData(COM2,ch);
return ch; }
PARTITION VolToPart[] = { {0, 1}, /* Logical drive 0 ==> Physical drive 0, 1st partition */ {0, 2}, /* Logical drive 1 ==> Physical drive 0, 2nd partition */ {1, 0} /* Logical drive 2 ==> Physical drive 1, auto detection */ };
void main(void) {
FATFS SDFatFs; /* File system object for SD card logical drive */ FIL MyFile; /* File object */ char SDPath[4]; /* SD card logical drive path */
FRESULT res; /* FatFs function common result code */ uint32_t byteswritten, bytesread; /* File write/read counts */ uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */ uint8_t rtext[100];
RCC_Configuration(); GPIO_Configuration(); Com2_Intial(); //usb to serial
printf("Naminic STM32 evaluation board\n");
/*##-1- Link the micro SD disk I/O driver ##################################*/ if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0) { /*##-2- Register the file system object to the FatFs module ##############*/ if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK) { /* FatFs Initialization Error */ while(1); } else { /*##-3- Create a FAT file system (format) on the logical drive #########*/ /* WARNING: Formatting the uSD card will delete all content on the device */ if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK) { /* FatFs Format Error */ while(1); } else { /*##-4- Create and Open a new text file object with write access #####*/ if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { /* 'STM32.TXT' file Open for write Error */ while(1); } else { /*##-5- Write data to the text file ################################*/ res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
/*##-6- Close the open text file #################################*/ if (f_close(&MyFile) != FR_OK ) { while(1); }
if((byteswritten == 0) || (res != FR_OK)) { /* 'STM32.TXT' file Write or EOF Error */ while(1); } else { /*##-7- Open the text file object with read access ###############*/ if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK) { /* 'STM32.TXT' file Open for read Error */ while(1); } else { /*##-8- Read data from the text file ###########################*/ res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
printf("%s",rtext);
if((bytesread == 0) || (res != FR_OK)) { /* 'STM32.TXT' file Read or EOF Error */ while(1); } else { /*##-9- Close the open text file #############################*/ f_close(&MyFile);
/*##-10- Compare read data with the expected data ############*/ if((bytesread != byteswritten)) { /* Read data is different from the expected data */ while(1); } } } } } } } }
printf("finish");
while(1); }