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:

Re: Writing to SD Card

Post by jollybv »

Hi Benj

The class is 4 on the new addlink SD cards but the sandisk card that works dose not have anything on except (micro SDHC I) its the older one. The return value is 254, both SD cards are 4GB

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,

Return value 254 is that the card has been detected and has replied to command 8 - CMD_SEND_IF_COND.

However the next command to get the card into operational mode are repeatedly failing 41 - CMD_SEND_OP_COND.

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 for that So what can we do to stop it failing?

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

I went and bough another make SD Card Storntium 4GB Class 6 and have the same problem returns 254 and and 239 when it tries to open the file on the card but i suppose that is because the card is not going into operational mode could this problem be linked to the speed of the 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,

I have a class 4 SanDisk SDHC card and it works very well. I've also tried class 10's with success before. The class specifies the minimum sustainable write speed per second in MB and the initialise only reads the partition tables etc so that shouldn't be the issue.

What micro are you using and how fast is it running?

For the init to work correctly you must clock the commands in at 400Kbps or lower and then once init is out the way you can bump up the SPI clock speed, this should all happen automatically but maybe we are not slow enough to begin with?

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 i have 2 sandisk cards and they both work well its the other new cards i have just purchased that don't work the Processor is a PIC18F46k80 the crystal is 19.6608 the PR Scale is FOSC/16 I have change it with no luck the channel is software.

In the test program below i have tried to change the PIC Configurations but nothing works please have a look and see if Im doing something wrong or if its a bug in the Fat1 component
Attachments
SD_Card_Test_Programming_Example.fcfx
(41.26 KiB) Downloaded 359 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 Benj

Have you any Ideas on how to solve this SD card problem as I'm about to put my product on the market but can't until this is sorted. I have teried changing the config settings on the micro controller no help what can you suggest to solve this? do you think this is a problem in the Fat1 or a timing problem

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,

I believe the problem is the cards your using / the FAT component. I know the FAT component uses the same initialise type code as Microchip and Arduino so my guess is that these would also fail on the cards your using. I have spend a lot of time working on the FAT component to get it to the level where it works on pretty much anything. However it appears there are new cards out now that for some reason do not work.

I will put it on the list to try and get hold of one of the new problematic cards and try to get it to work but I have a lot of scheduled work on at the moment and so will not likely get around to this for a while.

There should be lots and lots of cards available that will work so I suggest you mark the cards that don't work and put them to one side for now. If you could send me a none working card then that will likely make my life a bit easier and hopefully enable me to get around to looking into the problem for you a lot sooner.

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

I will use the sandisk cards for now and send you the 2 cards I have here that don't work please give me an address to send to

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,

Our address is:

ATO Ben Rowland
MatrixTSL
33 Gibbet Street
Halifax
HX15BA

If you include your return address in the package then I will make sure that the cards are returned to you after our investigation.

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

Is the any way to speed up the the reading time of the SD card what I'm doing is scanning 1000 16 bit positions on the SD card and comparing it with a value if matches then exit and and send a string this all works but it takes quite some time (10 Minutes)to read the SD card which wont work for my situation as it must take a second or 2 dose anyone know how I can speed up the read time on the Fat 1
Attachments
Read_SD_Test.fcfx
(15.15 KiB) Downloaded 351 times
Last edited by jollybv on Tue May 12, 2015 2:24 pm, edited 1 time in total.

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

Just been going over the Fat Component would it be better and faster if i used the (ReadStringFromFile) Component

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'm not 100% sure what your trying to do but found a number of problems so I have had a go at cleaning things up and getting rid of surplus code.

Certainly should not take 10 mins to read 1000 * 10 bytes from a card.
Read_SD_Test.fcfx
(13.74 KiB) Downloaded 306 times
The LCD writes inside the loop will likely add relatively big delays in the region of milliseconds so if you can get rid of some updates e.g. only draw the number if the .Index variable is a multiple of 100. I added a LCD update every 10 iterations through the loop to try and gain a bit more speed.

Do you want to read a single value at a time or read all values at once, all values at once will be a lot quicker than reading each item one after another but means you need a chunk of RAM to store the values. If you only need one value at a time then I can change the macro to make it more efficient.

Anyway 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 Ben

Thanks this all works fine now i took everything out the macro that was not needed i only had added the LCD wright just to see what was going wrong. To explain to you what in doing, I have for example 1000 of 10 digit phone numbers stored on the SD Card when i call the GSM module it must check if the number calling in is in the phone book if so then trigger an output this all works fine. there was one Change i needed to do to the .NumString[.idx] = Fat1 :: ReadByteFromBuffer(.idx + .SectorStartPosition + 1) as it was not reading the first digit so i changed it to
.NumString[.idx] = Fat1 :: ReadByteFromBuffer(.idx + .SectorStartPosition) dropping the +1 now it is working like a bomb reads the 1000 places in a second compares the numbers and outputs the string to the GSM module :D

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

I'm a bit lost and not sure how to sort it what I'm trying to do is build a RFID tag reader This i have done now I'm trying to store it to the SD card which must scan the card first (1 - 1000) and the first available position store the tag number. I have tried to scan the card and look for the length then if its below a certain value go store but this dos not work as the card is blank and returned nothing when doing check the length. Anyone got any idears :?:

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

Quick question when writing to the SD card how would I make the files to be stored in a column instead of a line I have tried using the retval=FAT1::AppendStringToFile"\n" followed by retval=FAT1::AppendStringToFile"\r" before and after the retval=FAT1::WriteFileSector but dose not seem to work any Ideas

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,

The write sector writes a full 512 byte sector to the card. If you then use the append to file function then it will add the carriage return and new line to the start of the next sector. However when you then write the next sector you will overwrite your appended data. Can you instead add the '\n' '\r' as bytes to the 512 byte sector?

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 Ben

Thanks i will try to add it and see how it goes

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

Bit lost in storing the data to the SD card In a column format, what I'm doing is taking the variable with the data to be stored then adding the "\n" then then adding the "\r"
StorVal = "255"
StorVal = StorVal +"\n"
StorVal = StorVal +"\r"

But this method dose not seem to work as all data is in a line not column any other ideas what i can do?
Attachments
Test_storage.fcfx
(15.88 KiB) Downloaded 191 times

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,

What file format are you saving your file in? Also what are you using to view the file. I find Textpad or Notepad++ works very well to show text files properly.

Windows needs the newline in the format of CR LF so you would need to reverse the order in which you assign the escape codes.

StorVal = "255"
StorVal = StorVal +"\r"
StorVal = StorVal +"\n"

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 Ben

Thanks for that i was using standard notepad but now Using Notpad ++ it is now sort of in a column is there any way to get rid of all the NUL's before the data so the data starts at the left like the first line

255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255
NNNNNNNNNNN255

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,

If you use the AppendString to file function then it will only write the string data and you don't have to worry about moving to the next sector etc.

Open File
Loop Start
Prepare String To Store e.g. "255\r\n"
Append String To File
Loop End

Your currently writing the data in fixed blocks of 16 which is why you are getting all the nulls in the data file.

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 Ben

I have sorted the Nul problem by putting spaces in to full up the blank but now i have another problem it dose not seem to change to the next sector it works up to 512 bytes but if i go over 512 bytes it clears everything stored and i cant view it
Attachments
Test_storage.fcfx
(16.89 KiB) Downloaded 174 times

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,

I have attached two modified versions of your program.

The first is doing things how you had them originally but now we should be able to write more than 512 bytes. Note that the card only gets written when the sector is full so this may miss the last few writes which is not ideal. This program will also have issues if the file already exists with when you start logging. You could call the write sector function every time like you used to which gets around the first issue but will take longer to execute.

The second example is using the append function which should get around all of the problems mentioned above.
Test_storage.fcfx
(14.85 KiB) Downloaded 202 times
Test_storage2.fcfx
(12.04 KiB) Downloaded 204 times
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 Ben

Thanks for this but my only problem is that it takes 5 minutes to complete which is no good i have tried changing the clock speed from 20Mhz to 48Mhz using the PLL but not sure if i'm setting the clock speed right. When I time it with the PLL selected it takes the same amount of time as when I just set the clock to run at 20Mhz
Attachments
Test_storage2.fcfx
(15.26 KiB) Downloaded 176 times

Post Reply