在TMS320F2812上移植lwip,网卡为ENC28J60。在收到数据包后lwip定义的struct eth_hdr与数据包对不起。
ENC28J60字节长8 bit,DSP字节长为16 bit,dsp按字节读取ENC28J60接收缓冲区,相当于把8位字节扩展成16位字节。
下面是lwip定义的报头struct eth_hdr,其字节长度应该是是8bit。
而dsp的字节是16 bit。报头前两项是MAC地址,即6字节的dest和6字节的src还能对应上,第13和14字节表示数据包类型(type),而dsp下u16_t type只对应第13字节,第14字节的数据存不到type中。在其他用到这个报头结构的地方都会有问题。
数据包从第14个字节开始对不齐,后面内容就全乱了。
我的理解问题原因是:
lwip中 sizeof(eth_hdr) = 14
TMS320F2812中 sizeof(eth_hdr) = 13
因为lwip定义类型:
typedef unsigned char u8_t;
typedef unsigned short u16_t;
所以
sizeof(unsigned char)=1
sizeof(unsigned short)=2
而F2812中类型定义 sizeof(unsigned char)=size(unsigned short)=1
不知道我表达清楚没有。
这个问题该怎么解决?
CCS中有没有PACK_这类的编译指示符解决这个问题?我找了,没找到,请各位给指点一下啊,谢谢啦!
#define ETHARP_HWADDR_LEN 6
PACK_STRUCT_BEGIN
struct eth_addr {
PACK_STRUCT_FIELD(u8_t addr[ETHARP_HWADDR_LEN]);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END
PACK_STRUCT_BEGIN
struct eth_hdr { //数据包包头
#if ETH_PAD_SIZE
PACK_STRUCT_FIELD(u8_t padding[ETH_PAD_SIZE]); //本程序不使用填充字节
#endif
PACK_STRUCT_FIELD(struct eth_addr dest); //1-6字节,目标MAC地址
PACK_STRUCT_FIELD(struct eth_addr src); //7-12字节,源MAC地址
PACK_STRUCT_FIELD(u16_t type); //13、14字节,数据包类型,0x0800 : IP包, 0x0806: ARP包
} PACK_STRUCT_STRUCT; // 在CCS的watch里可以看到,type值是0x0080, 后面的00或06在下个字节位置
PACK_STRUCT_END
数据包格式如下图
以太网数据包格式
// ------------------------------------------------------------------------
/// @brief Special 8bit unsigned integer class for 16bit devices
///
/// Internally this class uses a 16 integer
struct _uip_uint8
{
public:
_uip_uint8() : v(v & 0x00FF) {}
_uip_uint8(uint8 value) : v(value & 0x00FF) {}
_uip_uint8(const _uip_uint8 &src) { v = src.v; }
_uip_uint8 &operator=(const _uip_uint8 &value) { v = value.v; return *this; }
_uip_uint8 &operator=(uint8 value) { v = value & 0x00FF; return *this; }
// volatile...
volatile _uip_uint8 &operator=(const _uip_uint8 &value) volatile { v = value.v; return *this; }
volatile _uip_uint8 &operator=(uint8 value) volatile { v = value & 0x00FF; return *this; }
uint8 toUint8() const volatile { return (v & 0x00FF); }
operator uint8() const volatile { return toUint8(); }
// operators
_uip_uint8 &operator++() { inc(); return *this; }
_uip_uint8 &operator--() { dec(); return *this; }
_uip_uint8 operator++(int) { _uip_uint8 temp = v; inc(); return temp; }
_uip_uint8 operator--(int) { _uip_uint8 temp = v; dec(); return temp; }
_uip_uint8 &operator+=(uint8 b) { v += b; v &= 0x00FF; return *this; }
_uip_uint8 &operator-=(uint8 b) { v -= b; v &= 0x00FF; return *this; }
_uip_uint8 &operator+=(const _uip_uint8 &b) { v += b.v; v &= 0x00FF; return *this; }
_uip_uint8 &operator-=(const _uip_uint8 &b) { v -= b.v; v &= 0x00FF; return *this; }
_uip_uint8 &operator|=(uint8 value) { v |= value & 0x00FF; return *this; }
_uip_uint8 &operator&=(uint8 value) { v &= value; return *this; }
_uip_uint8 &operator|=(const _uip_uint8 &b) { v |= b.v; return *this; }
_uip_uint8 &operator&=(const _uip_uint8 &b) { v &= b.v; return *this; }
// volatile...
volatile _uip_uint8 &operator++() volatile { inc(); return *this; }
volatile _uip_uint8 &operator--() volatile { dec(); return *this; }
volatile _uip_uint8 operator++(int) volatile { _uip_uint8 temp = v; inc(); return temp; }
volatile _uip_uint8 operator--(int) volatile { _uip_uint8 temp = v; dec(); return temp; }
volatile _uip_uint8 &operator+=(uint8 b) volatile { v += b; v &= 0x00FF; return *this; }
volatile _uip_uint8 &operator-=(uint8 b) volatile { v -= b; v &= 0x00FF; return *this; }
volatile _uip_uint8 &operator|=(uint8 value) volatile { v |= value & 0x00FF; return *this; }
volatile _uip_uint8 &operator&=(uint8 value) volatile { v &= value; return *this; }
volatile _uip_uint8 &operator|=(const _uip_uint8 &b) volatile { v |= b.v; return *this; }
volatile _uip_uint8 &operator&=(const _uip_uint8 &b) volatile { v &= b.v; return *this; }
bool operator!() const { return v==0; }
protected:
uint16 v;
void inc() volatile { v++; v &= 0x00FF; }
void dec() volatile { v--; v &= 0x00FF; }
};
// ------------------------------------------------------------------------
/// @brief Special 16bit unsigned integer class for 16bit devicesstruct _uip_uint16
///
/// Internally this class used a 32bit integer
struct _uip_uint16
{
public:
_uip_uint16() : v(0) {}
_uip_uint16(uint16 value) : v((((uint32)(value & 0xFF00)) << 8) + (value & 0x00FF)) {}
_uip_uint16(const _uip_uint8 &src) { v = src.toUint8(); }
_uip_uint16(const _uip_uint16 &src) { v = src.v; }
_uip_uint16 &operator=(const _uip_uint8 &value) { v = value.toUint8(); return *this; }
_uip_uint16 &operator=(const _uip_uint16 &value) { v = value.v; return *this; }
_uip_uint16 &operator=(uint16 value) { bytes.l = value & 0x00FF; bytes.h = (value & 0xFF00) >> 8; return *this; }
// volatile...
_uip_uint16(const volatile _uip_uint8 &src) { v = src.toUint8(); }
_uip_uint16(const volatile _uip_uint16 &src) { v = src.v; }
volatile _uip_uint16 &operator=(const _uip_uint8 &value) volatile { v = value.toUint8(); return *this; }
volatile _uip_uint16 &operator=(const _uip_uint16 &value) volatile { v = value.v; return *this; }
volatile _uip_uint16 &operator=(uint16 value) volatile { bytes.l = value & 0x00FF; bytes.h = (value & 0xFF00) >> 8; return *this; }
uint16 toUint16() const volatile { return (bytes.l & 0x00FF) + (bytes.h<<8); }
operator uint16() const volatile { return toUint16(); }
// operators
_uip_uint16 &operator++() { inc(); return *this; }
_uip_uint16 &operator--() { dec(); return *this; }
_uip_uint16 operator++(int) { _uip_uint16 temp = v; inc(); return temp; }
_uip_uint16 operator--(int) { _uip_uint16 temp = v; dec(); return temp; }
_uip_uint16 &operator+=(unsigned long b) { bytes.l += b; correct(); return *this; }
_uip_uint16 &operator-=(unsigned long b) { bytes.l -= b; correct(); return *this; }
_uip_uint16 &operator+=(const _uip_uint8 &b) { v += b.toUint8(); correct(); return *this; }
_uip_uint16 &operator-=(const _uip_uint8 &b) { v -= b.toUint8(); v&=0x00FF00FF; return *this; }
_uip_uint16 &operator+=(const _uip_uint16 &b) { v += b.v; correct(); return *this; }
_uip_uint16 &operator-=(const _uip_uint16 &b) { v -= b.v; v&=0x00FF00FF; return *this; }
// volatile...
volatile _uip_uint16 &operator+=(unsigned long b) volatile { bytes.l += b; correct(); return *this; }
volatile _uip_uint16 &operator-=(unsigned long b) volatile { bytes.l -= b; correct(); return *this; }
volatile _uip_uint16 &operator+=(const volatile _uip_uint8 &b) volatile { v += b.toUint8(); correct(); return *this; }
volatile _uip_uint16 &operator-=(const volatile _uip_uint8 &b) volatile { v -= b.toUint8(); v&=0x00FF00FF; return *this; }
volatile _uip_uint16 &operator+=(const volatile _uip_uint16 &b) volatile { v += b.v; correct(); return *this; }
volatile _uip_uint16 &operator-=(const volatile _uip_uint16 &b) volatile { v -= b.v; v&=0x00FF00FF; return *this; }
volatile _uip_uint16 &operator++() volatile { inc(); return *this; }
volatile _uip_uint16 &operator--() volatile { dec(); return *this; }
volatile _uip_uint16 operator++(int) volatile { _uip_uint16 temp = v; inc(); return temp; }
volatile _uip_uint16 operator--(int) volatile { _uip_uint16 temp = v; dec(); return temp; }
bool operator!() const { return v==0; }
protected:
union
{
unsigned long v;
struct
{
unsigned short l;
unsigned short h;
} bytes;
};
_uip_uint16(unsigned long value,int) : v(value) {}
void inc() volatile { bytes.l++; correct(); }
void dec() volatile { bytes.l--; correct(); }
void correct() volatile { if(bytes.l & 0xFF00) { bytes.h += bytes.l>>8; v&=0x00FF00FF; } }
};
一周热门 更多>