calinp

[Energia Library] Petit FatFS SD Card Library

136 posts in this topic

Hello,

 

This is a port of ChaN Petit FatFS SD library targeted to low RAM microcontrollers and offers a minimal FAT implementation, with limited write support. For more details see http://elm-chan.org/fsw/ff/00index_p.html

 

A lot of credits goes to the main contributors to this topic http://forum.43oh.com/topic/1427-petitefs-fatfs-sdcard-usci-code/

 

 

Because this library makes use of the SPI library included in Energia it  works for both MSP430 and Stellaris launchpads.

 

With MSP430 I noticed some interference with Serial.print so before every call to the fat library I had to add a small delay. On Stellaris board these are not necessary. 

 

 

Unzip and copy in Energia\hardware\msp430\libraries or Energia\hardware\lm4f\libraries .

 

[uPDATE] See post #15 http://forum.43oh.com/topic/3209-energia-library-petit-fatfs-sd-card-library/#entry30031

 

[DOWNLOAD LINK] http://forum.43oh.com/index.php?app=core&module=attach&section=attach&attach_id=3019

larsie, Rickta59, xv4y and 3 others like this

Share this post


Link to post
Share on other sites

Calin,

Thanks for your reply regarding my SD card read problem. Thought it best to discuss it in this forum as it could help others that might have this problem.

 

Following your suggestion I lowered the SPI speed, by  two changes in diskio.c function DSTATUS disk_initialze() where the first occurrence of SPI_SET_DIVIDER edited from 64 to 128, and the second occurrence 2 to 4.

 

But still unable the read the two new micro SD cards. As the original SD card works ok  I think it is how I format the two new SD cards.

I formatted under Windows 7 and selecting FAT. When I compared properties  of the readable SD card with the other two, the only difference  is that the good SD does not support ReadyBoost, whereas the other two can. Hence Windows 7 has detected a difference, that may be a clue to the problem. All three SD 4GB cards are from the same manufacturer.

 

Will dig up my old XP notebook and see if a non Windows 7 FAT format fixes the problem.

 

Grant  

 

 

Share this post


Link to post
Share on other sites

G'day Calin,

Well using XP windows was disappointing, it only allows FAT32 formatting. Anyway have at least one Micro SD card working OK and that solves my immediate problem.

 

On another point, as you can only write to a preexisting file (created in Windows) it makes datalogging csv files a bit of a problem.

Ideally its nice to create a new file each time you start datalogging. Again I have been pampered by the Arduio libs that allow this.

Let me know if you are working on this, if not, I will have a go at modifying your library to include this feature.

 

Grant

 

 

Share this post


Link to post
Share on other sites

Hi Grant,

 

Try to format the cards that are not working with HP USB Disk Storage Format Tool : http://www.softpedia.com/get/System/Hard-Disk-Utils/HP-USB-Disk-Storage-Format-Tool.shtml

 

The write function has some limitations :

  • Cannot create file. Only existing file can be written.
  • Cannot expand file size.
  • Cannot update time stamp of the file.
  • Write operation can start/stop on the sector boundary.
  • Read-only attribute of the file cannot block write operation.

See more here: http://elm-chan.org/fsw/ff/pf/write.html

 

The FatFS lib does not have these limitations and provides more functions, but will not work with only 512bytes of RAM.

With Petit FatFS you should create one or more empty large files and write until they are full.

 

In case you want to append records to a non empty file you must find the last sector written and start writing on the next one.

 

Below is an update to the library:

- SPI clock divider can be specified in the begin function like FatFs.begin(cs_pin,2); -> 16MHZ / 2 = 8MHz SPI Clock

- added string functions from FatFS:

FatFs.gets - Read a string

FatFs.fputc - Write a character

FatFs.puts - Write a string

FatFs.printf - Write a formatted string

- Inherits from print class so you can use :

FatFS.print()

FatFS.println()

[EDIT] Forgot  about FatFs.close(). It only does pf_write(0, 0, &bw) but is easier to use.

 

Important! Apply these patches:

http://forum.43oh.com/topic/3272-msp430g2553-issue-hardwareserial-io-corrupts-spi-io/

http://forum.43oh.com/topic/3163-fixed-%E2%80%94-msp430g2553-spi-issue-first-byte-not-read/''>http://forum.43oh.com/topic/3163-fixed-%E2%80%94-msp430g2553-spi-issue-first-byte-not-read/'>http://forum.43oh.com/topic/3163-fixed-

PFatFs07.zip

bluehash likes this

Share this post


Link to post
Share on other sites

Calin,

As I've moved over to the Stellaris, RAM memory no longer a problem. So it looks like I'll leave Petit FatFS, and start using FatFS.

Can't see an energia type FatFS library anywhere, so will try to convert it over. Then again, it took me three weeks to do the same thing with OneWire, so this might take a while.

 

Tks for your past help.

 

Grant 

Share this post


Link to post
Share on other sites

Calin,

Yup the stellaris SD Lib looks good, I originally thought it was a Petit one. Have done a few changes and should be finished soon.

Will talk to you on the stellaris forum, as it's of no use for the 430, due to ram limitations

Grant.

 

Share this post


Link to post
Share on other sites

Of course. As a starting point you can grab the Stellaris Fatfs lib and adapt the diskio driver from Petit Fatfs (based on Energia SPI library).The Petit lib is more "Arduino style" and it works both on MSP430 and Stellaris Launchpads without any change.

 

I plan to make these changes to the Fatfs lib in the next weeks.

 

Calin

Share this post


Link to post
Share on other sites

Calin,

Am a bit naught here asking this stellaris type question (but the site is down at the moment) plus its to do with your other FatFS lib.

The Petit used SPI(2) where the FatFS uses SPI(0). Is there a command that can force Petit to useSPI(0) or to force FatFs to use SPI(2).

I want to allow the booster card I'm making to suit both Petie on the 430 and FatFs on stellaris.

Grant

Share this post


Link to post
Share on other sites

Hi,

Now you can't select a different port for FatFS, but I plan to implement this so existing shields can be used on both Launchpads. 

Share this post


Link to post
Share on other sites

In diskio.c that was taken from StellarisWare. I think you have to change SSI0 with SSI2 and make sure the GPIO are initialized properly

Share this post


Link to post
Share on other sites

A small update:

- all config settings in one file - pffconf.h

- a soft SPI driver ( shuftIn/shiftOut ) that can be enabled in pffconf.h. It defaults on HW SPI pins, but these can be changed using FatFs.begin(cs_pin, sclk_pin, mosi_pin, miso_pin); . The speed is very low, but it frees 3 pins that can be used as analog inputs or I2C.

- added a small benchmarking example

 

Tested on MSP430G2553 and Stellaris Launchpads. A minimal program should work also on MSP430G2452. 

On fraunchpad the examples compile without errors but I don't have the board to test it.

 

Calin

 

 

PFatFs10.zip

PFatFs_SpeedTest.txt

PFatFs_SpeedTest430.txt

muhammet, energia, sirri and 2 others like this

Share this post


Link to post
Share on other sites

See Calinp post above mine. There is an example in the package.

Thanks, i have seen the sample but it looks quite complicated :/  I will try it soon though.

Share this post


Link to post
Share on other sites
 

I have formatted my SD Card to FAT file system 


My pin connections are as below (checked in the petit fatfs libray)

(and 10 for the CS)

I have pasted t_read and t_write .txt files into the sd card..

P.S. My 16MHz Crystal is not soldered is this an issue for this task?

 

  uint8_t data;

  uint8_t _SCLK = 7;

  uint8_t _MOSI = 15;

  uint8_t _MISO = 14;  //defaults on HW SPI pins

 


I am getting this message on serial monitor and so when i check my sd card i see that t_write is still blank..

 

Press button to start...

 

Open a test file (t_read.txt).

 

Failed with rc=6

post-30056-0-04123200-1363720936_thumb.jpg

Share this post


Link to post
Share on other sites

Hi Sirri,

 

From the pictures I think you are using an SD adapter that have level shifting so it can interface to 5V microcontrollers (like Arduino).

This will not work, because the MSP430 is a 3.3V device and the voltage levels from the card will be to low.

Try to connect the card directly using the first method from http://www.43oh.com/2011/02/five-cheap-breadboard-prototyping-tips-and-tricks-list-2/

( this is what I used )

 

Regards,

Calin

sirri likes this

Share this post


Link to post
Share on other sites

Hi Sirri,

 

From the pictures I think you are using an SD adapter that have level shifting so it can interface to 5V microcontrollers (like Arduino).

This will not work, because the MSP430 is a 3.3V device and the voltage levels from the card will be to low.

Try to connect the card directly using the first method from http://www.43oh.com/2011/02/five-cheap-breadboard-prototyping-tips-and-tricks-list-2/

( this is what I used )

 

Regards,

Calin

Hi Calin,

But there is also +3.3V just above +5V..  What about connecting USB 5V to there (3.3V and also 5V) ? Will that burn something? : )

Thanks,

Sirri

Share this post


Link to post
Share on other sites

I looked again the picture and there is no need to supply 5V from USB to the SD board.

Try with FatFs.begin(cs_pin,16);   this will lower the SPI clock 

Check if your Energia/hardware/msp430/libraries/SPI/utility/usci_spi.cpp  and  Energia/hardware/msp430/cores/msp430/usci_isr_handler.c  files are the same as the ones in github.

 

Calin

sirri likes this

Share this post


Link to post
Share on other sites

I looked again the picture and there is no need to supply 5V from USB to the SD board.

Try with FatFs.begin(cs_pin,16);   this will lower the SPI clock 

Check if your Energia/hardware/msp430/libraries/SPI/utility/usci_spi.cpp  and  Energia/hardware/msp430/cores/msp430/usci_isr_handler.c  files are the same as the ones in github.

 

Calin

 

I have checked usci_spi.cpp and usci_isr_handler.c and i was suprised that they are really changed in github. i have replaced my files with the ones in github.

However it still doesn't work : /

Here is the result when i test the code (attached image)

Sirri

post-30056-0-29870900-1363893669_thumb.jpg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now