Posts: 72
Joined: Sat Jan 13, 2018 12:29 am

Writing to files on the raspberry pi 3

Mon May 07, 2018 12:11 am

Hi, I'm not quite sure where to start to achieve this I have read and mostly understand the tutorial series put out by bzt however writing to files is out of the scope of the tutorial. So how is this achieved? When you write to the sdcard how do you make is so that the file isn't all over the card is it contiguous or scattered. How do you know if your overwriting something else? Any help would be greatly appreciated!

Posts: 783
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing to files on the raspberry pi 3

Mon May 07, 2018 3:21 am

The Pi SDcard has a standard FAT16 or FAT32 file system and you are required to implement that or use a library from someone who has.

At it's most basic you have functions to read and write to given raw sector on the SD card. Sitting over that is the file system which controls the linking and data held in each raw sector as it does in any FAT implementation.

I did a minimal cut down read version if it helps to look at the code ... r/SD_FAT32

In my implementation the raw SDcard sector being read or written is controlled by this function

Code: Select all

SDRESULT sdTransferBlocks (uint32_t startBlock, uint32_t numBlocks, uint8_t* buffer, bool write );
That is I provide the raw start sector number, how many blocks I am going to read/write provide the buffer for or with the data and a flag as to read or write.There is a lot of code sitting under that call and it has one simple requirement that before you use that call in anyway you first call the function sdInitCard which initializes the SD card system. Optionally you can set the mount flag in that call and it will initialize and read the FAT system.

If you need more detail a good start would be

I also like to remind people if you are using FAT32 commercially, Microsoft has patents on the FAT32 format and use of them requires a license even on embedded systems. I refer you to ... TomTom_Inc.

Posts: 143
Joined: Sat Oct 14, 2017 9:57 pm

Re: Writing to files on the raspberry pi 3

Wed May 09, 2018 10:57 am

Hi @LdB,

Your code is awesome, thanks for sharing!

On the Microsoft patent issue, I'd like to point out, that FAT32 is not licensed. The license is limited to writing long file names along with generated short filenames (8+3) at once, so you can easily avoid all licensing issues if:
1. you only use ASCII 8+3 filenames (enough for bare metal development and embedded systems. Most devices use this, like cameras, video recorders etc. to avoid license fee. Like DSC00001.JPG is an ASCII 8+3 name, no need for LFN)
2. you only use long filenames, and use a random name for short ones (that's what Linux does, approved by lawyers)


Return to “Bare metal, Assembly language”

Who is online

Users browsing this forum: No registered users and 3 guests