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 support@ccsinfo.com

Slave SPI out of sync

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



Joined: 17 May 2010
Posts: 29
Location: I live in Spain, with the toros, paella and tortilla

View user's profile Send private message

Slave SPI out of sync
PostPosted: Tue Aug 30, 2011 2:11 pm     Reply with quote

Hello all.

I am trying to both send and receive data from an SPI interface but the data I send is always out of sync. Here is the code:

Code:
#ifndef _libds
#define _libds
#endif

unsigned int command;      //command rcv'd by spi


void dsconf(){
   input(DSATT);
   input(DSCMD);
   input(DSCLK);
   setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_XMIT_L_TO_H);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
}

unsigned int revbits(unsigned int x) {
   unsigned int h = 0;
   int i = 0;

   for(h = i = 0; i < 8; i++) {
      h = (h << 1) + (x & 1);
      x >>= 1;
   }

   return h;
}

void ack(unsigned int us){
   output_low(DSACK);
   delay_us(us);
   input(DSACK);
}

void write(unsigned int data, int doack = 1){
   //delay_us(2);
   SSPBUF = data;
   //if(doack)
   //   ack(3);
}

int _sspbuf() {
   return SSPBUF;
}

#INT_SSP
void libds_isr(){
   ack(3);
   command = revbits(_sspbuf());      //IMPORTANT
   
   if(command == 0x00){
      output_high(GREEN_LED);
   }

   switch(command){
      case 0x01:
         write(0x41);
      break;
      case 0x00:
         write(0xFF);
      break;
      case 0x42:
         write(0x5A);
      break;
   }
}


My main
Code:
void main(){
   setup_oscillator(OSC_32MHZ);
   dsconf();
   write(0xFF);
   do{/*if(bytecount < 1) write(0xFF,0);*/   }while(1);
   //do{output_LOW(GREEN_LED);delay_ms(1000);output_high(GREEN_LED);delay_ms(1000);}while(1);
}


And here is an example of out of sync communication attempt:



Thanks in advance.
uN_Eof



Joined: 17 May 2010
Posts: 29
Location: I live in Spain, with the toros, paella and tortilla

View user's profile Send private message

PostPosted: Thu Sep 15, 2011 12:08 pm     Reply with quote

nobody?
I'm totally desperate.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 15, 2011 1:57 pm     Reply with quote

The reason you didn't get a reply is because too many important things
are missing from your post. You didn't tell us what the Master is. Is
another PIC ? If so, where's the code for it ?

You didn't tell us what PIC you are using for the slave. Or your compiler
version.

From your setup_spi() statement for the slave, it's clear that the Slave PIC
uses the \SS pin. But we don't see any Master code, so we don't know if
you're toggling the Master's slave select line. Also, your logic analyzer
diagram doesn't show a \SS signal. So we are left wondering just what
you are doing ?

You are also using this construct which is an ANSI C data structures
library, which we don't normally see used in CCS programming.
Quote:
#ifndef _libds
#define _libds
#endif


Here you are using an access routine to read the SSPBUF. It's not totally
clear why you're doing this. One can speculate, but it is unusual in CCS.
Quote:

int _sspbuf() {
return SSPBUF;
}


Your post comes across as incomplete or strange and that's why you
didn't get any replies so far.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Sep 15, 2011 2:02 pm     Reply with quote

does this new situation have any relationship to the old

Slave SPI Inhibits SCK

you posted

????
uN_Eof



Joined: 17 May 2010
Posts: 29
Location: I live in Spain, with the toros, paella and tortilla

View user's profile Send private message

PostPosted: Fri Sep 16, 2011 8:51 am     Reply with quote

PCM programmer wrote:
The reason you didn't get a reply is because too many important things
are missing from your post. You didn't tell us what the Master is. Is
another PIC ? If so, where's the code for it ?

You didn't tell us what PIC you are using for the slave. Or your compiler
version.

From your setup_spi() statement for the slave, it's clear that the Slave PIC
uses the \SS pin. But we don't see any Master code, so we don't know if
you're toggling the Master's slave select line. Also, your logic analyzer
diagram doesn't show a \SS signal. So we are left wondering just what
you are doing ?

You are also using this construct which is an ANSI C data structures
library, which we don't normally see used in CCS programming.
Quote:
#ifndef _libds
#define _libds
#endif


Here you are using an access routine to read the SSPBUF. It's not totally
clear why you're doing this. One can speculate, but it is unusual in CCS.
Quote:

int _sspbuf() {
return SSPBUF;
}


Your post comes across as incomplete or strange and that's why you
didn't get any replies so far.


Hello, first of all sorry for the incompleteness.

My PIC18F4620 is slave. I am connecting to a Play Station 2, and my PIC is supposed to emulate the controller. The PS2 to dualshock2 (ds2 from now, the ps2 standard controller) protocol uses SPI with some slight modifications to the protocol. This modification is simple: after getting a byte from the PS2 the DS2 has to pull low a line for 3 or 4 us, thats all, as far as I know.

As long as my logic analyzer is a Pickit2 I only have 3 channels to sample at the same time. I know \SS is OK because its low when the clock cycles are done (I used the logic analyzer).

I removed the lines you mentioned in my code.

I used routine to access SSPBUF just in case I had to change something (like doing something prior to reading or things like that) each time I
read it.

Sorry again for being incomplete, and thanks. I hope the information I gave above helps.

asmboy wrote:
does this new situation have any relationship to the old

Slave SPI Inhibits SCK

you posted

????

I think it doesn't. That was because I wrote this:
Code:
   setup_spi(SPI_SLAVE);
   setup_spi(SPI_H_TO_L);
   setup_spi(SPI_XMIT_L_TO_H);

instead of this:
Code:
   setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_XMIT_L_TO_H);


Thanks again.
uN_Eof



Joined: 17 May 2010
Posts: 29
Location: I live in Spain, with the toros, paella and tortilla

View user's profile Send private message

PostPosted: Sun Sep 18, 2011 2:31 pm     Reply with quote

Still missing some info? Please tell me, I really need help.

Thanks in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 18, 2011 3:02 pm     Reply with quote

Quote:

My PIC18F4620 is slave. I am connecting to a Play Station 2, and my PIC
is supposed to emulate the controller. The PS2 to dualshock2 (ds2 from
now, the ps2 standard controller) protocol uses SPI with some slight
modifications to the protocol.

1. Post a link to documentation for the Playstation SPI Master, if it's available.


Quote:
I removed the lines you mentioned in my code.

2. Post your current complete 18F4620 SPI slave test program.
We should be able to copy and paste it into MPLAB and have it compile
with no changes, and no errors. It should have the #include for the PIC,
#fuses, #use delay, other #use library lines. It should have a main(),
all necessary #defines and variable declarations.

3. What's your CCS compiler version ? It's given at the top of the .LST
file, which will be in your project directory after a successful compilation.
Version numbers look like this:
http://www.ccsinfo.com/devices.php?page=versioninfo

4. What's the Vdd voltage of the 18F4620 PIC ?

5. What are the signal voltage levels of the Playstation ? Are they 3.3v
or are they 5v levels ?
uN_Eof



Joined: 17 May 2010
Posts: 29
Location: I live in Spain, with the toros, paella and tortilla

View user's profile Send private message

PostPosted: Mon Sep 19, 2011 2:14 pm     Reply with quote

1. This one is one of the best and also the one in which I based my design: http://store.curiousinventor.com/guides/ps2

And I think I found the problem. The PS2 receives and sends data in the same edge of the clock cycle (you can see this in the first o-oscope shoot), and the MSSP module can't handle this.

2. I'll post all my code if you tell me so after reading the above info.

3. 4.013

4. Exact same as the actual PS2 controller. 3v3. And it's a PIC18LF4620 (I didn't mention the L as I wasn't requesting hardware help, but it's good to know).

5. 3v3 levels.

I don't want to be tough or rude not posting my code, please understand that I don't want to waste my time copying my code here if the problem has no solution as the problem is in the PIC's internal hardware.

Thank you so much.
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