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

SPI receives bad response from a SD card

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dax
Guest







SPI receives bad response from a SD card
PostPosted: Sat Jun 18, 2005 4:29 pm     Reply with quote

I ve a problem with the SPI interface. Im comunicating with a SD card and I can send to it data and comands and everything goes OK. But the bytes that the card sends to the pic are not correctly received.

for example when the card receives a command correctly it should send a byte of response with this value 00000001. The PIC never receives that value, it receives 10000000. Testing more commands I realised that what the PIC really does its to take the 7 more significative bits of the command and add a 1 as the MSB.

The line is high if nothings happens, so

111111111111000000111111111111 --> this is the correct byte
111111111111000000111111111111 -->the PIC takes that

My SPI configuration is

SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_SS_DISABLED);

I use SPI_READ to receive the data.

So after thinking a lot I cannot see where is the problem, and why it do that, If somebody has any idea, all is welcome. Thanks a lot
Ttelmah
Guest







PostPosted: Sat Jun 18, 2005 4:52 pm     Reply with quote

You will get exactly this effect if you are reading the SPI data on the wrong clock edge. There is a seperate configuration bit for Spi, to determine the clock edge to use for transmit, and to use for receive. You are only setting the transmit configuration bit. Try adding the 'SPI_SAMPLE_AT_END' configuration to your setup.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Jun 18, 2005 8:03 pm     Reply with quote

Quote:
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_SS_DISABLED);
Ypur configuration equals Motorola SPI-mode 2.
My MMC software works fine with the configuration for Motorola SPI-mode 3, which in CCS code looks like:
Code:
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 | SPI_SS_DISABLED);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 18, 2005 10:55 pm     Reply with quote

Quote:
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 | SPI_SS_DISABLED);

The SPI_SS_DISABLED parameter should only be used with SPI_SLAVE
mode. If you use in it Master mode, it is OR'ed in with the clock
selection bits, and it will change the SPI clock to some frequency
other than the intended one. In the case above, it changes the
frequency to Fosc/16, instead of the intended Fosc/4.

The following method might be a better way to do the constants for
setup_spi() in the .H files. It fits the options in the SSPCON register
a lot better than the way CCS does it now. CCS's current method
offers setup options that don't really exist.
Code:
// SPI Master mode constants
#define SPI_MASTER_CLK_DIV_4       0x20
#define SPI_MASTER_CLK_DIV_16      0x21
#define SPI_MASTER_CLK_DIV_64      0x22
#define SPI_MASTER_CLK_T2          0x23
#define SPI_MASTER_SAMPLE_AT_END 0x8000

// SPI Slave mode constants
#define SPI_SLAVE_SS_ENABLED    0x24
#define SPI_SLAVE_SS_DISABLED   0x25

// Master or Slave mode constants
#define SPI_XMIT_L_TO_H   0x4000
#define SPI_L_TO_H        0
#define SPI_H_TO_L        0x10
dax



Joined: 19 Jun 2005
Posts: 2

View user's profile Send private message

it works!
PostPosted: Sun Jun 19, 2005 3:05 pm     Reply with quote

Thanks a lot to everybody, you save me lot of time!

Ive solved my problem with the SPI_SAMPLE_AT_END and Ive also wrotten the more eficient library to manage the SPI.


thank you again!
_________________
dax
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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