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.

Not able to read or write from in the Serial Flash in MQTT_server application

Other Parts Discussed in Thread: CC3200, UNIFLASH

Hello there,

I am facing issues while reading or writing or even getting the information related to the file status in the sample for the mqtt server in CC3200, is there anything extra which needs to be done to make it working of sl_FsGetInfo  or sl_FsOpen or sl_FsWrite or sl_FsClose.

I have attached my code snipped,

ti-report..txt
long ReadConfiguration()
{
	SlFsFileInfo_t pFsFileInfo;
	unsigned long ulToken;
	long lFileHandle;
	long lRetVal = -1;
	unsigned char *pData = "Testing to write something to file";

	UART_PRINT("Writing some file : \"%s\"\r\n", WIFI_CONFIG);

	 lRetVal = sl_FsGetInfo((unsigned char *)WIFI_CONFIG, NULL, &pFsFileInfo);

	//
	//  create a user file
	//
	lRetVal = sl_FsOpen(WIFI_CONFIG,
						FS_MODE_OPEN_CREATE(65536, \
						  _FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE),
						&ulToken,
						&lFileHandle);
	if(lRetVal < 0)
	{
		//
		// File may already be created
		//
		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
		return lRetVal;
	}
	else
	{
		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
		if (SL_RET_CODE_OK != lRetVal)
		{
			return FILE_CLOSE_ERROR;
		}
	}

	//
	//  open a user file for writing
	//
	lRetVal = sl_FsOpen(WIFI_CONFIG,
						FS_MODE_OPEN_WRITE,
						&ulToken,
						&lFileHandle);
	if(lRetVal < 0)
	{
		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
		return FILE_OPEN_WRITE_FAILED;
	}

	//
	// write the test buffer to another file.
	//
	lRetVal = sl_FsWrite(lFileHandle, 0, pData, sizeof(pData));
	if (lRetVal < 0)
	{
		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
		return FILE_WRITE_FAILED;
	}

	//
	// close the user file
	//
	lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
	if (SL_RET_CODE_OK != lRetVal)
	{
		return FILE_CLOSE_ERROR;
	}
	UART_PRINT("Written [%s] to file [%s]\r\n", WIFI_CONFIG, g_writingBuffer);
	return SUCCESS;
}

void
main()
{ 
    long lRetVal = -1;

    // Initialize the board configurations
    BoardInit();

    // Pinmux for UART
    PinMuxConfig();

    // Configuring UART
    InitTerm();

    // Init all required variables
    InitVariables();

    // Start the SimpleLink Host
    lRetVal = VStartSimpleLinkSpawnTask(SPAWN_TASK_PRIORITY);
    if(lRetVal < 0)
    {
        ERR_PRINT(lRetVal);
    }

    // Reading configuration
    ReadConfiguration();

    // Init i2c
    lRetVal = I2C_IF_Open(I2C_MASTER_MODE_FST);
    if(lRetVal < 0)
    {
        ERR_PRINT(lRetVal);
    }

    // Configure timer A and interrupt for that
    Timer_IF_Init(PRCM_TIMERA0, TIMERA0_BASE, TIMER_CFG_PERIODIC, TIMER_A, 0);
    Timer_IF_IntSetup(TIMERA0_BASE, TIMER_A, TimerBaseIntHandler);

    // Start the MQTT Server and Client task depending upon the jumper settings
	osi_MsgQCreate(&g_PBQueue,"PBQueue",sizeof(struct msg_queue),10);
	lRetVal = osi_TaskCreate(MqttClientServer, (const signed char *)"Mqtt Server", 2048, NULL, 2, NULL );
	if(lRetVal < 0)
	{
		ERR_PRINT(lRetVal);
	}

	// Start the task scheduler
    osi_start();
}
 

let me know in case I am missing any details 

  • HI,

    Can you try the "file_operations" (processors.wiki.ti.com/.../CC32xx_File_Operations) example and see if it works fine?

    Regards,
    Gigi Joseph.
  • Hi Joseph,

    Yes, It works fine with the board, but the question is why it is not working properly if I am adding that in any other SDK example or my own code as well.
    Is there any extra settings which needs to be done to make it working.
  • Hi,

    Can you elaborate what exactly is not working (including some printout from your code)?

    Looking at the code, you try to create the file (which should always succeed) and then immediately close it. This would make you loose all file content if the file already existed.

    Shlomi

  • Hi,

    Did the above post answer your query? please verify the answer.

    Shlomi

  • Hi Shalomi,

    Sorry for the late reply, the thing is I am using the sample code for the MQTT server application from the SDK and in the same, OSI Task ( i.e. " MqttClientServer" ) I am adding following two lines before connecting to the WiFi.

    Below is my snipped for the said OSI Task in which I am using two APIs to read and write, This APIs I have tested with stand alone application and it is working fine, For the reference I have attached those two APIs and the pin mux configuration also for the reference. 

    Please let me know in case I am missing any information which would be helpful in this case.

    pinmux.c
    /******************************************************************************
    *
    *   Copyright (C) 2014 Texas Instruments Incorporated
    *
    *   All rights reserved. Property of Texas Instruments Incorporated.
    *   Restricted rights to use, duplicate or disclose this code are
    *   granted through contract.
    *
    *   The program may not be used without the written permission of
    *   Texas Instruments Incorporated or against the terms and conditions
    *   stipulated in the agreement under which this program has been supplied,
    *   and under no circumstances can it be used with non-TI connectivity device.
    *
    ******************************************************************************/
    
    // This file was automatically generated by the CC31xx PinMux Utility
    // Version: 1.0.2
    //
    //*****************************************************************************
    
    #include "pinmux.h"
    #include "hw_types.h"
    #include "hw_memmap.h"
    #include "hw_gpio.h"
    #include "pin.h"
    #include "rom.h"
    #include "rom_map.h"
    #include "rom_patch.h"
    #include "gpio.h"
    #include "prcm.h"
    
    //*****************************************************************************
    void
    PinMuxConfig(void)
    {
        //
        // Enable Peripheral Clocks 
        //
        MAP_PRCMPeripheralClkEnable(PRCM_GPIOA1, PRCM_RUN_MODE_CLK);
    	MAP_PRCMPeripheralClkEnable(PRCM_GPIOA2, PRCM_RUN_MODE_CLK);
        MAP_PRCMPeripheralClkEnable(PRCM_GPIOA3, PRCM_RUN_MODE_CLK);
        MAP_PRCMPeripheralClkEnable(PRCM_UARTA0, PRCM_RUN_MODE_CLK);
        MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
    
    
        //
        // Configure PIN_64 for GPIO Output
        //
        MAP_PinTypeGPIO(PIN_64, PIN_MODE_0, false);
        MAP_GPIODirModeSet(GPIOA1_BASE, 0x2, GPIO_DIR_MODE_OUT);
    
        //
        // Configure PIN_04 for GPIO Input
        //
        MAP_PinTypeGPIO(PIN_04, PIN_MODE_0, false);
        MAP_GPIODirModeSet(GPIOA1_BASE, 0x20, GPIO_DIR_MODE_IN);
    
        //
        // Configure PIN_15 for GPIO Input
        //
        MAP_PinTypeGPIO(PIN_15, PIN_MODE_0, false);
        MAP_GPIODirModeSet(GPIOA2_BASE, 0x40, GPIO_DIR_MODE_IN);
    
        //
        // Configure PIN_18 for GPIO Input
        //
        MAP_PinTypeGPIO(PIN_18, PIN_MODE_0, false);
        MAP_GPIODirModeSet(GPIOA3_BASE, 0x10, GPIO_DIR_MODE_IN);
    
        //
        // Configure PIN_45 for GPIO Output
        //
        MAP_PinTypeGPIO(PIN_45, PIN_MODE_0, false);
        MAP_GPIODirModeSet(GPIOA3_BASE, 0x80, GPIO_DIR_MODE_OUT);
    
        //
        // Configure PIN_55 for UART0 UART0_TX
        //
        MAP_PinTypeUART(PIN_55, PIN_MODE_3);
    
        //
        // Configure PIN_57 for UART0 UART0_RX
        //
        MAP_PinTypeUART(PIN_57, PIN_MODE_3);
    
        //
        // Configure PIN_01 for I2C0 I2C_SCL
        //
        MAP_PinTypeI2C(PIN_01, PIN_MODE_1);
    
        //
        // Configure PIN_02 for I2C0 I2C_SDA
        //
        MAP_PinTypeI2C(PIN_02, PIN_MODE_1);
    }
    

    readWrite.c
    //*****************************************************************************
    //
    //!  This funtion includes the following steps:
    //!  - open a user file for writing
    //!  - write the supplied buffer
    //!  - close the user file
    //!
    //!  /param[in] fName : Name of the file to be written
    //!  /param[in] pData : Data which needs to be written
    //!
    //!  /return  0:Success, -ve: failure
    //
    //*****************************************************************************
    long WriteFileToDevice(unsigned char *fName, unsigned char *pData)
    {
    	unsigned long ulToken;
    	long lFileHandle;
        long lRetVal = -1;
    
        //
        //  create a user file
        //
        lRetVal = sl_FsOpen(fName,
                    		FS_MODE_OPEN_CREATE(65536, \
                              _FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE),
                            &ulToken,
                            &lFileHandle);
        if(lRetVal < 0)
        {
            //
            // File may already be created
            //
            lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
            return lRetVal;
        }
        else
        {
            lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
            if (SL_RET_CODE_OK != lRetVal)
            {
                return FILE_CLOSE_ERROR;
            }
        }
    
        //
        //  open a user file for writing
        //
        lRetVal = sl_FsOpen(fName,
                            FS_MODE_OPEN_WRITE,
                            &ulToken,
                            &lFileHandle);
        if(lRetVal < 0)
        {
            lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
            return FILE_OPEN_WRITE_FAILED;
        }
    
        //
        // write the test buffer to another file.
        //
    	lRetVal = sl_FsWrite(lFileHandle, 0, pData, sizeof(pData));
    	if (lRetVal < 0)
    	{
    		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
    		return FILE_WRITE_FAILED;
    	}
    
        //
        // close the user file
        //
        lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
        if (SL_RET_CODE_OK != lRetVal)
        {
            return FILE_CLOSE_ERROR;
        }
        UART_PRINT("Written [%s] to file [%s]\r\n", fName, g_writingBuffer);
        return SUCCESS;
    }
    
    //*****************************************************************************
    //
    //!  This funtion includes the following steps:
    //!    -open the user file for reading
    //!    -read the data and compare with the stored buffer
    //!    -close the user file
    //!
    //!  /param[in] ulToken : file token
    //!  /param[in] lFileHandle : file handle
    //!
    //!  /return 0: success, -ve:failure
    //
    //*****************************************************************************
    long ReadFileFromDevice(unsigned char *fName, unsigned char *pData)
    {
    	unsigned long ulToken;
    	long lFileHandle;
        long lRetVal = -1;
        SlFsFileInfo_t fileInfo;
    
        UART_PRINT("Getting file info[%s]\r\n", fName);
    
        // Checking if file is present or not
        int Status = sl_FsGetInfo(fName, 0, &fileInfo);
        if (Status < 0)
    	{
        	UART_PRINT("Probably read is failed as there is no file \r\n");
        	return FILE_READ_FAILED;
    	}
        else
        {
        	UART_PRINT("AllocatedLen : %d \r\n", fileInfo.AllocatedLen);
        	UART_PRINT("FileLen : %d \r\n", fileInfo.FileLen);
        	UART_PRINT("Token : %d\r\n", fileInfo.Token);
        	UART_PRINT("flags : %d\r\n", fileInfo.flags);
        }
    
        UART_PRINT("File info loaded \r\n" );
        if ( fileInfo.FileLen < 1 ) {
        	UART_PRINT("Empty file returned \r\n");
        	return EMPTY_FILE_FOUND;
        }
    
        UART_PRINT("Openning file for reading \r\n");
        // open a user file for reading
        lRetVal = sl_FsOpen(fName,
                            FS_MODE_OPEN_READ,
                            &ulToken,
                            &lFileHandle);
        if(lRetVal < 0)
        {
            lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
            return FILE_OPEN_READ_FAILED;
        }
    
        UART_PRINT("Reading data \r\n");
        // read the data
        lRetVal = sl_FsRead(lFileHandle, 0, pData, fileInfo.FileLen);
    	if (lRetVal < 0)
    	{
    		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
    		UART_PRINT("Closing File [%s] Token [%d] and File Handle : [%d] with return value : [%d] \r\n", fName, ulToken, lFileHandle, lRetVal);
    		return FILE_READ_FAILED;
    	}
    
    	UART_PRINT("Close after read\r\n");
        // close the user file
        lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
        if (SL_RET_CODE_OK != lRetVal)
        {
            return FILE_CLOSE_ERROR;
        }
        return SUCCESS;
    }
    

    void
    MqttClientServer(void *pvParameters)
    {
        
        struct msg_queue que_elem_recv;
        struct publish_msg_header msg_head;
        unsigned int uiTopOffset = 0;
        unsigned int uiPayloadOffset = 0;
        long lRetVal = -1;
        int iCount = 0;
        unsigned int uiConnFlag = 0;
    	
    	/* My Snippet starts */
    	// Writing something to file to test
        char *readingBuffer;
        WriteFileToDevice(USER_CONFIG_FILE, "Testing the file for verification");
        ReadFileFromDevice(USER_CONFIG_FILE, readingBuffer);
        UART_PRINT("Reading something from [%s] data is : {%s}\r\n", USER_CONFIG_FILE, readingBuffer);
    	/* My Snipped Ends here **/
    	
        //
        // Configure LED
        //
        GPIO_IF_LedConfigure(LED1|LED2|LED3);
    
        GPIO_IF_LedOff(MCU_RED_LED_GPIO);
        GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);

  • Hi,

    I don't have the full code but if I follow the MQTT example and the MqttClientServer() API, where exactly do you invoke Network_IF_InitDriver() which start the driver (and power on the networking device)? Since the flash is connected to the device as slave SPI device, it requires the device to be ON. Please verify sl_start() is invoked before you call to any API (including file system APIs).

    Shlomi

  • Hi Shalomi,

    I am using this snippet to read the files from the device. but it is only reading first 4 bytes of the file, and I am not getting what is wrong in this.

    long ReadFileFromDevice(unsigned char *fName, unsigned char *pData)
    {
    	unsigned long ulToken;
    	long lFileHandle;
        long lRetVal = -1;
        SlFsFileInfo_t fileInfo;
    
        // Checking if file is present or not
        int Status = sl_FsGetInfo(fName, 0, &fileInfo);
        if (Status < 0)
        	return FILE_READ_FAILED;
    
        if ( fileInfo.FileLen < 1 )
        	return EMPTY_FILE_FOUND;
    // open a user file for reading lRetVal = sl_FsOpen(fName, FS_MODE_OPEN_READ, &ulToken, &lFileHandle); if(lRetVal < 0) { lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); return FILE_OPEN_READ_FAILED; } // read the data lRetVal = sl_FsRead(lFileHandle, 0, pData, fileInfo.FileLen); if (lRetVal < 0) { lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); return FILE_READ_FAILED; } // close the user file lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); if (SL_RET_CODE_OK != lRetVal) { return FILE_CLOSE_ERROR; } return SUCCESS; }

  • Hi,

    Is it possible that the file only includes 4 bytes? I guess not but I want to understand the procedure you take when creating/writing a file. If you call to WriteFileToDevice() that you attached earlier in the post, it is problematic. The reason is that calling to sl_FsOpen() for WRITE would succeed but then calling to sl_FsClose() without writing anything would clear all content. I see later in the function that you open again and write the actual content so it might be OK after all but still the first part is redundent.

    What are the values of SlFsFileInfo_t structure? most important is FileLen and AllocatedLen arguments. If you get any error on the return values along the process please state it as well.

    Shlomi

  • Hi Shalomi,

    I have updated the code for writing, please find the same, as now on I am not doing check before writing as the file will be always present before any write. but still I am only able to read 4 bytes each time. I have attached a screen shot for the reference.

    For example I am having write successful for the 12 bytes, but after that if I read immediately it would be only 4 bytes. so something is wrong somewhere

    long WriteFileToDevice(unsigned char *fName, unsigned char *pData)
    {
    	unsigned long ulToken;
    	long lFileHandle;
        long lRetVal = -1;
    
        //
        //  open a user file for writing
        //
        lRetVal = sl_FsOpen(fName,
                            FS_MODE_OPEN_WRITE,
                            &ulToken,
                            &lFileHandle);
        if(lRetVal < 0)
        {
            lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
            return FILE_OPEN_WRITE_FAILED;
        }
    
        //
        // write the test buffer to another file.
        //
    	lRetVal = sl_FsWrite(lFileHandle, 0, pData, sizeof(pData));
    	if (lRetVal < 0)
    	{
    		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
    		return FILE_WRITE_FAILED;
    	}
    
        //
        // close the user file
        //
        lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
        if (SL_RET_CODE_OK != lRetVal)
        {
            return FILE_CLOSE_ERROR;
        }
        UART_PRINT("Written [%s] to file [%s]\r\n", fName, g_writingBuffer);
        return SUCCESS;
    }

    long ReadFileFromDevice(unsigned char *fName, unsigned char *pData)
    {
    	unsigned long ulToken;
    	long lFileHandle;
        long lRetVal = -1;
        SlFsFileInfo_t fileInfo;
    
        // Checking if file is present or not
        int Status = sl_FsGetInfo(fName, 0, &fileInfo);
        if (Status < 0)
    	{
        	UART_PRINT("No file present \r\n");
        	return FILE_READ_FAILED;
    	}
    
        if ( fileInfo.FileLen < 1 ) {
        	UART_PRINT("Empty file returned \r\n");
        	return EMPTY_FILE_FOUND;
        }
        // open a user file for reading
        lRetVal = sl_FsOpen(fName,
                            FS_MODE_OPEN_READ,
                            &ulToken,
                            &lFileHandle);
        if(lRetVal < 0)
        {
            lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
            return FILE_OPEN_READ_FAILED;
        }
    
        // read the data
        lRetVal = sl_FsRead(lFileHandle, 0, pData, fileInfo.FileLen);
    	if (lRetVal < 0)
    	{
    		lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
    		UART_PRINT("Closing File [%s] \r\n", fName);
    		return FILE_READ_FAILED;
    	}
    
        // close the user file
        lRetVal = sl_FsClose(lFileHandle, 0, 0, 0);
        if (SL_RET_CODE_OK != lRetVal)
        {
            return FILE_CLOSE_ERROR;
        }
        return SUCCESS;
    }

    Can you have any suggestions for the same. 

  • Hi,

    You have uploaded only ReadFileFromDevice() and WriteFileToDevice(), not the entire files.

    My comments:

    • at the beginning of WriteFileToDevice(), you open for WRITE. If the file does not exist, it fails. Not sure if this is what you want to do or maybe open with CREATE_IF_NOT_EXIST flag, i.e. like the first call in the original file_operations example code:

      lRetVal = sl_FsOpen((unsigned char *)USER_FILE_NAME,
      FS_MODE_OPEN_CREATE(65536, _FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE), ulToken, lFileHandle);

    • from your printouts, is the file wifi_config.ini? what is the other long name starting with "Pr"? it is printed from g_writingBuffer, what is it?
    • from your printouts, the fileLen is 4 so it seems only 4 bytes are there. Where did you write the 12 bytes you mentioned?

    There are many gaps in the procedure you described.

    Can you elaborate?

    1. do you have an existing file you try to write to or are you creating a new one?
    2. are you creating files with Uniflash (and then open again with the code and try to append to it for example) ot always creating if from your application?

    Please describe in details the entire procedure. How you create the file and how you appdate it.

    Shlomi

  • Hi Shalomi,

    I am using the same code which I posted above to read and write, so for your points please find the answers, 

    • from your printouts, is the file wifi_config.ini? what is the other long name starting with "Pr"? it is printed from g_writingBuffer, what is it?
      writing buffer is printing only the data which I am passing to the function, but I doubt that it is not writing that in serial flash properly. 
    • from your printouts, the fileLen is 4 so it seems only 4 bytes are there. Where did you write the 12 bytes you mentioned ?

    wifi_config is a file which I flashed using the uniflash, hence it is having proper string of 20 characters. so I guess it is having problem within write function as it is reading properly all the strings. 

    Please find the screenshot for the same. 

    1. do you have an existing file you try to write to or are you creating a new one?
    2. are you creating files with Uniflash (and then open again with the code and try to append to it for example) ot always creating if from your application?

    This file is already flashed on the device, and if it is not present, it will create the new one. As shown in above screen shot I write this files using the uniflash, which is read properly by the same function. So I guess there might be an issue with the write function only. 

  • Hi,

    I am confused. It seems you are sending to the function pData and not g_writingBuffer which contains "Pr!y@nk-2-Mon$ter1nc1".

    I did the same test on my end with the same buffer and it works as expected.

    See code snippet on main:

    _u8 testBuf[] = "Pr!y@nk-2-Mon$ter1nc1";

    retVal = sl_FsOpen((_u8 *)SL_FILE_NAME,
    FS_MODE_OPEN_CREATE(SIZE_64K,_FS_FILE_OPEN_FLAG_COMMIT),&Token, &fileHandle);
    if(retVal < 0)
    {
    printf(" Error in creating the file \n\r");
    LOOP_FOREVER();
    }

    retVal = sl_FsWrite(fileHandle, 0, (_u8 *)testBuf, sizeof(testBuf));

    /* Close the user file */
    retVal = sl_FsClose(fileHandle, 0, 0, 0);
    if (retVal < 0)
    {
    printf(" Error in closing the file \n\r");
    retVal = sl_FsDel((_u8 *)SL_FILE_NAME, Token);

    LOOP_FOREVER();
    }

    Status = sl_FsGetInfo((_u8 *)SL_FILE_NAME, Token ,&FsFileInfo);

    retVal = sl_FsOpen((_u8 *)SL_FILE_NAME,
    FS_MODE_OPEN_READ, &Token, &fileHandle);

    retVal = sl_FsRead(fileHandle, 0,(_u8 *)readBuffer, FsFileInfo.FileLen);

    retVal = sl_FsClose(fileHandle, 0, 0, 0);
    if (retVal < 0)
    {
    printf(" Error in closing the file \n\r");
    retVal = sl_FsDel((_u8 *)SL_FILE_NAME, Token);

    LOOP_FOREVER();
    }

    Shlomi

  • Hi,

    Any update on this thread? Can it be closed or do you have more concerns?

    Shlomi

  • Hi,

    I am closing the thread, if issue still exist please open a new thread and add a link to this one for reference.

    Shlomi