|
发表于 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; } }
}; |
|