/* XDemo63:The Demo of writing data to Flash,and reading the data from Flash Compiler: BC++ 3.1 Compile mode: large Project: user.c v7000.c vModbus.c [after vcom3002.lib] ..\Lib\7188EL.Lib ..\Lib\TCPIPL.Lib ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version. 23 r, 23 R -> read the data from Flash,then Erease Flash after reading. 23 w, 23 W -> write the data to Flash. 23 rm -> Multi-read the data from Flash in Base address. 23 wm data -> Multi-write the data to Flash in Base address. 23 c -> Erease one segment Flash in Base address. 23 ri, 23 RI -> Read one Integer from Flash. 23 ri, 23 RI -> Read one Integer from Flash. 23 wi, 23 WI -> Write one Integer to Flash. 23 rf, 23 RF -> Read one Float from Flash. 23 wf, 23 WF -> Write one Float to Flash. 23 rs, 23 RS -> Read String from Flash. 23 ws, 23 WS -> Write String to Flash. Hardware: I-7188E series There are two type Flash Memory used on 7188x/I-8000 series, 256K bytes and 512K bytes. The segment of the address of 256K is 0xC000,0xD000,0xE000 and 0xF000, and address of 512K is 0x8000,0x9000,0xA000,0xB000,0xC000,0xD000,0xE000,and 0xF000. The sizes is 64Kbytes per segment,MiniOs7 use the last 64K bytes(0xF000~0xFFFF), the others are used to store the user's program and read/write the data from Flash memory. Note:When user write the data to Flash Memory, it only can be write from "1" to "0", can not be write from "0" to "1". In general,user must erase it, before writing data to FLASH. The erase process will make all data to 0xFF, then all data bit become "1". The data can be written. The function FlashErase() is used to erase the FLASH, Erease one sector once(64K bytes). Refer 7188e\TCP\Doc\[Big5|Eng|Gb2312]\Vxcomm.htm 7188e\TCP\Xserver\Xserver.htm 7188e\TCP\Xserver\Function.htm 7188e\MiniOS7\doc\eng\lib\flash.htm to get more information. Because the kernal of Xserver used InstallCom() function. Please use printCom1() instead of Print() if you want to send formatted output from COM1. [25,Jan,2003] by Sean [06,Oct,2004] Commented by Annita [27,May,2005] Modified by Liam Add commands ri, rf and rs to read integer, float, string from Flash. Add commands wi, wf and ws to write integer, float, string to Flash. [10/Aug/2005] by Liam [26,July,2011] by Nicholas */ #include #include #include #include #include "..\lib\7188e.h" #include "..\lib\tcpip32.h" #include "..\lib\vxcomm.h" TCPREADDATA *fp; unsigned base=0xE000; /* 256K type,seg can be 0xC000~0xE000. 512K type,seg can be 0x8000~0xE000 */ unsigned offset=0; /* 0 to 65535(0xffff). */ char InFlash[100],OutFlash[100]; /*InFlash[]: the string for reading from Flash,OutFlash[]:the string for writing to Flash */ unsigned long lTimeTicks; int iSetTime=0; /* control the writing action in UserLoopFun() */ char cTemp[40],cInput[7]; /* cTemp for the error message of writing/reading ,cInput for 23 command to check read(r)/write(w) from Flash */ int ilen; /* the lenth of the OutFlash[] */ void Flash_MultiRead(int iSeg,int ioffset, int iCount, char* cData) { int i; for(i=ioffset;i<(ioffset+iCount);i++) { *cData=FlashRead(iSeg,i); cData++; } } int Flash_MultiWrite(int iSeg,int ioffset,int iCount, char* cData) { int i,iRet; for(i=ioffset;i<(ioffset+iCount);i++) { iRet=FlashWrite(iSeg,i,*cData); if(iRet<0) return iRet; else cData++; RefreshWDT(); } return 0; } void UserCount(void) { /* User's timer trigger function. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ } void UserInit(void) { /* Initialize user's program. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ unsigned int id,fid; /* All of the program user.c needn't function Installcom(). Becuase they have install When the Xserver initialize. If you want to change the baud rate.data format, please using the function SetBaudrate().SetDataFormat(). */ SetBaudrate1(115200L); if(!Is7188e()){ printCom1("Flash.exe must run under 7188E.\n\r"); return; } id=Is7188e(); printCom1("\n\r7188E(%dK)",id); if(id==512) base=0xB000; fid=FlashReadId(); /* Read Flash memory device code(high byte) and manufacture code(low byte). */ printCom1("\n\rFlash ID=%X -->",fid); if((fid&0xff)==1) printCom1("AMD"); /* rint("AMD"); */ else if((fid&0xff)==0xC2) printCom1("MXIC"); /* Print("MXIC"); */ else printCom1("???");/* Print("???"); */ if((fid>>8)==0xB0) printCom1("29F002T(256K)"); /*Print("29F002T(256K)");*/ else if((fid>>8)==0xA4) printCom1("29F040(512K)"); /*Print("29F040(512K)");*/ else printCom1("???(???K)");; /*Print("???(???K)");*/ /* sprintf(OutFlash,"The Flash demo"); */ /* ilen=strlen(OutFlash); */ } void UserLoopFun(void) { /* VxComm.exe will call this function every scan time Please refer to XDemo11 for Real-time I/O control */ } int UserCmd(unsigned char *Cmd,unsigned char *Response) { /* Xserver executes this function when received a package form TCP port 10000 and the first two bytes are "19". Funtion of Xserver, Please refer to XDemo04 for detail description. */ return 1; /* return OK */ } int VcomUserBinaryCmd(TCPREADDATA *p) { /* VXCOMM.EXE 2.6.12(04,Sep,2001) or later will support this function. Xserver executes this function when received a package form TCP port 10000 and the first two bytes are "23". Please refer to XDemo04 for detail description. Please refer to XDemo23 for example code. */ int iRet; int iData=0; float fData=0; char sData[26]; unsigned i; char str[50]; memset(cInput,'\0',sizeof(cInput)); memset(OutFlash,'\0',sizeof(OutFlash)); fp->Socket=p->Socket; p->ReadUartChar[p->Length]=0; sscanf(p->ReadUartChar+3,"%s %s",cInput,OutFlash); strlwr(cInput); if(!strcmp("r",cInput)) { for(i=0;i<15;i++) InFlash[i]=FlashRead(base,i); InFlash[i+1]=0; printCom1("%",InFlash); VcomSendSocket(fp->Socket,InFlash,strlen(InFlash)); } if(!strcmp("w",cInput)) { strcpy(OutFlash,"The Flash demo"); for(i=0;i<15;i++) { iRet=FlashWrite(base,i,OutFlash[i]); if(iRet<0) { sprintf(cTemp,"Write error seg:offset=%4X,%4X,error code:%d\n\r",base,offset+i,iRet); printCom1(cTemp); } else if(i>=14) { sprintf(cTemp,"Write OK!\r\n"); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } } /* printCom1("TimeTicks takes %d ms\r\n",*TimeTicks-lTimeTicks); */ /*debug*/ } if(!strcmp("rm",cInput)) /* Multi-read */ { Flash_MultiRead(base,offset,strlen(OutFlash),InFlash); sprintf(cTemp,"Flash_MultiRead:%s\r\n",InFlash); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } if(!strcmp("wm",cInput)) /* Multi-write */ { iRet==Flash_MultiWrite(base,offset,strlen(OutFlash),OutFlash); if(iRet<0){ sprintf(cTemp,"Flash_MultiWrite error:%d\r\n",iRet); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } else { sprintf(cTemp,"Flash_MultiWrite OK\r\n"); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } } if(!strcmp("c",cInput)) /* Ereas Flash */ { iRet=FlashErase(base); if(iRet!=0){ sprintf(cTemp,"Erase error:%d\n\r",iRet); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } else { sprintf(cTemp,"Erase OK\r\n"); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } } if(!strcmp("wi", cInput)) /*Write one Integer to Flash*/ { sscanf(OutFlash, "%d", &iData); iRet=Flash_MultiWrite(base, 15, 2, (char*)&iData); if(iRet<0) { sprintf(cTemp,"Write error seg:offset=%4X,%4X,error code:%d\n\r",base,offset+i,iRet); printCom1(cTemp); } else { sprintf(cTemp,"Write OK!\n\r"); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } } if(!strcmp("ri", cInput)) /*read one integer from Flash*/ { Flash_MultiRead(base, 15, 2, (char*)&iData); sprintf(cTemp,"Read Flash Integer: %d\r\n",iData); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } if(!strcmp("wf", cInput)) /*Write one float value to Flash*/ { sscanf(OutFlash, "%f", &fData); iRet=Flash_MultiWrite(base, 17, 4, (char*)&fData); if(iRet<0) { sprintf(cTemp,"Write error seg:offset=%4X,%4X,error code:%d\n\r",base,offset+i,iRet); printCom1(cTemp); } else { sprintf(cTemp,"Write OK!\n\r"); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } } if(!strcmp("rf", cInput)) /*read one float value from Flash*/ { Flash_MultiRead(base, 17, 4, (char*)&fData); sprintf(cTemp,"Read Flash Float: %f\r\n",fData); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } if(!strcmp("ws", cInput)) /*Write one float value to Flash*/ { iRet=Flash_MultiWrite(base, 21, 25, &OutFlash); if(iRet<0) { sprintf(cTemp,"Write error seg:offset=%4X,%4X,error code:%d\n\r",base,offset+i,iRet); printCom1(cTemp); } else { sprintf(cTemp,"Write OK!\n\r"); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } } if(!strcmp("rs", cInput)) /*read one float value from Flash*/ { Flash_MultiRead(base, 21, 25, &sData); sprintf(cTemp,"Read Flash String: %s\r\n", sData); printCom1(cTemp); VcomSendSocket(fp->Socket,cTemp,strlen(cTemp)); } return 1; /* any value will be accept */ } int VcomCmdUser(TCPREADDATA *p) { /* VCOM3005 (Feb,22,2002) or later will call this function for PortUser. When packets received by TCP PORT PortUser(user defined) of 7188E/8000E, Xserver will call this function. Please refer to XDemo04 for detail description. */ VcomSendSocket(p->Socket,p->ReadUartChar,p->Length); return 1; /* any value will be accept */ } void PortUserStart(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E via the user's defined port(PortUser), the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; /* do nothing */ } void Port9999Start(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E TCP port 9999, the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; /* do nothing */ } void Port502Start(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E TCP port 502, the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; /* do nothing */ }