Other Parts Discussed in Thread: CC3200
Hi,
Recently our product need to include file operation base on Mqtt_Client example code.
Procedure as below,
Starting system -> Start SimpleLink task -> FsOpen(Create file) -> FsClose -> Push button -> PushButtonIntHandler
-> signal a queue and MsgQWrite -> MsgQRead in a task, and FsOpen & FsWrite. And then, do the hibernate command to restart system
-> FsOpen & FsRead after start SimpleLink task
It can't success to read data as I write in before hibernate system.
Could anyone help?
Thanks!
Part of code is attached.
/*File opertation variables*/ unsigned char readbuffer[BUF_SIZE]; SlFsFileInfo_t FileInfo; long lFileHandle; unsigned long ulToken; /*Write file into flash*/ long WriteFileToDevice(unsigned long *ulToken, long *lFileHandle) { long lRetVal = -1; // // open a user file for writing // lRetVal = sl_FsOpen((unsigned char *)USER_FILE_NAME, FS_MODE_OPEN_WRITE, ulToken, lFileHandle); if(lRetVal < 0) { lRetVal = sl_FsClose(*lFileHandle, 0, 0, 0); return FAILURE; } lRetVal = sl_FsGetInfo(USER_FILE_NAME, 0, &FileInfo); if(lRetVal < 0) { GPIO_IF_LedOn(MCU_RED_LED_GPIO); LOOP_FOREVER(); } UART_PRINT("Get file information:\n"); UART_PRINT("FileLen: [%d]\n", FileInfo.FileLen); UART_PRINT("AllocatedLen: [%d]\n", FileInfo.AllocatedLen); UART_PRINT("File token: [%d]\n", FileInfo.Token); UART_PRINT("File flags: [%d]\n", FileInfo.flags); char ssid_name[] = "Lex_Hsiao"; char *writebuffer=(char*)malloc(1024); memset(writebuffer,'\0',sizeof(writebuffer)); memcpy(writebuffer, (char*)ssid_name, sizeof(ssid_name)-1); // // write new ssid, password and secure type into file // lRetVal = sl_FsWrite(*lFileHandle, 0, (unsigned char *)writebuffer, sizeof(ssid_name)-1); if (lRetVal < 0) { lRetVal = sl_FsClose(*lFileHandle, 0, 0, 0); return FAILURE; } // // close the user file // lRetVal = sl_FsClose(*lFileHandle, 0, 0, 0); UART_PRINT("Write file success\n\n"); return SUCCESS; } /*Read file from flash*/ long ReadFileFromDevice(unsigned long ulToken, long lFileHandle) { long lRetVal = -1; // // open a user file for reading // lRetVal = sl_FsOpen((unsigned char *)USER_FILE_NAME, FS_MODE_OPEN_READ, &ulToken, &lFileHandle); if(lRetVal < 0) { lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); return FAILURE; } // // read the data, lRetVal returns the number of read bytes // lRetVal = sl_FsRead(lFileHandle, 0, readbuffer, sizeof(readbuffer)); UART_PRINT("Readbuffer length: %d\n", lRetVal); if(lRetVal < 0) { UART_PRINT("Use default wifi setting.\n"); lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); return SUCCESS; } else { for (int i = 0; i < lRetVal; i++) UART_PRINT("%c", readbuffer[i]); UART_PRINT("\n"); // // close the user file // lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); return SUCCESS; } } /*File operation*/ void FileOperation(void) { long lRetVal; lRetVal = sl_Start(NULL, NULL, NULL); if(lRetVal < 0) { GPIO_IF_LedOn(MCU_RED_LED_GPIO); LOOP_FOREVER(); } // // create a user file // lRetVal = sl_FsOpen((unsigned char *)USER_FILE_NAME, FS_MODE_OPEN_CREATE(65536, _FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE|_FS_FILE_PUBLIC_READ), &ulToken, &lFileHandle); if(lRetVal < 0) { UART_PRINT("File %s already exist.\n", USER_FILE_NAME); // // File may already be created // lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); } else { // // close the user file // lRetVal = sl_FsClose(lFileHandle, 0, 0, 0); } lRetVal = sl_FsGetInfo(USER_FILE_NAME, 0, &FileInfo); if(lRetVal < 0) { GPIO_IF_LedOn(MCU_RED_LED_GPIO); LOOP_FOREVER(); } UART_PRINT("Get file information:\n"); UART_PRINT("FileLen: [%d]\n", FileInfo.FileLen); UART_PRINT("AllocatedLen: [%d]\n", FileInfo.AllocatedLen); UART_PRINT("File token: [%d]\n", FileInfo.Token); UART_PRINT("File flags: [%d]\n", FileInfo.flags); if(ReadFileFromDevice(ulToken, lFileHandle) < 0) { GPIO_IF_LedOn(MCU_RED_LED_GPIO); UART_PRINT("Read file fail\n\n"); } //lRetVal = sl_Stop(SL_STOP_TIMEOUT); } /*Push button interrupt handler*/ void pushButtonInterruptHandler2() { event_msg msg; msg.event = PUSH_BUTTON_SW2_PRESSED; msg.hndl = NULL; // // write message indicating exit from sending loop // osi_MsgQWrite(&g_PBQueue,&msg,OSI_NO_WAIT); Button_IF_EnableInterrupt(SW2); } /*Task for setting wifi direct information flag*/ void WifiDirectFlagSetting(void *pvParameters) { event_msg RecvQue; for(;;) { osi_MsgQRead( &g_PBQueue, &RecvQue, OSI_WAIT_FOREVER); if(PUSH_BUTTON_SW2_PRESSED == RecvQue.event) { if(WriteFileToDevice(&ulToken, &lFileHandle) < 0) { GPIO_IF_LedOn(MCU_RED_LED_GPIO); LOOP_FOREVER(); } // Restart system MAP_PRCMHibernateIntervalSet(330); MAP_PRCMHibernateWakeupSourceEnable(PRCM_HIB_SLOW_CLK_CTR); MAP_PRCMHibernateEnter(); } } } /*MqttClient task*/ void MqttClient(void *pvParameters) { FileOperation(); }
And print some log below.
*************************************************
CC3200 MQTT_Client Application
*************************************************
[WLAN EVENT] STA Connected to the AP: iTeam , BSSID: 6c:b0:ce:14:6d:bc
Create file: /usr/wificonfigfile.txt.
Get file information:
FileLen: [0]
AllocatedLen: [69192]
File token: [536999368]
File flags: [0]
Open file for read: /usr/wificonfigfile.txt.
Readbuffer length: -15
Use default wifi setting.
Get file information:
FileLen: [0]
AllocatedLen: [69192]
File token: [536999368]
File flags: [0]
Write file success
*************************************************
CC3200 MQTT_Client Application
*************************************************
[WLAN EVENT] STA Connected to the AP: iTeam , BSSID: 6c:b0:ce:14:6d:bc
Create file: /usr/wificonfigfile.txt.
Token [0]
File handle [1210682119]
Get file information:
FileLen: [0]
AllocatedLen: [69192]
File token: [536999368]
File flags: [0]
Open file for read: /usr/wificonfigfile.txt.
Readbuffer length: -15
Use default wifi setting.
Regards,
Lex