修正各个单元测试项目

This commit is contained in:
nnhy 2015-12-05 08:11:36 +00:00
parent 1038b27b03
commit 1a490db5d8
5 changed files with 59 additions and 45 deletions

View File

@ -39,15 +39,16 @@ void Init()
} }
// 硬件实现的Crc // 硬件实现的Crc
uint HardCrc(const void* buf, int len, uint crc) uint HardCrc(const Array& bs, uint crc)
{ {
if (!inited) Init(); if (!inited) Init();
CRC_ResetDR(); CRC_ResetDR();
// STM32的初值是0xFFFFFFFF而软Crc初值是0 // STM32的初值是0xFFFFFFFF而软Crc初值是0
CRC->DR = __REV(crc ^ 0xFFFFFFFF); CRC->DR = __REV(crc ^ 0xFFFFFFFF);
//CRC->DR = 0xFFFFFFFF; //CRC->DR = 0xFFFFFFFF;
uint* ptr = (uint*)buf; uint* ptr = (uint*)bs.GetBuffer();
uint len = bs.Length();
len >>= 2; len >>= 2;
while(len-- > 0) while(len-- > 0)
{ {
@ -74,48 +75,52 @@ void TestCrc()
debug_printf("TestCrc Start......\r\n"); debug_printf("TestCrc Start......\r\n");
debug_printf("\r\n"); debug_printf("\r\n");
uint size = ArrayLength(DataBuffer); uint size = ArrayLength(DataBuffer);
// Sys.Crc是软校验HardCrc是硬件实现要求硬件实现的结果跟软件实现一致 // Sys.Crc是软校验HardCrc是硬件实现要求硬件实现的结果跟软件实现一致
uint data = 0x12345678; uint data = 0x12345678;
uint crc = Crc::Hash((byte*)&data, 4, 0); ByteArray bs(&data, 4);
uint crc2 = Crc::Hash(&data, 4); uint crc = Crc::Hash(bs, 0);
ByteArray((byte*)&data, 4)).Show(); uint crc2 = Crc::Hash(bs, 4);
bs.Show();
debug_printf("\r\n\tSoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("\r\n\tSoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 无初值时,两者一样 // 无初值时,两者一样
uint temp = crc; uint temp = crc;
// 试试二次计算Crc // 试试二次计算Crc
crc = Crc::Hash((byte*)&crc, 4, 0); crc = Crc::Hash(Array(&crc, 4), 0);
crc2 = Crc::Hash(&crc2, 4); crc2 = Crc::Hash(Array(&crc2, 4));
ByteArray((byte*)&temp, 4)).Show(); ByteArray(&temp, 4).Show();
debug_printf("\r\n\t"); debug_printf("\r\n\t");
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 结果相同但都不是0 // 结果相同但都不是0
// 连续测试。构建8字节前面是data后面是前面的crc // 连续测试。构建8字节前面是data后面是前面的crc
ulong data2 = temp; ulong data2 = temp;
data2 <<= 32; data2 <<= 32;
data2 += data; data2 += data;
crc = Crc::Hash((byte*)&data2, 8, 0); ByteArray bs2(&data2, 8);
crc2 = Crc::Hash(&data2, 8); crc = Crc::Hash(bs2, 0);
ByteArray((byte*)&data2, 8)).Show(); crc2 = Crc::Hash(bs2);
bs2.Show();
debug_printf("\r\n\t"); debug_printf("\r\n\t");
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 结果相同但都不是0 // 结果相同但都不是0
// 实际应用中,先计算数据的校验,然后接着附加校验码部分,跟直接连续计算效果应该一致 // 实际应用中,先计算数据的校验,然后接着附加校验码部分,跟直接连续计算效果应该一致
// 实际上就是数字为初值,对它自身进行校验码计算 // 实际上就是数字为初值,对它自身进行校验码计算
crc = Crc::Hash((byte*)&temp, 4, data); ByteArray bs3(&temp, 4);
crc2 = HardCrc(&temp, 4, data); crc = Crc::Hash(bs3, data);
ByteArray((byte*)&temp, 4)).Show(); crc2 = HardCrc(bs3, data);
bs3.Show();
debug_printf(" <= 0x%08x\r\n\t", data); debug_printf(" <= 0x%08x\r\n\t", data);
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 结果不同HardCrc结果跟8字节测试相同 // 结果不同HardCrc结果跟8字节测试相同
crc = Crc::Hash((byte*)&temp, 4, temp); ByteArray bs4(&temp, 4);
crc2 = HardCrc(&temp, 4, temp); crc = Crc::Hash(bs4, temp);
ByteArray((byte*)&temp, 4)).Show(); crc2 = HardCrc(bs4, temp);
bs4.Show();
debug_printf(" <= 0x%08x\r\n\t", temp); debug_printf(" <= 0x%08x\r\n\t", temp);
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 结果不同SoftCrc结果跟8字节测试相同 // 结果不同SoftCrc结果跟8字节测试相同
@ -123,9 +128,10 @@ void TestCrc()
// 对大数据块进行校验 // 对大数据块进行校验
debug_printf("\r\n"); debug_printf("\r\n");
crc = Crc::Hash((byte*)DataBuffer, size*4, 0); ByteArray bs5(DataBuffer, size*4);
crc2 = Crc::Hash(DataBuffer, size*4); crc = Crc::Hash(bs5, 0);
ByteArray((byte*)DataBuffer, 0x20)).Show(); crc2 = Crc::Hash(bs5);
bs5.Show();
debug_printf("\r\n\t"); debug_printf("\r\n\t");
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 无初值时,两者一样 // 无初值时,两者一样
@ -133,29 +139,31 @@ void TestCrc()
temp = crc; temp = crc;
// 实际应用中,先计算数据的校验,然后接着附加校验码部分 // 实际应用中,先计算数据的校验,然后接着附加校验码部分
crc = Crc::Hash((byte*)&temp, 4, temp); ByteArray bs6(&temp, 4);
crc2 = HardCrc((byte*)&temp, 4, temp); crc = Crc::Hash(bs6, temp);
ByteArray((byte*)&temp, 4)).Show(); crc2 = HardCrc(bs6, temp);
bs6.Show();
debug_printf(" <= 0x%08x\r\n\t", temp); debug_printf(" <= 0x%08x\r\n\t", temp);
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 有初值时,两者不一样 // 有初值时,两者不一样
// 增量计算CRC // 增量计算CRC
crc = Crc::Hash((byte*)DataBuffer, size*4, 0); ByteArray bs7(DataBuffer, size*4);
temp = crc; crc = Crc::Hash(bs7, 0);
crc = Crc::Hash((byte*)&crc, 4, crc); temp = crc;
crc2 = HardCrc(DataBuffer, size*4, 0); crc = Crc::Hash(Array(&crc, 4), crc);
crc2 = HardCrc((byte*)&crc2, 4, crc2); crc2 = HardCrc(bs7, 0);
ByteArray((byte*)DataBuffer, 0x20).Show(); crc2 = HardCrc(Array(&crc2, 4), crc2);
bs7.Show();
debug_printf(" <= 0x%08x\r\n\t", temp); debug_printf(" <= 0x%08x\r\n\t", temp);
debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2); debug_printf("SoftCrc:0x%08x HardCrc:0x%08x \r\n", crc, crc2);
// 测试Crc16数据和crc部分一起计算crc16结果为0 // 测试Crc16数据和crc部分一起计算crc16结果为0
debug_printf("\r\n"); debug_printf("\r\n");
byte data16[] = { 0x01, 0x08, 0x00, 0x00}; byte data16[] = { 0x01, 0x08, 0x00, 0x00};
ushort crc16 = Crc::Hash16(data16, 4); ushort crc16 = Crc::Hash16(Array(data16, 4));
debug_printf("Crc::Hash16(#%08x) = 0x%04x\r\n", __REV(*(uint*)data16), crc16); debug_printf("Crc::Hash16(#%08x) = 0x%04x\r\n", __REV(*(uint*)data16), crc16);
ushort crc17 = Crc::Hash16((byte*)&crc16, 2, crc16); ushort crc17 = Crc::Hash16(Array(&crc16, 2), crc16);
debug_printf("Crc::Hash16(#%08x, 0x%04x) = 0x%04x\r\n", __REV(*(uint*)data16), crc16, crc17); debug_printf("Crc::Hash16(#%08x, 0x%04x) = 0x%04x\r\n", __REV(*(uint*)data16), crc16, crc17);
debug_printf("\r\n"); debug_printf("\r\n");

View File

@ -11,8 +11,11 @@ void OnSend(void* param)
// 最后4个字节修改为秒数 // 最后4个字节修改为秒数
// 大概4.86%的误差 // 大概4.86%的误差
uint s = __REV(Sys.Ms() >> 10); uint s = __REV(Sys.Ms() >> 10);
byte* p = tx_buf + ArrayLength(tx_buf) - 8; //byte* p = tx_buf + ArrayLength(tx_buf) - 8;
Sys.ToHex(p, (byte*)&s, 4); //Sys.ToHex(p, (byte*)&s, 4);
String str(tx_buf);
str.SetLength(str.Length() - 8);
str.Append(s, 16, 8);
//nrf->SetMode(false); //nrf->SetMode(false);
if(!nrf->Write(CArray(tx_buf))) if(!nrf->Write(CArray(tx_buf)))
@ -46,8 +49,11 @@ void TestNRF24L01()
debug_printf("TestNRF24L01 Start......\r\n"); debug_printf("TestNRF24L01 Start......\r\n");
// 修改数据加上系统ID // 修改数据加上系统ID
byte* p = tx_buf + 5; //byte* p = tx_buf + 5;
Sys.ToHex(p, (byte*)Sys.ID, 6); //Sys.ToHex(p, (byte*)Sys.ID, 6);
String str(tx_buf);
str.SetLength(5);
str.Append(ByteArray(Sys.ID, 6));
nrf = Create2401(); nrf = Create2401();
nrf->Timeout = 1000; nrf->Timeout = 1000;

View File

@ -39,12 +39,12 @@ void TestTimer(OutputPort& leds)
timer = new Timer(TIM2); timer = new Timer(TIM2);
timer->SetFrequency(50); timer->SetFrequency(50);
timer->Register(TimerTask, &leds); timer->Register(TimerTask, &leds);
timer->Start(); timer->Open();
Timer* timer2 = Timer::Create(); Timer* timer2 = Timer::Create();
timer2->SetFrequency(10); timer2->SetFrequency(10);
timer2->Register(TimerTask2, NULL); timer2->Register(TimerTask2, NULL);
timer2->Start(); timer2->Open();
debug_printf("\r\n TestTimer Finish!\r\n"); debug_printf("\r\n TestTimer Finish!\r\n");
} }

View File

@ -606,7 +606,7 @@ String& String::Append(byte bt)
return *this; return *this;
} }
String& String::Append(ByteArray& bs) String& String::Append(const ByteArray& bs)
{ {
bs.ToHex(*this); bs.ToHex(*this);

2
Type.h
View File

@ -341,7 +341,7 @@ public:
String& Append(const char* str, int len = 0); String& Append(const char* str, int len = 0);
String& Append(int value, int radix = 10, int width = 0); // 写入整数,第二参数指定宽带,不足时补零 String& Append(int value, int radix = 10, int width = 0); // 写入整数,第二参数指定宽带,不足时补零
String& Append(byte bt); // 十六进制 String& Append(byte bt); // 十六进制
String& Append(ByteArray& bs); // 十六进制 String& Append(const ByteArray& bs); // 十六进制
// 调试输出字符串 // 调试输出字符串
virtual void Show(bool newLine = false) const; virtual void Show(bool newLine = false) const;