/* X561Demo.c: Demo program for X561 Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum) Compile mode: large Project: X561Demo.c ..\..\Lib\7188el.lib ..\..\lib\Xboard\X600.h Hardware: 7188EX + X561 X506: COM3 (3-wire RS-232) COM4 (3-wire RS-232) COM5 (3-wire RS-232) Flash (64M) [07/July/2005] by Liam [29/Sep/2007] by Jose */ #include #include #include "..\..\lib\7188xb.h" #include "..\..\lib\Xboard\X600.h" #define BUFSIZE 1024 void main(void) { char OutpBuf[BUFSIZE], InpBuf[BUFSIZE]; int idata, iAction, quit; unsigned char flash_status; int block_num=0; int page_num=0; int addr_to_WR=0; char main_buf[512]; char spare_buf[16]; char r_main_buf[512]; char r_spare_buf[16]; char page_buf[528]; char r_page_buf[528]; InitLib(); Print("\n\r"); Print("****************************************\n\r"); Print("| 7188XB + X561 |\n\r"); Print("****************************************\n\r"); InstallCom(3,115200L,8,0,1); InstallCom(4,115200L,8,0,1); InstallCom(5,115200L,8,0,1); while(iAction!=7) { iAction=0; quit=0; Print("\n\r"); Print("1) Test COM3 (3-wire RS-232)\n\r"); Print("2) Test COM4 (3-wire RS-232)\n\r"); Print("3) Test COM5 (3-wire RS-232)\n\r"); Print("4) Show Flash Information and Flash Status \n\r"); Print("5) Test Flash by Writing to a Particular Page and Reading Back to Check\r\n"); Print("6) Test Flash by Writing to a Particular Page with specified address and specifed length\r\n"); Print(" and Reading Back with specified address and specified length to Check\r\n"); Print("\n\r"); Print("7) Quits Demo program\n\r\n\r"); Print("Choose an option and press [Enter]:"); iAction=0; Scanf("%d",&iAction); switch(iAction) { // FOR COM3 case 1: Print("\n\rSend & Receive data to/from COM3\n\r"); Print("Enter a string to COM port:"); Scanf("%s",&OutpBuf); ToComStr(3,OutpBuf); Delay(2); idata=ReadComn(3,InpBuf,BUFSIZE); InpBuf[idata]=0x0; Print("Read COM port==>%s,idata=%d\r\n",InpBuf,idata); if(!strcmp(OutpBuf,InpBuf)) Print("Send COM3 <==> Receiv COM3 -->Test OK\n\r"); else Print("Send COM3 <==> Receiv COM3 -->Test error\n\r"); break; // FOR COM4 case 2: Print("\n\rSend & Receive data to/from COM4\n\r"); Print("Enter a string to COM port:"); Scanf("%s",&OutpBuf); ToComStr(4,OutpBuf); Delay(2); idata=ReadComn(4,InpBuf,BUFSIZE); InpBuf[idata]=0x0; Print("Read COM port==>%s,idata=%d\r\n",InpBuf,idata); if(!strcmp(OutpBuf,InpBuf)) Print("Send COM4 <==> Receiv COM4 -->Test OK\n\r"); else Print("Send COM4 <==> Receiv COM4 -->Test error\n\r"); break; // FOR COM5 case 3: Print("\n\rSend & Receive data to/from COM5\n\r"); Print("Enter a string to COM port:"); Scanf("%s",&OutpBuf); ToComStr(5,OutpBuf); Delay(2); idata=ReadComn(5,InpBuf,BUFSIZE); InpBuf[idata]=0x0; Print("Read COM port==>%s,idata=%d\r\n",InpBuf,idata); if(!strcmp(OutpBuf,InpBuf)) Print("Send COM5 <==> Receiv COM5 -->Test OK\n\r"); else Print("Send COM5 <==> Receiv COM5 -->Test error\n\r"); break; case 4: X600_Init(); X600_WP_FLASH(1); //write protection, X600_WRITE_ENABLE = 1 flash_status = X600_Read_FLASH_Status(); //BIT 0: 0=erase/write OK, 1=erase/write Error //BIT 6: 0=device busy, 1=device ready //BIT 7: 0=write protection, 1=no write protection //the other bits have no function //thus, for example, bit 0, 6, 7 are all = 1, flash_status = 128+64+1= 193 Print("Flash_status = %d\r\n",flash_status); Print("MaxBlock %d, PageNoPerBlock: %d.\n", MaxBlock, PageNoPerBlock); Print("PageDataSize : %d, PageSpareDataSize :%d.\r\n",PageDataSize,PageSpareDataSize); Print("FLASH size=MaxBlock * PageNoPerBlock * (PageDataSize+PageSpareDataSize)\r\n"); break; case 5: X600_Init(); X600_WP_FLASH(1); //write protection, X600_WRITE_ENABLE = 1 Print("Please input a block ( 0 - %d ) to write Flash:",MaxBlock-1); Scanf("%d",&block_num); Print("Please input a page ( 0 - %d ) to write Flash:",PageNoPerBlock-1); Scanf("%d",&page_num); Print("Please input what you want to write to main data area of Flash:(at most %d bytes)",PageDataSize); Scanf("%s",main_buf); Print("Please input what you want to write to spare data area of Flash:(at most %d bytes)",PageSpareDataSize); Scanf("%s",spare_buf); Print("Erase block %d all to 0xFF.\r\n",block_num); X600_Erase_FLASH_Block(block_num); //erase block one to 0xFF /*before writing data to flash, one needs to erase flash because writing can only change data from 1 to 0, not from 0 to 1. erase one block, makes all data in that block to 1, that is, 0xFF*/ Print("Write data to main and spare data area of block %d, page %d.\r\n",block_num,page_num); X600_Write_Page(block_num,page_num, main_buf, spare_buf); Print("Read data from main and spare data area of block %d, page %d.\r\n",block_num,page_num); X600_Read_Page(block_num,page_num, r_main_buf, r_spare_buf); Print("main data area = %s\r\nspare data area = %s\r\n",r_main_buf, r_spare_buf); if(!strcmp(main_buf,r_main_buf)){ Print("main data area of block %d, page %d test is OK.\r\n",block_num, page_num); } if(!strcmp(spare_buf,r_spare_buf)){ Print("spare data area of block %d, page %d test is OK.\r\n",block_num, page_num); } if(!(strcmp(main_buf,r_main_buf)||strcmp(spare_buf,r_spare_buf))){ Print("Test on block %d, page %d test is OK.\r\n",block_num, page_num); } break; case 6: X600_Init(); X600_WP_FLASH(1); //write protection, X600_WRITE_ENABLE = 1 Print("Please input a block ( 0 - %d ) to write Flash:",MaxBlock-1); Scanf("%d",&block_num); Print("Please input a page ( 0 - %d ) to write Flash:",PageNoPerBlock-1); Scanf("%d",&page_num); Print("Please input address ( 0 - %d ) to write Flash:",PageDataSize+PageSpareDataSize-1); Scanf("%d",&addr_to_WR); Print("Please input what you want to write Flash:"); Scanf("%s",page_buf); Print("Erase block %d all to 0xFF.\r\n",block_num); X600_Erase_FLASH_Block(block_num); Print("Write data to block %d, page %d from address %d with data length %d (bytes).\r\n",block_num,page_num,addr_to_WR,strlen(page_buf)); X600_Write_Page_nByte(block_num,page_num,addr_to_WR,page_buf,strlen(page_buf)); X600_WP_FLASH(0); //#define X600_WRITE_DISABLE 0 Print("Read data to block %d, page %d from address %d with data length %d (bytes).\r\n",block_num,page_num,addr_to_WR,strlen(page_buf)); X600_Read_Page_nByte(block_num,page_num,addr_to_WR,r_page_buf,strlen(r_page_buf)); /*read out data to check if writing is not malfunction*/ if(!(strcmp(main_buf,r_main_buf)||strcmp(spare_buf,r_spare_buf))){ Print("Test on block %d, page %d test is OK.\r\n",block_num, page_num); } break; case 7: quit=1; RestoreCom(3); RestoreCom(4); RestoreCom(5); break; default : quit=1; break; } if(!quit){ Print("\n\rPress any key to continue...\n\r"); Getch(); } } }