|
楼主 |
发表于 2011-7-28 10:52:30
|
显示全部楼层
回复【楼主位】quanhaiqiang
-----------------------------------------------------------------------
以下是我的ds28e01(Copy Scratchpad)写数据的函数,比如把unsigned char wdatato[8]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38
};这8个字节写到以0x60地址开始的存储空间,就调用protect_write_data(0x60, chip_num, secret, wdatato);
但结果写失败,向ds28e01传完MAC之后读一个字节老是读到temp=0xFF,失败。请高手指点,谢谢了。
unsigned char protect_write_data(unsigned char address,unsigned char* chip, unsigned char* secret, unsigned char* data)
{
unsigned char sha_input[64], err, i, page, temp, mac_array[20], scratch[11];
err=1;
page = address & 0xE0;
//Build array needed for MAC calculation
//See DS8E01 datasheet for details
//M0
for(i = 0; i < 4; ++i)
sha_input = *(secret + i);
//M1 - M7
if(page == 0x80)
{
for(i = 0; i < 8; i++)
sha_input[i + 4] = *(secret + i);
if(err)
err = read_block((page + 8), (sha_input + 12), 20);
sha_input[28] = 0xFF; sha_input[29] = 0xFF; sha_input[30] = 0xFF; sha_input[31] = 0xFF;
}
else
{
if(err)
err = read_block(page, (sha_input + 4), 28);
}
//Write data to the scratch pad
if(err)
err = WriteScratch(address, data, 8);
//M8 - M9
if(err)
{
err = ReadScratch(scratch);
if(err)
{
for(i = 3; i < 11; i++)
sha_input[i + 29] = scratch;
//sha_input[address - page] = data;
//M10[31:24]
sha_input[40] = 0x00 | (address >> 5);
//M10[23:0] - M11
for(i = 0; i < 7; i++)
sha_input[i + 41] = *(chip + i);
//M12
for(i = 4; i < 8; i++)
sha_input[i + 44] = *(secret + i);
//M13-M15
sha_input[52] = 0xFF; sha_input[53] = 0xFF; sha_input[54] = 0xFF; sha_input[55] = 0x80;
sha_input[56] = 0x00; sha_input[57] = 0x00; sha_input[58] = 0x00; sha_input[59] = 0x00;
sha_input[60] = 0x00; sha_input[61] = 0x00; sha_input[62] = 0x01; sha_input[63] = 0xB8;
//Generate the MAC using SHA-1 algorithm
SHA1_Perform(sha_input, 64, mac_array);
//DS2432的SHA1算法跟标准的不一样,它没有进行最后的一步加初始值?
changeform(mac_array);
if(err)
{
//Copy the scratchpad to memory
//注意:不管什么情况,在进行存储或SHA功能命令前都得复+ROM命令
Dallas_RES(); //DS28E01-100复位初始化
Read_SerialNum(); //ROM功能命令
Dallas_write_byte(0x55); //写Copy scratchpad命令
for(i = 0; i < 3; i++)
Dallas_write_byte(scratch);
delay_ms(1); //内部SHA1计算需要1ms左右
for(i = 0; i < 20; i++)
Dallas_write_byte(mac_array);
delay_ms(11);
temp = Dallas_read_byte();
//主机至少应读一个字节,读到0xAA表示复制成功;读到0x00失败,原因是计算出的MAC与主机发
//送的MAC不匹配;读到oxFF也失败,原因是写保护或授权码不正确。
if(temp == 0 && err)
err = 0;
else if(temp == 0xFF && err)
err = 0;
}
}
}
return err;
} |
|