我写入的DataFlash的驱动程序如下
/* * MyDataFlash.c * * Created on: Aug 25, 2022 * Author: Lee */ #include "MyDataFlash.h" #include "config.h" #include "multiCentral.h" volatile uint8_t MacAddr_Update_Flag=0; uint8_t MyDataFlash_Check(void) { u8 temp; EEPROM_READ(FIRST_POWON_ADDR,&temp, 1);//避免每次开机都写DataFlash EEPROM_READ(SLAVE1_STARTADDR,&PeerAddrDef[0], 6); EEPROM_READ(SLAVE2_STARTADDR,&PeerAddrDef[1], 6); PRINT("Device %d - Addr %02x %02x %02x %02x %02x %02x \n", 0, PeerAddrDef[0].peerAddr[0], PeerAddrDef[0].peerAddr[1], PeerAddrDef[0].peerAddr[2], PeerAddrDef[0].peerAddr[3], PeerAddrDef[0].peerAddr[4], PeerAddrDef[0].peerAddr[5]); PRINT("Device %d - Addr %02x %02x %02x %02x %02x %02x \n", 1, PeerAddrDef[1].peerAddr[0], PeerAddrDef[1].peerAddr[1], PeerAddrDef[1].peerAddr[2], PeerAddrDef[1].peerAddr[3], PeerAddrDef[1].peerAddr[4], PeerAddrDef[1].peerAddr[5]); if(temp==FIRST_POWON_VALU)return 0; else//排除第一次初始化的情况 { memcpy(&PeerAddrDef[0],"\x99\x59\x9C\x4C\x17\x20",6); EEPROM_WRITE(SLAVE1_STARTADDR, &PeerAddrDef[0], 6); memcpy(&PeerAddrDef[1],"\x59\x59\x9C\x4C\x17\x20",6); EEPROM_WRITE(SLAVE2_STARTADDR, &PeerAddrDef[1], 6); temp=FIRST_POWON_VALU; EEPROM_WRITE(FIRST_POWON_ADDR, &temp, 1); EEPROM_READ(FIRST_POWON_ADDR, &temp, 1); if(temp==FIRST_POWON_VALU)return 0; } return 1; } void MyDataFlash_SaveMAC( uint8_t* Addr1,uint8_t* Addr2) { uint8_t d[12]={0}; PRINT("Device %d - Addr %02x %02x %02x %02x %02x %02x \n", 0, Addr1[0], Addr1[1], Addr1[2], Addr1[3], Addr1[4], Addr1[5]); PRINT("Device %d - Addr %02x %02x %02x %02x %02x %02x \n", 1, Addr2[0], Addr2[1], Addr2[2], Addr2[3], Addr2[4], Addr2[5]); EEPROM_WRITE(SLAVE1_STARTADDR, Addr1, 6); EEPROM_WRITE(SLAVE2_STARTADDR, Addr2, 6); }
另有头文件如下
/* * MyDataFlash.h * * Created on: Aug 25, 2022 * Author: Lee */ #ifndef _MYDATAFLASH_H_ #define _MYDATAFLASH_H_ #include "CH58x_common.h" #define FIRST_POWON_ADDR 0xFF//第一次开机校验的值存储的地址 #define FIRST_POWON_VALU 0xA5//第一次开机校验的值 #define SLAVE1_STARTADDR 0x00//从机1的mac在dataflash中的首地址 #define SLAVE2_STARTADDR SLAVE1_STARTADDR+6//从机2的mac在dataflash中的首地址 extern volatile uint8_t MacAddr_Update_Flag; extern uint8_t MyDataFlash_Check(void); extern void MyDataFlash_SaveMAC( uint8_t* Addr1,uint8_t* Addr2); #endif /* _MYDATAFLASH_H_ */
现在发现MyDataFlash_SaveMAC这个函数写入的值如下
Device 1 - Addr 99 59 9c 4c 17 20
Device 2 - Addr 59 59 9c 4c 17 20
然后开机通过这个
MyDataFlash_Check
函数读出:
Device 0 - Addr 59 19 9c 4c 17 20
Device 1 - Addr 89 51 9c 4c 17 20
发现前面两个字节变掉了
热门产品 :
CH397: 高集成度、低功耗USB网卡芯片