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.

CC3220SF-LAUNCHXL: Unable to write or read data from the SPI based external EEPROM 25LC1024

Part Number: CC3220SF-LAUNCHXL
Other Parts Discussed in Thread: CC3220SF

Hi community,

I'm working on SPI based EEPROM write read operation.
Using CC3220SF-LAUNCHXL Rev.A to write and read operation to/from the 25LC1024 SPI based EEPROM with the help of simplelink cc32xx sdk 6.10.00.05

Searched for NORTOS example code for SPI based EEPROM operation, did not found if find any Example(s) code for SimpleLink CC3220SF to EEPROM read write operation please do share.

So i modified the TI RTOS bases SPI master example code as per requirement. 

After that modification when tried to write data 0xAA to the EEPROM and read back from the same address (0x0010FF) i'm getting 0xFF as output i changed address to 0x001000 then also same output. Can any one help me out to solve this.

The modified code as follows:

/*
 *  ======== spi_eeprom.c ========
 */
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>


/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SPI.h>


/* Driver configuration */
#include "ti_drivers_config.h"

#define SPI_MSG_LENGTH (8)


unsigned char controllerRxBuffer[SPI_MSG_LENGTH];
unsigned char controllerTxBuffer[SPI_MSG_LENGTH];

/* EEPROM instructions */
#define EEPROM_WRITE_ENABLE     (0x06)
#define EEPROM_WRITE_DISABLE    (0x04)
#define EEPROM_READ             (0x03)
#define EEPROM_WRITE            (0x02)
#define EEPROM_ID               (0xAB)
SPI_Handle      controllerSpi;

SPI_Handle Init_SPI(void){
    SPI_Params      spiParams;
    //printf("SPI initiating\n");
    SPI_Params_init(&spiParams);  // Initialize SPI parameters
    spiParams.dataSize = 8;       // 8-bit data size
    spiParams.frameFormat = SPI_POL0_PHA0;
    spiParams.bitRate     = 1000000;
    controllerSpi = SPI_open(CONFIG_SPI_0, &spiParams);
    if (controllerSpi == NULL)
        {
            printf("Error initializing controller SPI\n");
        }
        else
        {
            printf("SPI initialized\n");
        }
    return controllerSpi;
}


int writeToEEPROM(uint32_t address, uint8_t* data, uint32_t dataSize)
{
    SPI_Transaction spiTransaction;
    bool            transferOK;
    printf("To write\n");
    uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
    tmp_txBuffer[0] = EEPROM_WRITE_ENABLE;          // EEPROM write enable latch
    tmp_txBuffer[1] = EEPROM_WRITE;                 // EEPROM write command
    tmp_txBuffer[2] = (address >> 16) & 0xFF;       // Address MSB
    tmp_txBuffer[3] = (address >> 8) & 0xFF;        // Address middle byte
    tmp_txBuffer[4] = address & 0xFF;               // Address LSB
    tmp_txBuffer[5] = data;                         // data to be written to 25LC1024

    strncpy((char *)controllerTxBuffer, tmp_txBuffer, sizeof(SPI_MSG_LENGTH));
    memset((void *)controllerRxBuffer, 0, SPI_MSG_LENGTH);

    spiTransaction.count = SPI_MSG_LENGTH;
    spiTransaction.txBuf = (void *)controllerTxBuffer;
    spiTransaction.rxBuf = (void *)controllerRxBuffer;

    /* Toggle user LED, indicating a SPI transfer is in progress */
    GPIO_toggle(CONFIG_GPIO_LED_1);

    /* Perform SPI transfer */
    transferOK = SPI_transfer(controllerSpi, &spiTransaction);
    if (transferOK)
    {
        printf("Master transmitted data: %x\n", *(char *)controllerTxBuffer);
        //printf("bufr address of transmitted data: %#x\n", controllerTxBuffer);
    }
    else
    {
        printf("Unsuccessful SPI transfer");
        return -1;
    }
    return 0;
}

int readFromEEPROM(uint32_t address, uint8_t* receiveBuffer, uint32_t dataSize)
{
    printf("To read\n");

    // Prepare the read command and address
    SPI_Transaction spiTransaction;
    bool            transferOK;
    uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
    tmp_txBuffer[0] = EEPROM_READ;                     // Read command
    tmp_txBuffer[1] = (address >> 16) & 0xFF;       // Address MSB
    tmp_txBuffer[2] = (address >> 8) & 0xFF;        // Address middle byte
    tmp_txBuffer[3] = address & 0xFF;               // Address LSB
    tmp_txBuffer[4] = 0xBB;                         // dummy data

    strncpy((char *)controllerTxBuffer, tmp_txBuffer, sizeof(SPI_MSG_LENGTH-1));
    memset((void *)controllerRxBuffer, 0, SPI_MSG_LENGTH-1);

    spiTransaction.count = SPI_MSG_LENGTH - 1;          // Read command + address + dummy with dataSize
    spiTransaction.txBuf = (void *)controllerTxBuffer;
    spiTransaction.rxBuf = (void *)controllerRxBuffer;

    /* Toggle user LED, indicating a SPI transfer is in progress */
    GPIO_toggle(CONFIG_GPIO_LED_1);

    /* Perform SPI transfer */
    transferOK = SPI_transfer(controllerSpi, &spiTransaction);
    if (transferOK)
    {
        printf("Master received data: %x\n", *(char *)controllerRxBuffer);
        //printf("bufr address of received data : %#x\n", controllerRxBuffer);

    }
    else
    {
        printf("Unsuccessful SPI transfer");
        return -1;
    }
    return 0;
}

void *mainThread(void *arg0)
{
    uint8_t writeData[] = {0xAA};  // Data to write to the EEPROM
    uint32_t address = 0x000010FF;                  // Address to write to and read from
    uint32_t dataSize = sizeof(writeData);          // Size of data to write and read
    uint8_t receiveBuffer[dataSize];
    /* Call driver init functions. */
    GPIO_init();
    SPI_init();
    /* Configure the LED pins */
    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    GPIO_setConfig(CONFIG_GPIO_LED_1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    Init_SPI();
    /* Turn on user LED */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);

    // Write data to the EEPROM
    writeToEEPROM(address, writeData, dataSize);

    // Read data from the EEPROM
    readFromEEPROM(address, receiveBuffer, dataSize);
    SPI_close(controllerSpi);

    printf("\nDone");
}

Output which i got for the above code:

To write
Master transmitted data: 6

To read
Master received data: ff

Here is the pin configuration of SPI for SimpleLink board

Hardware is configured as follows:

Simple Link 25LC1024
SCLK SCK
MISO SO
MOSI SI
SS CS bar
3V3 VCC
GND VSS

Thanks and Regards,

Ajaykumar V

  • Hi Ajaykumar V,

    from what I see from the code you supplied, you are calling SPI transaction that contains the write command for the EEPROM.

    you then write a SPI transaction that contains the read command for the EEPROM.

    however the EEPROM has not yet procced this request and placed it onto the peripheral SPI tx, so you are not reading anything from the EEPROM on the SPI controller and you get 0xff.

    Id try 2 things:

    1) making sure the EEPROM processed your read request and placed it onto the EEPROM SPI tx.

    2) add another call to SPI transfer again on the controller side .

    Hope this helps,

    best regards, Avi Epstein.

  • Hi Avi,

    • strncpy((char *)controllerTxBuffer, tmp_txBuffer, sizeof(SPI_MSG_LENGTH)); This string copy instruction copied part of data to controllerTxBuffer from tmp_txBuffer (i.e., only Read or Write Instructions are copied rest of data is not copied), so using following instruction
      memcpy(controllerTxBuffer, tmp_txBuffer, sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]));
      may solve that issue.
    • When i did loopback (Launchpad MOSI to Launchpad MISO on same board), data in controllerTxBuffer  getting transferred to controllerRxBuffer.

    • If i tried to read Manufacturing ID of 25LC1024, i thought IC is not responding viewed in Memory Browser is responding, data is controllerRxBuffer, is not printed on the console. As shown below

      for below code:
      /*
       *  ======== eeprom_id_spi.c ========
       */
      #include <stdint.h>
      #include <stdio.h>
      #include <stddef.h>
      #include <unistd.h>
      #include <string.h>
      
      /* Driver Header files */
      #include <ti/drivers/GPIO.h>
      #include <ti/drivers/SPI.h>
      #include <ti/display/Display.h>
      
      /* Driver configuration */
      #include "ti_drivers_config.h"
      /*
       *  ======== spi.c ========
       */
      
      #define SPI_MSG_LENGTH (8)
      
      unsigned char controllerRxBuffer[SPI_MSG_LENGTH];
      unsigned char controllerTxBuffer[SPI_MSG_LENGTH];
      
      
      #define EEPROM_SIZE     131072  // Size of 25LC1024 in bytes
      #define WRITE_CMD       (0x02)
      #define READ_CMD        (0x03)
      #define WRITE_EN_CMD    (0x06)
      #define EEPROM_ID       (0xAB)
      
      
      void Read_MFGID_EEPROM(void)
      {
          SPI_Handle controllerSpi;
          SPI_Params spiParams;
          SPI_Transaction transaction;
          uint32_t i;
          bool transferOK;
          int32_t status;
          uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
      
          /* Open SPI as controller (default) */
          SPI_Params_init(&spiParams);
          spiParams.frameFormat = SPI_POL0_PHA0;
          spiParams.dataSize = 8;       // 8-bit data size
      
          spiParams.bitRate     = 1000000;
          controllerSpi = SPI_open(CONFIG_SPI_CONTROLLER, &spiParams);
          if (controllerSpi == NULL)
          {
              printf("Error initializing controller SPI\n");
          }
          else
          {
              printf("SPI initialized\n");
          }
      
          tmp_txBuffer[0] = EEPROM_ID;                    // instruction for EEPROM MFG ID
          tmp_txBuffer[1] = 0x00;                         // Dummy Address 
          tmp_txBuffer[2] = 0x10;                         // 
          tmp_txBuffer[3] = 0xFF;                         // 
          tmp_txBuffer[4] = 0x00;                         // dummy data
      
          memcpy(controllerTxBuffer, tmp_txBuffer, sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]));
          memset(controllerRxBuffer, 0, sizeof(controllerRxBuffer));
      
          printf("Master before transmit: %#016X\n", *(int *)controllerTxBuffer);
          printf("Master before received: %#016X\n", *(int *)controllerRxBuffer);
      
              /* Initialize controller SPI transaction structure */
              transaction.count = SPI_MSG_LENGTH;
              transaction.txBuf = (void *)controllerTxBuffer;
              transaction.rxBuf = (void *)controllerRxBuffer;
      
              /* Toggle user LED, indicating a SPI transfer is in progress */
              GPIO_toggle(CONFIG_GPIO_LED_1);
      
              /* Perform SPI transfer */
              transferOK = SPI_transfer(controllerSpi, &transaction);
              if (transferOK)
              {
                  printf("Master transmitted addrs: %#X\n", controllerTxBuffer);
                  printf("Master transmitted: %016X\n", *(int *)controllerTxBuffer);
                  printf("Master receive addrs: %#X\n", controllerRxBuffer);
                  printf("Master received: %#016X\n", *(int *)controllerRxBuffer);
              }
              transferOK = SPI_transfer(controllerSpi, &transaction);
              if (transferOK)
              {
                  printf("Master transmitted addrs: %#X\n", controllerTxBuffer);
                  printf("Master transmitted: %#08X\n", *(int *)controllerTxBuffer);
                  printf("Master receive addrs: %#X\n", controllerRxBuffer);
                  printf("Master received: %#08X\n", *(int *)controllerRxBuffer);
      
              }
              else
              {
                  printf("Unsuccessful SPI transfer\n");
              }
                  printf("Master transmitted loop: %#016X\n", *(int *)controllerTxBuffer);
      
          SPI_close(controllerSpi);
      
          printf("\nDone");
      
      }
      
      /*
       *  ======== mainThread ========
       */
      void *mainThread(void *arg0)
      {
      
          /* Call driver init functions. */
      
          GPIO_init();
          SPI_init();
      
          /* Configure the LED pins */
          GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
          GPIO_setConfig(CONFIG_GPIO_LED_1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
      
      
          /* Turn on user LED */
          GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
      
          printf("Starting the SPI Transfer\n");
          
          /* Read MFG ID of EEPROM */
          Read_MFGID_EEPROM();
      }

    In the above code it self i have tried what you were suggested

    add another call to SPI transfer again on the controller side
    • Gave some delay to EEPROM process before read / after write operation also called SPI transfer at the time of Read operation for the original code is sent at the time of this thread creation. 

    making sure the EEPROM processed your read request and placed it onto the EEPROM SPI tx.


    Regards, 
    Ajaykumar V

  • Hi Ajaykumar V,

    1) Can you explain exactly what you are trying to achieve?

    2) what is your current issue? 

    3) why are you running a loopback?

    From what I see in your code change you are calling spi transfer twice with the same data on controller tx. what are you trying to achieve by this?

    SPI transfer(called from the controller) simultaneously writes to the peripheral and reads from the peripheral (although spi is bidirectional I think in your application it should be used as request response).

    if you want a cycle of write/read to your eeprom you need to:

    1) write command (from controller): call spi transfer containing EEPROM write command with the data you intend to write(the eeprom will receive and save it in the address you specified)

    2) read command(from controller): call spi transfer containing EEPROM read command( the eeprom will process your request and place the data you requested on the peripheral tx, this is probably not immediate that's why you need step 3)

    3) read operation(from controller): now that the EEPROM placed the data on the peripheral tx, the controller is ready to receive so call spi transfer again to fetch the data.

    go over your EEPROM data sheet and see the exact details.

    best regards,

    Avi Epstein.

  • Hi Avi,

    Can you explain exactly what you are trying to achieve?

    I wanted to achieve External EEPROM write/read operation using SimpleLink CC3220SF Launch pad.

    what is your current issue? 

    Issue was with the reading writing and reading is not happening.

    why are you running a loopback?

    Only to test transmitted data is get transmitted fully or not, over SPI. Loopback is not related to EEPROM write/ read operation.

    add another call to SPI transfer again on the controller side .

    i miss understood above statement.

    you are calling spi transfer twice with the same data on controller tx.

    below Instructions solved my issue.

    strncpy((char *)controllerTxBuffer, tmp_txBuffer, sizeof(SPI_MSG_LENGTH)); This string copy instruction copied part of data to controllerTxBuffer from tmp_txBuffer (i.e., only Read or Write Instructions are copied rest of data is not copied), so using following instruction
    memcpy(controllerTxBuffer, tmp_txBuffer, sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]));
    may solve that issue.

    below code solve my issues:

    SPI_Handle Init_SPI(void){
        SPI_Params      spiParams;
        printf("SPI initiating\n");
        SPI_init();  // Initialize the SPI driver
        SPI_Params_init(&spiParams);  // Initialize SPI parameters
        spiParams.dataSize = 8;       // 8-bit data size
        spiParams.frameFormat = SPI_POL0_PHA0;
        spiParams.bitRate     = 1000000;
        controllerSpi = SPI_open(CONFIG_SPI_0, &spiParams);
        if (controllerSpi == NULL)
            {
                printf("Error initializing controller SPI\n");
            }
            else
            {
                printf("Controller SPI initialized\n");
            }
        return controllerSpi;
    }
    int EEPROM_WriteLatch(void){
        SPI_Transaction spiTransaction;
        bool            transferOK;
        uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
        tmp_txBuffer[0] = EEPROM_WRITE_ENABLE;          // EEPROM write enable latch
        spiTransaction.count = 1;
        memcpy(controllerTxBuffer, tmp_txBuffer, sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]));
        spiTransaction.txBuf = (void *)controllerTxBuffer;
        spiTransaction.rxBuf = NULL;
        SPI_transfer(controllerSpi, &spiTransaction);
        transferOK = SPI_transfer(controllerSpi, &spiTransaction);
        if (transferOK)
        {
            printf("EEPROM is ready to write data\n");
            return 0;
        }
        else
        {
            printf("Unsuccessful in Latching Write EEPROM\n");
            return -1;
        }
    }
    
    int writeToEEPROM(uint32_t address, uint8_t* data, uint32_t dataSize)
    {
        EEPROM_WriteLatch();
        SPI_Transaction spiTransaction;
        bool            transferOK;
        uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
        int i;
        printf("To write\n");
    
        tmp_txBuffer[0] = EEPROM_WRITE;                 // EEPROM write command
        tmp_txBuffer[1] = (address >> 16) & 0xFF;       // Address MSB
        tmp_txBuffer[2] = (address >> 8) & 0xFF;        // Address middle byte
        tmp_txBuffer[3] = address & 0xFF;               // Address LSB
        tmp_txBuffer[4] = 0xAA;                         // data to be written to 25LC1024
        
    
        memcpy(controllerTxBuffer, tmp_txBuffer, sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]));
        memset((void *)controllerRxBuffer, 0, SPI_MSG_LENGTH);
    
        spiTransaction.count = SPI_MSG_LENGTH;
        spiTransaction.txBuf = (void *)controllerTxBuffer;
        spiTransaction.rxBuf = NULL;
    
        /* Toggle user LED, indicating a SPI transfer is in progress */
        GPIO_toggle(CONFIG_GPIO_LED_1);
    
        /* Perform SPI transfer */
        transferOK = SPI_transfer(controllerSpi, &spiTransaction);
        if (transferOK)
        {
            for(i = SPI_MSG_LENGTH -1 ; i > 3; i--){
            printf("Master transmitted data: %#X\n", *(char *)(controllerTxBuffer + i));
            }
            printf("bufr transmitted data: %#X\n", *(int *)(controllerTxBuffer + 4));
            printf("bufr address of transmitted data: %#X\n", controllerTxBuffer);
        }
        else
        {
            printf("Unsuccessful controller SPI transfer");
            return -1;
        }
        return 0;
    }
    
    int readFromEEPROM(uint32_t address, uint8_t* receiveBuffer, uint32_t dataSize)
    {
        printf("To read\n");
    
        /* Prepare the read command and address */
        SPI_Transaction spiTransaction;
        bool            transferOK;
        uint8_t tmp_txBuffer[SPI_MSG_LENGTH];
        int i;
        tmp_txBuffer[0] = EEPROM_READ;                  // Read command
        tmp_txBuffer[1] = (address >> 16) & 0xFF;       // Address MSB
        tmp_txBuffer[2] = (address >> 8) & 0xFF;        // Address middle byte
        tmp_txBuffer[3] = address & 0xFF;               // Address LSB
        tmp_txBuffer[4] = 0xBB;                         // dummy data
        
        memcpy(controllerTxBuffer, tmp_txBuffer, sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]));
        memset((void *)controllerRxBuffer, 0,SPI_MSG_LENGTH);
        spiTransaction.count = sizeof(tmp_txBuffer) / sizeof(tmp_txBuffer[0]);    // Read command + address + dummy data to transmit while receiving
        spiTransaction.txBuf = (void *)controllerTxBuffer;
        spiTransaction.rxBuf = (void *)controllerRxBuffer;
    
        /* Toggle user LED, indicating a SPI transfer is in progress */
        GPIO_toggle(CONFIG_GPIO_LED_1);
    
        /* Perform SPI transfer */
        transferOK = SPI_transfer(controllerSpi, &spiTransaction);
        if (transferOK)
        {
            for(i = SPI_MSG_LENGTH -1 ; i > 3; i--){
            printf("Master received data: %#X\n", *(char *)(controllerRxBuffer + i));
            }
            printf("bufr received data: %#X\n", *(int *)(controllerRxBuffer + 4));
            printf("bufr address of received data : %#X\n", controllerRxBuffer);
        }
        else
        {
            printf("Unsuccessful controller SPI transfer");
            return -1;
        }
        return 0;
    }

    Thank you for your patience and continuous support Avi E. while bringing up the External EEPROM 25LC1024 write read operation.

    Thanks and regards,

    Ajaykumar V