This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PGE: spi protocol

Part Number: TM4C123GH6PGE


hi

i am currently working on the SPI communication in tm4c123gh6pge  board,the main thing is....

i want to communicate two SPI's in tm4c123gh6pge   microcontrollers ,one SPI0 (tiva) for master and another  SPI2 (tiva) for slave  by using SPI protocol.

Is it is possible to communicated b/w two tiva by using SPI protocol.

by using

1.Tiva(Master)      2.Tiva(Slave)

SPI0                           SPI2

1,Clk-------------->   Clk

2,MISO---------->    MISO

3.MOSI---------->    MOSI

4.SS-------------->   SS

the code i have written is ..

SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);

SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);

GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);


GPIOPinConfigure(GPIO_PB4_SSI2CLK);
GPIOPinConfigure(GPIO_PB5_SSI2FSS);
GPIOPinConfigure(GPIO_PH6_SSI2RX);
GPIOPinConfigure(GPIO_PH7_SSI2TX);

GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 |GPIO_PIN_3 |
GPIO_PIN_2);

GPIOPinTypeSSI(GPIO_PORTB_BASE, GPIO_PIN_4 |GPIO_PIN_5);
GPIOPinTypeSSI(GPIO_PORTH_BASE, GPIO_PIN_6 |GPIO_PIN_7);

SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
SSI_MODE_MASTER, 1000000, 8);


SSIConfigSetExpClk(SSI2_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
SSI_MODE_MASTER, 1000000, 8);

SSIEnable(SSI0_BASE);
SSIEnable(SSI2_BASE);

while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
{
}

while(SSIDataGetNonBlocking(SSI2_BASE, &pui32DataRx[0]))
{
}


pui32DataTx[0] = 's';
pui32DataTx[1] = 'p';
pui32DataTx[2] = 'i';


UARTprintf("Sent:\n ");


while(1)
{
spi_protocol();
}
}

void spi_protocol()
{
asd++;

for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
{

UARTprintf("'%c' ", pui32DataTx[ui32Index]);
UARTprintf("\ntx:\n ");

SSIDataPut(SSI0_BASE, pui32DataTx[ui32Index]);
}


while(SSIBusy(SSI0_BASE))
{
}


asd1++;
for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
{

SSIDataGet(SSI2_BASE, &pui32DataRx[ui32Index]);


pui32DataRx[ui32Index] &= 0x00FF;

UARTprintf("\nRx:\n ");

UARTprintf("'%c' ", pui32DataRx[ui32Index]);
}

while(SSIBusy(SSI2_BASE))
{
}

}

  • the above code is not working, please give me correct thing
  • Hello Ravi

    Please search the forum as well. There is a similar code that was posted on the forum in the month of January.
  • ravi kumar said:

    2,MISO---------->    MISO

    3.MOSI---------->    MOSI

    Did you really connect IN on one side to IN on the other side? And OUT from one side to the OUT on the other side?

    Bruno

  • ok Amit  sir ,i can check the previous code that you have given to me.

  • hi burno saraiva sir ,
    i really connected the MISO to MISO and MOSI to MOSI but it is not working .but one time i can check the connections and program again.
  • Ravi,

    The names can be a bit confusing, but let me try to explain.

    Simply put, you need to connect MISO to MOSI, and the other line is MOSI to MISO. Your connection is wrong.

    MISO means "Master In Slave Out" - and these words could cause confusion. Maybe it is better if you think:

    "Mine Input, Someone-else's Output". Anyway, one TM4's Output shall be connected to the other TM4's Input.

    BUT:
    Are you sure that SPI is the best communication solution for your needs?
    If you simply use UART to exchange data, there are some advantages:
    - Programming is more simple
    - There is no need to configure one side as Master and the other as Slave, and you can start communication from either side
    - Only two signal lines are needed (Tx->Rx and Rx<- Tx), plus of course a common GND
    - Very reliable communication that can easily transfer 100000 bytes in one second. Probably twice as much if your lines are short.

    On my company's first project that involved two MCU's, we designed the boards for SPI. It was a mess to make software to allow communication to begin from either side... the we learned the details and today we simply use "good old UART".

    All the best

    Bruno
  • hi Amit sir ,

    i worked out but it is not working ,please tell me the reason . the code is

    SPI0 SPI1

    1.Clk--------------> Clk

    2.MISO----------> MISO ................> the connection i made

    3.MOSI----------> MOSI

    4.SS--------------> SS

    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);


    GPIOPinConfigure(GPIO_PA2_SSI0CLK);
    GPIOPinConfigure(GPIO_PA3_SSI0FSS);
    GPIOPinConfigure(GPIO_PA4_SSI0RX);
    GPIOPinConfigure(GPIO_PA5_SSI0TX);


    GPIOPinConfigure(GPIO_PD0_SSI1CLK);
    GPIOPinConfigure(GPIO_PD1_SSI1FSS);
    GPIOPinConfigure(GPIO_PD2_SSI1RX);
    GPIOPinConfigure(GPIO_PD3_SSI1TX);

    GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 |GPIO_PIN_3 |
    GPIO_PIN_2);

    GPIOPinTypeSSI(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 |GPIO_PIN_2 |
    GPIO_PIN_3);

    SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
    SSI_MODE_MASTER, 1000000, 8);


    SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
    SSI_MODE_MASTER, 1000000, 8);

    SSIEnable(SSI0_BASE);
    SSIEnable(SSI1_BASE);

    //while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
    // {
    //}

    while(SSIDataGetNonBlocking(SSI1_BASE, &pui32SlaveRx[0]))
    {
    }

    UARTprintf("\n\nClear SSI0 and SSI1 fifos.\n");

    for(ui32Index = 0; ui32Index < 8; ui32Index++)
    {
    ui32DummyWrite = DUMMY_CHAR;
    UARTprintf("TX master: '%c'\n", ui32DummyWrite);
    SSIDataPut(SSI0_BASE, ui32DummyWrite);
    }

    while(SSIBusy(SSI0_BASE))
    {
    }

    while(SSIDataGetNonBlocking(SSI1_BASE, &ui32Trash))
    {
    ui32Trash &= 0x00FF;
    UARTprintf("RX slave (trash): '%c'\n", ui32Trash);
    }

    while(SSIDataGetNonBlocking(SSI0_BASE, &ui32Trash))
    {
    ui32Trash &= 0x00FF;
    UARTprintf("RX master (trash): '%c'\n", ui32Trash);
    }



    UARTprintf("SSI 0 and SSI1 cleared.\n\n");


    while(1)
    {
    UARTprintf("\n*** START ***\n");
    UARTprintf("\nMaster TX --> Slave RX\n\n");

    pui32MasterTx[0] = 's';
    pui32MasterTx[1] = 'p';
    pui32MasterTx[2] = 'i';

    for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++) it is working........................ok
    {
    UARTprintf("TX master (data): '%c'\n", pui32MasterTx[ui32Index]);
    SSIDataPut(SSI0_BASE, pui32MasterTx[ui32Index]);
    }

    while(SSIBusy(SSI0_BASE))
    {
    }

    UARTprintf("\n");

    for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
    {
    asd1++;
    SSIDataGet(SSI1_BASE, &pui32SlaveRx[ui32Index]); -------------------------> it is not working .
    asd++; at this position the controller is struck or hanging
    pui32SlaveRx[ui32Index] &= 0x00FF;
    UARTprintf("RX slave (data): '%c'\n", pui32SlaveRx[ui32Index]);

    }

    }
    }

    The Output from terminal is..........................

    Clear SSI0 and SSI1 fifos.

    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    SSI 0 and SSI1 cleared.


    *** START ***

    Master TX --> Slave RX

    TX master (data): 's'
    TX master (data): 'p'
    TX master (data): 'i'

    at this position the controller is struck or hanging i.e SSIDataGet(SSI1_BASE, &pui32SlaveRx[ui32Index]); .
  • hi Amit sir ,

    i worked out but it is not working ,please tell me the reason . the code is

    SPI0 SPI1

    1.Clk--------------> Clk

    2.MISO----------> MISO ................> the connection i made

    3.MOSI----------> MOSI

    4.SS--------------> SS

    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);


    GPIOPinConfigure(GPIO_PA2_SSI0CLK);
    GPIOPinConfigure(GPIO_PA3_SSI0FSS);
    GPIOPinConfigure(GPIO_PA4_SSI0RX);
    GPIOPinConfigure(GPIO_PA5_SSI0TX);


    GPIOPinConfigure(GPIO_PD0_SSI1CLK);
    GPIOPinConfigure(GPIO_PD1_SSI1FSS);
    GPIOPinConfigure(GPIO_PD2_SSI1RX);
    GPIOPinConfigure(GPIO_PD3_SSI1TX);

    GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 |GPIO_PIN_3 |
    GPIO_PIN_2);

    GPIOPinTypeSSI(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 |GPIO_PIN_2 |
    GPIO_PIN_3);

    SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
    SSI_MODE_MASTER, 1000000, 8);


    SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
    SSI_MODE_MASTER, 1000000, 8);

    SSIEnable(SSI0_BASE);
    SSIEnable(SSI1_BASE);

    //while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
    // {
    //}

    while(SSIDataGetNonBlocking(SSI1_BASE, &pui32SlaveRx[0]))
    {
    }

    UARTprintf("\n\nClear SSI0 and SSI1 fifos.\n");

    for(ui32Index = 0; ui32Index < 8; ui32Index++)
    {
    ui32DummyWrite = DUMMY_CHAR;
    UARTprintf("TX master: '%c'\n", ui32DummyWrite);
    SSIDataPut(SSI0_BASE, ui32DummyWrite);
    }

    while(SSIBusy(SSI0_BASE))
    {
    }

    while(SSIDataGetNonBlocking(SSI1_BASE, &ui32Trash))
    {
    ui32Trash &= 0x00FF;
    UARTprintf("RX slave (trash): '%c'\n", ui32Trash);
    }

    while(SSIDataGetNonBlocking(SSI0_BASE, &ui32Trash))
    {
    ui32Trash &= 0x00FF;
    UARTprintf("RX master (trash): '%c'\n", ui32Trash);
    }



    UARTprintf("SSI 0 and SSI1 cleared.\n\n");


    while(1)
    {
    UARTprintf("\n*** START ***\n");
    UARTprintf("\nMaster TX --> Slave RX\n\n");

    pui32MasterTx[0] = 's';
    pui32MasterTx[1] = 'p';
    pui32MasterTx[2] = 'i';

    for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++) it is working........................ok
    {
    UARTprintf("TX master (data): '%c'\n", pui32MasterTx[ui32Index]);
    SSIDataPut(SSI0_BASE, pui32MasterTx[ui32Index]);
    }

    while(SSIBusy(SSI0_BASE))
    {
    }

    UARTprintf("\n");

    for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
    {
    asd1++;
    SSIDataGet(SSI1_BASE, &pui32SlaveRx[ui32Index]); -------------------------> it is not working .
    asd++; at this position the controller is struck or hanging
    pui32SlaveRx[ui32Index] &= 0x00FF;
    UARTprintf("RX slave (data): '%c'\n", pui32SlaveRx[ui32Index]);

    }

    }
    }

    The Output from terminal is..........................

    Clear SSI0 and SSI1 fifos.

    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    TX master: 'D'
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    RX master (trash): ''
    SSI 0 and SSI1 cleared.


    *** START ***

    Master TX --> Slave RX

    TX master (data): 's'
    TX master (data): 'p'
    TX master (data): 'i'

    at this position the controller is struck or hanging i.e SSIDataGet(SSI1_BASE, &pui32SlaveRx[ui32Index]); .
  • Hello Ravi

    Did you check the comments from others on the forum, i.e. MISO is connected to MISO. It is a misnomer on the TM4C and the pins are refered to as TX and RX as per the data sheet. So you need to connect Master TX to Slave RX (i.e MISO to MOSI)