View previous topic :: View next topic |
Author |
Message |
dax Guest
|
SPI receives bad response from a SD card |
Posted: Sat Jun 18, 2005 4:29 pm |
|
|
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
|
|
Posted: Sat Jun 18, 2005 4:52 pm |
|
|
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
|
|
Posted: Sat Jun 18, 2005 8:03 pm |
|
|
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
|
|
Posted: Sat Jun 18, 2005 10:55 pm |
|
|
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
|
it works! |
Posted: Sun Jun 19, 2005 3:05 pm |
|
|
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 |
|
|
|