USB3.2超高速数据扰频
2021-06-06
1461
0
在数据时行发送前,一般先扰频,然后再序序列化数据,最后通过差分发送器将数据从LSB-MSB发送出去。在接收端,同理也需要进行解扰频。
在实际的USB物理IP核开发调试中,可以通过禁用扰频来进行调试开发以降低调试的难度。
以下子程序用LFSR对“inbyte”中包含的8位值进行编码和解码。
这个例子演示了如何在一个操作中将LFSR进行八次,以及如何在一个操作中将数据异或。当然也可以用其它方法实现,但它们都必须产生与这里所示相同的输出。
加扰频
/*
this routine implements the serial descrambling algorithm in parallel
form
for the LSFR polynomial: x^16+x^5+x^4+x^3+1
this advances the LSFR 8 bits every time it is called
this requires fewer than 25 xor gates to implement (with a static
register)
The XOR required to advance 8 bits/clock is:
bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
8 9 10 11 12 13 14 15
8 9 10 11 12 13 14 15
The serial data is just the reverse of the upper byte:
bit 0 1 2 3 4 5 6 7
15 14 13 12 11 10 9 8
*/
int scramble_byte(int inbyte)
{
static int scrambit[16];
static int bit[16];
static int bit_out[16];
static unsigned short lfsr = 0xffff; // 16 bit short for polynomial
int i, outbyte;
if (inbyte == COMMA) // if this is a comma
{
lfsr = 0xffff; // reset the LFSR
return (COMMA); // and return the same data
}
if (inbyte == SKIP) // don't advance or encode on skip
return (SKIP);
for (i = 0; i<16; i++) // convert LFSR to bit array for legibility
bit[i] = (lfsr >> i) & 1;
for (i = 0; i<8; i++) // convert byte to be scrambled for legibility
scrambit[i] = (inbyte >> i) & 1;
// apply the xor to the data
if (!(inbyte & 0x100) && // if not a KCODE, scramble the data
!(TrainingSequence == TRUE)) // and if not in the middle of
{ // a training sequence
scrambit[0] ^= bit[15];
scrambit[1] ^= bit[14];
scrambit[2] ^= bit[13];
scrambit[3] ^= bit[12];
scrambit[4] ^= bit[11];
scrambit[5] ^= bit[10];
scrambit[6] ^= bit[9];
scrambit[7] ^= bit[8];
}
// Now advance the LFSR 8 serial clocks
bit_out[0] = bit[8];
bit_out[1] = bit[9];
bit_out[2] = bit[10];
bit_out[3] = bit[11] ^ bit[8];
bit_out[4] = bit[12] ^ bit[9] ^ bit[8];
bit_out[5] = bit[13] ^ bit[10] ^ bit[9] ^ bit[8];
bit_out[6] = bit[14] ^ bit[11] ^ bit[10] ^ bit[9];
bit_out[7] = bit[15] ^ bit[12] ^ bit[11] ^ bit[10];
bit_out[8] = bit[0] ^ bit[13] ^ bit[12] ^ bit[11];
bit_out[9] = bit[1] ^ bit[14] ^ bit[13] ^ bit[12];
bit_out[10] = bit[2] ^ bit[15] ^ bit[14] ^ bit[13];
bit_out[11] = bit[3] ^ bit[15] ^ bit[14];
bit_out[12] = bit[4] ^ bit[15];
bit_out[13] = bit[5];
bit_out[14] = bit[6];
bit_out[15] = bit[7];
lfsr = 0;
for (i = 0; i <16; i++) // convert the LFSR back to an integer
lfsr += (bit_out[i] << i);
outbyte = 0;
for (i = 0; i<8; i++) // convert data back to an integer
outbyte += (scrambit[i] << i);
return outbyte;
}
解扰频
/* NOTE THAT THE DESCRAMBLE ROUTINE IS IDENTICAL TO THE SCRAMBLE ROUTINE
this routine implements the serial descrambling algorithm in parallel
form
this advances the lfsr 8 bits every time it is called
this uses fewer than 25 xor gates to implement (with a static
register)
The XOR tree is the same as the scrambling routine
*/
int unscramble_byte(int inbyte)
{
static int descrambit[8];
static int bit[16];
static int bit_out[16];
static unsigned short lfsr = 0xffff; // 16 bit short for polynomial
int outbyte, i;
if (inbyte == COMMA) // if this is a comma
{
lfsr = 0xffff; // reset the LFSR
return (COMMA); // and return the same data
}
if (inbyte == SKIP) // don't advance or encode on skip
return (SKIP);
for (i = 0; i<16; i++) // convert the LFSR to bit array for legibility
bit[i] = (lfsr >> i) & 1;
for (i = 0; i<8; i++) // convert byte to be de-scrambled for
legibility
descrambit[i] = (inbyte >> i) & 1;
// apply the xor to the data
if (!(inbyte & 0x100) && // if not a KCODE, scramble the data
!(TrainingSequence == TRUE)) // and if not in the middle of
{ // a training sequence
descrambit[0] ^= bit[15];
descrambit[1] ^= bit[14];
descrambit[2] ^= bit[13];
descrambit[3] ^= bit[12];
descrambit[4] ^= bit[11];
descrambit[5] ^= bit[10];
descrambit[6] ^= bit[9];
descrambit[7] ^= bit[8];
}
// Now advance the LFSR 8 serial clocks
bit_out[0] = bit[8];
bit_out[1] = bit[9];
bit_out[2] = bit[10];
bit_out[3] = bit[11] ^ bit[8];
bit_out[4] = bit[12] ^ bit[9] ^ bit[8];
bit_out[5] = bit[13] ^ bit[10] ^ bit[9] ^ bit[8];
bit_out[6] = bit[14] ^ bit[11] ^ bit[10] ^ bit[9];
bit_out[7] = bit[15] ^ bit[12] ^ bit[11] ^ bit[10];
bit_out[8] = bit[0] ^ bit[13] ^ bit[12] ^ bit[11];
bit_out[9] = bit[1] ^ bit[14] ^ bit[13] ^ bit[12];
bit_out[10] = bit[2] ^ bit[15] ^ bit[14] ^ bit[13];
bit_out[11] = bit[3] ^ bit[15] ^ bit[14];
bit_out[12] = bit[4] ^ bit[15];
bit_out[13] = bit[5];
bit_out[14] = bit[6];
bit_out[15] = bit[7];
lfsr = 0;
for (i = 0; i <16; i++) // convert the LFSR back to an integer
lfsr += (bit_out[i] << i);
outbyte = 0;
for (i = 0; i<8; i++) // convert data back to an integer
outbyte += (descrambit[i] << i);
return outbyte;
}
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936