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

Error in response from CMD0 in SPI for mmcard in Proteus

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



Joined: 07 Jul 2015
Posts: 31
Location: Ecuador

View user's profile Send private message

Error in response from CMD0 in SPI for mmcard in Proteus
PostPosted: Tue May 30, 2017 5:08 pm     Reply with quote

Hi, please i need some help. I am trying just to send the CMD0 command to an mmcard but always the response is 0Xff. I know its a software error because i am wiring exactly as this example from Proteus where i can see what is being sent...
[img]
http://i.imgur.com/OIg5Eyo.jpg

and i know what i am receiving...
[/img]
http://i.imgur.com/bCjhbGM.jpg

The problem is that when i use my code it doesn't work. I am sending the same numbers as in the Proteus sample but i always receive 0XFF.
Honestly i don't know how to configure the SPI. In the Sandisk datasheet i haven't found anything about bauds or bits. Mode 0 i know is needed because I found info about that. If you have a link to learn how to configure these parameters it would be great. Thanks.
Code:

#include <18F4550.h>
#FUSES XT
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(c)

#use spi(master, mode=0, baud=1000000, BITS=16,FORCE_HW,stream=SP)

spi_init(SP);


void main()
{
set_tris_d(0x00);
output_d(0x00);
set_tris_b(0b00000001);//SDI as input,clk and SDO as outputs
set_tris_c(0b00000000);

{
delay_ms(1000);
//set spi mode CMD0
output_low(pin_d0);//CCS PIN low to start the transmission
spi_write(0x40);
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x95);
spi_write(0xFF); //the commands need additional clock signals to finish the
spi_write(0xFF); //transmission (sandisk datasheet)
output_high(pin_d0); //CS PIN high to finish the transmission
}
}


this is my design in proteus:
[img]
http://i.imgur.com/OpENbXN.jpg[/img]
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 30, 2017 6:23 pm     Reply with quote

#1 Proteus is WRONG.... your project, if wired as the 'schematic' can never, ever work. Hint....the 4550 is a 5 volt PIC, SD cards are almost always 3 volt devices.

#2 Proteus is WRONG.. your project cannot work with real hardware as shown. There are NO power connections (+3v, gnd) to the SD card slot

#3 Proteus is WRONG.. your project doesn't have power (+5v, gnd) to the PIC and NO cap on VUSB.

There are at least a dozen serious wiring 'issues' with the schematic, any of which will not allow the PIC to communiate with the SD card.

Others may offer help.......
I strongly urge you to get a 1Hz LED program coded/compiled/tested with the PIC. It would be a good necessary starting point.

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 30, 2017 6:27 pm     Reply with quote

1. You setup the #use spi() statement for 16-bits SPI transfers. But, all
the CCS examples for MMC cards, such as mmcsd.c or mmc_spi.c all
use 8-bit transfers. You should change it to 8 bits.

2. You setup the SPI using a #use spi() statement. This method is
intended to use the spi_xfer() function to send and receive SPI data.
Instead, you use the older spi_write() function, which is associated with
the setup_spi() statement. You're not supposed to mix the two methods.

3. You setup a stream in your #use spi() statement, but then in your
code you never reference this stream. If you're going to use a stream,
then you're supposed to enter the stream name as one of the parameters
in each spi function. The CCS manual shows how to do this.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

4. There is a bug-fixed version of the CCS MMC/SD routines in the
Code Library forum.

5. You're going to get chastised for using Proteus.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed May 31, 2017 12:53 am     Reply with quote

The big one though is to read the sticky at the head of the forum about interfacing an SD card....

The SPI input on a 5v PIC requires the signal to get up to 4v, to be seen as a '1'. A 3.3v SD card cannot do this without a buffer chip. As Temtronic says, Proteus is wrong (the input pin has a different Vih, when used by the SPI peripheral, from when it is used as a conventional input - Proteus can't handle such complexity, so 'assumes' the input has the same sensitivity...).
You need to either switch to using a 3.3v PIC, or add a buffer chip on the incoming signal.
Mustang1945



Joined: 07 Jul 2015
Posts: 31
Location: Ecuador

View user's profile Send private message

PostPosted: Wed May 31, 2017 10:54 am     Reply with quote

Hi, good morning

PCM Programmer and Ttelmah, thanks for your responses. I think i haven't been clear, sorry, i am not testing in real hardware, it is just a simulation in Proteus. That is why i am trying to do it, because if i am wiring my simulation same as the simulation sample in Proteus (that works) so my simulation must work...

I have a doubt about #use spi, because in setup_spi i can set the clock frequency. I read that the clock for mmcards should be set from 100khz to 400 KHz, but the #use spi doesn't have that option.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed May 31, 2017 12:21 pm     Reply with quote

Actually it does.

Two methods. The first, select the 'NOINIT' option in #use SPI
Then when you want to set the baud rate, use the command spi_init

spi_init(baud);
or
spi_init(stream,baud);

Or use spi_speed:

spi_speed(baud);
spi_speed(stream,baud);
spi_speed(stream,baud,clock);

However I usually do this a third way.

Specify two streams on the same pins. One using the hardware (force_hw), and NOINIT. The other set to the low rate and using software SPI.

Then to initialise use the slow stream. once you have the card ready to use the high speed, call spi_init for the fast stream, and switch to using this.

The reason this is better is that on many fast chips, the hardware SPI can't actually switch down to the 100KHz rate. Generally it doesn't matter (most cards will initialise even if you clock the line too fast), but if one wants to be really careful the software solution is safer for the initialisation. I prefer this on really fast chips (DSPIC's).

Honestly though , don't waste time with Proteus. You can design the entire project in this, and it then won't work in the real hardware, or transfer a working project into Proteus and it then won't work. As a PIC simulator it is a waste of time.
Mustang1945



Joined: 07 Jul 2015
Posts: 31
Location: Ecuador

View user's profile Send private message

Error from CMD0 in SPI for mmcard in Proteus[SOLVED]
PostPosted: Fri Jun 02, 2017 5:06 pm     Reply with quote

SOLVED!!!

Thanks Ttelmah, i didn't know that bauds and Hertz's are the same. I decided to use the setup_spi function because i read that #use spi has bugs, but i understood your idea about changing the bauds once the mmcard is initialized. PCM Programmer thanks to teach me about the difference between setup spi and #use spi, really i was lost ... this is my code.
Code:

#include <18F4550.h>
#FUSES XT
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(c)


void main()
{

set_tris_b(0b00000001); //SDI as input,CS and CLK as outputs
set_tris_c(0b00000000); //SDO as output
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16 | SPI_XMIT_L_TO_H);

/*
I had forgotten to include this part, that is why it didn't work (and also the spi configuration was wrong), each spi write generates 8 clock cycles and the mmcard requires at least 74 cycles for the initialization sequence(Sandisk datasheet). I am sending 80 cycles.
*/
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);
spi_write(0xFF);

//send CMD0
output_low(pin_b3); //CS low to start transmission
spi_write(0x40);
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x95);
spi_write(0xFF); //the commands need additional clock signals to finish the
spi_write(0xFF); //transmission (Sandisk datasheet)
output_high(pin_b3); //CS high to end transmission
}

This is the Proteus design:
[img]
http://i.imgur.com/bOK9eF1.jpg
[/img]

I have a question:
You told me that i must check the logic levels because mmcard works with 3.3v, but what if i use the PIC16F887 ? In this part of the datasheet it says that SDI and SDO are CMOS compatible. Could i connect directly the mmcard to these pins ?
[img]
http://i.imgur.com/GiycOmT.jpg
[/img]

And you are right about Proteus. Once i asked all of you for a better PIC simulator but you all said that its better to test in real hardware. But must be at least one simulator recommended. I also use Proteus for designing the PCB, but what do you think, could there be a better simulator and pcb designer tool ? Which do you recommend me?

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Jun 03, 2017 1:24 am     Reply with quote

Don't get me wrong, but the best simulator is the hardware.
Now, given that most PIC's now have ICD capabilities, with a cheap ICD unit, and the chip you want to test, you have the ultimate simulator, _including the real hardware bugs_, and it costs less than any commercial simulator is going to be. I have a programmable ICE system. These contain programmable logic that you can setup to emulate almost any chip. However a 'code core' for a PIC for this starts at a few hundred dollars, while the unit itself, runs to many tens of thousands.... Compare to the price of an ICD and a prototype board with the PIC.
Unfortunately also many of the 'little glitches' only appear in the real PIC hardware, when peripherals are used together. 'Design rules' for things like Proteus miss these.

#USE SPI, had bugs when it was first introduced. However it's been out and working well for years now. However it still has the complexity of 'hiding things'. So (for instance), this thread:
<http://www.ccsinfo.com/forum/viewtopic.php?t=56241>
Illustrates the problems it can bring. The poster is not aware of the capabilities/limitations of the hardware.

Baud, only equals 'Hz', and bps, when there is one signal state on the interface.
So a modem that uses trellis encoding and sends (say) 16 possible states in the trellis, sending data at 2400baud, sends 9600bps. SPI using one wire (all the PIC hardware supports), only sends one state per clock. A 'baud' is a single change in the signaling state. For simple interfaces, 1baud = 1bps = 1Hz.

It's dangerous to use terms like 'CMOS', and 'TTL'. Much better to actually look at the Vih and Vil levels. The PIC's are all CMOS internally. A CMOS gate can still have Schmitt inputs...
So RC3 & RC4 on the 887. Look at 'Block diagram of RC3'. Shows the I/O pin feeding into a Schmitt buffer (This is what the little squiggly symbol means). Then look at D041 in the electrical parameters, where Vih for a Schmitt input, is specified as 0.8*Vdd. Now the Voh of a SD card is specified as Vmmc-0.2 (for all cards with push-pull drivers). So with the card at 3.3v, 3.1v. The Vih using 5v, is 4v.....

Realistically if you look at the sticky at the head of the forum on this, the link to the basic circuit using a buffer, shows the reliable way to connect to a 5v PIC. Alternatively now, it is so much easier to use a 3.3v PIC.
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 03, 2017 4:38 am     Reply with quote

As Mr. T points out the BEST simulator is hardware in the Real World ! In today's 'world of electronics design', you've got it easy. Inexpensive, full blown computers for a dollar, in-circuit programming and PC compilers like CCS. When I got into this field of fun, you machine coded using pencil and paper, made a paper tape, then fed that into a Teletype 33ASR to program the minicomputer (PDP8), that's AFTER you hand toggled a 'boot loader' into the PDP8 ! Back then you spent a LOT of time crafting your code as the actual downloading was painfully slow. These days you can cut/compile/download hundreds of versions in a day.......
One of the biggest problem with any 'simulator' is their lack of emulating the Real World. They do NOT see EMI, bad ground planes, unsoldered connections or missing xtal caps.....or VDD with high AC content (filter cap too small...) or backfeed from a sensor.....the list is endless, the point is that it's EASY to test in the Real World where you'll get real results.

Jay
Mustang1945



Joined: 07 Jul 2015
Posts: 31
Location: Ecuador

View user's profile Send private message

PostPosted: Sat Jun 03, 2017 8:27 am     Reply with quote

Thanks!
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