1
votes

I'm trying to develop a interface SPI and I have started with a simple configuration. The question is that SCK seems to work fine but MOSI doesnt works. Here is my code and my test logical tester.

#include <stdlib.h>   

#include <plib.h>   

// example functions prototypes   
int     SpiDoMasterSlaveExample(int nCycles);   

void    SpiInitDevice(int chn, int isMaster, int frmEn, int frmMaster);   


// some definitions   

#define MIN_SPI_TXFER_SIZE      8       // min number of words per transfer   
#define MAX_SPI_TXFER_SIZE      512     // max number of words per transfer   


// configuration settings   
#pragma config FNOSC = PRIPLL, POSCMOD = HS, FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPBDIV = DIV_2, FPLLODIV = DIV_1   
#pragma config FWDTEN = OFF   

int main(void)   
{   

    SYSTEMConfigPerformance(72000000L);   


    srand(ReadCoreTimer());     // seed the pseudo random generator   

    if(!SpiDoMasterSlaveExample(100))   
    {   
        return 0;   // our example failed   
    }   

    return 1;   

}   

int SpiDoMasterSlaveExample(int nCycles)   
{   
    int fail=0;     // overall result   

    SpiInitDevice(1, 1, 1, 1);  // initialize the SPI channel 1 as master, frame master   
    SpiInitDevice(2, 0, 1, 0);  // initialize the SPI channel 2 as slave, frame slave   

    while(nCycles-- && !fail)   
    {   
        unsigned int    txferSize;   
        unsigned short* pTxBuff;   
        unsigned short* pRxBuff;   

        txferSize=MIN_SPI_TXFER_SIZE+rand()%(MAX_SPI_TXFER_SIZE-MIN_SPI_TXFER_SIZE+1);  // get a random transfer size   

        pTxBuff=(unsigned short*)malloc(txferSize*sizeof(short));   
        pRxBuff=(unsigned short*)malloc(txferSize*sizeof(short));       // we'll transfer 16 bits words   

        if(pTxBuff && pRxBuff)   
        {   
            unsigned short* pSrc=pTxBuff;   
            unsigned short* pDst=pRxBuff;   
            int             ix;   
            int             rdData;   

            for(ix=0; ix<txferSize; ix++)   
            {   
                pTxBuff[ix]='A'; // fill buffer with some random data   
            }   

            ix=txferSize+1;             // transfer one extra word to give the slave the possibility to reply back the last sent word   
            while(ix--)   
            {   
                SpiChnPutC(1, *pSrc++);     // send data on the master channel, SPI1   
                rdData=SpiChnGetC(1);       // get the received data   
                if(ix!=txferSize)   
                {   // skip the first received character, it's garbage   
                    *pDst++=rdData;         // store the received data   
                }   
                rdData=SpiChnGetC(2);           // receive data on the slave channel, SPI2   
                SpiChnPutC(2, rdData);          // relay back data   
            }   

            // now let's check that the data was received ok   
            pSrc=pTxBuff;   
            pDst=pRxBuff;   
            for(ix=0; ix<txferSize; ix++)   
            {   
                if(*pDst++!=*pSrc++)   
                {   
                    fail=1;     // data mismatch   
                    break;   
                }   
            }   
        }   
        else   
        {   // memory allocation failed   
            fail=1;   
        }   

        free(pRxBuff);   
        free(pTxBuff);  // free the allocated buffers   
    }   


    return !fail;   
}   

void SpiInitDevice(int chn, int isMaster, int frmEn, int frmMaster)   
{   
    unsigned int    config=SPI_CON_MODE16|SPI_CON_SMP|SPI_CON_ON;   // SPI configuration word   
    if(isMaster)   
    {   
        config|=SPI_CON_MSTEN;   
    }   
    if(frmEn)   
    {   
        config|=SPI_CON_FRMEN;   
        if(!frmMaster)   
        {   
            config|=SPI_CON_FRMSYNC;   
        }   
    }   


    SpiChnOpen(chn, config, 4); // divide fpb by 4, configure the I/O ports. Not using SS in this example   

}   

Sorry, I can't post the logical analyser image for my reputation points.

I'm trying to send "A" all time (fill buffer transmit). That's send data to SPI1.

I'm reading SPI1 from my Microchip Expansion Board I/O where SPI1 is in pins 41 and 43 (41 SCK and 43 SDO). In SPI2, pin 23 and 25, obviously I have not any traffic.

Does anyone have idea of this error?

Thanks a lot

2
Please preview your post before posting. Your code formatting is all over the place.Lundin
I'm sorry about that. I inside code in tags. I'll try to fix.vincenzorochetta
To be sure. Are you looping SPI1 to SPI2 and ou want to send from SPI1 and recieve on SPI2?LPs

2 Answers

0
votes

The PIC32MX series has mapable input and output pins for some peripherals, including the SPI. This means, that MOSI and MISO can be mapped to different pins, depending on your specific needs.

You need to specify this in code before you start using the SPI, otherwise, the PIC won't know which pins to use.

The following is just an example of how to setup the pins (peripheral pin select). You need to look in your PIC's datasheet for the mappings. The first parameter in the calls is the table index from the datasheet.

/* inputs */
PPSInput(2, SDI1, RPF2);    // F2, MEMORY MISO -> SPI1SDI
PPSInput(2, SDI2, RPG7);    // G7, ZB MISO -> SPI2SDI

/* outputs */
PPSOutput(4, RPF3, SDO1);   // F3, MEMORY MOSI -> SPI1SDO
PPSOutput(1, RPG8, SDO2);   // G8, ZB MOSI -> SPI2SDO
0
votes

As described before, check your peripheral pin select. Additional you must set your port direction to Output (PDx-Register). MISO must be set to an Input.