View previous topic :: View next topic |
Author |
Message |
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
Using fat.c |
Posted: Tue Apr 26, 2016 5:58 am |
|
|
Hello there,
Could I get any example using the library fat.c where the SD is written via SPI?(http://www.ccsinfo.com/forum/viewtopic.php?t=53787)
I usually like to take my time reading and experimenting, but this time I'm in a bit of a hurry, so...
Thank you very much. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Apr 26, 2016 6:46 am |
|
|
well first place to look is in the 'code library' here...
or general search 'SD'...
also
You MUST use a 3 volt( 'L' series) PIC OR proper logic level translation between the 3 volt SD card and a 5 volt PIC. If you don't .it won't work correctly !!
Jay |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Tue Apr 26, 2016 7:43 am |
|
|
temtronic wrote: | well first place to look is in the 'code library' here...
or general search 'SD'...
also
You MUST use a 3 volt( 'L' series) PIC OR proper logic level translation between the 3 volt SD card and a 5 volt PIC. If you don't .it won't work correctly !!
Jay |
Hey, thanks for the quick reply
I've tried to use the search and won't return any results when I use SD as the keyword. Anyways, thanks for the tip about the code library, found a few threads there that are being really helpful .
And yes, I'm using a 3 volt PIC, I don't want to fry the SD!
The thing is, I won't have any way to test this until thursday, and it's due friday (Don't you guys love when your deadlines are completely ridiculous? I totally do :D ) so I'm trying to put together a code that'll work quick.
Here I'm trying (or failing hard) to create a file called log1.txt, would this work?
Code: |
#define MMCSD_PIN_SCL PIN_B10 //o
#define MMCSD_PIN_SDI PIN_B11 //i
#define MMCSD_PIN_SDO PIN_B12 //o
#define MMCSD_PIN_SELECT PIN_B13 //o
main()
{
mmcsd_init()
char filename[]="log1.txt";
fat_init();
mk_file(filename);
mmcsd_write_byte(0, filename);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Tue Apr 26, 2016 8:19 am |
|
|
Comments in line:
Code: |
#define MMCSD_PIN_SCL PIN_B10 //o
#define MMCSD_PIN_SDI PIN_B11 //i
#define MMCSD_PIN_SDO PIN_B12 //o
#define MMCSD_PIN_SELECT PIN_B13 //o
//These need to be declared _before_ you load the mmc library.
main()
{
char filename[]="log1.txt";
//keep to the C standard and declare variables at the start of sections
FILE file_stream; //You need a file variable created.
mmcsd_init(); //semi-colon missing
fat_init();
mk_file(filename);
//Now you need to open the file....
//mmcsd_write_byte(0, filename);
//No. You write to the open [u]file[/u].
fatopen(filename,"w", file_stream);
fatputc('\0',file_stream); //This now writes the 0 byte to the file
fatclose(file_stream); //then you must close the file
while (TRUE)
;
//
}
|
Also general comment you must have the pull-up resistors on some of the SD lines. required to ensure it wakes in the right mode, and also recommended to speed the rising edges. |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Tue Apr 26, 2016 8:36 am |
|
|
Ttelmah wrote: | Comments in line:
Code: |
#define MMCSD_PIN_SCL PIN_B10 //o
#define MMCSD_PIN_SDI PIN_B11 //i
#define MMCSD_PIN_SDO PIN_B12 //o
#define MMCSD_PIN_SELECT PIN_B13 //o
//These need to be declared _before_ you load the mmc library.
main()
{
char filename[]="log1.txt";
//keep to the C standard and declare variables at the start of sections
FILE file_stream; //You need a file variable created.
mmcsd_init(); //semi-colon missing
fat_init();
mk_file(filename);
//Now you need to open the file....
//mmcsd_write_byte(0, filename);
//No. You write to the open [u]file[/u].
fatopen(filename,"w", file_stream);
fatputc('\0',file_stream); //This now writes the 0 byte to the file
fatclose(file_stream); //then you must close the file
while (TRUE)
;
//
}
|
Also general comment you must have the pull-up resistors on some of the SD lines. required to ensure it wakes in the right mode, and also recommended to speed the rising edges. |
First of all, thank you very much Ttelmah for being so thorough, this helps a lot, as I don't even have the IDE in the PC I'm using today, so I'm using notepad.
I think I have the basics down by now, will work on it tomorrow and hope everything works fine on Thursday (Praying to the gods of compilation now...) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Tue Apr 26, 2016 9:24 am |
|
|
One warning. In the code library you will find three threads, including an SDHC modification, and a couple of fixes for faults in the standard SD driver. There is then an 'accrued' version at the start of the forum as a sticky (from electr0dave). Use this.
This fixes all the current bugs with the MMC driver and also gives support for cards over 2GB. |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Wed Apr 27, 2016 2:02 am |
|
|
Ttelmah wrote: | One warning. In the code library you will find three threads, including an SDHC modification, and a couple of fixes for faults in the standard SD driver. There is then an 'accrued' version at the start of the forum as a sticky (from electr0dave). Use this.
This fixes all the current bugs with the MMC driver and also gives support for cards over 2GB. |
Roger.
I could get my hands on the IDE earlier than I expected, tried this:
Code: | #include <main.h>
#define MMCSD_PIN_SCL PIN_B10 //o
#define MMCSD_PIN_SDI PIN_B11 //i
#define MMCSD_PIN_SDO PIN_B12 //o
#define MMCSD_PIN_SELECT PIN_B13 //o
#include <mmcsd.h>
#include <fat.c>
void main()
{
char filename[]="log1.txt";
FILE file_stream;
mmcsd_init();
fat_init();
mk_file(filename);
fatopen(filename,"w",file_stream);
fatputc('\0',file_stream);
fatclose(file_stream);
while(TRUE)
{
}
} |
But gives me a few errors:
Code: | *** Error 90 "main.c" Line 17(27,28): Attempt to create a pointer to a constant
*** Error 51 "main.c" Line 18(20,31): A numeric expression must appear here
*** Error 51 "main.c" Line 19(16,27): A numeric expression must appear here
|
Line 17 is fatopen.
Before I forget, my compiler version is 5.045. |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Wed Apr 27, 2016 2:08 am |
|
|
Fixed:
Added the line
Code: | #device PASS_STRINGS=IN_RAM |
after the #include <33EP512GP504.h>
and changed the declaration of
to
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Apr 27, 2016 2:15 am |
|
|
#device PASS_STRINGS=IN_RAM
Needs to be added.
Then all the references to file_stream, need & (except in the declaration). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Apr 27, 2016 2:31 am |
|
|
Whoa.
Your way of 'fixing' it, means you can be writing to an undeclared area of memory. It'll compile, but won't necessarily work.....
Problem is that:
File *file_stream;
Means that 'file_stream' is now a pointer, but doesn't actually declare any where for it to point 'to'.....
Declaring 'FILE file_stream;' declares the actual variable.
If you want to save typing '&', do this:
Code: |
FILE actual_stream;
FILE *file_stream=&actual_stream;
|
Then you can use 'file_stream' as a pointer to 'actual_stream', and it will be pointing to legitimate memory.... |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Wed Apr 27, 2016 2:34 am |
|
|
Ttelmah wrote: | Whoa.
Your way of 'fixing' it, means you can be writing to an undeclared area of memory. It'll compile, but won't necessarily work.....
Problem is that:
File *file_stream;
Means that 'file_stream' is now a pointer, but doesn't actually declare any where for it to point 'to'.....
Declaring 'FILE file_stream;' declares the actual variable.
If you want to save typing '&', do this:
Code: |
FILE actual_stream;
FILE *file_stream=&actual_stream;
|
Then you can use 'file_stream' as a pointer to 'actual_stream', and it will be pointing to legitimate memory.... |
It's 9 AM in the morning, I've had way too little coffee and I'm absolutely blaming it on that.
Thanks Ttelmah, you're being a real life-saver. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Apr 27, 2016 3:06 am |
|
|
I'm sorry I left the '&' off when typing my stuff.
We all do it. |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Fri May 06, 2016 2:43 am |
|
|
Hello again , things got really busy and I couldn't work on this project, but I'm back on it :D.
It seems the program keeps looping at
If I comment this line here:
Code: | #use FIXED_IO( B_outputs=PIN_B2, PIN_B10, PIN_B11, PIN_B15) |
It won't get stuck on that part, but will keep returning -1 (Error I guess).
Any idea why could this be? (I'll be uploading the signals I got with my logic analyzer in a few minutes)
Last edited by skelzer on Fri May 06, 2016 3:59 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Fri May 06, 2016 3:51 am |
|
|
Have you actually tested that you have the sector read and write working on the device?.
Fat_init, calls the mmcsd_init. This does the speed changes to switch the device up to the faster SPI clock. The way it is written, is fractionally silly. Instead of exiting if mmcsd_init errors, it ploughs on and tries to read, to get the sector size, and data layout. So if the init has failed, it can get into a really problematical state.
I'd be checking that you have the mmcsd driver working properly before trying to get fat working. |
|
|
skelzer
Joined: 21 Apr 2016 Posts: 20 Location: Spain
|
|
Posted: Fri May 06, 2016 4:01 am |
|
|
Ttelmah wrote: | Have you actually tested that you have the sector read and write working on the device?.
Fat_init, calls the mmcsd_init. This does the speed changes to switch the device up to the faster SPI clock. The way it is written, is fractionally silly. Instead of exiting if mmcsd_init errors, it ploughs on and tries to read, to get the sector size, and data layout. So if the init has failed, it can get into a really problematical state.
I'd be checking that you have the mmcsd driver working properly before trying to get fat working. |
I'd swear you comment everytime I'm editing my post...
Will check it out as soon as I'm done uploading the signals I got with the logic analyzer. |
|
|
|