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

dsPIC33: Can not write to SPI slave output buffer

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



Joined: 11 Nov 2013
Posts: 13

View user's profile Send private message

dsPIC33: Can not write to SPI slave output buffer
PostPosted: Thu Apr 07, 2016 10:56 pm     Reply with quote

I am trying to implement an SPI slave. The test program shall receive one byte, increment it by one and then send that byte back. Receiving works OK but sending does not:

CCS V 5.056
dsPIC33FJ32MC304

Code:

#word CROA_SPIBUF = 0x248

void Interface_Init()
{
   // Init SPI slave
   setup_spi(SPI_SLAVE | 0x4010 );
   int16 dummyToClearBuf = CROA_SPIBUF;
     
   // Enable SPI interrupts
   clear_interrupt(INT_SPI1);
   enable_interrupts(INT_SPI1);
   enable_interrupts(GLOBAL);
}

#INT_SPI1
void ssp_isr(void)
{
   int16 receivedData = spi_read();
   receivedData++;
   CROA_SPIBUF = receivedData;  // This write seems to fail!
}


At the end of the ISR CROA_SPIBUF still contains the received and not the incremented value. Why?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: dsPIC33: Can not write to SPI slave output buffer
PostPosted: Fri Apr 08, 2016 4:53 am     Reply with quote

arnadan wrote:

At the end of the ISR CROA_SPIBUF still contains the received and not the incremented value. Why?


Probably because it always gives the last received value when read. Its functionality when read - the last byte received - is separate and distinct from what it does when its written - the next byte to be transmitted.

Why are you writing to the buffer directly? Why are you not using the spi_write() function? That's what its there for. Then you would not have to worry about what the buffer was called in that PIC. You're clearly happy with using spi_read so why not spi_write?

The setup_spi, spi_read, spi_write functions are the older, less flexible implementation of SPI in CCS C. Its better, for new code, to use #use spi and spi_xfer.
arnadan



Joined: 11 Nov 2013
Posts: 13

View user's profile Send private message

PostPosted: Fri Apr 08, 2016 7:21 am     Reply with quote

OK, I see the point about that register having different read/write purpose and thus not be able to see the correct content in the debugger.

Why I am not using spi_write? Because I did not find a proper documentation what this function does in slave mode.

I am awar of the #use spi. But when I use that to setup my SPI then my #INT_SPI ISR never gets called. This lead me to the assumption that #use spi does not work with ISR interrupts. I am wrong?
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Fri Apr 08, 2016 8:16 am     Reply with quote

#USE SPI will call INT_SSP (assuming it is enabled), if you set it up using 'SPI1' as the port to talk to. This specifies to use the hardware pins, and to use the hardware port. Problem is that it does not always default to using the actual hardware, unless you use 'FORCE_HW', or the port number. If it isn't using the hardware, then no INT_SSP....

spi_xfer(xx)

Checks if the output buffer is 'free', and if it is loads the value 'xx' directly to the output hardware port, and returns immediately (if not, it waits till it can load it).

val=spi_xfer(xx)

Does the same as the above, but then waits for the 'xx' to send, and loads the returned data into val.

val=spi_xfer()

Checks if the input buffer has a byte, and if it does transfers it immediately into 'val' without waiting to clock.
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