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.

CCS/TM4C123GH6PM: sending data using spi

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

hello all,

I am using TM4C123GH6PM development. I have configured one board as master and other board as a slave. I am trying to send data from master to slave. But data is not received in the slave properly.

Best Regards,

Siva

  • THIS is my master code

    //spi_master.c

    /**
    * main.c
    */
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<stdint.h>
    #include"tm4c123gh6pm.h"
    void uart0_init() //uart 0
    {
    SYSCTL_RCGCUART_R |=0x00000001;
    SYSCTL_RCGCGPIO_R |=0x00000001;
    GPIO_PORTA_LOCK_R |=0x4C4F434B;
    GPIO_PORTA_CR_R |=0x00000003;
    GPIO_PORTA_DEN_R |=0x00000003;
    GPIO_PORTA_AFSEL_R |=0x00000003;
    GPIO_PORTA_PCTL_R |=0x00000011;
    UART0_CTL_R &=~0x00000001;
    UART0_IBRD_R =104;
    UART0_FBRD_R=11;
    UART0_LCRH_R |=0x00000060;
    UART0_CC_R |=0x00000000;
    UART0_CTL_R |=0x00000301;
    }
    void U0_send(unsigned char *p)
    {
    while(*p)
    {
    while((UART0_FR_R & 0x20)!=0);
    UART0_DR_R=*p++;
    }
    }
    void delay(unsigned int x)
    {
    unsigned int i,j;
    for(i=0;i<x;i++)
    {
    for(j=0;j<x;j++)
    {

    }
    }
    }

    void spi_init()
    {
    // clock configuration
    SYSCTL_RCGCSSI_R |=0x00000001;
    SYSCTL_RCGCGPIO_R|=0x00000001;

    // gpio configuration

    GPIO_PORTA_AFSEL_R|=0x0000003C;
    GPIO_PORTA_PCTL_R |=0x00222200;
    GPIO_PORTA_DEN_R |=0x0000003C;
    GPIO_PORTA_PUR_R |=0x0000003C;
    GPIO_PORTA_DR8R_R |=0x0000003C;

    //ssi configuration
    SSI0_CR1_R &=~0x00000002; //1.
    SSI0_CR1_R |=0x00000000; //2.
    SSI0_CC_R |=0x00000000; //3.
    SSI0_CPSR_R |=0x00000002; //4.
    SSI0_CR0_R |=0x000007C7; //5.
    SSI0_CR1_R |=0x00000002; //6.
    }
    void buffer(unsigned char k)
    {

    while((SSI0_SR_R &0x00000001)==0); //Tx FIF0 not empty
    SSI0_DR_R =k;
    }
    void send_byte(unsigned char *ptr)
    {
    while(*ptr)
    {
    buffer(*ptr++);
    }
    }
    int main()
    {
    uart0_init();
    delay(2000);
    U0_send("uart master initialized\r\n");
    spi_init();
    while(1)
    {
    send_byte("k");
    delay(1000);
    U0_send("sucess\r\n");
    }
    }

    //this is my slave code

    /**
    * main.c
    */
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<stdint.h>
    #include"tm4c123gh6pm.h"
    unsigned char d;
    void spi_init()
    {
    // clock configuration
    SYSCTL_RCGCSSI_R |=0x00000001;
    SYSCTL_RCGCGPIO_R|=0x00000001;

    // gpio configuration

    GPIO_PORTA_AFSEL_R|=0x0000003C;
    GPIO_PORTA_PCTL_R |=0x00222200;
    GPIO_PORTA_DEN_R |=0x0000003C;
    GPIO_PORTA_PUR_R |=0x0000003C;
    GPIO_PORTA_DR8R_R |=0x0000003C;

    //ssi configuration
    SSI0_CR1_R &=~0x00000002; //1.
    SSI0_CR1_R |=0x00000000; //2.
    SSI0_CC_R |=0x00000000; //3.
    SSI0_CPSR_R |=0x00000002; //4.
    SSI0_CR0_R |=0x000007C7; //5.
    SSI0_CR1_R |=0x00000006; //6.
    }
    void uart0_init() //uart 0
    {
    SYSCTL_RCGCUART_R |=0x00000001;
    SYSCTL_RCGCGPIO_R |=0x00000001;
    GPIO_PORTA_LOCK_R |=0x4C4F434B;
    GPIO_PORTA_CR_R |=0x00000003;
    GPIO_PORTA_DEN_R |=0x00000003;
    GPIO_PORTA_AFSEL_R |=0x00000003;
    GPIO_PORTA_PCTL_R |=0x00000011;
    UART0_CTL_R &=~0x00000001;
    UART0_IBRD_R =104;
    UART0_FBRD_R=11;
    UART0_LCRH_R |=0x00000060;
    UART0_CC_R |=0x00000000;
    UART0_CTL_R |=0x00000301;
    }
    char spi_recv()
    {
    while((SSI0_SR_R & 0x00000004)==0);// rx not empty
    return SSI0_DR_R;
    }
    void U0_send(unsigned char *p)
    {
    while(*p)
    {
    while((UART0_FR_R & 0x20)!=0);
    UART0_DR_R=*p++;
    }
    }

    void delay(unsigned int x)
    {
    unsigned int i,j;
    for(i=0;i<x;i++)
    {
    for(j=0;j<x;j++)
    {

    }
    }
    }

    int main(void)
    {
    spi_init();
    delay(1000);
    uart0_init();
    delay(1000);
    U0_send("uart slave new initialized\r\n");
    while(1)
    {
    d=spi_recv();
    delay(1000);
    U0_send(d);
    delay(1000);
    }
    }

  • Hi,
    Please refer to #4 in the below thread.
    e2e.ti.com/.../695568

    I will suggest you use a scope to capture the SPI waveform to find out if the problem is with the master or slave. The master needs to generate the SPI clock and MOSI signals while the slave sends its data using MISO. If you see proper signals on the SPICLK and MOSI then the master is probably OK. You can send isolate your debug to the slave. Make sure your slave has the matching baud rate, clock polarity and phase in order to work. Check the DR register and the status register to see if they are receiving any data and setting any flags.

  • Hello Charles,
    In my slave code, I have changed the SPI receive function as below and after that I able to receive the data from the master.


    uint16_t spi_recv()
    {
    while((SSI0_SR_R & 0x00000004)==0);
    return ((uint16_t)(SSI0_DR_R & 0xFF));
    }

    Best Regards,
    Siva