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/LAUNCHXL-CC1352R1: SPI Read example

Part Number: LAUNCHXL-CC1352R1

Tool/software: Code Composer Studio

Hi,

I need some help in using SPI in CC1352R1 on the Launchpad. I want to read/write from/to registers in an E Paper Driver (S1D13541) by Epson.

Background

I am following a project done for MSP430 by the E Paper manufacturers. I need to drive the EPD using CC1352R1. So far I have written the code for SPI Init and Write.

I will paste the code snippets below.

Requirement

Have I implemented the spi_read(..) correctly? Can someone guide me on how to read from a register from a SPI Slave?

void init_s1d135xx_spi()
{
    SPI_init();

    SPI_Params_init(&spiParams);
    spiParams.frameFormat = SPI_POL0_PHA0;
    spiParams.bitRate = 4000000;
    spiParams.mode = SPI_MASTER;
    spiParams.dataSize = 8;

    masterSpi = SPI_open(CONFIG_SPI_0, &spiParams);

    if (masterSpi == NULL) {
        //Display_printf(display, 0, 0, "Error initializing master SPI\n");
        while (1);
    }
    else {
        //Display_printf(display, 0, 0, "Master SPI initialized\n");
    }
}
void s1d135xx_write_reg(uint16_t reg, uint16_t val)
{
    uint16_t params[] = { reg, val };
    GPIO_write(SPI_CS,0);   // Pull Down CS

    send_cmd(S1D135XX_CMD_WRITE_REG);
    send_params(params, ARRAY_SIZE(params));

    GPIO_write(SPI_CS,1);   // Pull UP & release CS
    Display_printf(display, 0, 0, "Wrote 0x%x to 0x%x", val, reg);
}

uint8_t s1d135xx_read_reg(uint16_t reg)
{
    uint8_t readRegVal1,readRegVal2;

    GPIO_write(SPI_CS,0);   // Pull Down CS
    send_cmd(S1D135XX_CMD_READ_REG);
    send_param(reg);

    readRegVal1 = spi_read(0);
    readRegVal2 = spi_read(0);

    GPIO_write(SPI_CS,1);   // Pull UP CS
    readRegVal1 = swap_uint16(readRegVal1);
    return readRegVal1;
}
static void send_cmd(uint16_t cmd)
{
    cmd = swap_uint16(cmd);
    GPIO_write(HDC,0);   // Pull Down HDC
    //spi_write((uint8_t *)&cmd, sizeof(uint16_t));
    spi_write(cmd);
    GPIO_write(HDC,1);   // Pull UP & release HDC
}

static void send_params(uint16_t *paramsPtr, size_t n)
{
    size_t i;
    for(i = 0; i < n; i++)
    {
        send_param(paramsPtr[i]);
    }

}

static void send_param( uint16_t param)
{
    Display_printf(display, 0, 0, "Sending param 0x%x", param);
    param = swap_uint16(param);
    //spi_write((uint8_t *)&param, sizeof(uint16_t));
    spi_write(param);
}
void spi_write(uint8_t reg)
{
    masterTxBuffer[0] = reg;

    spiTransaction.count = SPI_MSG_LENGTH;
    spiTransaction.txBuf = (void *) masterTxBuffer;;
    spiTransaction.rxBuf = (void *) masterRxBuffer;

    transferOK = SPI_transfer(masterSpi, &spiTransaction);
    if (!transferOK){
        Display_printf(display, 0, 0, "Unsuccessful SPI transmit!\n");
        while(1);
    }
    Display_printf(display, 0, 0, "Successful SPI transmit!\n");
}

uint8_t spi_read(uint8_t readFromReg)
{
    masterTxBuffer[0] = readFromReg;
    masterTxBuffer[1] = 0;
    memset((void *) masterRxBuffer, 0, SPI_MSG_LENGTH);

    spiTransaction.count = SPI_MSG_LENGTH;
    spiTransaction.txBuf = (void *) masterTxBuffer;
    spiTransaction.rxBuf = (void *) masterRxBuffer;

    transferOK = SPI_transfer(masterSpi, &spiTransaction);
    if (!transferOK){
        Display_printf(display, 0, 0, "Unsuccessful SPI read!\n");
        while(1);
    }
    return masterRxBuffer[0]; //Forget the return element. spiTransaction.rxBuf does not gets populated. 
}

  • You can refer to spimaster example. For you code, it looks OK but you might need to test it by yourself. If it doesn't work, I would suggest you to use SPI protocol analyzer or scope to check SPI signal first.

  • Hi ,

    Please ignore my previous message if there are contradictory statements. This message is the latest.

    I am using MX25R8035F SPI FLASH Hardware on the Launchpad connected to my S1D13541 EPD Controller.

    Wiring is as follows,

    • Since I am using the 4-wire SPI, I don't need to use another CS line to manually pull up/down, do I?
    • Do I still need to pull down SS (DIO20) in my code to do the SPI Transaction? Or is it handled by the SPI Driver whenever SPI_transfer(masterSpi, &spiTransaction) is called?

    I hooked up a scope as you suggested and observed the SS line (DIO20). But I do not see any GPIO state change in SS. I will append my latest code snippets and excerpt from the S1D13541 below.

    .
    .
    prod_code = s1d135xx_read_reg(S1D135XX_REG_REV_CODE);
    .
    .
    
    
    
    

    uint16_t s1d135xx_read_reg(uint16_t reg)
    {
        //GPIO_write(SPI_CS,0);   // Pull Down CS. Removed while using 4 Wire SPI mode
        send_cmd(S1D135XX_CMD_READ_REG);
        send_param(reg);
        spi_dummy_read(NULL);
        spi_dummy_read(NULL);
        spi_read(NULL);
        //GPIO_write(SPI_CS,1);   // Pull UP & release CS. Removed while using 4 Wire SPI mode
        return 1;
    }
    
    static void send_cmd(uint16_t cmd)
    {
        GPIO_write(HDC,0);   // Pull Down HDC
        memset((void *) masterTxBuffer, 0, SPI_MSG_LENGTH);
        memset((void *) masterRxBuffer, 0, SPI_MSG_LENGTH);
    
        masterTxBuffer[0] = cmd;
    
        spiTransaction.count = SPI_MSG_LENGTH;
        spiTransaction.txBuf =  &masterTxBuffer;    //(void *)masterTxBuffer;
        spiTransaction.rxBuf =  &masterRxBuffer;    //(void *)masterRxBuffer;
    
        /* Perform SPI transfer */
        transferOK = SPI_transfer(masterSpi, &spiTransaction);
        if (transferOK)
        {
            Display_printf(display, 0, 0, "Master received: %s", masterRxBuffer);
        }
        else
        {
            Display_printf(display, 0, 0, "Unsuccessful master SPI transfer");
            while (1);
        }
        GPIO_write(HDC,1);   // Pull UP & release HDC
    }
    
    static void send_param( uint16_t param)
    {
        masterTxBuffer[0] = param;
    
        spiTransaction.count = SPI_MSG_LENGTH;
        spiTransaction.txBuf =  &masterTxBuffer;    //(void *)masterTxBuffer;
        spiTransaction.rxBuf =  &masterRxBuffer;    //(void *)masterRxBuffer;
    
        /* Perform SPI transfer */
        transferOK = SPI_transfer(masterSpi, &spiTransaction);
        if (transferOK)
        {
            Display_printf(display, 0, 0, "Master received: %s", masterRxBuffer);
        }
        else
        {
            Display_printf(display, 0, 0, "Unsuccessful master SPI transfer");
            while (1);
        }
    }
    
    uint8_t spi_dummy_read(uint16_t readFromReg)
    {
        masterTxBuffer[0] = readFromReg;
    
        spiTransaction.count = SPI_MSG_LENGTH;
        spiTransaction.txBuf = NULL;
        spiTransaction.rxBuf =  &masterRxBuffer;        //(void *)masterRxBuffer;
    
        /* Perform SPI transfer */
        transferOK = SPI_transfer(masterSpi, &spiTransaction);
        if (transferOK)
        {
            Display_printf(display, 0, 0, "Master received: %s", masterRxBuffer);
        }
        else
        {
            Display_printf(display, 0, 0, "Unsuccessful master SPI transfer");
            while (1);
        }
        return 1;
    }
    
    uint8_t spi_read(uint16_t readFromReg)
    {
        masterTxBuffer[0] = readFromReg;
        masterTxBuffer[1] = 0;
    
        spiTransaction.count = SPI_MSG_LENGTH;
        spiTransaction.txBuf =  &masterTxBuffer;    //(void *) masterTxBuffer;
        spiTransaction.rxBuf =  &masterRxBuffer;    //(void *) masterRxBuffer;
    
        transferOK = SPI_transfer(masterSpi, &spiTransaction);
        if (!transferOK){
            Display_printf(display, 0, 0, "Unsuccessful SPI read!\n");
            while(1);
        }
        return masterRxBuffer[0];
    }

    Am I doing the "Dummy Data Read Cycle" Correct? Even at the end of spi_read(NULL); I do not see masterRxBuffer getting populated.
    Any advice is highly appreciated.

    PS: GPIO State change of HDC Pin works fine.

  • If SS signal is not working, I suggest you to control SS using GPO driver by yourself instead of using SPI driver. I am not familiar with S1D13541 EPD Controller so I am not sure if your "Dummy Data Read Cycle" is Correct. I would suggest you to make SS signal correct first and then you can check if you can do SPI read and write successfully.