📄 mbus_packet.c
字号:
// uint8* pPacket - Pointer to Wireless MBUS packet
// uint16 packetSize - Total Size of the uncoded Wireless MBUS packet
//
// RETURNS
// uint16 - Total size of bytes to transmit
//----------------------------------------------------------------------------------
void encodeTXBytesSmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
{
uint16 bytesEncoded;
bytesEncoded = 0;
// Last byte of synchronization word
(*(pByte)) = 0x96;
pByte++;
// Manchester encode packet
while (bytesEncoded < packetSize)
{
manchEncode((pPacket + bytesEncoded), (pByte + 2*bytesEncoded));
bytesEncoded++;
}
// Append the postamble sequence
(*(pByte + 2*bytesEncoded)) = 0x55;
}
//----------------------------------------------------------------------------------
// void encodeTXBytesTmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
//
// DESCRIPTION:
// Encodes a wireless MBUS packet into a TMODE packet. This includes
// - 3 out of 6 encode the Wireless MBUS packet.
// - Append postamble sequence to the TX array
//
// ARGUMENTS:
// uint8* pByte - Pointer to TMODE packet
// uint8* pPacket - Pointer to Wireless MBUS packet
// uint16 packetSize - Total size of the Wireless MBUS packet
//----------------------------------------------------------------------------------
void encodeTXBytesTmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
{
uint16 bytesRemaining;
bytesRemaining = packetSize;
// 3 our of 6 encode packet
while (bytesRemaining)
{
// If 1 byte left to encode, include
// Postamble in "3 out of 6" encoding routine
if (bytesRemaining == 1)
{
encode3outof6(pPacket, pByte, 1);
bytesRemaining -= 1;
}
// Else if 2 byte left to encode, append Postamble
else if (bytesRemaining == 2)
{
encode3outof6(pPacket, pByte, 0);
// Append postamble
pByte += 3;
*pByte = 0x55;
bytesRemaining -= 2;
}
else
{
encode3outof6(pPacket, pByte, 0);
pByte += 3;
pPacket += 2;
bytesRemaining -= 2;
}
}
}
//----------------------------------------------------------------------------------
// uint16 decodeRXBytesSmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
//
// DESCRIPTION:
// Decode a SMODE packet into a Wireless MBUS packet. Checks for 3 out of 6
// decoding errors and CRC errors.
//
// ARGUMENTS:
// uint8 *pByte - Pointer to SMBUS packet
// uint8 *pPacket - Pointer to Wireless MBUS packet
// uint16 packetSize - Total Size of the Wireless MBUS packet
//
// RETURNS:
// PACKET_OK 0
// PACKET_CODING_ERROR 1
// PACKET_CRC_ERROR 2
//----------------------------------------------------------------------------------
uint16 decodeRXBytesSmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
{
uint16 bytesRemaining;
uint16 bytesEncoded;
uint16 decodingStatus;
uint16 crc; // Current CRC
uint16 crcField1; // Current byte is CRC high byte
uint16 crcField0; // Current byte is CRC low byte
bytesRemaining = packetSize;
bytesEncoded = 0;
crcField1 = 0;
crcField0 = 0;
crc = 0;
// Decode packet
while (bytesRemaining)
{
decodingStatus = manchDecode(pByte, pPacket);
// Check for valid Manchester decoding
if ( decodingStatus != MAN_DECODING_OK)
return (PACKET_CODING_ERROR);
// Check if current field is CRC field number 1, e.g.
// - Field 10 + 18*n
// - Less than 2 bytes
if (bytesRemaining == 2)
crcField1 = 1;
else if ( bytesEncoded > 9 )
crcField1 = !((bytesEncoded - 10) % 18);
// If CRC field number 0, check the low byte of the CRC
if (crcField0)
{
if (LO_UINT16(~crc) != *pPacket )
return (PACKET_CRC_ERROR);
crcField0 = 0;
crc = 0;
}
// If CRC field number 1, check the high byte of the CRC
else if (crcField1)
{
if (HI_UINT16(~crc) != *pPacket )
return (PACKET_CRC_ERROR);
// Next field is CRC field 1
crcField0 = 1;
crcField1 = 0;
}
// If not a CRC field, increment CRC calculation
else
crc = crcCalc(crc, *pPacket);
bytesRemaining--;
bytesEncoded++;
pByte += 2;
pPacket++;
}
return (PACKET_OK);
}
//----------------------------------------------------------------------------------
// uint16 decodeRXBytesTmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
//
// DESCRIPTION:
// Decode a TMODE packet into a Wireless MBUS packet. Checks for 3 out of 6
// decoding errors and CRC errors.
//
// ARGUMENTS:
// uint8 *pByte - Pointer to TMBUS packet
// uint8 *pPacket - Pointer to Wireless MBUS packet
// uint16 packetSize - Total Size of the Wireless MBUS packet
//
// RETURNS:
// PACKET_OK 0
// PACKET_CODING_ERROR 1
// PACKET_CRC_ERROR 2
//----------------------------------------------------------------------------------
uint16 decodeRXBytesTmode(uint8* pByte, uint8* pPacket, uint16 packetSize)
{
uint16 bytesRemaining;
uint16 bytesEncoded;
uint16 decodingStatus;
uint16 crc; // Current CRC value
uint16 crcField; // Current fields are a CRC field
bytesRemaining = packetSize;
bytesEncoded = 0;
crcField = 0;
crc = 0;
// Decode packet
while (bytesRemaining)
{
// If last byte
if (bytesRemaining == 1)
{
decodingStatus = decode3outof6(pByte, pPacket, 1);
// Check for valid 3 out of 6 decoding
if ( decodingStatus != DECODING_3OUTOF6_OK)
return (PACKET_CODING_ERROR);
bytesRemaining -= 1;
bytesEncoded += 1;
// The last byte the low byte of the CRC field
if (LO_UINT16(~crc) != *(pPacket ))
return (PACKET_CRC_ERROR);
}
else
{
decodingStatus = decode3outof6(pByte, pPacket, 0);
// Check for valid 3 out of 6 decoding
if ( decodingStatus != DECODING_3OUTOF6_OK)
return (PACKET_CODING_ERROR);
bytesRemaining -= 2;
bytesEncoded += 2;
// Check if current field is CRC fields
// - Field 10 + 18*n
// - Less than 2 bytes
if (bytesRemaining == 0)
crcField = 1;
else if ( bytesEncoded > 10 )
crcField = !((bytesEncoded - 12) % 18);
// Check CRC field
if (crcField)
{
if (LO_UINT16(~crc) != *(pPacket + 1 ))
return (PACKET_CRC_ERROR);
if (HI_UINT16(~crc) != *pPacket)
return (PACKET_CRC_ERROR);
crcField = 0;
crc = 0;
}
// If 1 bytes left, the field is the high byte of the CRC
else if (bytesRemaining == 1)
{
crc = crcCalc(crc, *(pPacket));
// The packet byte is a CRC-field
if (HI_UINT16(~crc) != *(pPacket + 1))
return (PACKET_CRC_ERROR);
}
// Perform CRC calculation
else
{
crc = crcCalc(crc, *(pPacket));
crc = crcCalc(crc, *(pPacket + 1));
}
pByte += 3;
pPacket += 2;
}
}
return (PACKET_OK);
}
/***********************************************************************************
Copyright 2008 Texas Instruments Incorporated. All rights reserved.
IMPORTANT: Your use of this Software is limited to those specific rights
granted under the terms of a software license agreement between the user
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
Software unless you agree to abide by the terms of the License. The License
limits your use, and you acknowledge, that the Software may not be modified,
copied or distributed unless embedded on a Texas Instruments microcontroller
or used solely and exclusively in conjunction with a Texas Instruments radio
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
works of, modify, distribute, perform, display or sell this Software and/or
its documentation for any purpose.
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
PROVIDED 揂S IS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -