搜索
bottom↓
回复: 4

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

[复制链接]

出0入0汤圆

发表于 2010-10-19 11:06:25 | 显示全部楼层 |阅读模式
在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[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);
  PACK_STRUCT_FIELD(struct eth_addr src);
  PACK_STRUCT_FIELD(u16_t type);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

 楼主| 发表于 2010-10-25 09:05:05 | 显示全部楼层
各位给看看啊!

出0入0汤圆

发表于 2010-11-5 21:47:16 | 显示全部楼层
看看啊

出0入0汤圆

发表于 2010-12-24 15:24:26 | 显示全部楼层
我也遇到这个问题了···

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

出0入0汤圆

发表于 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; } }
};
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-19 05:04

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表