View previous topic :: View next topic |
Author |
Message |
arnadan
Joined: 11 Nov 2013 Posts: 13
|
dsPIC33: Can not write to SPI slave output buffer |
Posted: Thu Apr 07, 2016 10:56 pm |
|
|
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
|
Re: dsPIC33: Can not write to SPI slave output buffer |
Posted: Fri Apr 08, 2016 4:53 am |
|
|
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
|
|
Posted: Fri Apr 08, 2016 7:21 am |
|
|
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: 19541
|
|
Posted: Fri Apr 08, 2016 8:16 am |
|
|
#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. |
|
|
|