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.

TM4C1294NCPDT: SPI Flash Read & Write

Part Number: TM4C1294NCPDT

Tool/software:

I am using SSI communication between a TM4C1294NCPDT MCU and an MX25L51245G flash, but I am unable to read the continuously written data correctly. How can I retrieve the data without junk or null characters? When I try to write continuous data and then read it line by line, I receive the following logs:

Writing data1...
Written data 0x000000: {ate-event:354033090105688,110,alarm,event,normal,active,primary,1,001,2019-07-13T08:24:14+00:00,Common Fire Alarm}

Written data 0x000075:

Data written 0x000000: {ate-event:354033090105688,110,alarm,event,normal,active,primary,1,001,2019-07-13T08:24:14+00:00,Common Fire Alarm}

Writing data2...
Written data 0x000077: {"ate-event":"354033090105688,950,alarm,event,normal,active,secondary,1025,0,2019-07-13T09:08:35+00:00,SIM Failure"}
Written data 0x0000EB:

Data written 0x000077: {"ate-event":"354033090105688,950,alarm,event,normal,active,secondary,1025,0,2019-07-13T09:08:35+00:00,SIM Failure"}
Writing data3...
Written data 0x0000ED: {"ate-event":"354033090105688,774,status,event,normal,active,secondary,1014,0,2019-07-13T09:08:57+00:00,Low Signal"}
Written data 0x000161:

Data written 0x0000ED: {"ate-event":"354033090105688,774,status,event,normal,active,secondary,1014,0,2019-07-13T09:08:57+00:00,Low Signal"}
Reading all stored data...
Line: 3 0 ! 4$(0(354 30 001 %6 ($ a`` a d`dd$ dcbl`` a 0 0$,0 !0) 1,00 0008(07)0000(
Line: $2 ) ,Common Fire Alarm}
Line: {"ate-event":"354033090105688,950,alarm,event,normal,active,secondary,1025,0,2019-07-13T09:08:35+00:00,SIM Failure"}
Line:

  • Hi,

      I don't understand what your log means. Who prints these logs? Are you using some custom SPI Flash library? Where did you get the library from? 

  • Hi,
         I am using FreeRTOS for the TM4C129 MCU.I used the #include "driverlib/ssi.h", no other custom library been used. I am trying to write and read data in the  MX25L51245G flash chip. So the logs were read via UART which I shared you above. I could able to write the data by the command MX25_CMD_WREN  (0x06). Facing issue on reading data. 

  • Can you share your code?

    I'm still not clear how you are writing and reading the SPI flash and how you output the logs via UART. 

    What is MX25_CMD_WREN? How would I know what this function is doing? This must be your own function, certainly not a TivaWare SDK function. 

  • Yes, Here with the "driverlib/ssi.h", created function for flash write which mentioned below, 

    void EEPROM_PageWrite(uint32_t address, char *data, uint32_t length)
    {
    uint32_t i;
    // Send Write Enable command
    EEPROM_WriteEnable();
    // Begin write operation
    EEPROM_CS_LOW();
    // Send Page Program command
    SSIDataPut(SSI3_BASE, MX25_CMD_PP);
    while (SSIBusy(SSI3_BASE));
    // Send address (24-bit address, MSB first)
    SSIDataPut(SSI3_BASE, (address >> 16) & 0xFF);
    while (SSIBusy(SSI3_BASE));
    SSIDataPut(SSI3_BASE, (address >> 8) & 0xFF);
    while (SSIBusy(SSI3_BASE));
    SSIDataPut(SSI3_BASE, address & 0xFF);
    while (SSIBusy(SSI3_BASE));
    // Write data
    for (i = 0; i < length; i++)
    {
    SSIDataPut(SSI3_BASE, data[i]);
    while (SSIBusy(SSI3_BASE));
    }
    // End write operation
    EEPROM_CS_HIGH();
    // Log the write operation
    snprintf(log_buffer, sizeof(log_buffer), "Written data 0x%06X: %.*s\r\n", address, length, data);
    UART2Write(log_buffer, strlen(log_buffer));
    // Wait for the EEPROM to complete the write operation
    SysCtlDelay(1000); 
    }

     From the below function,facing issue on reading data. Also, the logs which I mentioned is read from SPI chip and stored in log_buffer. 

    void EEPROM_ReadAllData(uint32_t start_address, uint32_t max_length)
    {
    char read_buffer[PAGE_SIZE] = {0};
    char line_buffer[PAGE_SIZE] = {0};
    uint32_t current_address = start_address;
    uint32_t bytes_read = 0;
    uint32_t line_index = 0;

    while (bytes_read < max_length)
    {
    // Read a page of data
    memset(read_buffer, 0, PAGE_SIZE);
    EEPROM_Read(current_address / PAGE_SIZE, read_buffer, PAGE_SIZE);
    int i;
    // Process the read buffer line by line
    for (i = 0; i < PAGE_SIZE; i++)
    {
    if (read_buffer[i] == '\0' || read_buffer[i] == '\r' || read_buffer[i] == '\n')
    {
    // End of line or data
    if (line_index > 0)
    {
    line_buffer[line_index] = '\0'; 
    snprintf(log_buffer, sizeof(log_buffer), "Line: %s\r\n", line_buffer);
    UART2Write(log_buffer, strlen(log_buffer));
    line_index = 0; 
    }
    }
    else
    {
    line_buffer[line_index++] = read_buffer[i];
    }
    }

    current_address += PAGE_SIZE;
    bytes_read += PAGE_SIZE;
    }
    }

     Here, The mentioned MX25_CMD_PP (0x02) , MX25_CMD_WREN(0x06) write_enable with these command able to enable the address for the corresponding function on the flashchip MX25L51245G.Setting the PAGE_SIZE as 264. For the Flash read, 

    void EEPROM_Read(uint32_t page, char *buffer, uint32_t length)
    {
    uint32_t address = page * PAGE_SIZE;
    uint32_t i, rx_data, dummy;

    EEPROM_CS_LOW();

    // Send Fast Read command
    SSIDataPut(SSI3_BASE, MX25_CMD_FAST_READ);
    while (SSIBusy(SSI3_BASE));

    // Send address (24-bit address, MSB first)
    SSIDataPut(SSI3_BASE, (address >> 16) & 0xFF);
    while (SSIBusy(SSI3_BASE));
    SSIDataPut(SSI3_BASE, (address >> 8) & 0xFF);
    while (SSIBusy(SSI3_BASE));
    SSIDataPut(SSI3_BASE, address & 0xFF);
    while (SSIBusy(SSI3_BASE));

    // Send dummy byte (required for Fast Read)
    SSIDataPut(SSI3_BASE, 0xFF);
    while (SSIBusy(SSI3_BASE));

    // Clear RX FIFO
    while (SSIDataGetNonBlocking(SSI3_BASE, &dummy));

    // Read data
    for (i = 0; i < length; i++)
    {
    SSIDataPut(SSI3_BASE, 0xFF); // Dummy byte to clock out data
    while (SSIBusy(SSI3_BASE));
    SSIDataGet(SSI3_BASE, &rx_data);
    buffer[i] = (char)(rx_data & 0xFF);
    }

    // Null terminate the buffer
    buffer[length] = '\0';

    EEPROM_CS_HIGH();
    }

     

  • Hi,

    but I am unable to read the continuously written data correctly.

      Sorry, I really don't know why you are reading unexpected data. You said you cannot read the continuously written data. I have a few questions. 

      - What if you just do one simple write and followed by read instead of continuously writes? Can you get it to work? This is to isolate if the problem has anything to do with writing continuously vs writing just once or infrequently. 

      - Your log string seems to be very long. Can you do an experiment to write a simple and short data? Can you read back the short data? This is to isolate if the problem has anything to do with the length of the string. 

      - I will suggest you also use a logic analyzer to look at the SPI bus and see what the data is written and read? This will help greatly in debugging your issue. 

  • 1) I don't see any concession to page (256-byte) boundaries. It seems probable that your write to 0xEB wrapped back and overwrote 0x00. [Ref datasheet "(11) Page Program (PP)" description]. Try (as Charles suggests) writing and reading back one at a time.

    2) SysCtlDelay(1000); Are you fairly certain this is longer than Tpp=5ms (max)? It looks rather short.

  • Yes, Before reading the continous data, I Tried to write only one data and read that one data. For that case I am able to read the data, But issue been there, when it comes to writting 3 to 5 data and then reading that whole data. To my case, firstly I need to store some set of data and then I want to read those. So, I try to such way.  

  • Hi,
        Will go through mentioned page once. Yes, it was too short in SysCtlDelay . I set that as SysCtlDelay(10000000). Here, I'm able to get one written data. After reading one data facing issue on reading as mentioned.  
     

  • Hi,

      I have not heard back from you.  I will close the thread for now. If you have any update, you can write back to this post and the status will change to OPEN.