Provide low level access functions to FatFS library
The FAT library abstracts the physical medium on which data is stored by using a few low level interface functions. Such methods are:
- disk_initialize(): initializes the disk for access
- disk_status(): returns the disk status
- disk_read(): (surprisingly) read from disk
- disk_write(): write to disk
- disk_ioctl(): miscellaneous commands
- get_fattime(): get the current time (used to update access time)
If you use the SD card driver provided with BeRTOS, these functions are already defined, otherwise you need to define them.
Provide a communication channel for SD driver
The SD driver assumes that the device is accessible through the KFile interface, so you need to open such communication channel. You can use pretty much everything that has a KFile interface, so for example SPI or Serial are fine.
Notice, of course, that the device attached to this channel must "talk" the SD protocol.
Register a work area
Now that the code is in place you are almost done. The FatFS library requires you to register a work area, which you can do with:
FATFS fs; f_mount(0, &fs);
The first parameter of f_mount() is the drive number, the second is the "filesystem" to register. You won't need this variable anymore in your program.
If you use the SD card driver you are restricted to use only one drive, but it shouldn't be too difficult to add support for more drives, if you need it.
Open and use a file
Finally you can open a file! Just declare a FatFile structure and call fatfile_open() on it. Then you can use the KFile interface to read/write from the file.
FatFile input; fatfile_open(&input, "foo.txt", FA_READ); //... uint8_t buf[10]; kfile_read(&input.fd, buf, sizeof(buf));
The last parameter to fatfile_open() (ie. open mode) is a combination of flags, which are declared in bertos/fs/fatfs/ff.h. The most common are:
- FA_READ: open a file for reading;
- FA_WRITE: open a file for writing. Can be combined with FA_READ;
- FA_OPEN_EXISTING: open the file, fail if not present.
Return values for fatfile_open() are also described in bertos/fs/fatfs/ff.h
When everything is set up, you may need to fine tune your code. For example, in some cases the channel you're using for the SD card driver is too slow for your application. I suggest to create a device that uses DMA for transfers, then tell the SD driver to use it.
If you're concerned about ROM usage, you can consider switching off some FatFS features, such as write access, long file name support and so on. Have a look at FatFS application note for further information.
Add a comment
Reactions
-
http://www.imatica.org/bloges/2009/10/221047982009.html
Publicada la versión 2.3 de BertOS, sistema operativo para trabajo en tiempo real para dispositivos empotrados. Como principal novedad, se incorporan varios drivers para distintos tipos de sensores...
-
http://www.imatica.org/blogcat/2009/10/221047972009.html
Publicada la versió 2.3 de BertOS, sistema operatiu per a treball en temps real per a dispositius encastats. Com a principal novetat, s’incorporen diversos drivers per a diferents tipus de sensors...
-
Social comments and analytics for this post
This post was mentioned on Twitter by BlackMouseLab: #embedded - A new BeRTOS version is available. Many new drivers and algorithms are available, and bugfixes too. http://ow.ly/vdK3 - ^FP

0 Comments (Add comment)