Writing to SD Card

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Writing to SD Card

Post by jollybv »

Hi guys

Just got started with the FAT1 Component not to sure how it all works but i was looking at the (Fat_Data Logger) example and i see its quite easy, you Initialize the card if it returns a 0 then it moves on in the program to create a File then once the file is created it goes to open the file if file ok then it moves on to the write procedure (retval=FAT1::AppendStringToFile(string veriable) ) then the next instruction I"m not sure what they do ...retval=FAT1::AppendStringToFile("/n")... and .... retval=FAT1::AppendStringToFile("/r") is it termination characters?? Another thing how do i change mem locations and dose the simulator simulate the SD Card or must i test it in hardware.

Brian

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hello Brian,

"\n" is a new line and "\r" is a carriage return, the \ character is referred to as an escape character and used to elevate the next character to perform a function. e.g. "\t" is a tab and "\\" allows the \ character to be displayed. The new line and carriage return are added to allow each of the data strings to appear on a new line when viewing the file in a text editor.

The latest FAT component in v6 has a read line which allows you to read a single line from a file so adding the "\n\r" can be very useful, say you need number index 32 you simply open the file and then read 32 lines, the last read gives you the data you need.

The AppendStringToFile function automatically skips to the end of the file and adds the data you have entered using the string variable. This means you can store string in the file and every time you need to store a new number you call the append function again.

The FAT component in v6 is fully functional simulation wise apart from the scan folder function which is used for dynamic file access such as photo galleries where you simply dump the images into a folder on the card. The console window should be helpful in helping to debug whats going on.

Let me know how you get on.

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

This seems so much easier than writing to EEPROM :D you say that the simulation works 100% then i must have a bug or doing somthing wrong when i run the simulation the program sits in the initialise loop and i get a a message in the consol FAT Data...
Initialise Card- Simiulation root folder not found... do i need to put one of the injectors components in.

Brian

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

If you select the FAT component on the panel and look at the component properties then there is a property named Root directory.

If you want the directory to be the Flowcode project directory then change the setting to $(srcdir)

Otherwise change the directory to a folder on your file system, e.g. I often use C:\FC\ as my test directory.

Looks like your current setting is pointing to as directory that does not exist on your computer.

No need for injector components, they are more designed for comms based components.

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Thanks that worked fine creating a file on my HD (C:\FC\ Phone_Number). I can also write to the mem and change the values using ....ReadFileSector() followed by WriteByteToBuffer-Variable[index] then do a WriteSector this is great :D I can put 36 numbers into a sector how would I change from sector to sector Iv'e tried using the ....MoveToNextFileSector command what happens is that it clears the first file sector so not sure what I"m doing wrong please Help. Also when i try to reed from the mem location i have to open the file twice if i reed it once it gives me a 0 and not the ASCII Character In the string im reading back but on the second pass it reads back correctly is this wright??

Brian
Attachments
SD_Card_Test_Programming_Example.fcfx
(40.47 KiB) Downloaded 538 times

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Guys

This file sector changing is really confusing me :? In the FAT1 dose it automatically change to the next sector when the first 512 bytes are full ?? When I go over I get a message... Move To Next File Sector: End Of File Sector... If i need to change the sectors how do i do this?? If i do a ReadFileLengt it always gives me a 512 because im using the WriteFileSector so i cant calculate when the file is full it always seems to be full. Also when I put the MoveToNextFileSector(1) instruction in the program and write a number I cannot see anything in the file I have created C:\FC\...file... im only able to view the first sector in Notepad thereafter nothing. Is there anyone who can help.

Brian

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

I want to try and help you out as we've been batting the problem of saving and restoring numbers on various targets for a while.

I can create a simple program that allows you to store and retrieve say up to 255 string variables each of which can be up to 16 characters in length.

Is this all you need or do you also need to store a reference number with the string? If so then what is the range of this ref number? Would a UINT (0 - 65535) do the job?


In answer to your questions:
dose it automatically change to the next sector when the first 512 bytes are full ??
It depends what function your using. Append string to file jumps to the end of the file. The file read example might be useful if you want to extract data from a specific location.
If i need to change the sectors how do i do this
A FAT file system is made up of sectors, groups of 512 bytes, which are arranged into clusters of normally 32 sectors, each cluster is then addressable by the FAT table near the start of the disk. The readfile length macro tells you the number of bytes in the current sector. 512 would likely mean that the sector is full and if there is no "next sector" in the file then that is the end of the file.

The datalogger example shows how to write a file the simple way using the append function and also the harder yet faster way using sector manipulations.
Also when I put the MoveToNextFileSector(1) instruction in the program and write a number I cannot see anything in the file I have created C:\FC\...file... im only able to view the first sector in Notepad
I would need to see your program but are you sending the updated buffer to the card after performing the write?

e.g.
Write Byte To Buffer(0, 'H')
Write Byte To Buffer(1, 'i')
Write File Sector ()

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Ok I have made a example program to allow you to store a 16 character string to one of 256 locations 0-255 using a keypad.

From tests here it looks like the simulation might have some bugs as the file open seems to fail every other time I call it. I know I tested all this a while ago so hopefully I can find out what's gone wrong. I'm using a high speed SSD so maybe this has something to do with it?

Anyway here is the file. It seems to mostly work in simulation but there are problems with going over the first sector i.e. index 32 and so on. The embedded hardware should function correctly. A fix to get around the issue would be to make the text file first using say notepad and just make a massive text file filled with garbage. This way all the sectors exist and you can read / write correctly, this worked for me.

You will likely need to change the filename variable and the FAT sim root directory to match your setup.
NumberStorage.fcfx
(24.09 KiB) Downloaded 533 times
# or * ends the number and string data entries, might be good to have a delete / back option to back out of a incorrect entry.

Any questions let me know.

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Thanks a lot for this i will go through this thoroughly and and let you know how I get along, I also created a test program which Iv'e posted witch works well except I did not know how to change the sectors I'm not sure if you would like to check to see what am doing wrong there?(SD_CARD_Test_Programming_Example) The delete file also dose not work all the time as well it only delete's every second or 3rd pass is this correct??

Brian

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

This program is great just wondering using this method I can now get away with only storing a 10 digit number I also need to to modify it from 0 - 255 to 0 - 999 would I need to change the...Work out sector and start position... calculation

From
.sector = (.Index * 16) >> 9
.SectorStartPosition = (.Index * 16) - (.sector << 9)

To
.sector = (.Index * 10) >> 9
.SectorStartPosition = (.Index * 10) - (.sector << 9)

as I'm not quite sure exactly how this works could you please explain it to me

Brian

Brian
Last edited by jollybv on Thu Jun 19, 2014 5:42 pm, edited 1 time in total.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

I would advise to keep the string length at 16 bytes to maintain simplicity. The 512 byte sector splits into 16 very nicely e.g. 32 x 16 = 512 where as 10 might give more of a headache as you would sometimes have to do processing in two sectors.

Your on a SD card so there is tonnes of room.

To modify to allow more than 255 you would need to change the byte variable to an int in some places. That might be all you need to do.

>> 9 is a more efficient way of writing / 512.

<< 9 is a more efficient way of writing * 512.

To explain this lets add some values, say index = 0

Calculates the sector the string is stored in
.sector = (0 * 16) >> 9
.sector = 0 >> 9
.sector = 0

Calculates the start byte within the sector
.SectorStartPosition = (0 * 16) - (.sector << 9)
.SectorStartPosition = (0) - (0)
.SectorStartPosition = 0

Now lets say index = 1

.sector = (1 * 16) >> 9
.sector = 16 >> 9
.sector = 0

.SectorStartPosition = (1 * 16) - (.sector << 9)
.SectorStartPosition = (16) - (0)
.SectorStartPosition = 16

Now lets say index = 125

.sector = (125 * 16) >> 9
.sector = 2000 >> 9
.sector = 3

.SectorStartPosition = (125 * 16) - (3 << 9)
.SectorStartPosition = (2000) - (1536)
.SectorStartPosition = 464

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Thanks a lot for all your help I'm starting to get it now :D and seeing how you did this code had opened my eyes to so many other ways of using flow code.

Brian

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Guys

My Chips finally arrived now I'm trying to test and having a problem with connecting to a micro SD Card, not sure if it is a level shift problem or what, what i have is a PIC18F46K80 processor and the SD Card is connected as follows
PIC.......... SD Card
C5 MISO .. DO Pin 7
C4 MOSI .. DI Pin 3
C3 SCK..... SCK Pin 5
C2 CS....... CS Pin 2
...............3.3V pin 4
...............GND Pin 6

The processor clock speed is 19.660800 FOSC/16 the SD Card is SanDisk 4GB HC 1. I have a resistor network to drop the voltage to 3.3V on DI, SCK, CS and DO is connected straight to the PIC there is 3V on this pin DO. If i connect a scope to the DO out I'm getting data coming out if i disconnected the DI pin the data stops so something is happening but the test (NumberStorage) program will not go past the Initialize stage. Dose anyone have any suggestions..??

Brian

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

Your connections look ok to me and the SPI / 16 should be giving you a 306KHz clock which is below the 400khz threshold so this should also be fine.

In your config settings do you have the extended CPU option disabled?

How are you generating the 3V3 and is there a capacitor between the 3V3 rail and Ground near to the SD card?

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Yes The Extended instruction set is off I'm getting the 3.3V from a the 5V rail dropping it through a 1K8 resistor and a 3K3 to ground then I have a 10uF cap across the 3K3 resistor verry close to the SD Card.

Brian

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

From a brief bit of reading around it looks like the current requirements of the SD card can get as high as 80mA. Your potential divider resistances are only currently allowing 1mA to get to the card.

if you drop your resistances to the following then it might work, a 3V3 regulator would be more efficient.

180R and 330R would give you around 100mA at the 3V3 junction for the card but a lot of this might also be lost through the 330R resistor so let me know how you get on.

Another way might be to use diodes to drop the voltage, a standard diode will drop 0.7V and a schottky diode will drop about 0.2V. So two standard diodes and a schottky in series will drop from 5V to 3.4V without any significant current being lost.

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Thanks I will put a 3.3V regulator on board for the power will the resistor network still work for the DI, CS and SCK ??

Brian

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

Yes I have used similar potential divider networks in my projects and they work great, your resistor values should work fine as it's just the 3V3 rail that supplies the current to operate the card.

Let me know how you get on.

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Sorry took so long to get back yo you but only just got components and made a new board today. Putting a 3.3V regulator to power the SD Card and a 2N7002 FET as a level converter on DO the system works like a dream I will never have storage problems again :D Thanks for your help

Brian

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Not sure why I'm having a problem with the new SD card I have just plugged into my board its a addlink 4GB HC micro class 4 SD card when i plug it in it wont initialise but plug the old one in it works great
if i plug the new card into my phone it reads it so card seams fine. What could the problem be

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hello Brian,

We have a couple of SDHC cards here I use for testing and these are working well. Do you have a link to the specific card your using so I can try and sniff around it and see if there is an issue.

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Thanks the link i have ishttp://www.addlink.com.tw/#!microsdhc/c240v what i have noticed i also have a 4GB Sandisk card I can see it but not write to it. The 2GB SanDisk card works fine reading and writing i have taken the cards off the board and checked it in my phone by copping pictures to them and they work great so a bit lost

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

Another thing i have seen is the 4GB Sandisk card that dose initialise dose not create a phone book

User avatar
jollybv
Flowcode v5 User
Posts: 374
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Has thanked: 81 times
Been thanked: 25 times
Contact:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

The not creating of the file in the SD card was a fault in my software but the initialise of the addlink SD card wont work. After i exit the loop in a time out of the initialise it checks to see if a file has been created on the SD card if not it creates a new file which works and i can read and wright to it. So its very strange that the initialise dose not work for this card.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Writing to SD Card

Post by Benj »

Hi Brian,

Hmm so it won't initialise but if you bail out of the initialise process then it works. I'm guessing most of the bits and pieces during the init process have been found and its a technicality that is stopping it from working correctly. Otherwise it would just garbage the file system on the card.

Could you tell me what number the initialise routine is returning? That might help to determine what is going wrong.

Out of interest what class is the card? It's the number in the circle printed on the card.

Post Reply