⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setparam.c

📁 一个uclinux下进行网络数据处理,及串口读取,CGI配置
💻 C
字号:
#include "setparam.h"int erase_sector(unsigned long flash_addr){    *(volatile unsigned short *)(flash_addr&0xfffff000|0xaaa)=0xaaaa;    *(volatile unsigned short *)(flash_addr&0xfffff000|0x554)=0x5555;    *(volatile unsigned short *)(flash_addr&0xfffff000|0xaaa)=0x8080;    *(volatile unsigned short *)(flash_addr&0xfffff000|0xaaa)=0xaaaa;    *(volatile unsigned short *)(flash_addr&0xfffff000|0x554)=0x5555;    *(volatile unsigned short *)(flash_addr)=0x3030;    printf("flash secter erased!\n");}int write_word(unsigned long flash_addr,unsigned short value){    unsigned short temp_w;    unsigned long i;    //value = strtoul(input_argv3,end,16);    *(volatile unsigned short *)(flash_addr&0xfffff000|0xaaa)=0xaaaa;    *(volatile unsigned short *)(flash_addr&0xfffff000|0x554)=0x5555;    *(volatile unsigned short *)(flash_addr&0xfffff000|0xaaa)=0xa0a0;    *(volatile unsigned short *)(flash_addr)=value;    temp_w=*(volatile unsigned short *)flash_addr;    i=0;    while(temp_w != value){        temp_w=*(volatile unsigned short *)flash_addr;        i++;        if(i==0xfff){            printf("err :%08X ",flash_addr);            printf("write = %04X ",value);            printf("read = %04X \n",temp_w);            return 1;        }    }    return 0;}////////////////////////////////////////////////////////////////////////////////file operation(flat FS)long int getfilelength(char *filename){    FILE *fp;    long int i;    int j;    fp=fopen(filename,"r");    fseek(fp,0L,2);    i=ftell(fp);    j=fclose(fp);    return(i);}/////////////////////////////////////////////////////////////////////////////HEAD:filename(32bytes)+filelength(4bytes)+filecontents(filelength)///////////////////////////////////////////////////////////////////////////int write_file_to_flash(char * filename,unsigned long addr){    FILE *fp;    unsigned long i,j,len;    unsigned short value_w,temp_w;    unsigned char *p;    char fname[32];    bzero(fname,32);    strncpy(fname,filename,32);    p=(unsigned char *)&len;    //////////////////////////////////////////////////////////////////////////    //write filename    for(i=0;i<32;i+=2){        value_w=*(unsigned short *)(fname+i);        if(write_word(addr,value_w)!=0)            return 2;        addr+=2;    }    //////////////////////////////////////////////////////////////////////////    //write file length    len=getfilelength(filename);    printf("filelength=0x%08X\n",len);    value_w=*(unsigned short *)p;    if(write_word(addr,value_w)!=0)        return 2;    addr+=2;    value_w=*(unsigned short *)(p+2);    if(write_word(addr,value_w)!=0)        return 2;    addr+=2;    //////////////////////////////////////////////////////////////////////////    fp=fopen(filename,"r");    if(fp==NULL){        printf("open file error");        return 1;    }    for(;;){        j=fread(&value_w,sizeof(unsigned short),1,fp);        //value_w = exchangew(value_w);        if(j!=1){            if(ferror(fp) != 0)                printf("over or wrong\n");            else                printf("File write successfully!\n");            break;        }        if(write_word(addr,value_w)!=0)            return 2;        addr+=2;    }    if (len%2==1)    {       value_w=0x0AFF;       if (write_word(addr,value_w)!=0) return 2;    }    fclose(fp);    return 0;}int main(){ int i; int pid; htmlinput_struct htmlinputs[100]; int htmlinputcount; commparam_struct commparam; unsigned long ipaddr,commaddr;// html_message("success","you have successfully set the parameters!"); htmlinputcount=get_CGI_request(htmlinputs); if (!htmlinputcount) {   html_message("error","set parameters error!");   exit(1); } else {   for(i=0;i<htmlinputcount;i++)   {      if (!strcmp(htmlinputs[i].name,"localip"))        strcpy(commparam.localip,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"netmask"))        strcpy(commparam.netmask,htmlinputs[i].value);            if (!strcmp(htmlinputs[i].name,"serverip"))        strcpy(commparam.serverip,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"serverport"))        strcpy(commparam.serverport,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"baudrate"))        strcpy(commparam.baudrate,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"parity"))        strcpy(commparam.parity,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"databits"))        strcpy(commparam.databits,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"stopbits"))        strcpy(commparam.stopbits,htmlinputs[i].value);      if (!strcmp(htmlinputs[i].name,"handshaking"))        strcpy(commparam.handshaking,htmlinputs[i].value);   }   if (!strcmp(commparam.serverport,""))   {      html_message("error","the serverport should not be empty!");      exit(1);   } }  print_file("/etc/config/start","/etc/config/param.conf",commparam);    //save the ip data to 0xffef0000  ipaddr = 0xffef0000;  commaddr = 0xfff00000;  erase_sector(ipaddr);  erase_sector(commaddr);  sleep(6);  write_file_to_flash("/etc/config/start",ipaddr);    write_file_to_flash("/etc/config/param.conf",commaddr);//  html_message("success","you have successfully set the parameters!");/*  if ((pid=vfork())<0)  {     printf("vfork error\n");  }  else if (pid==0)  {    execl("/bin/reboot");    exit(0);  }  else  {    // html_message("success","you have successfully set the parameters!");	  } // html_message("success","you have successfully set the parameters!");// exit(0);*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -