打印

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

[复制链接]
5578|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
在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

数据包格式如下图

相关帖子

沙发
warcraftiii|  楼主 | 2010-10-20 11:24 | 只看该作者
我理解根本问题出在这句: typedef unsigned short   u16_t;
lwip本意的unsigned short应该是2 byte,所以eth_hdr中的 u16_t type 正好对应数据包中的第13、14字节,存储数据包类型。
而TMS320F2812下 unsigned short 是1 byte,所以eth_hdr中的u16_t type 只对应第13字节。

按照这种理解,ARP、IP数据包结构体的定义都存在这个问题。改哪里好呢?
改lwip的源文件?好像有好多地方都要改。

要是上面理解正确的话,lwip应该不适合移植到TMS320F2812这种字节长度是16bit的dsp下。
各位给提示下啊!

使用特权

评论回复
板凳
warcraftiii|  楼主 | 2010-10-25 09:04 | 只看该作者
各位,给看看啊!

使用特权

评论回复
地板
gongxd126com| | 2011-1-12 10:02 | 只看该作者
我也想知道啊 否则要改的很多

使用特权

评论回复
5
gongxd126com| | 2011-1-12 10:08 | 只看该作者
见过一个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; } }
};

使用特权

评论回复
6
3B1105| | 2011-1-19 17:02 | 只看该作者
好复杂,编译器不是自动帮你考虑了吗

使用特权

评论回复
7
warcraftiii|  楼主 | 2014-7-1 11:23 | 只看该作者
5楼的办法应该可行。但C++不懂...
有人建议重定义short类型,再定义对新类型的访问方法,好像工作量很大

使用特权

评论回复
8
warcraftiii|  楼主 | 2014-7-1 11:26 | 只看该作者
给分怎么总错误呢?

使用特权

评论回复
9
lishong| | 2016-1-13 10:42 | 只看该作者
楼主怎么解决的?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

56

帖子

1

粉丝