您的位置:首页 > 编程语言 > C语言/C++

DSP 2812: 使用C++封装GPIO

2016-03-02 22:33 696 查看
2812中的GPIO被分成很多组:GPA,GPB,GPD,GPE,GPF,GPG。

我们将组进行抽象。

组抽象:CGpio类

使用命名空间对类进行包裹

namespace NF281x{
class CGpio{
CGpio( const CGpio& );
public:
CGpio( volatile unsigned int& mux,volatile unsigned int& dir,
volatile unsigned int& dat,volatile unsigned int& set,volatile unsigned int& clr,
volatile unsigned int& tog );


这里对拷贝构造进行了限制。

一下包含了相关寄存器的引用。定义这些寄存器,是为了在头文件中暴露出来,可以使用内联函数访问到。在一些需要快速操作的应用中,这样的设计是必要的。

protected:
volatile unsigned int& m_mux;
volatile unsigned int& m_dir;

volatile unsigned int& m_dat;
volatile unsigned int& m_set;
volatile unsigned int& m_clr;
volatile unsigned int& m_toggle;
};
}


对MUX进行封装

/**
* 获取该组GPIO第i个io口的MUX
* @param i 0~15
* @return
* - 0 作为IO
* - 1 作为外设
*/
inline unsigned int getMux( const int& i )const{
return m_mux&(0x0001<<i);
}

inline bool isMuxIo( const int& i )const{
return getMux(i)==0;
}

inline bool isMuxPeripheral( const int& i )const{
return getMux(i)!=0;
}

/**
* 设置GPIO作为IO
* @param i
*/
void setMuxIo( const int& i );

/**
* 设置GPIO为外设模式
* @param i
*/
void setMuxPeripheral( const int& i );

/**
* 设置GPIO的MUX
* @param i
* @param mux
* - 0 IO模式
* - 1 外设模式
*/
inline void setMux( const int& i,const unsigned int& mux ){
if( mux==0 )
setMuxIo(i);
else
setMuxPeripheral(i);
}


对IO模式的操作进行封装

inline unsigned int getIoDir( const int& i )const{
return m_dir&(0x0001<<i);
}

inline void setIoDir( const int& i,const unsigned int& dir ){
if( dir==0 )
setIoIn(i);
else
setIoOut(i);
}

void setIoIn( const int& i );
void setIoOut( const int& i );

inline bool isIoIn( const int& i )const{
return getIoDir(i)==0;
}

inline bool isIoOut( const int& i )const{
return getIoDir(i)!=0;
}


对于作为DI的封装

inline unsigned int getIoState( const int& i )const{
return m_dat&(0x0001<<i);
}

inline bool isIoSet( const int& i )const{
return getIoState(i)!=0;
}

inline bool isIoUnset( const int& i )const{
return getIoState(i)==0;
}


对于DO进行封装

inline void set( const int& i ){
m_set |= (0x0001<<i);
}

inline void clear( const int& i ){
m_clr |= (0x0001<<i);
}

inline void toggle( const int& i ){
m_toggle |= (0x0001<<i);
}


子类CGpa,CGpb等

- 这里举一个例子,CGpa的类设计

namespace NF281x{

class CGpa:public CGpio{
CGpa( const CGpa& );
public:
CGpa();
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DSP 2812 C++ GPIO