FreeRTOS+FAT SL, SD Card, initialization issue

zealhero wrote on Friday, June 27, 2014:

HI.

I want to use SD Card with FAT SL.
First, I tested FAT window example project, FAT_SL_and_CLI_Windows_Simulator. So I understand how it works. So I applied it to my system board(ARM) and checked it. Next, I make a initialization for SD Card, it works fine. They’re in same project.

But I don’t know how can I connect them. They’re working independently now…

I want use filesystem functions on my SD Card like dir, write, open, etc…

I tried searching and analyzing codes. This post is similar with my situation.
https://sourceforge.net/p/freertos/discussion/382005/thread/272eac75/?limit=25#363a

‘ramdrv_f.c’ file is only for using ram? If it is true, should I make a ‘SDdrv_f.c’ file like ‘ramdrv_f.c’??

I think that it is related with F_DRIVERINIT in Media Driver API.
I don’t know how ulDriverparameter is decided.

I use FreeRTOS 8.0 and FAT SL 1.0.

Please help me using FAT for SD CARD or other memory type except RAM.

Regards.

rtel wrote on Friday, June 27, 2014:

I’ve just reviewed that thread and am not sure what else to add.

You can reuse the RAM disk file and structures, and then re-implement the functions that actually perform the reading and writing to make use of your SD card read and write functions. That might be confusing though if you have a structure that says ‘RAM’ with read and write functions that use an SD card driver. I would therefore suggest taking a copy of the RAM disk files, then rename the file name, and the structures used within the file, so they are called something relevant to the SD card.

Regards.

zealhero wrote on Friday, June 27, 2014:

I’m sorry. My post was not enough to describe my situation and what I want.
I’ll describe my situation and question again.

  1. The main question is how can I connect(or initialize) SD card for using in FAT SL file system like using RAM.

I did just initalize SD card hardware, and I can write something to SD card by SPI. This is not using FAT file system API, just reset and initalize SD card. So it is ready for use and wait.

I understood that the first step to use FAT file system is ‘init volume’.
How can I use init_volume function in FAT file system API for my SD card…

Something what I understand is wrong…? I don’t know how it works on SD card.

  1. Is it possible using FreeROTS+FAT SL APIs on flash memory?

It is easy to use on the RAM. For example, I can copy something to another place by memcpy(). But it is different on flash memory…

davedoors wrote on Saturday, June 28, 2014:

You have to implement each of the functions on this page
http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API_Functions.shtml

So the code you have to initialize the SD card hardware would go in F_DRIVERINIT(), and the code to write data to the card will go in F_WRITESECTOR(). As you point out though, writing data to the card is more involved than writing to RAM so you will have to replace the memcpy() calls with the commands needed by the SD card controller over the SPI bus.

Search SPI on https://www.sdcard.org/developers

zealhero wrote on Sunday, June 29, 2014:

Thank you.
I read that page so many times… it is very simple…

I will try it~

Regards.

zealhero wrote on Monday, June 30, 2014:

I spend whole day for this issue… but I don’t know how it works, initialize memory.

I read that it needs first address in flash memory or SD card on another post.
But I don’t know how can use it…

Can you give me more tips for initiate flash memory file system ?

I read web page and another docs. But I don’t know not in the least.

Regards.

rtel wrote on Monday, June 30, 2014:

Can you be more specific about where the issue is, for example:

Is it that you don’t know the SPI commands necessary to read and write to the card, or

Is it that you don’t know how to arrange data on the SD card so it correctly represents the file system (because the data should be exactly as it is in the RAM disk memory array, just stored on the card rather than in RAM), or

Is it that you don’t know how to link your SD read/write functions into the portable layer of the SD card?

Regards.

zealhero wrote on Tuesday, July 01, 2014:

Hi.

This is my situation… I want to use file system for flash memory(e.g. SD card). My embedded system use FreeRTOS 8.0, so I chose FreeRTOS_FAT_SL for file system on my board and bought license.

Unfortunately, this is the first time to develop file system to me. So I’m not familiar with it. Then I studied it and analyze ‘FAT_SL_and_CLI_Windows_Simulator’ project. But there is only codes for using RAM. I applied it to my embedded system. I made a some folders and files, and read & write something to those files in RAM on my board. But I don’t know how it applied to flash memory(SD card).

So I searched way of using flash memory because my purpose is using flash memory. I chose SD card and studied how can use it. Then I can initialize it with several CMDs by SPI communication. It forced block size to 512 bytes. But this is not related with file system, hardware was just initialized independently. I think that it is ready for use… and I know some CMDs for read or write sectors. perhaps, I can modify read or write sector functions in FAT for SD card.

I want to use file system on my SD card like using it on RAM. For example, if I input ‘dir’, file system return directory and file list in SD card to me. There are many functions in FreeRTOS FAT and I want to use them. But I don’t know how can initialize(or link?, connect?) file system on SD card. I want kinds of sequence or sample codes for initialization. or please tell me if I have misunderstood something…

Regards.

+) I think that I can use file system if it will be initialized. So please explain initialize sequence(or func) more specifically if you are okay…

rtel wrote on Tuesday, July 01, 2014:

So starting at the beginning, with initialisation, you have to provide an implementation of F_DRIVERINIT() to initialise the card before you can use it.

http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT_SL/Media_Driver_API/F_DRIVERINIT.shtml

First get the RAM disk demo running on your target hardware. You may have to set the size of the RAM disk down to a couple of K bytes to fit it in the RAM you have available, so the file system won’t be usable, but that doesn’t matter it just needs to be compiling.

To do that you can use the Win32 simulator demo as a reference, just include the same files and configuration file.

Once it is compiling and running, step into the function vCreateAndVerifySampleFiles(). It in turn calls f_initvolume(). f_initvolume() passes in a single parameter, which is a pointer to the F_DRIVERINIT() function - which in this case is called ram_initfunc(). If you continue stepping through the code you will see the first line of fn_initvolume() is “mdrv = initfunc( 0 );” which is calling the function passed in as a parameter.

Step into initfunc() and you will find yourself in ram_initfunc(). The top of that function does some RAM disk initialisation - replace that with the code you say you have that initialises the SD card (assuming that is working).

ram_initfunc() then populates a structure with functions for reading and writing sectors - just leave those for now. You will have to come back and replace them with your own SD card read and write functions - but for now just get the code compiling and the SD card initialising.

Regards.

zealhero wrote on Wednesday, July 02, 2014:

Thank you for your detailed & kind answer :slight_smile:

I can understand it and how do I make it. But I bump again…

I guess ‘ramdrv0’ is the first(start? or base?) address of the memory and I will replace it to SD card first memory address. Is it right?

but I don’t know how do I get the address of my SD card. I searched datasheet of SD card and google. But failed…

I’m sorry, I know this is not related with FreeRTOS…
But if you have any idea with this issue, please help me.

Thank you!

Regards.

rtel wrote on Wednesday, July 02, 2014:

So unlike the RAM disk, the SD card is a block device - so data can only be read or written in blocks (or sectors) or 512 bytes.

Rather than taking an address from the beginning of an array (as in the RAM disk) you take an address from the beginning of the SD card (sector 0). So to read an address you work out which sector the address falls within, and read the whole sector (from the start of the sector) to get a buffer that contains the data at the address you are actually interested in. Likewise when writing - read the whole sector back, modify the bytes you need to write to, then write the whole sector back to the disk.

Maybe the following link will help?
http://www.usna.edu/EE/ec463/notes/09_SD_Card_student.pdf

Regards.

zealhero wrote on Friday, July 04, 2014:

I’m sorry. I didn’t clear it…

It likes comeback to starting point.

I want to know how do I make ‘t_RamDrv’ structure.

typedef struct
{
char * ramdrv;
unsigned long maxsector;
int use;
F_DRIVER * driver;
} t_RamDrv;

and ‘F_DRIVER’ structure has serveral members and user_data, user_ptr, etc…

I want how they were decided…
Please give me some comments about this structure and parameters.

I have another questions. I bought both FreeRTOS and ‘FreeRTOS+FAT SL’ licenses.
Also I bougth some reference document for FreeRTOS.

Is there any reference of FreeRTOS+FAT SL?
Your webpage and comments in the code files are too brief.

Regards.

davedoors wrote on Friday, July 04, 2014:

I’m looking at that file and dont think the structure is needed at all because you can only have a single drive. The information in the structure can just be file scope variables.

// Declare an F_DRIVER structure at the top of the file.
static F_DRIVER my_driver;

So ram_initfunc() becomes sd_initfunc() and populated my_driver with the sd_readsector, sd_writesector, sd_getphy, sd_release and user_ptr members, just as you can see being done in ram_initfunct(). sd_initfunc() returns &my_driver.

sd_readsector() is the sd card version of ram_readsector(), sd_writesector() is the sd card version of ram_writesector, etc. These functions you will have to implement yourself using your SPI interface and the SD card commands to read and write sectors.

rtel wrote on Friday, July 04, 2014:

I’m looking at that file and dont think the structure is needed at all
because you can only have a single drive. The information in the structure
can just be file scope variables.

…like the attached file. A lot of the clutter, including the structure, is removed, leaving really just the init, read and write functions. Is that clearer?

Regards.

zealhero wrote on Friday, July 11, 2014:

Hi.

I have understood way to connect file system and SD card. It is really helpful to me your helps.

I made new initialize, read, write, getphy functions for filesystem and f_initvolume() function returns ‘0’. I made it. Next, I tried to format my SD card. f_format() is worked well and return NO ERROR. So I can open, read and write example txt file.

But there is a problem…
My desktop PC or cell phone cannot read that SD Card. PC or cell phone say “this card did not formatted”.

So, I formatted SD card on PC and insert it to embedded board. My system recognize it formatted already. But Some error occure when it
try read or write file. The error is… response from SD card is not
returned accurately. SD card returns 0x00 first and 0xFE second and
then other values comming when it receive some command. But it returns only 0x00 and next is not come…

I don’t know why it happen.

It is works in embedded system but not work other system(PC, cell phone) when it formatted in embedded system.

On the contrary, it works in PC or cellphone when it formatted on PC,
but it does not work on embedded system. They were formatted FAT32 on both system.

One more question… attached files are master boot record of each
system.(It is little Endian) upper one is formatted by FreeRTOS FAT+SL and lower one is by PC. FAT size 32 is represented ‘0x00000D3A’(=973930496 sectors = about 464GB)

I missed something…?

Please advise something related this issue.

Regards.

owaisfazal wrote on Thursday, September 11, 2014:

Hello, I am writing to the creator of this thread. I am doing something similar to you and if you read this, can you please provide some updates as to what is the progress of your work regarding SD card and FreeRTOS+FAT. I am working with SD card but in SD mode and I am very far off from what you have achieved. Looking forward to your help.

Regards,
Owais

zealhero wrote on Friday, September 12, 2014:

Hi. I read your another thread too and I can understand your condition.

Actually, My progress is same with my last post because I couldn’t find the reason of the problem and I should to do another work first(porting to another core).

It was finished the other works, so now I’ll try to use it again. I will make a TFTP system with SD card this time.

I think that you are almost finished. I can help you if you need. you can help me later too. We can be a good co-worker :slight_smile:

Best Regards.
Wonjae

owaisfazal wrote on Friday, September 12, 2014:

Thank you very much for your reply Wonjae.

Actually I am very far from where you have managed to reach. I am still struggling with how to read and write on the card and how is the addressing done and basic things like this.

Can you please tell me which micro-controller were you using for this matter ?

I really look forward to understand this all and hope I could learn from you a big deal.

Please stay in contact. Thanks.

Regards,
Owais

owaisfazal wrote on Friday, September 12, 2014:

zealhero wrote on Monday, September 15, 2014:

Hello Owais.

I used RM48 when I write this post. Now I’m using RM57(TI).

Regards.
Wonjae