WCH沁恒单片机
直播中

贾小龙

7年用户 1618经验值
私信 关注
[问答]

CH582F DataFlash的读写问题求解

我写入的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],"x99x59x9Cx4Cx17x20",6);
EEPROM_WRITE(SLAVE1_STARTADDR, &PeerAddrDef[0], 6);
memcpy(&PeerAddrDef[1],"x59x59x9Cx4Cx17x20",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

发现前面两个字节变掉了

回帖(1)

潘务单

2022-9-19 14:48:10
可以打印8个字节的大小,uuid是8个字节的大小,包含了mac地址的信息,上面打印6个字节的大小可能正好是错开了。在处理时先擦除一下。
参考一下我们的FLASH例程,里面就有获取MAC地址的函数

如果想要获取配对设备的MAC地址信息,可以根据广播包对应筛选
举报

更多回帖

发帖
×
20
完善资料,
赚取积分