CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

MicroSD question (SOLVED!)
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Feb 02, 2013 9:28 am     Reply with quote

Ttelmah wrote:
You are using SPI streams, and then _not_ using these for the transfer.
You need to use:

SPI_XFER(SPI2,0x55);


Tried that -- no change. Which is not surprising, since it compiles to precisely the same code.

Ttelmah wrote:
Then, get rid of the setup_spi2.

If I do, it doesn't initialize the SPI hardware registers.

Ttelmah wrote:
There are two distinct ways of using SPI with CCS. #use spi with spi_xfer, and streams, or setup_spix with spi_write/spi_read (no streams). You need to use one or the other, not both.

Your #use spi, needs to select SPI2 and the correct format.

Ummm... it does:
Code:
#use spi(SPI2, MASTER, bits=8, mode=0, ENABLE=MMCSD_PIN_SELECT, ENABLE_ACTIVE=1)

Note that the .LST file shows identical results for this line, with or without the STREAM=xxx parameter. The test code I am running now came from CCS support, in fact. As for sip_xfer() vs. spi_write() and spi_read(), either way will work with hardware SPI but apparently only spi_xfer() will work with streams. If you only have one stream, the compiled code is identical. If you look at the LST file I posted earlier, spi_xfer() passes the byte to the #use spi routine, which places the byte in the SSP2BUF register at 0xF75.

If you take a look at the code generated from the setup_spi2() and #use spi lines, you'll see that #use spi creates the code that gets called to read and write to the SPI interface, but nothing to actually set up and initialize that interface. Check it out:
Code:

.................... #use spi(SPI2, bits=8, mode=0, ENABLE=MMCSD_PIN_SELECT, ENABLE_ACTIVE=1)
01C08:  BCF    F95.4
01C0A:  BSF    F8C.4
01C0C:  MOVF   F75,W
01C0E:  MOVFF  05,F75
01C12:  RRCF   F73,W
01C14:  BNC   1C12
01C16:  MOVFF  F75,01
01C1A:  BCF    F95.4
01C1C:  BCF    F8C.4
01C1E:  GOTO   1CC2 (RETURN)

vs. this:
Code:
.................... #use spi(SPI2, MASTER, FORCE_HW, STREAM=stream1, bits=8, mode=0, ENABLE=MMCSD_PIN_SELECT, ENABLE_ACTIVE=1)
01C08:  BCF    F95.4
01C0A:  BSF    F8C.4
01C0C:  MOVF   F75,W
01C0E:  MOVFF  05,F75
01C12:  RRCF   F73,W
01C14:  BNC   1C12
01C16:  MOVFF  F75,01
01C1A:  BCF    F95.4
01C1C:  BCF    F8C.4
01C1E:  GOTO   1CC2 (RETURN)

Without setup_spi2() neither one actually initializes the SPI2 hardware. Only setup_spi2() does that.
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Sat Feb 02, 2013 10:52 am     Reply with quote

#use SPI, does setup the SPI. Look at the start of the main, _not_ where the #use spi is declared. This is like #use RS232, where the 'handler' code is shown where the #use is declared, but the initialisation is placed in the main.
The point is the two are not compatible. They don't work together (sometimes they do, but rarely). If you want to use setup_spi2, then use spi_write/read, not spi_xfer.

Best Wishes
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Feb 02, 2013 2:14 pm     Reply with quote

Ttelmah wrote:
#use SPI, does setup the SPI. Look at the start of the main, _not_ where the #use spi is declared.

Ah, I do see that. Still, no harm in doing it twice, which is what the setup_sip() does. #use spi also doesn't give you all of the options that setup_spi() does -- for example, specifying the clock divisor.
Ttelmah wrote:
The point is the two are not compatible. They don't work together (sometimes they do, but rarely). If you want to use setup_spi2, then use spi_write/read, not spi_xfer.

We'll just have to disagree on that point. As it turns out, it all works perfectly.

The problem turned out to be something completely external to the code I was writing -- at least the code I have been writing now. I had looked at the fuses statements a number of times to make sure I had specified NOIOL1WAY. The problem, as it turns out, was that when I compiled and loaded the USB bootloader quite a while ago, the fuse was set for IOL1WAY. As a result, regardless of what I put in the program loaded by the bootloader -- the config words don't get overwritten, and neither did the PPS registers. My code was executing as it should, but the SPI data and clock were going nowhere since they didn't have pins defined. Sure, the code to unlock the PPS registers was there, but it was being ignored.

So now I have SPI2 working. I'm still getting abysmally slow throughput, but at least I can eliminate a couple of other sources of that problem and find the root cause. I know the card can transfer data at more than 4-5Kbytes/sec, since it does so when plugged into a PC. Off to look at the rest of the code and see where my delay is happening.
asmallri



Joined: 12 Aug 2004
Posts: 1680
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Feb 02, 2013 2:39 pm     Reply with quote

dbotkin wrote:
I'm still getting abysmally slow throughput, but at least I can eliminate a couple of other sources of that problem and find the root cause. I know the card can transfer data at more than 4-5Kbytes/sec, since it does so when plugged into a PC. Off to look at the rest of the code and see where my delay is happening.


When plugged into a PC the media's interface typically runs in native mode - not SPI mode. The speed of a card in native is typically UNRELATED to its performance in SPI mode.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Sat Feb 02, 2013 3:42 pm     Reply with quote

I can tell you one thing that is _killing_ your performance....

#USE SPI.....

Because you have specified the select line. It waits for every byte to send before exiting the transmission routine. Look at the code it is generating in the snippet you posted.
Hence the sequence is load a byte, wait for this to send, then go and get the next byte. Effectively no use of the hardware buffering at all.....

It has to do this, since if the select line needs to go off, it needs to wait for the transmission to complete. The same happens with the UART routines when select is enabled.

You'll get far better performance using setup_spi, and handling the select yourself.

As a comment on mixing the two types of routines, read the manual. Note that the #use spi, refers you to the routines associated with it. Setup_spi does the same. In neither case do they refer to the other method.
The reason it can get really messy to mix them, is the differences in the routines. #use SPI for example, will switch to performing software SPI, if you specify something the hardware can't do (silently). If you then use the setup_spi and it's functions, the routines differ.
There are quite a few threads about this.

Best Wishes
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Feb 04, 2013 11:27 pm     Reply with quote

Ttelmah wrote:
I can tell you one thing that is _killing_ your performance....

#USE SPI.....

Seriously -- I really don't think it's the issue here. Just for the record, putting the select line in the #use spi statement was just something I did in the test program to see if it made a difference. It didn't. Mmcsd.c normally manages the chip select pin, turning it on and off as needed.

The file I'm trying to read from the SD card -- just a test file -- is about 42K bytes. If I just read 42K bytes from the card using spi_xfer() it finishes in under half a second. Plenty fast. If I read the FAT file... 13 seconds. It seems like the FAT driver is sucking up a lot of time. I'll see if I can find any obvious delays there, but I'm not inclined to rewrite the entire thing. Who knows? Maybe there's a statement I missed, like perhaps
Code:
 #define WAIT_FOR_GODOT

I may have to look at other solutions if I can't get this up to speed. We're only talking 10-15 KB per second. Since the files I need from the card are read-only, I suppose I could have the user insert the card and just copy the data to a serial FLASH chip -- they're pretty cheap in the size I need. Then I could read them without the FAT filesystem overhead. On a "live" system the files could be as much as *maybe* 250-300K bytes. Certainly no longer than that. A 2MB serial memory chip will do the job.

It's a bummer -- reading the files from a FAT formatted SDcard would be a whole lot easier for the user, and I've already written the code for it. Que sera sera, I guess.
asmallri



Joined: 12 Aug 2004
Posts: 1680
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Feb 05, 2013 12:55 am     Reply with quote

If this is for a commercial project check out my file system driver.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group