在国标GB29768源代码移植修改的符合ISO18000 - 6C协议的PIE发送编码
例:导引头为:
0-1编码为:
其它详见协议规定。
代码如下:
#define PW 16600
#define MAX_RESPONSE_BYTES 64
typedef struct
{
uint32_t numBytes;
uint32_t numBits;
uint8_t data[MAX_RESPONSE_BYTES];
} leadinno_RxTxData_t;
extern leadinno_RxTxData_t leadinnoRxTxData;
#define LEADINNO_CMD_QUERY 0x08
#define LEADINNO_CMD_READ 0xC2
#define LEADINNO_CMD_WRITE 0xC3
#define SET_PULSE_LOW() GPIOC->BRR = (uint32_t)GPIO_PIN_0
#define SET_PULSE_HIGH() GPIOC->BSRR = (uint32_t)GPIO_PIN_0
static void highLowPulse(uint8_t nbTC);
static void separatorPulse(void);
leadinno_RxTxData_t leadinnoRxTxData;
uint16_t value_crc16;
static void leadinnoInitializeCmd(leadinno_RxTxData_t *RxTxData)
{
RxTxData->data[0] = 0x00;
RxTxData->numBytes = 0;
RxTxData->numBits = 0;
}
void leadinnoAddByteToBitStream(leadinno_RxTxData_t *RxTxData, const uint8_t data, const uint8_t bits)
{
if(bits > 0)
{
if(RxTxData->numBits == 8)
{
RxTxData->numBytes++;
RxTxData->data[ RxTxData->numBytes ] = 0x00;
RxTxData->numBits = 0;
}
uint32_t cumulatedBits = RxTxData->numBits + bits;
if (cumulatedBits >= 8)
{
RxTxData->data[ RxTxData->numBytes ] |= (uint8_t)(data >> (cumulatedBits-8));
RxTxData->numBytes++;
RxTxData->data[ RxTxData->numBytes ] = (uint8_t)(data << (16-cumulatedBits));
RxTxData->numBits = cumulatedBits - 8;
}
else
{
RxTxData->data[ RxTxData->numBytes ] |= (uint8_t)(data << (8-cumulatedBits));
RxTxData->numBits = cumulatedBits;
}
}
}
static void leadinnoAddWordToBitStream(leadinno_RxTxData_t *RxTxData, const uint16_t data, const uint8_t bits)
{
if(bits>8) {
leadinnoAddByteToBitStream(RxTxData, (uint8_t)(data>>8), 8);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)(data), bits-8);
}
else {
leadinnoAddByteToBitStream(RxTxData, (uint8_t)(data>>8), bits);
}
}
static void leadinnoAddCrc(leadinno_RxTxData_t *RxTxData)
{
uint16_t crc;
uint16_t num = RxTxData->numBytes*8 + RxTxData->numBits;
if (num%2 != 0)
{
leadinnoAddByteToBitStream(RxTxData, 0x00,1);
num++;
}
crc = crc16Bitwise(RxTxData->data, num);
leadinnoAddWordToBitStream(RxTxData, crc,16);
}
static void leadinnoFinalizeCmdAndSend(leadinno_RxTxData_t *RxTxData)
{
INIT_DWT();
SET_PULSE_HIGH();
separatorPulse();
highLowPulse(1);
highLowPulse(5);
int16_t i, j;
uint8_t data;
for(i=0 ; i<RxTxData->numBytes ; ++i)
{
data = RxTxData->data[i];
for(j = 7 ; j >= 0; j--)
{
if(((data >> j) & 0x01) == 0)
highLowPulse(1);
else
highLowPulse(3);
}
}
if (RxTxData->numBits) {
data = RxTxData->data[i];
for(j = 7 ; j >= (8 - RxTxData->numBits); --j)
{
if(((data >> j) & 0x01) == 0)
highLowPulse(1);
else
highLowPulse(3);
}
}
leadinnoInitializeCmd(RxTxData);
}
uint8_t leadinnoSendCmdQuery(leadinno_RxTxData_t *RxTxData, uint16_t rn16, uint32_t tsp, uint32_t t0, uint32_t trt, uint32_t ttr)
{
leadinnoInitializeCmd(RxTxData);
uint8_t gLastCommand = LEADINNO_CMD_QUERY;
leadinnoAddByteToBitStream(RxTxData, gLastCommand, 4);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)(rn16 >> 8), 8);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)rn16, 8);
leadinnoAddCrc(RxTxData);
INIT_DWT();
SET_PULSE_HIGH();
SET_PULSE_LOW();
nsdelay(tsp);
SET_PULSE_HIGH();
nsdelay(t0);
SET_PULSE_LOW();
nsdelay(t0);
SET_PULSE_HIGH();
nsdelay(trt);
SET_PULSE_LOW();
nsdelay(t0);
SET_PULSE_HIGH();
nsdelay(ttr);
SET_PULSE_LOW();
nsdelay(t0);
SET_PULSE_HIGH();
int16_t i, j;
uint8_t data;
for(i=0 ; i<RxTxData->numBytes ; ++i)
{
data = RxTxData->data[i];
for(j = 7 ; j >= 0; j--)
{
if(((data >> j) & 0x01) == 0)
highLowPulse(1);
else
highLowPulse(3);
}
}
if (RxTxData->numBits) {
data = RxTxData->data[i];
for(j = 7 ; j >= (8 - RxTxData->numBits); --j)
{
if(((data >> j) & 0x01) == 0)
highLowPulse(1);
else
highLowPulse(3);
}
}
leadinnoInitializeCmd(RxTxData);
return 0;
}
uint8_t leadinnoSendCmdRead(leadinno_RxTxData_t *RxTxData, uint8_t reg, uint8_t *data, uint16_t rn16)
{
#define MEM_BANK 0
#define WORD_COUNT 1
leadinnoInitializeCmd(RxTxData);
uint8_t gLastCommand = LEADINNO_CMD_READ;
leadinnoAddByteToBitStream(RxTxData, gLastCommand, 8);
leadinnoAddByteToBitStream(RxTxData, MEM_BANK, 2);
leadinnoAddByteToBitStream(RxTxData, reg, 8);
leadinnoAddByteToBitStream(RxTxData, WORD_COUNT, 8);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)(rn16 >> 8), 8);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)rn16, 8);
leadinnoAddCrc(RxTxData);
leadinnoFinalizeCmdAndSend(RxTxData);
return 0;
}
uint8_t leadinnoSendCmdWrite(leadinno_RxTxData_t *RxTxData, uint8_t reg, uint8_t *data, uint16_t rn16)
{
#define MEM_BANK 0
leadinnoInitializeCmd(RxTxData);
uint8_t gLastCommand = LEADINNO_CMD_WRITE;
leadinnoAddByteToBitStream(RxTxData, gLastCommand, 8);
leadinnoAddByteToBitStream(RxTxData, MEM_BANK, 2);
leadinnoAddByteToBitStream(RxTxData, reg, 8);
leadinnoAddByteToBitStream(RxTxData, data[0], 8);
leadinnoAddByteToBitStream(RxTxData, data[1], 8);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)(rn16 >> 8), 8);
leadinnoAddByteToBitStream(RxTxData, (uint8_t)rn16, 8);
leadinnoAddCrc(RxTxData);
leadinnoFinalizeCmdAndSend(RxTxData);
return 0;
}
static void highLowPulse(uint8_t nbTC)
{
SET_PULSE_HIGH();
nsdelay(nbTC * PW);
SET_PULSE_LOW();
nsdelay(PW);
SET_PULSE_HIGH();
}
static void separatorPulse(void)
{
SET_PULSE_LOW();
nsdelay(4 * PW);
SET_PULSE_HIGH();
}
|