Welcome, Guest. Please Login.
Surveyor Corporation Surveyor SRV-1
Home Help Search Login

Surveyor Robotics Forum

Welcome to the user support forum for Surveyor SRV-1 robots, SRV-1 robot controllers and SVS stereo vision systems. To register for this forum, please send an email to support@surveyor.com which includes your desired forum user name, your registration email address, and a brief explanation of why you wish to join, and we will create a forum account for you.

Please note that there is a Search button in the forum toolbar for forum topics. Another effective search method for the entire surveyor.com site is to use Google, e.g. "xyz site://www.surveyor.com" where "xyz" is the search topic.



Pages: 1
Send Topic Print
SVS : communication via the SPI bus (Read 2228 times)
LLGR
YaBB Newbies
*




Posts: 2
SVS : communication via the SPI bus
12/30/08 at 10:02am
 
Hello,
I'm trying to make the two cameras communicate with each other via the SPI bus. But it only works from the slave to the master : when I type "$2" on the master, and then "$1" on the slave, the slave sends its message but the master remains blocked and I have to manually reboot it. I may miss something about the way SPI works, and I'd like to know what "svs_master()" and "svs_slave()" exactly do.
Thanks for your help.
Back to top
 
 
WWW   IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: SVS : communication via the SPI bus
Reply #1 - 12/30/08 at 12:14pm
 
The communication is actually bidirectional, though it is initiated by the master.  The spi_slave() function will sit and wait to be contacted by the master, but for every 16-bit word the slave receives, it sends a 16-bit word in response to the master.  At the moment, the slave code only sends a 0x5555 value for every read, but it could be sending valid data.  The only trick is that the first word send from slave to master might not have valid data - it's like the pipeline has to get primed, but it is good by the 2nd word.  
 
The plan has been to extend the spi_master() and spi_slave() functions to set up buffers for both directions.  If you work on this code, perhaps you could post your enhancements.
 
Note that the bidirectional transfer is valid for program i/o, but doesn't work if the SPI interface is configured for DMA, so we don't use DMA.
Back to top
 
« Last Edit: 12/30/08 at 12:15pm by admin »  

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
LLGR
YaBB Newbies
*




Posts: 2
Re: SVS : communication via the SPI bus
Reply #2 - 01/02/09 at 11:36am
 
Well, I've rewritten the two functions given in spi.c to support two buffers. Each function returns the number of bytes received.
Code:
unsigned short svs_master(unsigned short *bufin16, unsigned short *bufout16, int bufoutsize) {
    unsigned short bufinsize = 0;
    int remainingBytes;
    unsigned short dummy;
    
    *pPORTF_FER |= (PF14|PF13|PF12|PF11|PF10);  // SLCK, MISO, MOSI perhipheral
    *pPORT_MUX |= PJSE;  // Enable PJ10 (SSEL2/3) PORT_MUX PJSE=1 
    *pSPI_BAUD = 4;
    *pSPI_FLG = FLS3;    // set FLS3 (FLG3 not required with CHPA=0
    *pSPI_CTL = SPE|MSTR|CPOL|SIZE|EMISO|0x00;  // we use CPHA=0 hardware control
    SSYNC;

  
    *pSPI_TDBR = bufoutsize & 0xffff;
    dummy = *pSPI_RDBR;  // slave will not have written yet
    SSYNC;
    while((*pSPI_STAT&SPIF) ==0 ); // ensure spi tranfer complete 
    while((*pSPI_STAT&TXS) >0 );  // ensure tx buffer empty
    while((*pSPI_STAT&RXS) ==0 ); // ensure rx buffer full
    *pSPI_TDBR = *bufout16++;
    SSYNC;
    bufinsize = *pSPI_RDBR;
    SSYNC;

    remainingBytes = max(bufinsize,bufoutsize);

    while(remainingBytes--) {        
        while((*pSPI_STAT&SPIF) == 0 ); // ensure spi tranfer complete 
        while((*pSPI_STAT&TXS) > 0 );  // ensure tx buffer empty
        while((*pSPI_STAT&RXS) ==0 ); // ensure rx buffer full
        *pSPI_TDBR = *bufout16++;
        SSYNC;
        *bufin16++ = *pSPI_RDBR; // read the dummy value from slave processor
        SSYNC;
    }
    *pSPI_CTL = 0x400;
    *pSPI_FLG = 0x0;
    *pSPI_BAUD = 0x0;
    SSYNC;
    return bufinsize;
}

unsigned short svs_slave(unsigned short *bufin16, unsigned short *bufout16, int bufoutsize) {
    unsigned short bufinsize = 0;
    int remainingBytes;

    *pPORTF_FER    |= (PF14|PF13|PF12|PF11|PF10);    // SPISS select PF14 input as slave,
                        // MOSI PF11 enabled (note shouldn't need PF10 as that's the flash memory)
    *pPORT_MUX    |= PJSE;     // Enable PJ10 SSEL2 & 3 PORT_MUX PJSE=1  not required as we're slave..
    *pSPI_BAUD    = 4;
    *pSPI_CTL     = SPE|CPOL|SIZE|EMISO|0x00; // tc on read
    SSYNC;

    *pSPI_TDBR = bufoutsize;
    SSYNC;
    bufinsize = *pSPI_RDBR;
    SSYNC;
    while( (remainingBytes>0) && ((*pSPI_STAT&TXS) > 0)  ); // ensure tx buffer empty
    while( (*pSPI_STAT&SPIF) == 0 ); // ensure spif transfer complete
    while( (*pSPI_STAT&RXS) == 0  ); // ensure rx buffer full
    *pSPI_TDBR = *bufout16++; 
    SSYNC;
    bufinsize = *pSPI_RDBR; // read full 16 bits in one
    while( ((*pSPI_STAT&TXS) > 0)  ); // ensure tx buffer empty

    remainingBytes = max(bufinsize,bufoutsize);
        
    while(remainingBytes--) {
        while( (*pSPI_STAT&SPIF) == 0 ); // ensure spif transfer complete
        while( (*pSPI_STAT&RXS) == 0  ); // ensure rx buffer full
        *pSPI_TDBR = *bufout16++; 
        SSYNC;
        *bufin16++ = *pSPI_RDBR; // read full 16 bits in one
        while( (remainingBytes>=0) && ((*pSPI_STAT&TXS) > 0)  ); // ensure tx buffer empty
    };

    *pSPI_CTL = 0x400;
    *pSPI_FLG = 0x0;
    *pSPI_BAUD = 0x0;
    SSYNC;
    return bufinsize;
}
 

Back to top
 
 
WWW   IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: SVS : communication via the SPI bus
Reply #3 - 01/02/09 at 11:56am
 
Cool.  Have you confirmed that the data received on the svs_master() side starts and finishes with the right data ?  I did something similar a few weeks ago and ended up with the received data being off by one word.  However, if your version is working correctly, I'll integrate your changes into the source tree, though we probably should increase the transfer size to an "int" rather than "short", as I don't recall there being a 65kB limit to transfers.
 
 
Back to top
 
« Last Edit: 01/02/09 at 11:59am by admin »  

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
Pages: 1
Send Topic Print