void Data_Load(void) { uint8_t count; uint8_t i; FLASH_Unlock(); while(FLASH_GetFlagStatus(FLASH_FLAG_BSY)==1); for(count=0;count < SAVEDATA_NUM;count++) { for(i=0;i<4;i++) { flash_save[count].u16_data[i] = flashread((PAGE_ADDRESS +count*8+i*2)); //flash 为一个字节存储,16位数据必须地址加2 } } Gyro_Float = flash_save[0].i32_data[0]; Gyro_Convert1 = flash_save[1].f64_data; Gyro_Convert2 = flash_save[2].f64_data; Encoders[0].Convert1 = flash_save[3].f64_data; Encoders[0].Convert2 = flash_save[4].f64_data; Encoders[0].Radius = flash_save[5].f64_data; Encoders[0].radian = flash_save[6].f64_data; Encoders[1].Convert1 = flash_save[7].f64_data; Encoders[1].Convert2 = flash_save[8].f64_data; Encoders[1].Radius = flash_save[9].f64_data; Encoders[1].radian = flash_save[10].f64_data; while(FLASH_GetFlagStatus(FLASH_FLAG_BSY)==1); FLASH_Lock(); }
void gethwmac(unsigned char *mac) { unsigned char tmpbuf[6]; unsigned short len; unsigned char *buf; unsigned char sum=0; int i; if (flashread(tmpbuf, HW_SETTING_OFFSET,6)==0 ) { return; } if(tmpbuf[0] == 'h') { memcpy(&len, &tmpbuf[4], 2); if(len > 0x2000) return; if(NULL==(buf=(unsigned char *)malloc(len))) return; flashread(buf,HW_SETTING_OFFSET+6,len); if(len != 0 && len <= 0x2000) { for (i=0;i<len;i++) sum += buf[i]; } else sum=1; if(0 == sum) { memcpy(mac,buf+HW_NIC0_MAC_OFFSET,6); if(memcmp(mac,"\x0\x0\x0\x0\x0\x0", 6) && !(mac[0] & 0x1)) { /*normal mac*/ } else { memset(mac,0x0,6); } } if(buf) free(buf); } return; }
/* 写入实际地址add重复读错times次后赋值flash_error, 或者在规定重读次数前读出正确的值并返回。*/ unsigned char repeat_read(unsigned int add,unsigned char times) { unsigned char i=0,y; do { if(i>times) break; y=flashread(add); i++; } while(flash_error&read_error); return y; }
void Data_Save(void) { uint8_t count; uint8_t i,flag; LED4_on; flash_save[0].i32_data[0] = Gyro_Float; flash_save[0].i32_data[1] = 0x00000000; flash_save[1].f64_data = Gyro_Convert1; flash_save[2].f64_data = Gyro_Convert2; flash_save[3].f64_data = Encoders[0].Convert1; flash_save[4].f64_data = Encoders[0].Convert2; flash_save[5].f64_data = Encoders[0].Radius; flash_save[6].f64_data = Encoders[0].radian; flash_save[7].f64_data = Encoders[1].Convert1; flash_save[8].f64_data = Encoders[1].Convert2; flash_save[9].f64_data = Encoders[1].Radius; flash_save[10].f64_data = Encoders[1].radian; FLASH_Unlock(); while(FLASH_GetFlagStatus(FLASH_FLAG_BSY)==1); FLASH_ErasePage(PAGE_ADDRESS); for(count=0;count < SAVEDATA_NUM;count++) { for(i=0;i<4;i++) { flag = 0; while(flag==0) { FLASH_ProgramHalfWord((PAGE_ADDRESS +count*8+i*2),flash_save[count].u16_data[i]); //flash 为一个字节存储,16位数据必须地址加2 if(flashread((PAGE_ADDRESS +count*8+i*2)) == (flash_save[count].u16_data[i])) { flag = 1; LED2_off; } else LED2_on; } } } while(FLASH_GetFlagStatus(FLASH_FLAG_BSY)==1); FLASH_Lock(); LED4_off; }
void getmacandip(unsigned char *mac,unsigned char *ip) { unsigned char tmpbuf[6]; unsigned short len; unsigned char *buf; unsigned char sum=0; int i; int currSettingMaxLen = 0x4000; if (flashread(tmpbuf, CURRENT_SETTING_OFFSET,6)==0 ) { return; } #if defined(RTL8196B) || defined(RTL8198) if(tmpbuf[0] == '6') #else if(tmpbuf[0] == 'c') #endif { /*current setting*/ memcpy(&len, &tmpbuf[4], 2); if(len > currSettingMaxLen) return; /*alloc mem for reading current setting*/ if(NULL==(buf=(unsigned char *)malloc(len))) return; flashread(buf,CURRENT_SETTING_OFFSET+6,len); if(len != 0 && len <= currSettingMaxLen) { for (i=0;i<len;i++) sum += buf[i]; } else sum=1; if(0 == sum) { /*check sum ok*/ memcpy(ip,buf+CURRENT_IP_ADDR_OFFSET,4); memcpy(mac,buf+CURRENT_ELAN_MAC_OFFSET,6); //printf("\n ip: %d.%d.%d.%d__%u\n",*ip,*(ip+1),*(ip+2),*(ip+3),__LINE__); //printf("\n mac: %x-%x-%x-%x-%x-%x __%u\n",*mac,*(mac+1),*(mac+2),*(mac+3),*(mac+4),*(mac+5),__LINE__); if(memcmp(ip,"\x0\x0\x0\x0",4) && !(0xFF==ip[3] ||0x0==ip[3])) { /*normal ip*/ if(memcmp(mac,"\x0\x0\x0\x0\x0\x0", 6) && !(mac[0] & 0x1)) { /*normal mac*/ } else { /*bad mac. user hw setting mac*/ gethwmac(mac); } if(memcmp(ip,"\xC0\xA8\x0\x1",4) !=0) { /* different ip with 192.168.0.1, MUST use different MAC */ eth0_mac[0]=0x56; eth0_mac[1]=0xaa; eth0_mac[2]=0xa5; eth0_mac[3]=0x5a; eth0_mac[4]=0x7d; eth0_mac[5]=0xe8; } else { /* same ip with 192.168.0.1, so use the same mac */ gethwmac(eth0_mac); } } else { /*use hard code 192.168.1.6*/ memset(ip,0x0,4); } } if(buf) free(buf); } }