DSP 2812: 使用C++封装GPIO
2016-03-02 22:33
696 查看
2812中的GPIO被分成很多组:GPA,GPB,GPD,GPE,GPF,GPG。
我们将组进行抽象。
这里对拷贝构造进行了限制。
一下包含了相关寄存器的引用。定义这些寄存器,是为了在头文件中暴露出来,可以使用内联函数访问到。在一些需要快速操作的应用中,这样的设计是必要的。
对MUX进行封装
对IO模式的操作进行封装
对于作为DI的封装
对于DO进行封装
我们将组进行抽象。
组抽象: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(); }; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- Linux下GPIO驱动详解文章
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解