warcraftiii 发表于 2010-10-19 11:06:25

请教DSP与lwip的字节对齐问题。

在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。报头中的6字节的dest和6字节的src还能对应上,第13和14字节表示数据包类型(type),而dsp下u16_t type只对应第13字节,第14字节的数据存不到type中。在其他用到这个报头结构的地方都会有问题。

我的理解问题原因是:
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);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

PACK_STRUCT_BEGIN
struct eth_hdr {
#if ETH_PAD_SIZE
PACK_STRUCT_FIELD(u8_t padding);
#endif
PACK_STRUCT_FIELD(struct eth_addr dest);
PACK_STRUCT_FIELD(struct eth_addr src);
PACK_STRUCT_FIELD(u16_t type);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

warcraftiii 发表于 2010-10-25 09:05:05

各位给看看啊!

gongxd 发表于 2010-11-5 21:47:16

看看啊

jielove2003 发表于 2010-12-24 15:24:26

我也遇到这个问题了···

是在2812上移植uIP,还请有经验的人指导下

gongxd 发表于 2011-1-12 10:12:10

见过一个UIP 的移植处理方法 是定义一个新的数据类
// ------------------------------------------------------------------------
/// @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; } }
};
页: [1]
查看完整版本: 请教DSP与lwip的字节对齐问题。