cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RBT6 and SD Card with FAT32

mike_mitza
Associate II
Posted on January 23, 2015 at 13:59

Hello.

I have a project in which I want to log some data into a text file on a SD card, using STM32F103RBT6. I managed to program the interface driver through SPI, since this chip doesn't have SDIO. It works fine and I can read the card's data from its CSD register. I want to write into a text file and I have to create the text file first.

I began experimenting with FatFS but I didn't manage to understand how it works. The library has several functions used for disk and file system initializations but I don't understand which to use first. On top of that, after I managed to write and compile a program with some of those functions, I got a linker error stating that the memory was overflown by 55kB.

I then experimented with dosfs and managed to read the FAT header. But now I can't create or write the file, although I made the proper setting, I think.

The code is:

int

write_file(uint8_t *buffer)

{

uint8_t sector[SECTOR_SIZE], sector2[SECTOR_SIZE];

uint32_t pstart, psize, i;

uint8_t pactive, ptype;

uint32_t cache;

FILEINFO* fi;

/* Opens an existing file. If not exist, creates a new file. */

if

(DFS_OpenFile(&vi,

''WRTEST.TXT''

, DFS_WRITE, sector, &fi))

{

//printf(''error opening file\n'');

return

-1;

}

else

{

DFS_Seek(&fi, f_size(fi), sector);

}

DFS_WriteFile(&fi, sector, &buffer[0], &cache, SECTOR_SIZE);

}

The code was ported from the stsw-stm32040 pack, which is for STM32F4, I think.

I am opened to any suggestions. Thank you and have a nice day.

Mihai

#stm32 #fatfs #sdio #stm32-stm32f103-sd-spi-fat32 #sd #spi

6 REPLIES 6
stm322399
Senior
Posted on January 23, 2015 at 14:27

Mihai,

FatFs works like a charm on F103RTB6 (eg. Olimex board with SD socket). If you choose to use FatFs, you are only required to mount the SD card when your main program starts.

static FATFS mydisk;
f_mount(&mydisk, '''', 1);

This first driver function called by FatFs is disk_initialize. This function is required to perform all steps to initialize an SDCard (like you already did to read CSD). Then provide others driver functions: ioctl, read, status and write. About the linker error, use this thread to give more detail, and you'll certainly receive some help. -- Laurent
mike_mitza
Associate II
Posted on January 23, 2015 at 15:12

Hello, Laurent.

Thank you for your answer. Indeed, the FatFS library is very interesting and it has more options that dosFs, but I looked into the functions that initialize the drive and it calls SDIO functions that have no corespondence in the SPI driver. I will look a bit more to see if I can port them, but I saw that there are some functions that are exclusively for SDIO because it uses the pins that are not used on the SPI interface.

The linker error that I got is the following: `.text' will not fit in region `rom'. From what I found, a fix might be putting __attribute__((__packed__)) on the FatFS structures because the gcc compiles them badly.

If you know any implementations of FatFs on SPI, not SDIO, can you share the code? Maybe I am missing something.

Thank you.

Mihai

stm322399
Senior
Posted on January 23, 2015 at 17:26

FatFs has no code dealing with SDIO inside. Where did you get the code from ? What function does work, and which one does not work ? Explain me how is setup your project or the code I can give you could may end in garbage bin.

mike_mitza
Associate II
Posted on January 24, 2015 at 18:45

Hello again.

I finally managed to get FatFs working by adapting this guy's STM32F4 implementation: http://stm32f4-discovery.com/2014/07/library-21-read-sd-card-fatfs-stm32f4xx-devices/ with my SPI driver. I can now do anything a SD card can do with the SD card.

Sorry but I cannot give you the FatFs code that I tried and had SDIO functions in it because I don't know where it is 🙂 . I tried so many options, so many implementations from the Internet that I lost track of them. But in that STM32F4 link, for example, the disk_read function from diskio.c has his own adaptations to SDIO and SPI drivers, so that was what I was referring to. I didn't know exactly how to put the driver's function to work for FAT, but now I have managed it.

I have one last question about using FAT.

Basically, my program has to read and interpret some data from a sensor and by using a trigger level, it has to write the data on the SD card. A sort-of pseudo-code is:

initialize sensor and card driver (using f_mount)

while (1)

read and interpret sensor data

if result > trigger then

open file (f_open)

write_data (f_write)

close file (f_close)

My question is if I have to unmount the card every time I close the file. Is there any chance to corrupt the file or f_close will help? I know that there is a chance that the power to the board is cut in the middle of the write but that's a risk I have to take until I add a battery pack to the implementation.

Thank you.

Mihai

 

 

From: gonzalez.laurent

Posted: Friday, January 23, 2015 5:26 PM

Subject: STM32F103RBT6 and SD Card with FAT32

FatFs has no code dealing with SDIO inside. Where did you get the code from ? What function does work, and which one does not work ? Explain me how is setup your project or the code I can give you could may end in garbage bin.

stm322399
Senior
Posted on January 27, 2015 at 10:54

As far as I know about FatFS, you are only required to call f_sync to make sure that writes are not pending anymore (if the driver layer implements sync correctly, of course). f_sync performs the same operation than f_close, but keeping the file open (see fatfs documentation). There is no need to close and re-open continuously if your file is *write only*.

Posted on June 30, 2015 at 11:19

Hi mihai,

I am in the same situation. I am using Olimexino-Stm32 (Stm32F103RBT6) with CAN BUS and SD Card.

SD Card with SPI2 works fine but now I have to implement FatFs. If I understand right, I have to include the FatFs library and to write my one disk.io? or is it easier to migrate the example of Stm32F4? Can you give me some help please? I am using IAR Workbench.

Best Regards

Chris